From cb37f58bdc18db30b731748d39e6e0a144d3acd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 13 Jan 2020 12:24:42 -0500 Subject: [PATCH] fix: generated builder parameter should respect builder keys --- .../babel-types/scripts/generators/flow.js | 6 +++-- .../scripts/generators/typescript.js | 27 ++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/packages/babel-types/scripts/generators/flow.js b/packages/babel-types/scripts/generators/flow.js index 8b40554e2123..6c81a28af71f 100644 --- a/packages/babel-types/scripts/generators/flow.js +++ b/packages/babel-types/scripts/generators/flow.js @@ -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) => { @@ -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 + ";"); diff --git a/packages/babel-types/scripts/generators/typescript.js b/packages/babel-types/scripts/generators/typescript.js index 01e6c4782819..1b2707aff04d 100644 --- a/packages/babel-types/scripts/generators/typescript.js +++ b/packages/babel-types/scripts/generators/typescript.js @@ -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 = []; @@ -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+$/;