fix: generated builder parameter should respect builder keys (#11002)

This commit is contained in:
Huáng Jùnliàng 2020-01-13 16:30:24 -05:00 committed by Nicolò Ribaudo
parent 8fce431d88
commit 6874c244ab
2 changed files with 19 additions and 14 deletions

View File

@ -54,6 +54,7 @@ for (const type in t.NODE_FIELDS) {
const struct = ['type: "' + type + '";']; const struct = ['type: "' + type + '";'];
const args = []; const args = [];
const builderNames = t.BUILDER_KEYS[type];
Object.keys(t.NODE_FIELDS[type]) Object.keys(t.NODE_FIELDS[type])
.sort((fieldA, fieldB) => { .sort((fieldA, fieldB) => {
@ -80,8 +81,9 @@ for (const type in t.NODE_FIELDS) {
if (typeAnnotation) { if (typeAnnotation) {
suffix += ": " + typeAnnotation; suffix += ": " + typeAnnotation;
} }
if (builderNames.includes(fieldName)) {
args.push(t.toBindingIdentifierName(fieldName) + suffix); args.push(t.toBindingIdentifierName(fieldName) + suffix);
}
if (t.isValidIdentifier(fieldName)) { if (t.isValidIdentifier(fieldName)) {
struct.push(fieldName + suffix + ";"); struct.push(fieldName + suffix + ";");

View File

@ -56,6 +56,7 @@ const lines = [];
for (const type in t.NODE_FIELDS) { for (const type in t.NODE_FIELDS) {
const fields = t.NODE_FIELDS[type]; const fields = t.NODE_FIELDS[type];
const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type); const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
const builderNames = t.BUILDER_KEYS[type];
const struct = ['type: "' + type + '";']; const struct = ['type: "' + type + '";'];
const args = []; const args = [];
@ -75,18 +76,20 @@ for (const type in t.NODE_FIELDS) {
typeAnnotation += " | null"; typeAnnotation += " | null";
} }
if (areAllRemainingFieldsNullable(fieldName, fieldNames, fields)) { if (builderNames.includes(fieldName)) {
args.push( if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) {
`${t.toBindingIdentifierName(fieldName)}${ args.push(
isNullable(field) ? "?:" : ":" `${t.toBindingIdentifierName(fieldName)}${
} ${typeAnnotation}` isNullable(field) ? "?:" : ":"
); } ${typeAnnotation}`
} else { );
args.push( } else {
`${t.toBindingIdentifierName(fieldName)}: ${typeAnnotation}${ args.push(
isNullable(field) ? " | undefined" : "" `${t.toBindingIdentifierName(fieldName)}: ${typeAnnotation}${
}` isNullable(field) ? " | undefined" : ""
); }`
);
}
} }
const alphaNumeric = /^\w+$/; const alphaNumeric = /^\w+$/;