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:
parent
3a53f7244d
commit
b1a8e72e16
@ -463,32 +463,32 @@ describe("programmatic generation", function () {
|
|||||||
|
|
||||||
describe("typescript generate parentheses if necessary", function () {
|
describe("typescript generate parentheses if necessary", function () {
|
||||||
it("wraps around union for array", () => {
|
it("wraps around union for array", () => {
|
||||||
const typeStatement = t.TSArrayType(
|
const typeStatement = t.tsArrayType(
|
||||||
t.TSUnionType([
|
t.tsUnionType([
|
||||||
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
|
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
|
||||||
t.TSNullKeyword(),
|
t.tsNullKeyword(),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
const output = generate(typeStatement).code;
|
const output = generate(typeStatement).code;
|
||||||
expect(output).toBe("((number & boolean) | null)[]");
|
expect(output).toBe("((number & boolean) | null)[]");
|
||||||
});
|
});
|
||||||
it("wraps around intersection for array", () => {
|
it("wraps around intersection for array", () => {
|
||||||
const typeStatement = t.TSArrayType(
|
const typeStatement = t.tsArrayType(
|
||||||
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
|
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
|
||||||
);
|
);
|
||||||
const output = generate(typeStatement).code;
|
const output = generate(typeStatement).code;
|
||||||
expect(output).toBe("(number & boolean)[]");
|
expect(output).toBe("(number & boolean)[]");
|
||||||
});
|
});
|
||||||
it("wraps around rest", () => {
|
it("wraps around rest", () => {
|
||||||
const typeStatement = t.tsRestType(
|
const typeStatement = t.tsRestType(
|
||||||
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
|
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
|
||||||
);
|
);
|
||||||
const output = generate(typeStatement).code;
|
const output = generate(typeStatement).code;
|
||||||
expect(output).toBe("...(number & boolean)");
|
expect(output).toBe("...(number & boolean)");
|
||||||
});
|
});
|
||||||
it("wraps around optional type", () => {
|
it("wraps around optional type", () => {
|
||||||
const typeStatement = t.tsOptionalType(
|
const typeStatement = t.tsOptionalType(
|
||||||
t.TSIntersectionType([t.TSNumberKeyword(), t.TSBooleanKeyword()]),
|
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]),
|
||||||
);
|
);
|
||||||
const output = generate(typeStatement).code;
|
const output = generate(typeStatement).code;
|
||||||
expect(output).toBe("(number & boolean)?");
|
expect(output).toBe("(number & boolean)?");
|
||||||
|
|||||||
@ -25,7 +25,7 @@ exports.default = function(_ref) {
|
|||||||
.forEach(function(decorator) {
|
.forEach(function(decorator) {
|
||||||
resultantDecorator = types.callExpression(
|
resultantDecorator = types.callExpression(
|
||||||
decorator.expression,
|
decorator.expression,
|
||||||
[resultantDecorator || types.Identifier(paramUidName)]
|
[resultantDecorator || types.identifier(paramUidName)]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -40,12 +40,12 @@ exports.default = function(_ref) {
|
|||||||
"body",
|
"body",
|
||||||
types.variableDeclaration("var", [
|
types.variableDeclaration("var", [
|
||||||
types.variableDeclarator(
|
types.variableDeclarator(
|
||||||
types.Identifier(decoratedParamUidName),
|
types.identifier(decoratedParamUidName),
|
||||||
resultantDecorator
|
resultantDecorator
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
param.replaceWith(types.Identifier(paramUidName));
|
param.replaceWith(types.identifier(paramUidName));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@ -11,15 +11,29 @@ module.exports = function generateBuilders() {
|
|||||||
*/
|
*/
|
||||||
import builder from "../builder";\n\n`;
|
import builder from "../builder";\n\n`;
|
||||||
|
|
||||||
|
const reservedNames = new Set(["super", "import"]);
|
||||||
Object.keys(definitions.BUILDER_KEYS).forEach(type => {
|
Object.keys(definitions.BUILDER_KEYS).forEach(type => {
|
||||||
output += `export function ${type}(...args: Array<any>): Object { return builder("${type}", ...args); }
|
const formatedBuilderName = formatBuilderName(type);
|
||||||
export { ${type} as ${formatBuilderName(type)} };\n`;
|
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.
|
// This is needed for backwards compatibility.
|
||||||
// It should be removed in the next major version.
|
// It should be removed in the next major version.
|
||||||
// JSXIdentifier -> jSXIdentifier
|
// JSXIdentifier -> jSXIdentifier
|
||||||
if (/^[A-Z]{2}/.test(type)) {
|
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];
|
const newType = definitions.DEPRECATED_KEYS[type];
|
||||||
output += `export function ${type}(...args: Array<any>): Object {
|
output += `export function ${type}(...args: Array<any>): Object {
|
||||||
console.trace("The node type ${type} has been renamed to ${newType}");
|
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`;
|
export { ${type} as ${formatBuilderName(type)} };\n`;
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
|||||||
import { TSUnionType } from "../generated";
|
import { tsUnionType } from "../generated";
|
||||||
import removeTypeDuplicates from "../../modifications/typescript/removeTypeDuplicates";
|
import removeTypeDuplicates from "../../modifications/typescript/removeTypeDuplicates";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,6 +14,6 @@ export default function createTSUnionType(
|
|||||||
if (flattened.length === 1) {
|
if (flattened.length === 1) {
|
||||||
return flattened[0];
|
return flattened[0];
|
||||||
} else {
|
} else {
|
||||||
return TSUnionType(flattened);
|
return tsUnionType(flattened);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user