Skip to content

Commit

Permalink
[@babel/types] Moved generators related to babel-types into the babel…
Browse files Browse the repository at this point in the history
…-types package directory. (#9245)
  • Loading branch information
cameron-martin authored and nicolo-ribaudo committed Jan 8, 2019
1 parent 46e3f6d commit 778a61a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
7 changes: 4 additions & 3 deletions Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
@@ -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
Expand Down Expand Up @@ -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(", ") +
")"
Expand Down Expand Up @@ -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") {
Expand Down
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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};`
);
Expand Down
@@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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};`
);
}
}
Expand Down
@@ -1,7 +1,4 @@
exports.stringifyValidator = function stringifyValidator(
validator,
nodePrefix
) {
module.exports = function stringifyValidator(validator, nodePrefix) {
if (validator === undefined) {
return "any";
}
Expand Down Expand Up @@ -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").
Expand Down
4 changes: 4 additions & 0 deletions 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);
};

0 comments on commit 778a61a

Please sign in to comment.