Simplify (transpiled) babel-types builder wrappers (#13843)

This commit is contained in:
Mickey Rose 2021-10-21 18:55:42 +02:00 committed by GitHub
parent 362c623a1c
commit 35ec4394a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 272 additions and 271 deletions

View File

@ -100,7 +100,7 @@ import type * as t from "../..";
formatedBuilderNameLocal === formatedBuilderName ? "export " : "" formatedBuilderNameLocal === formatedBuilderName ? "export " : ""
}function ${formatedBuilderNameLocal}(${defArgs.join( }function ${formatedBuilderNameLocal}(${defArgs.join(
", " ", "
)}): t.${type} { return builder("${type}", ...arguments); }\n`; )}): t.${type} { return builder.apply("${type}", arguments); }\n`;
if (formatedBuilderNameLocal !== formatedBuilderName) { if (formatedBuilderNameLocal !== formatedBuilderName) {
output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`; output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`;
} }
@ -119,9 +119,9 @@ import type * as t from "../..";
const newType = definitions.DEPRECATED_KEYS[type]; const newType = definitions.DEPRECATED_KEYS[type];
const formatedBuilderName = formatBuilderName(type); const formatedBuilderName = formatBuilderName(type);
output += `/** @deprecated */ output += `/** @deprecated */
function ${type}(...args: Array<any>): any { function ${type}(${generateBuilderArgs(newType).join(", ")}): t.${type} {
console.trace("The node type ${type} has been renamed to ${newType}"); console.trace("The node type ${type} has been renamed to ${newType}");
return builder("${type}", ...args); return builder.apply("${type}", arguments);
} }
export { ${type} as ${formatedBuilderName} };\n`; export { ${type} as ${formatedBuilderName} };\n`;
// This is needed for backwards compatibility. // This is needed for backwards compatibility.

View File

@ -2,12 +2,10 @@ import { NODE_FIELDS, BUILDER_KEYS } from "../definitions";
import validate from "../validators/validate"; import validate from "../validators/validate";
import type * as t from ".."; import type * as t from "..";
export default function builder<T extends t.Node>( export default function builder<T extends t.Node>(this: T["type"]): T {
type: T["type"], const type = this;
...args: Array<any>
): T {
const keys = BUILDER_KEYS[type]; const keys = BUILDER_KEYS[type];
const countArgs = args.length; const countArgs = arguments.length;
if (countArgs > keys.length) { if (countArgs > keys.length) {
throw new Error( throw new Error(
`${type}: Too many arguments passed. Received ${countArgs} but can receive no more than ${keys.length}`, `${type}: Too many arguments passed. Received ${countArgs} but can receive no more than ${keys.length}`,
@ -16,21 +14,22 @@ export default function builder<T extends t.Node>(
const node = { type }; const node = { type };
let i = 0; for (let i = 0; i < keys.length; ++i) {
keys.forEach(key => { const key = keys[i];
const field = NODE_FIELDS[type][key]; const field = NODE_FIELDS[type][key];
let arg; let arg;
if (i < countArgs) arg = args[i]; if (i < countArgs) arg = arguments[i];
if (arg === undefined) { if (arg === undefined) {
arg = Array.isArray(field.default) ? [] : field.default; arg = Array.isArray(field.default) ? [] : field.default;
} }
node[key] = arg; node[key] = arg;
i++; }
});
for (const key of Object.keys(node)) { // (assume all enumerable properties are own)
// eslint-disable-next-line guard-for-in
for (const key in node) {
validate(node as t.Node, key, node[key]); validate(node as t.Node, key, node[key]);
} }

File diff suppressed because it is too large Load Diff

View File

@ -31,9 +31,11 @@ toFastProperties(DEPRECATED_KEYS);
toFastProperties(PLACEHOLDERS_ALIAS); toFastProperties(PLACEHOLDERS_ALIAS);
toFastProperties(PLACEHOLDERS_FLIPPED_ALIAS); toFastProperties(PLACEHOLDERS_FLIPPED_ALIAS);
const TYPES: Array<string> = Object.keys(VISITOR_KEYS) const TYPES: Array<string> = [].concat(
.concat(Object.keys(FLIPPED_ALIAS_KEYS)) Object.keys(VISITOR_KEYS),
.concat(Object.keys(DEPRECATED_KEYS)); Object.keys(FLIPPED_ALIAS_KEYS),
Object.keys(DEPRECATED_KEYS),
);
export { export {
VISITOR_KEYS, VISITOR_KEYS,