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 " : ""
}function ${formatedBuilderNameLocal}(${defArgs.join(
", "
)}): t.${type} { return builder("${type}", ...arguments); }\n`;
)}): t.${type} { return builder.apply("${type}", arguments); }\n`;
if (formatedBuilderNameLocal !== formatedBuilderName) {
output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`;
}
@ -119,9 +119,9 @@ import type * as t from "../..";
const newType = definitions.DEPRECATED_KEYS[type];
const formatedBuilderName = formatBuilderName(type);
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}");
return builder("${type}", ...args);
return builder.apply("${type}", arguments);
}
export { ${type} as ${formatedBuilderName} };\n`;
// 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 type * as t from "..";
export default function builder<T extends t.Node>(
type: T["type"],
...args: Array<any>
): T {
export default function builder<T extends t.Node>(this: T["type"]): T {
const type = this;
const keys = BUILDER_KEYS[type];
const countArgs = args.length;
const countArgs = arguments.length;
if (countArgs > keys.length) {
throw new Error(
`${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 };
let i = 0;
keys.forEach(key => {
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const field = NODE_FIELDS[type][key];
let arg;
if (i < countArgs) arg = args[i];
if (i < countArgs) arg = arguments[i];
if (arg === undefined) {
arg = Array.isArray(field.default) ? [] : field.default;
}
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]);
}

File diff suppressed because it is too large Load Diff

View File

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