Refactor generated builder names in @babel/types (#11582)

* 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
This commit is contained in:
Bogdan Savluk 2020-07-07 09:53:00 +02:00 committed by GitHub
parent 3a53f7244d
commit b1a8e72e16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 589 additions and 573 deletions

View File

@ -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)?");

View File

@ -25,7 +25,7 @@ exports.default = function(_ref) {
.forEach(function(decorator) {
resultantDecorator = types.callExpression(
decorator.expression,
[resultantDecorator || types.Identifier(paramUidName)]
[resultantDecorator || types.identifier(paramUidName)]
);
});
@ -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));
}
});
},

View File

@ -11,15 +11,29 @@ 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`;
}
});
@ -27,7 +41,7 @@ export { ${type} as ${formatBuilderName(type)} };\n`;
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`;

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
import { TSUnionType } from "../generated";
import { tsUnionType } from "../generated";
import removeTypeDuplicates from "../../modifications/typescript/removeTypeDuplicates";
/**
@ -14,6 +14,6 @@ export default function createTSUnionType(
if (flattened.length === 1) {
return flattened[0];
} else {
return TSUnionType(flattened);
return tsUnionType(flattened);
}
}