From 778a61a3c2ee367e598da8e6c3a2ab9ffe85a064 Mon Sep 17 00:00:00 2001 From: Cameron Martin Date: Tue, 8 Jan 2019 23:14:31 +0000 Subject: [PATCH] [@babel/types] Moved generators related to babel-types into the babel-types package directory. (#9245) --- Makefile | 7 ++++--- .../babel-types/scripts}/generators/docs.js | 9 +++++---- .../babel-types/scripts}/generators/flow.js | 11 ++++++----- .../babel-types/scripts}/generators/typescript.js | 13 ++++++------- .../babel-types/scripts/utils/stringifyValidator.js | 10 +--------- .../babel-types/scripts/utils/toFunctionName.js | 4 ++++ 6 files changed, 26 insertions(+), 28 deletions(-) rename {scripts => packages/babel-types/scripts}/generators/docs.js (92%) rename {scripts => packages/babel-types/scripts}/generators/flow.js (90%) rename {scripts => packages/babel-types/scripts}/generators/typescript.js (92%) rename scripts/generators/utils.js => packages/babel-types/scripts/utils/stringifyValidator.js (77%) create mode 100644 packages/babel-types/scripts/utils/toFunctionName.js diff --git a/Makefile b/Makefile index 5f9edf90b2..e7e8663f09 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,8 @@ build: clean clean-lib # call build again as the generated files might need to be compiled again. ./node_modules/.bin/gulp build # generate flow and typescript typings - node scripts/generators/flow.js > ./packages/babel-types/lib/index.js.flow - node scripts/generators/typescript.js > ./packages/babel-types/lib/index.d.ts + node packages/babel-types/scripts/generators/flow.js > ./packages/babel-types/lib/index.js.flow + node packages/babel-types/scripts/generators/typescript.js > ./packages/babel-types/lib/index.d.ts ifneq ("$(BABEL_COVERAGE)", "true") make build-standalone make build-preset-env-standalone @@ -46,7 +46,8 @@ watch: clean clean-lib # development too. BABEL_ENV=development ./node_modules/.bin/gulp build-no-bundle node ./packages/babel-types/scripts/generateTypeHelpers.js - node scripts/generators/flow.js > ./packages/babel-types/lib/index.js.flow + node packages/babel-types/scripts/generators/flow.js > ./packages/babel-types/lib/index.js.flow + node packages/babel-types/scripts/generators/typescript.js > ./packages/babel-types/lib/index.d.ts BABEL_ENV=development ./node_modules/.bin/gulp watch flow: diff --git a/scripts/generators/docs.js b/packages/babel-types/scripts/generators/docs.js similarity index 92% rename from scripts/generators/docs.js rename to packages/babel-types/scripts/generators/docs.js index a4f9e6c12b..3bbb523624 100644 --- a/scripts/generators/docs.js +++ b/packages/babel-types/scripts/generators/docs.js @@ -1,9 +1,10 @@ "use strict"; const util = require("util"); -const utils = require("./utils"); +const stringifyValidator = require("../utils/stringifyValidator"); +const toFunctionName = require("../utils/toFunctionName"); -const types = require("../../packages/babel-types"); +const types = require("../../"); const readme = [ `# @babel/types @@ -43,7 +44,7 @@ Object.keys(types.BUILDER_KEYS) readme.push("```javascript"); readme.push( "t." + - utils.toFunctionName(key) + + toFunctionName(key) + "(" + types.BUILDER_KEYS[key].join(", ") + ")" @@ -87,7 +88,7 @@ Object.keys(types.BUILDER_KEYS) } else if (validator) { try { fieldDescription.push( - ": `" + utils.stringifyValidator(validator, "") + "`" + ": `" + stringifyValidator(validator, "") + "`" ); } catch (ex) { if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") { diff --git a/scripts/generators/flow.js b/packages/babel-types/scripts/generators/flow.js similarity index 90% rename from scripts/generators/flow.js rename to packages/babel-types/scripts/generators/flow.js index 55fc28292c..1ef30ad55b 100644 --- a/scripts/generators/flow.js +++ b/packages/babel-types/scripts/generators/flow.js @@ -1,12 +1,13 @@ "use strict"; -const t = require("../../packages/babel-types"); -const utils = require("./utils"); +const t = require("../../"); +const stringifyValidator = require("../utils/stringifyValidator"); +const toFunctionName = require("../utils/toFunctionName"); const NODE_PREFIX = "BabelNode"; let code = `// NOTE: This file is autogenerated. Do not modify. -// See scripts/generators/flow.js for script used. +// See packages/babel-types/scripts/generators/flow.js for script used. declare class ${NODE_PREFIX}Comment { value: string; @@ -73,7 +74,7 @@ for (const type in t.NODE_FIELDS) { const validate = field.validate; if (validate) { - typeAnnotation = utils.stringifyValidator(validate, NODE_PREFIX); + typeAnnotation = stringifyValidator(validate, NODE_PREFIX); } if (typeAnnotation) { @@ -94,7 +95,7 @@ for (const type in t.NODE_FIELDS) { // Flow chokes on super() and import() :/ if (type !== "Super" && type !== "Import") { lines.push( - `declare function ${utils.toFunctionName(type)}(${args.join( + `declare function ${toFunctionName(type)}(${args.join( ", " )}): ${NODE_PREFIX}${type};` ); diff --git a/scripts/generators/typescript.js b/packages/babel-types/scripts/generators/typescript.js similarity index 92% rename from scripts/generators/typescript.js rename to packages/babel-types/scripts/generators/typescript.js index 5b4ea4fe3e..9a286f2086 100644 --- a/scripts/generators/typescript.js +++ b/packages/babel-types/scripts/generators/typescript.js @@ -1,10 +1,11 @@ "use strict"; -const t = require("../../packages/babel-types"); -const utils = require("./utils"); +const t = require("../../"); +const stringifyValidator = require("../utils/stringifyValidator"); +const toFunctionName = require("../utils/toFunctionName"); let code = `// NOTE: This file is autogenerated. Do not modify. -// See scripts/generators/typescript.js for script used. +// See packages/babel-types/scripts/generators/typescript.js for script used. interface BaseComment { value: string; @@ -61,7 +62,7 @@ for (const type in t.NODE_FIELDS) { fieldNames.forEach(fieldName => { const field = fields[fieldName]; - let typeAnnotation = utils.stringifyValidator(field.validate, ""); + let typeAnnotation = stringifyValidator(field.validate, ""); if (isNullable(field) && !hasDefault(field)) { typeAnnotation += " | null"; @@ -97,9 +98,7 @@ for (const type in t.NODE_FIELDS) { // super and import are reserved words in JavaScript if (type !== "Super" && type !== "Import") { lines.push( - `export function ${utils.toFunctionName(type)}(${args.join( - ", " - )}): ${type};` + `export function ${toFunctionName(type)}(${args.join(", ")}): ${type};` ); } } diff --git a/scripts/generators/utils.js b/packages/babel-types/scripts/utils/stringifyValidator.js similarity index 77% rename from scripts/generators/utils.js rename to packages/babel-types/scripts/utils/stringifyValidator.js index 4f217edfcb..ff33e8e25a 100644 --- a/scripts/generators/utils.js +++ b/packages/babel-types/scripts/utils/stringifyValidator.js @@ -1,7 +1,4 @@ -exports.stringifyValidator = function stringifyValidator( - validator, - nodePrefix -) { +module.exports = function stringifyValidator(validator, nodePrefix) { if (validator === undefined) { return "any"; } @@ -37,11 +34,6 @@ exports.stringifyValidator = function stringifyValidator( return ["any"]; }; -exports.toFunctionName = function toFunctionName(typeName) { - const _ = typeName.replace(/^TS/, "ts").replace(/^JSX/, "jsx"); - return _.slice(0, 1).toLowerCase() + _.slice(1); -}; - /** * Heuristic to decide whether or not the given type is a value type (eg. "null") * or a Node type (eg. "Expression"). diff --git a/packages/babel-types/scripts/utils/toFunctionName.js b/packages/babel-types/scripts/utils/toFunctionName.js new file mode 100644 index 0000000000..627c9a7d8f --- /dev/null +++ b/packages/babel-types/scripts/utils/toFunctionName.js @@ -0,0 +1,4 @@ +module.exports = function toFunctionName(typeName) { + const _ = typeName.replace(/^TS/, "ts").replace(/^JSX/, "jsx"); + return _.slice(0, 1).toLowerCase() + _.slice(1); +};