New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@babel/types builder improvements #14519
Changes from all commits
1302e69
293f3fd
f21d5f5
050dd50
9c87b43
7e540ff
91502a3
4f2bb2d
6e1ff5d
8dff475
06411a5
181b281
c63fffb
1a56005
fbadf00
2600b5a
38703fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,20 +51,23 @@ function generateBuilderArgs(type) { | |
} | ||
|
||
if (builderNames.includes(fieldName)) { | ||
const field = definitions.NODE_FIELDS[type][fieldName]; | ||
const def = JSON.stringify(field.default); | ||
const bindingIdentifierName = t.toBindingIdentifierName(fieldName); | ||
let arg; | ||
if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) { | ||
args.push( | ||
`${bindingIdentifierName}${ | ||
isNullable(field) ? "?:" : ":" | ||
} ${typeAnnotation}` | ||
); | ||
arg = `${bindingIdentifierName}${ | ||
isNullable(field) && !def ? "?:" : ":" | ||
} ${typeAnnotation}`; | ||
} else { | ||
args.push( | ||
`${bindingIdentifierName}: ${typeAnnotation}${ | ||
isNullable(field) ? " | undefined" : "" | ||
}` | ||
); | ||
arg = `${bindingIdentifierName}: ${typeAnnotation}${ | ||
isNullable(field) ? " | undefined" : "" | ||
}`; | ||
} | ||
if (def !== "null" || isNullable(field)) { | ||
arg += `= ${def}`; | ||
} | ||
args.push(arg); | ||
} | ||
}); | ||
|
||
|
@@ -82,11 +85,8 @@ function generateLowercaseBuilders() { | |
* This file is auto-generated! Do not modify it directly. | ||
* To re-generate run 'make build' | ||
*/ | ||
import builder from "../builder"; | ||
import validateNode from "../validateNode"; | ||
import type * as t from "../.."; | ||
|
||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
`; | ||
|
||
const reservedNames = new Set(["super", "import"]); | ||
|
@@ -96,11 +96,39 @@ import type * as t from "../.."; | |
const formatedBuilderNameLocal = reservedNames.has(formatedBuilderName) | ||
? `_${formatedBuilderName}` | ||
: formatedBuilderName; | ||
|
||
const fieldNames = sortFieldNames( | ||
Object.keys(definitions.NODE_FIELDS[type]), | ||
type | ||
); | ||
const builderNames = definitions.BUILDER_KEYS[type]; | ||
const objectFields = [["type", JSON.stringify(type)]]; | ||
fieldNames.forEach(fieldName => { | ||
const field = definitions.NODE_FIELDS[type][fieldName]; | ||
if (builderNames.includes(fieldName)) { | ||
const bindingIdentifierName = t.toBindingIdentifierName(fieldName); | ||
objectFields.push([fieldName, bindingIdentifierName]); | ||
} else if (!field.optional) { | ||
const def = JSON.stringify(field.default); | ||
objectFields.push([fieldName, def]); | ||
} | ||
}); | ||
|
||
output += `${ | ||
formatedBuilderNameLocal === formatedBuilderName ? "export " : "" | ||
}function ${formatedBuilderNameLocal}(${defArgs.join( | ||
", " | ||
)}): t.${type} { return builder.apply("${type}", arguments); }\n`; | ||
}function ${formatedBuilderNameLocal}(${defArgs.join(", ")}): t.${type} {`; | ||
|
||
const nodeObjectExpression = `{\n${objectFields | ||
.map(([k, v]) => (k === v ? ` ${k},` : ` ${k}: ${v},`)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: since these just come from local param names, can't we make it always match? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the argument name match the AST field name. The exceptions are non-valid binding identifier names, such as CallExpression's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhh |
||
.join("\n")}\n }`; | ||
|
||
if (builderNames.length > 0) { | ||
output += `\n return validateNode<t.${type}>(${nodeObjectExpression});`; | ||
} else { | ||
output += `\n return ${nodeObjectExpression};`; | ||
} | ||
output += `\n}\n`; | ||
|
||
if (formatedBuilderNameLocal !== formatedBuilderName) { | ||
output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`; | ||
} | ||
|
@@ -118,10 +146,11 @@ import type * as t from "../.."; | |
Object.keys(definitions.DEPRECATED_KEYS).forEach(type => { | ||
const newType = definitions.DEPRECATED_KEYS[type]; | ||
const formatedBuilderName = formatBuilderName(type); | ||
const formatedNewBuilderName = formatBuilderName(newType); | ||
output += `/** @deprecated */ | ||
function ${type}(${generateBuilderArgs(newType).join(", ")}): t.${type} { | ||
function ${type}(${generateBuilderArgs(newType).join(", ")}) { | ||
console.trace("The node type ${type} has been renamed to ${newType}"); | ||
return builder.apply("${type}", arguments); | ||
return ${formatedNewBuilderName}(${t.BUILDER_KEYS[newType].join(", ")}); | ||
} | ||
export { ${type} as ${formatedBuilderName} };\n`; | ||
// This is needed for backwards compatibility. | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type annotation is preserved because we skip
validateNode
for nodes with empty builder keys.