Skip to content

Commit

Permalink
fix: generated builder parameter should respect builder keys (#11002)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored and nicolo-ribaudo committed Jan 13, 2020
1 parent 8fce431 commit 6874c24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
6 changes: 4 additions & 2 deletions packages/babel-types/scripts/generators/flow.js
Expand Up @@ -54,6 +54,7 @@ for (const type in t.NODE_FIELDS) {

const struct = ['type: "' + type + '";'];
const args = [];
const builderNames = t.BUILDER_KEYS[type];

Object.keys(t.NODE_FIELDS[type])
.sort((fieldA, fieldB) => {
Expand All @@ -80,8 +81,9 @@ for (const type in t.NODE_FIELDS) {
if (typeAnnotation) {
suffix += ": " + typeAnnotation;
}

args.push(t.toBindingIdentifierName(fieldName) + suffix);
if (builderNames.includes(fieldName)) {
args.push(t.toBindingIdentifierName(fieldName) + suffix);
}

if (t.isValidIdentifier(fieldName)) {
struct.push(fieldName + suffix + ";");
Expand Down
27 changes: 15 additions & 12 deletions packages/babel-types/scripts/generators/typescript.js
Expand Up @@ -56,6 +56,7 @@ const lines = [];
for (const type in t.NODE_FIELDS) {
const fields = t.NODE_FIELDS[type];
const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
const builderNames = t.BUILDER_KEYS[type];

const struct = ['type: "' + type + '";'];
const args = [];
Expand All @@ -75,18 +76,20 @@ for (const type in t.NODE_FIELDS) {
typeAnnotation += " | null";
}

if (areAllRemainingFieldsNullable(fieldName, fieldNames, fields)) {
args.push(
`${t.toBindingIdentifierName(fieldName)}${
isNullable(field) ? "?:" : ":"
} ${typeAnnotation}`
);
} else {
args.push(
`${t.toBindingIdentifierName(fieldName)}: ${typeAnnotation}${
isNullable(field) ? " | undefined" : ""
}`
);
if (builderNames.includes(fieldName)) {
if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) {
args.push(
`${t.toBindingIdentifierName(fieldName)}${
isNullable(field) ? "?:" : ":"
} ${typeAnnotation}`
);
} else {
args.push(
`${t.toBindingIdentifierName(fieldName)}: ${typeAnnotation}${
isNullable(field) ? " | undefined" : ""
}`
);
}
}

const alphaNumeric = /^\w+$/;
Expand Down

0 comments on commit 6874c24

Please sign in to comment.