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

Refactor generated builder names in @babel/types #11582

Merged
merged 4 commits into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions packages/babel-generator/test/index.js
Expand Up @@ -463,32 +463,32 @@ describe("programmatic generation", function () {

describe("typescript generate parentheses if necessary", function () {
it("wraps around union for array", () => {
const typeStatement = t.TSArrayType(
t.TSUnionType([
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
t.TSNullKeyword(),
const typeStatement = t.tsArrayType(
t.tsUnionType([
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
t.tsNullKeyword(),
]),
);
const output = generate(typeStatement).code;
expect(output).toBe("((number & boolean) | null)[]");
});
it("wraps around intersection for array", () => {
const typeStatement = t.TSArrayType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
const typeStatement = t.tsArrayType(
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("(number & boolean)[]");
});
it("wraps around rest", () => {
const typeStatement = t.tsRestType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("...(number & boolean)");
});
it("wraps around optional type", () => {
const typeStatement = t.tsOptionalType(
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
);
const output = generate(typeStatement).code;
expect(output).toBe("(number & boolean)?");
Expand Down
Expand Up @@ -25,7 +25,7 @@ exports.default = function(_ref) {
.forEach(function(decorator) {
resultantDecorator = types.callExpression(
decorator.expression,
[resultantDecorator || types.Identifier(paramUidName)]
[resultantDecorator || types.identifier(paramUidName)]
);
});

Expand All @@ -40,12 +40,12 @@ exports.default = function(_ref) {
"body",
types.variableDeclaration("var", [
types.variableDeclarator(
types.Identifier(decoratedParamUidName),
types.identifier(decoratedParamUidName),
resultantDecorator
),
])
);
param.replaceWith(types.Identifier(paramUidName));
param.replaceWith(types.identifier(paramUidName));
}
});
},
Expand Down
22 changes: 18 additions & 4 deletions packages/babel-types/scripts/generators/generateBuilders.js
Expand Up @@ -11,23 +11,37 @@ module.exports = function generateBuilders() {
*/
import builder from "../builder";\n\n`;

const reservedNames = new Set(["super", "import"]);
Object.keys(definitions.BUILDER_KEYS).forEach(type => {
output += `export function ${type}(...args: Array<any>): Object { return builder("${type}", ...args); }
export { ${type} as ${formatBuilderName(type)} };\n`;
const formatedBuilderName = formatBuilderName(type);
const formatedBuilderNameLocal = reservedNames.has(formatedBuilderName)
? `_${formatedBuilderName}`
: formatedBuilderName;
output += `${
formatedBuilderNameLocal === formatedBuilderName ? "export " : ""
}function ${formatedBuilderNameLocal}(...args: Array<any>): Object { return builder("${type}", ...args); }\n`;
// This is needed for backwards compatibility.
// arrayExpression -> ArrayExpression
output += `export { ${formatedBuilderNameLocal} as ${type} };\n`;
if (formatedBuilderNameLocal !== formatedBuilderName) {
output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`;
}

// This is needed for backwards compatibility.
// It should be removed in the next major version.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
output += `export { ${type} as ${lowerFirst(type)} }\n`;
output += `export { ${formatedBuilderNameLocal} as ${lowerFirst(
type
)} }\n`;
}
});

Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
const newType = definitions.DEPRECATED_KEYS[type];
output += `export function ${type}(...args: Array<any>): Object {
console.trace("The node type ${type} has been renamed to ${newType}");
return ${type}("${type}", ...args);
return builder("${type}", ...args);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here was a bug, which would result in code like this:

export function RestProperty(...args: Array<any>): Object {	
  console.trace("The node type RestProperty has been renamed to RestElement");
  return RestProperty("RestProperty", ...args);
}

which will cause infinite recursion when using such deprecated builder…

}
export { ${type} as ${formatBuilderName(type)} };\n`;

Expand Down