Skip to content

Commit

Permalink
Refactor generated builder names in @babel/types (#11582)
Browse files Browse the repository at this point in the history
* ensure only builders starting with lowercase are used

* update generate builders to have function name starting flow lowercase

* fix bug in deprecated builders

* remove comment about not yet discussed change in next major version
  • Loading branch information
zxbodya committed Jul 7, 2020
1 parent 3a53f72 commit b1a8e72
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 573 deletions.
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);
}
export { ${type} as ${formatBuilderName(type)} };\n`;

Expand Down

0 comments on commit b1a8e72

Please sign in to comment.