Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: refine babel-types docs generator #13148

Merged
merged 4 commits into from Apr 14, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 84 additions & 47 deletions packages/babel-types/scripts/generators/docs.js
Expand Up @@ -5,7 +5,14 @@ import toFunctionName from "../utils/toFunctionName.js";
import t from "../../lib/index.js";

const readme = [
`# @babel/types
`---
id: babel-types
title: @babel/types
---
<!-- Do not modify! This file is automatically generated by
github.com/babel/babel/babel-types/scripts/generators/docs.js !-->

# @babel/types

> This module contains methods for building ASTs manually and for checking the types of AST nodes.

Expand Down Expand Up @@ -34,17 +41,44 @@ const customTypes = {
ObjectProperty: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
ClassPrivateMethod: {
computed: "'false'",
},
ClassPrivateProperty: {
computed: "'false'",
},
};
const APIHistory = {
ClassProperty: [["v7.6.0", "Supports `static`"]],
};
function formatHistory(historyItems) {
const lines = historyItems.map(
item => "| `" + item[0] + "` | " + item[1] + " |"
);
return [
"<details>",
" <summary>History</summary>",
"| Version | Changes |",
"| --- | --- |",
...lines,
"</details>",
];
}
Object.keys(t.BUILDER_KEYS)
.sort()
.forEach(function (key) {
readme.push("### " + key[0].toLowerCase() + key.substr(1));
readme.push("### " + toFunctionName(key));
Copy link
Member

@hzoo hzoo Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is outside your PR but just wanted to note it! Looking at the rendered part again, maybe in another PR, we should have a quick section above that explains the terms "builder" and the difference between

t.anyTypeAnnotation();
t.isAnyTypeAnnotation(node, opts)
t.assertAnyTypeAnnotation(node, opts).

I would just mention that instead of manually creating the AST node as an object, we provide this interface with some assertions etc. And then the other two methods as an alias t.isX = node.type === "x", and same with assert?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where we would do it, but it would also be nice to have a short snippet of what that code even looks like since this page assume you know what each node is. We can also link to astexplorer as a way to test it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a quick section above that explains the terms "builder" and the difference between

t.anyTypeAnnotation();
t.isAnyTypeAnnotation(node, opts)
t.assertAnyTypeAnnotation(node, opts).

Agreed. I have split the "API" section, tentatively, to "Node Builders" and "Aliases" in #13151, maybe we can add new sections about the usage of is* and assert*.

readme.push("");
readme.push("```javascript");
readme.push(
"t." + toFunctionName(key) + "(" + t.BUILDER_KEYS[key].join(", ") + ")"
"t." + toFunctionName(key) + "(" + t.BUILDER_KEYS[key].join(", ") + ");"
);
readme.push("```");
readme.push("");
if (APIHistory[key]) {
readme.push(...formatHistory(APIHistory[key]));
readme.push("");
}
readme.push(
"See also `t.is" +
key +
Expand All @@ -53,7 +87,54 @@ Object.keys(t.BUILDER_KEYS)
"(node, opts)`."
);
readme.push("");
if (Object.keys(t.NODE_FIELDS[key]).length > 0) {
readme.push("AST Node `" + key + "` shape:");
Object.keys(t.NODE_FIELDS[key])
.sort(function (fieldA, fieldB) {
const indexA = t.BUILDER_KEYS[key].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[key].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(function (field) {
const defaultValue = t.NODE_FIELDS[key][field].default;
const fieldDescription = ["`" + field + "`"];
const validator = t.NODE_FIELDS[key][field].validate;
if (customTypes[key] && customTypes[key][field]) {
fieldDescription.push(`: ${customTypes[key][field]}`);
} else if (validator) {
try {
fieldDescription.push(
": `" + stringifyValidator(validator, "") + "`"
);
} catch (ex) {
if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") {
console.log(
"Unrecognised validator type for " + key + "." + field
);
console.dir(ex.validator, { depth: 10, colors: true });
}
}
}
if (defaultValue !== null || t.NODE_FIELDS[key][field].optional) {
fieldDescription.push(
" (default: `" + util.inspect(defaultValue) + "`"
);
if (t.BUILDER_KEYS[key].indexOf(field) < 0) {
fieldDescription.push(", excluded from builder function");
}
fieldDescription.push(")");
} else {
fieldDescription.push(" (required)");
}
readme.push("- " + fieldDescription.join(""));
});
}

if (t.ALIAS_KEYS[key] && t.ALIAS_KEYS[key].length) {
readme.push("");
readme.push(
"Aliases: " +
t.ALIAS_KEYS[key]
Expand All @@ -62,51 +143,7 @@ Object.keys(t.BUILDER_KEYS)
})
.join(", ")
);
readme.push("");
}
Object.keys(t.NODE_FIELDS[key])
.sort(function (fieldA, fieldB) {
const indexA = t.BUILDER_KEYS[key].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[key].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(function (field) {
const defaultValue = t.NODE_FIELDS[key][field].default;
const fieldDescription = ["`" + field + "`"];
const validator = t.NODE_FIELDS[key][field].validate;
if (customTypes[key] && customTypes[key][field]) {
fieldDescription.push(`: ${customTypes[key][field]}`);
} else if (validator) {
try {
fieldDescription.push(
": `" + stringifyValidator(validator, "") + "`"
);
} catch (ex) {
if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") {
console.log(
"Unrecognised validator type for " + key + "." + field
);
console.dir(ex.validator, { depth: 10, colors: true });
}
}
}
if (defaultValue !== null || t.NODE_FIELDS[key][field].optional) {
fieldDescription.push(
" (default: `" + util.inspect(defaultValue) + "`"
);
if (t.BUILDER_KEYS[key].indexOf(field) < 0) {
fieldDescription.push(", excluded from builder function");
}
fieldDescription.push(")");
} else {
fieldDescription.push(" (required)");
}
readme.push(" - " + fieldDescription.join(""));
});

readme.push("");
readme.push("---");
readme.push("");
Expand Down