Commit 484dc1df authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

add required modules

parent 5e7b89bb
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isNodesEquivalent;
var _definitions = require("../definitions");
function isNodesEquivalent(a, b) {
if (typeof a !== "object" || typeof b !== "object" || a == null || b == null) {
return a === b;
}
if (a.type !== b.type) {
return false;
}
const fields = Object.keys(_definitions.NODE_FIELDS[a.type] || a.type);
const visitorKeys = _definitions.VISITOR_KEYS[a.type];
for (const field of fields) {
const val_a = a[field];
const val_b = b[field];
if (typeof val_a !== typeof val_b) {
return false;
}
if (val_a == null && val_b == null) {
continue;
} else if (val_a == null || val_b == null) {
return false;
}
if (Array.isArray(val_a)) {
if (!Array.isArray(val_b)) {
return false;
}
if (val_a.length !== val_b.length) {
return false;
}
for (let i = 0; i < val_a.length; i++) {
if (!isNodesEquivalent(val_a[i], val_b[i])) {
return false;
}
}
continue;
}
if (typeof val_a === "object" && !(visitorKeys != null && visitorKeys.includes(field))) {
for (const key of Object.keys(val_a)) {
if (val_a[key] !== val_b[key]) {
return false;
}
}
continue;
}
if (!isNodesEquivalent(val_a, val_b)) {
return false;
}
}
return true;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isPlaceholderType;
var _definitions = require("../definitions");
function isPlaceholderType(placeholderType, targetType) {
if (placeholderType === targetType) return true;
const aliases = _definitions.PLACEHOLDERS_ALIAS[placeholderType];
if (aliases) {
for (const alias of aliases) {
if (targetType === alias) return true;
}
}
return false;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isReferenced;
function isReferenced(node, parent, grandparent) {
switch (parent.type) {
case "MemberExpression":
case "OptionalMemberExpression":
if (parent.property === node) {
return !!parent.computed;
}
return parent.object === node;
case "JSXMemberExpression":
return parent.object === node;
case "VariableDeclarator":
return parent.init === node;
case "ArrowFunctionExpression":
return parent.body === node;
case "PrivateName":
return false;
case "ClassMethod":
case "ClassPrivateMethod":
case "ObjectMethod":
if (parent.key === node) {
return !!parent.computed;
}
return false;
case "ObjectProperty":
if (parent.key === node) {
return !!parent.computed;
}
return !grandparent || grandparent.type !== "ObjectPattern";
case "ClassProperty":
case "ClassAccessorProperty":
if (parent.key === node) {
return !!parent.computed;
}
return true;
case "ClassPrivateProperty":
return parent.key !== node;
case "ClassDeclaration":
case "ClassExpression":
return parent.superClass === node;
case "AssignmentExpression":
return parent.right === node;
case "AssignmentPattern":
return parent.right === node;
case "LabeledStatement":
return false;
case "CatchClause":
return false;
case "RestElement":
return false;
case "BreakStatement":
case "ContinueStatement":
return false;
case "FunctionDeclaration":
case "FunctionExpression":
return false;
case "ExportNamespaceSpecifier":
case "ExportDefaultSpecifier":
return false;
case "ExportSpecifier":
if (grandparent != null && grandparent.source) {
return false;
}
return parent.local === node;
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
case "ImportSpecifier":
return false;
case "ImportAttribute":
return false;
case "JSXAttribute":
return false;
case "ObjectPattern":
case "ArrayPattern":
return false;
case "MetaProperty":
return false;
case "ObjectTypeProperty":
return parent.key !== node;
case "TSEnumMember":
return parent.id !== node;
case "TSPropertySignature":
if (parent.key === node) {
return !!parent.computed;
}
return true;
}
return true;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isScope;
var _generated = require("./generated");
function isScope(node, parent) {
if ((0, _generated.isBlockStatement)(node) && ((0, _generated.isFunction)(parent) || (0, _generated.isCatchClause)(parent))) {
return false;
}
if ((0, _generated.isPattern)(node) && ((0, _generated.isFunction)(parent) || (0, _generated.isCatchClause)(parent))) {
return true;
}
return (0, _generated.isScopable)(node);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isSpecifierDefault;
var _generated = require("./generated");
function isSpecifierDefault(specifier) {
return (0, _generated.isImportDefaultSpecifier)(specifier) || (0, _generated.isIdentifier)(specifier.imported || specifier.exported, {
name: "default"
});
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isType;
var _definitions = require("../definitions");
function isType(nodeType, targetType) {
if (nodeType === targetType) return true;
if (_definitions.ALIAS_KEYS[targetType]) return false;
const aliases = _definitions.FLIPPED_ALIAS_KEYS[targetType];
if (aliases) {
if (aliases[0] === nodeType) return true;
for (const alias of aliases) {
if (nodeType === alias) return true;
}
}
return false;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isValidES3Identifier;
var _isValidIdentifier = require("./isValidIdentifier");
const RESERVED_WORDS_ES3_ONLY = new Set(["abstract", "boolean", "byte", "char", "double", "enum", "final", "float", "goto", "implements", "int", "interface", "long", "native", "package", "private", "protected", "public", "short", "static", "synchronized", "throws", "transient", "volatile"]);
function isValidES3Identifier(name) {
return (0, _isValidIdentifier.default)(name) && !RESERVED_WORDS_ES3_ONLY.has(name);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isValidIdentifier;
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
function isValidIdentifier(name, reserved = true) {
if (typeof name !== "string") return false;
if (reserved) {
if ((0, _helperValidatorIdentifier.isKeyword)(name) || (0, _helperValidatorIdentifier.isStrictReservedWord)(name, true)) {
return false;
}
}
return (0, _helperValidatorIdentifier.isIdentifierName)(name);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isVar;
var _generated = require("./generated");
var _constants = require("../constants");
function isVar(node) {
return (0, _generated.isVariableDeclaration)(node, {
kind: "var"
}) && !node[_constants.BLOCK_SCOPED_SYMBOL];
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = matchesPattern;
var _generated = require("./generated");
function matchesPattern(member, match, allowPartial) {
if (!(0, _generated.isMemberExpression)(member)) return false;
const parts = Array.isArray(match) ? match : match.split(".");
const nodes = [];
let node;
for (node = member; (0, _generated.isMemberExpression)(node); node = node.object) {
nodes.push(node.property);
}
nodes.push(node);
if (nodes.length < parts.length) return false;
if (!allowPartial && nodes.length > parts.length) return false;
for (let i = 0, j = nodes.length - 1; i < parts.length; i++, j--) {
const node = nodes[j];
let value;
if ((0, _generated.isIdentifier)(node)) {
value = node.name;
} else if ((0, _generated.isStringLiteral)(node)) {
value = node.value;
} else if ((0, _generated.isThisExpression)(node)) {
value = "this";
} else {
return false;
}
if (parts[i] !== value) return false;
}
return true;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isCompatTag;
function isCompatTag(tagName) {
return !!tagName && /^[a-z]/.test(tagName);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _buildMatchMemberExpression = require("../buildMatchMemberExpression");
const isReactComponent = (0, _buildMatchMemberExpression.default)("React.Component");
var _default = isReactComponent;
exports.default = _default;
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = validate;
exports.validateChild = validateChild;
exports.validateField = validateField;
var _definitions = require("../definitions");
function validate(node, key, val) {
if (!node) return;
const fields = _definitions.NODE_FIELDS[node.type];
if (!fields) return;
const field = fields[key];
validateField(node, key, val, field);
validateChild(node, key, val);
}
function validateField(node, key, val, field) {
if (!(field != null && field.validate)) return;
if (field.optional && val == null) return;
field.validate(node, key, val);
}
function validateChild(node, key, val) {
if (val == null) return;
const validate = _definitions.NODE_PARENT_VALIDATIONS[val.type];
if (!validate) return;
validate(node, key, val);
}
\ No newline at end of file
{
"name": "@babel/types",
"version": "7.18.7",
"description": "Babel Types is a Lodash-esque utility library for AST nodes",
"author": "The Babel Team (https://babel.dev/team)",
"homepage": "https://babel.dev/docs/en/next/babel-types",
"bugs": "https://github.com/babel/babel/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3A%22pkg%3A%20types%22+is%3Aopen",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-types"
},
"main": "./lib/index.js",
"types": "./lib/index-legacy.d.ts",
"typesVersions": {
">=3.7": {
"lib/index-legacy.d.ts": [
"lib/index.d.ts"
]
}
},
"dependencies": {
"@babel/helper-validator-identifier": "^7.18.6",
"to-fast-properties": "^2.0.0"
},
"devDependencies": {
"@babel/generator": "^7.18.7",
"@babel/parser": "^7.18.6",
"chalk": "^4.1.0",
"glob": "^7.1.7"
},
"engines": {
"node": ">=6.9.0"
},
"type": "commonjs"
}
\ No newline at end of file
import * as definitions from "../../lib/definitions/index.js";
function addAssertHelper(type) {
const result =
definitions.NODE_FIELDS[type] || definitions.FLIPPED_ALIAS_KEYS[type]
? `node is t.${type}`
: "boolean";
return `export function assert${type}(node: object | null | undefined, opts?: object | null): asserts ${
result === "boolean" ? "node" : result
} {
assert("${type}", node, opts) }
`;
}
export default function generateAsserts() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import is from "../../validators/is";
import type * as t from "../..";
function assert(type: string, node: any, opts?: any): void {
if (!is(type, node, opts)) {
throw new Error(
\`Expected type "\${type}" with option \${JSON.stringify(opts)}, \` +
\`but instead got "\${node.type}".\`,
);
}
}\n\n`;
Object.keys(definitions.VISITOR_KEYS).forEach(type => {
output += addAssertHelper(type);
});
Object.keys(definitions.FLIPPED_ALIAS_KEYS).forEach(type => {
output += addAssertHelper(type);
});
Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
const newType = definitions.DEPRECATED_KEYS[type];
output += `export function assert${type}(node: any, opts: any): void {
console.trace("The node type ${type} has been renamed to ${newType}");
assert("${type}", node, opts);
}\n`;
});
return output;
}
import * as t from "../../lib/index.js";
import stringifyValidator from "../utils/stringifyValidator.js";
export default function generateAstTypes() {
let code = `// NOTE: This file is autogenerated. Do not modify.
// See packages/babel-types/scripts/generators/ast-types.js for script used.
interface BaseComment {
value: string;
start?: number;
end?: number;
loc?: SourceLocation;
// generator will skip the comment if ignore is true
ignore?: boolean;
type: "CommentBlock" | "CommentLine";
}
export interface CommentBlock extends BaseComment {
type: "CommentBlock";
}
export interface CommentLine extends BaseComment {
type: "CommentLine";
}
export type Comment = CommentBlock | CommentLine;
export interface SourceLocation {
start: {
line: number;
column: number;
};
end: {
line: number;
column: number;
};
}
interface BaseNode {
type: Node["type"];
leadingComments?: Comment[] | null;
innerComments?: Comment[] | null;
trailingComments?: Comment[] | null;
start?: number | null;
end?: number | null;
loc?: SourceLocation | null;
range?: [number, number];
extra?: Record<string, unknown>;
}
export type CommentTypeShorthand = "leading" | "inner" | "trailing";
export type Node = ${t.TYPES.filter(k => !t.FLIPPED_ALIAS_KEYS[k])
.sort()
.join(" | ")};\n\n`;
const deprecatedAlias = {};
for (const type in t.DEPRECATED_KEYS) {
deprecatedAlias[t.DEPRECATED_KEYS[type]] = type;
}
for (const type in t.NODE_FIELDS) {
const fields = t.NODE_FIELDS[type];
const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
const struct = [];
fieldNames.forEach(fieldName => {
const field = fields[fieldName];
// Future / annoying TODO:
// MemberExpression.property, ObjectProperty.key and ObjectMethod.key need special cases; either:
// - convert the declaration to chain() like ClassProperty.key and ClassMethod.key,
// - declare an alias type for valid keys, detect the case and reuse it here,
// - declare a disjoint union with, for example, ObjectPropertyBase,
// ObjectPropertyLiteralKey and ObjectPropertyComputedKey, and declare ObjectProperty
// as "ObjectPropertyBase & (ObjectPropertyLiteralKey | ObjectPropertyComputedKey)"
let typeAnnotation = stringifyValidator(field.validate, "");
if (isNullable(field) && !hasDefault(field)) {
typeAnnotation += " | null";
}
const alphaNumeric = /^\w+$/;
const optional = field.optional ? "?" : "";
if (t.isValidIdentifier(fieldName) || alphaNumeric.test(fieldName)) {
struct.push(`${fieldName}${optional}: ${typeAnnotation};`);
} else {
struct.push(`"${fieldName}"${optional}: ${typeAnnotation};`);
}
});
code += `export interface ${type} extends BaseNode {
type: "${type}";
${struct.join("\n ").trim()}
}\n\n`;
if (deprecatedAlias[type]) {
code += `/**
* @deprecated Use \`${type}\`
*/
export interface ${deprecatedAlias[type]} extends BaseNode {
type: "${deprecatedAlias[type]}";
${struct.join("\n ").trim()}
}\n\n
`;
}
}
for (const type in t.FLIPPED_ALIAS_KEYS) {
const types = t.FLIPPED_ALIAS_KEYS[type];
code += `export type ${type} = ${types
.map(type => `${type}`)
.join(" | ")};\n`;
}
code += "\n";
code += "export interface Aliases {\n";
for (const type in t.FLIPPED_ALIAS_KEYS) {
code += ` ${type}: ${type};\n`;
}
code += "}\n\n";
code += `export type DeprecatedAliases = ${Object.keys(
t.DEPRECATED_KEYS
).join(" | ")}\n\n`;
return code;
}
function hasDefault(field) {
return field.default != null;
}
function isNullable(field) {
return field.optional || hasDefault(field);
}
function sortFieldNames(fields, type) {
return fields.sort((fieldA, fieldB) => {
const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
});
}
import * as t from "../../lib/index.js";
import * as definitions from "../../lib/definitions/index.js";
import formatBuilderName from "../utils/formatBuilderName.js";
import lowerFirst from "../utils/lowerFirst.js";
import stringifyValidator from "../utils/stringifyValidator.js";
function areAllRemainingFieldsNullable(fieldName, fieldNames, fields) {
const index = fieldNames.indexOf(fieldName);
return fieldNames.slice(index).every(_ => isNullable(fields[_]));
}
function hasDefault(field) {
return field.default != null;
}
function isNullable(field) {
return field.optional || hasDefault(field);
}
function sortFieldNames(fields, type) {
return fields.sort((fieldA, fieldB) => {
const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
});
}
function generateBuilderArgs(type) {
const fields = t.NODE_FIELDS[type];
const fieldNames = sortFieldNames(Object.keys(t.NODE_FIELDS[type]), type);
const builderNames = t.BUILDER_KEYS[type];
const args = [];
fieldNames.forEach(fieldName => {
const field = fields[fieldName];
// Future / annoying TODO:
// MemberExpression.property, ObjectProperty.key and ObjectMethod.key need special cases; either:
// - convert the declaration to chain() like ClassProperty.key and ClassMethod.key,
// - declare an alias type for valid keys, detect the case and reuse it here,
// - declare a disjoint union with, for example, ObjectPropertyBase,
// ObjectPropertyLiteralKey and ObjectPropertyComputedKey, and declare ObjectProperty
// as "ObjectPropertyBase & (ObjectPropertyLiteralKey | ObjectPropertyComputedKey)"
let typeAnnotation = stringifyValidator(field.validate, "t.");
if (isNullable(field) && !hasDefault(field)) {
typeAnnotation += " | null";
}
if (builderNames.includes(fieldName)) {
const field = definitions.NODE_FIELDS[type][fieldName];
const def = JSON.stringify(field.default);
const bindingIdentifierName = t.toBindingIdentifierName(fieldName);
let arg;
if (areAllRemainingFieldsNullable(fieldName, builderNames, fields)) {
arg = `${bindingIdentifierName}${
isNullable(field) && !def ? "?:" : ":"
} ${typeAnnotation}`;
} else {
arg = `${bindingIdentifierName}: ${typeAnnotation}${
isNullable(field) ? " | undefined" : ""
}`;
}
if (def !== "null" || isNullable(field)) {
arg += `= ${def}`;
}
args.push(arg);
}
});
return args;
}
export default function generateBuilders(kind) {
return kind === "uppercase.js"
? generateUppercaseBuilders()
: generateLowercaseBuilders();
}
function generateLowercaseBuilders() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import validateNode from "../validateNode";
import type * as t from "../..";
`;
const reservedNames = new Set(["super", "import"]);
Object.keys(definitions.BUILDER_KEYS).forEach(type => {
const defArgs = generateBuilderArgs(type);
const formatedBuilderName = formatBuilderName(type);
const formatedBuilderNameLocal = reservedNames.has(formatedBuilderName)
? `_${formatedBuilderName}`
: formatedBuilderName;
const fieldNames = sortFieldNames(
Object.keys(definitions.NODE_FIELDS[type]),
type
);
const builderNames = definitions.BUILDER_KEYS[type];
const objectFields = [["type", JSON.stringify(type)]];
fieldNames.forEach(fieldName => {
const field = definitions.NODE_FIELDS[type][fieldName];
if (builderNames.includes(fieldName)) {
const bindingIdentifierName = t.toBindingIdentifierName(fieldName);
objectFields.push([fieldName, bindingIdentifierName]);
} else if (!field.optional) {
const def = JSON.stringify(field.default);
objectFields.push([fieldName, def]);
}
});
output += `${
formatedBuilderNameLocal === formatedBuilderName ? "export " : ""
}function ${formatedBuilderNameLocal}(${defArgs.join(", ")}): t.${type} {`;
const nodeObjectExpression = `{\n${objectFields
.map(([k, v]) => (k === v ? ` ${k},` : ` ${k}: ${v},`))
.join("\n")}\n }`;
if (builderNames.length > 0) {
output += `\n return validateNode<t.${type}>(${nodeObjectExpression});`;
} else {
output += `\n return ${nodeObjectExpression};`;
}
output += `\n}\n`;
if (formatedBuilderNameLocal !== formatedBuilderName) {
output += `export { ${formatedBuilderNameLocal} as ${formatedBuilderName} };\n`;
}
// This is needed for backwards compatibility.
// It should be removed in the next major version.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
output += `export { ${formatedBuilderNameLocal} as ${lowerFirst(
type
)} }\n`;
}
});
Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
const newType = definitions.DEPRECATED_KEYS[type];
const formatedBuilderName = formatBuilderName(type);
const formatedNewBuilderName = formatBuilderName(newType);
output += `/** @deprecated */
function ${type}(${generateBuilderArgs(newType).join(", ")}) {
console.trace("The node type ${type} has been renamed to ${newType}");
return ${formatedNewBuilderName}(${t.BUILDER_KEYS[newType].join(", ")});
}
export { ${type} as ${formatedBuilderName} };\n`;
// This is needed for backwards compatibility.
// It should be removed in the next major version.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
output += `export { ${type} as ${lowerFirst(type)} }\n`;
}
});
return output;
}
function generateUppercaseBuilders() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
/**
* This file is written in JavaScript and not TypeScript because uppercase builders
* conflict with AST types. TypeScript reads the uppercase.d.ts file instead.
*/
export {\n`;
Object.keys(definitions.BUILDER_KEYS).forEach(type => {
const formatedBuilderName = formatBuilderName(type);
output += ` ${formatedBuilderName} as ${type},\n`;
});
Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
const formatedBuilderName = formatBuilderName(type);
output += ` ${formatedBuilderName} as ${type},\n`;
});
output += ` } from './index';\n`;
return output;
}
import * as definitions from "../../lib/definitions/index.js";
export default function generateConstants() {
let output = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'make build'
*/
import { FLIPPED_ALIAS_KEYS } from "../../definitions";\n\n`;
Object.keys(definitions.FLIPPED_ALIAS_KEYS).forEach(type => {
output += `export const ${type.toUpperCase()}_TYPES = FLIPPED_ALIAS_KEYS["${type}"];\n`;
});
return output;
}
import util from "util";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";
import * as t from "../../lib/index.js";
const readme = [
`---
id: babel-types
title: @babel/types
---
<!-- Do not modify! This file is automatically generated by
github.com/babel/babel/babel-types/scripts/generators/docs.js !-->
> This module contains methods for building ASTs manually and for checking the types of AST nodes.
## Install
\`\`\`sh
npm install --save-dev @babel/types
\`\`\`
## API`,
];
const customTypes = {
ClassMethod: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
Identifier: {
name: "`string`",
},
MemberExpression: {
property: "if computed then `Expression` else `Identifier`",
},
ObjectMethod: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
ObjectProperty: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
ClassPrivateMethod: {
computed: "'false'",
},
ClassPrivateProperty: {
computed: "'false'",
},
};
const APIHistory = {
ClassProperty: [["v7.6.0", "Supports `static`"]],
};
function formatHistory(historyItems) {
const lines = historyItems.map(
item => "| `" + item[0] + "` | " + item[1] + " |"
);
return [
"<details>",
" <summary>History</summary>",
"| Version | Changes |",
"| --- | --- |",
...lines,
"</details>",
];
}
function printAPIHistory(key, readme) {
if (APIHistory[key]) {
readme.push("");
readme.push(...formatHistory(APIHistory[key]));
}
}
function printNodeFields(key, readme) {
if (Object.keys(t.NODE_FIELDS[key]).length > 0) {
readme.push("");
readme.push("AST Node `" + key + "` shape:");
Object.keys(t.NODE_FIELDS[key])
.sort(function (fieldA, fieldB) {
const indexA = t.BUILDER_KEYS[key].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[key].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(function (field) {
const defaultValue = t.NODE_FIELDS[key][field].default;
const fieldDescription = ["`" + field + "`"];
const validator = t.NODE_FIELDS[key][field].validate;
if (customTypes[key] && customTypes[key][field]) {
fieldDescription.push(`: ${customTypes[key][field]}`);
} else if (validator) {
try {
fieldDescription.push(
": `" + stringifyValidator(validator, "") + "`"
);
} catch (ex) {
if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") {
console.log(
"Unrecognised validator type for " + key + "." + field
);
console.dir(ex.validator, { depth: 10, colors: true });
}
}
}
if (defaultValue !== null || t.NODE_FIELDS[key][field].optional) {
fieldDescription.push(
" (default: `" + util.inspect(defaultValue) + "`"
);
if (t.BUILDER_KEYS[key].indexOf(field) < 0) {
fieldDescription.push(", excluded from builder function");
}
fieldDescription.push(")");
} else {
fieldDescription.push(" (required)");
}
readme.push("- " + fieldDescription.join(""));
});
}
}
function printAliasKeys(key, readme) {
if (t.ALIAS_KEYS[key] && t.ALIAS_KEYS[key].length) {
readme.push("");
readme.push(
"Aliases: " +
t.ALIAS_KEYS[key]
.map(function (key) {
return "[`" + key + "`](#" + key.toLowerCase() + ")";
})
.join(", ")
);
}
}
readme.push("### Node Builders");
readme.push("");
Object.keys(t.BUILDER_KEYS)
.sort()
.forEach(function (key) {
readme.push("#### " + toFunctionName(key));
readme.push("");
readme.push("```javascript");
readme.push(
"t." + toFunctionName(key) + "(" + t.BUILDER_KEYS[key].join(", ") + ");"
);
readme.push("```");
printAPIHistory(key, readme);
readme.push("");
readme.push(
"See also `t.is" +
key +
"(node, opts)` and `t.assert" +
key +
"(node, opts)`."
);
printNodeFields(key, readme);
printAliasKeys(key, readme);
readme.push("");
readme.push("---");
readme.push("");
});
function generateMapAliasToNodeTypes() {
const result = new Map();
for (const nodeType of Object.keys(t.ALIAS_KEYS)) {
const aliases = t.ALIAS_KEYS[nodeType];
if (!aliases) continue;
for (const alias of aliases) {
if (!result.has(alias)) {
result.set(alias, []);
}
const nodeTypes = result.get(alias);
nodeTypes.push(nodeType);
}
}
return result;
}
const aliasDescriptions = {
Accessor: "Deprecated. Will be removed in Babel 8.",
Binary:
"A cover of BinaryExpression and LogicalExpression, which share the same AST shape.",
Block: "Deprecated. Will be removed in Babel 8.",
BlockParent:
"A cover of AST nodes that start an execution context with new [LexicalEnvironment](https://tc39.es/ecma262/#table-additional-state-components-for-ecmascript-code-execution-contexts). In other words, they define the scope of `let` and `const` declarations.",
Class:
"A cover of ClassExpression and ClassDeclaration, which share the same AST shape.",
CompletionStatement:
"A statement that indicates the [completion records](https://tc39.es/ecma262/#sec-completion-record-specification-type). In other words, they define the control flow of the program, such as when should a loop break or an action throws critical errors.",
Conditional:
"A cover of ConditionalExpression and IfStatement, which share the same AST shape.",
Declaration:
"A cover of any [Declaration](https://tc39.es/ecma262/#prod-Declaration)s.",
EnumBody: "A cover of Flow enum bodies.",
EnumMember: "A cover of Flow enum membors.",
ExportDeclaration:
"A cover of any [ExportDeclaration](https://tc39.es/ecma262/#prod-ExportDeclaration)s.",
Expression:
"A cover of any [Expression](https://tc39.es/ecma262/#sec-ecmascript-language-expressions)s.",
ExpressionWrapper:
"A wrapper of expression that does not have runtime semantics.",
Flow: "A cover of AST nodes defined for Flow.",
FlowBaseAnnotation: "A cover of primary Flow type annotations.",
FlowDeclaration: "A cover of Flow declarations.",
FlowPredicate: "A cover of Flow predicates.",
FlowType: "A cover of Flow type annotations.",
For: "A cover of [ForStatement](https://tc39.es/ecma262/#sec-for-statement)s and [ForXStatement](#forxstatement)s.",
ForXStatement:
"A cover of [ForInStatements and ForOfStatements](https://tc39.es/ecma262/#sec-for-in-and-for-of-statements).",
Function:
"A cover of functions and [method](#method)s, the must have `body` and `params`. Note: `Function` is different to `FunctionParent`. For example, a `StaticBlock` is a `FunctionParent` but not `Function`.",
FunctionParent:
"A cover of AST nodes that start an execution context with new [VariableEnvironment](https://tc39.es/ecma262/#table-additional-state-components-for-ecmascript-code-execution-contexts). In other words, they define the scope of `var` declarations. FunctionParent did not include `Program` since Babel 7.",
Immutable:
"A cover of immutable objects and JSX elements. An object is [immutable](https://tc39.es/ecma262/#immutable-prototype-exotic-object) if no other properties can be defined once created.",
JSX: "A cover of AST nodes defined for [JSX](https://facebook.github.io/jsx/).",
LVal: "A cover of left hand side expressions used in the `left` of assignment expressions and [ForXStatement](#forxstatement)s. ",
Literal:
"A cover of [Literal](https://tc39.es/ecma262/#sec-primary-expression-literals)s, [Regular Expression Literal](https://tc39.es/ecma262/#sec-primary-expression-regular-expression-literals)s and [Template Literal](https://tc39.es/ecma262/#sec-template-literals)s.",
Loop: "A cover of loop statements.",
Method: "A cover of object methods and class methods.",
Miscellaneous:
"A cover of non-standard AST types that are sometimes useful for development.",
ModuleDeclaration:
"A cover of ImportDeclaration and [ExportDeclaration](#exportdeclaration)",
ModuleSpecifier:
"A cover of import and export specifiers. Note: It is _not_ the [ModuleSpecifier](https://tc39.es/ecma262/#prod-ModuleSpecifier) defined in the spec.",
ObjectMember:
"A cover of [members](https://tc39.es/ecma262/#prod-PropertyDefinitionList) in an object literal.",
Pattern:
"A cover of [BindingPattern](https://tc39.es/ecma262/#prod-BindingPattern) except Identifiers.",
PatternLike:
"A cover of [BindingPattern](https://tc39.es/ecma262/#prod-BindingPattern)s. ",
Private: "A cover of private class elements and private identifiers.",
Property: "A cover of object properties and class properties.",
Pureish:
"A cover of AST nodes which do not have side-effects. In other words, there is no observable behaviour changes if they are evaluated more than once.",
Scopable:
"A cover of [FunctionParent](#functionparent) and [BlockParent](#blockparent).",
Standardized:
"A cover of AST nodes which are part of an official ECMAScript specification.",
Statement:
"A cover of any [Statement](https://tc39.es/ecma262/#prod-Statement)s.",
TSBaseType: "A cover of primary TypeScript type annotations.",
TSEntityName: "A cover of ts entities.",
TSType: "A cover of TypeScript type annotations.",
TSTypeElement: "A cover of TypeScript type declarations.",
TypeScript: "A cover of AST nodes defined for TypeScript.",
Terminatorless:
"A cover of AST nodes whose semantic will change when a line terminator is inserted between the operator and the operand.",
UnaryLike: "A cover of UnaryExpression and SpreadElement.",
UserWhitespacable: "Deprecated. Will be removed in Babel 8.",
While:
"A cover of DoWhileStatement and WhileStatement, which share the same AST shape.",
};
const mapAliasToNodeTypes = generateMapAliasToNodeTypes();
readme.push("### Aliases");
readme.push("");
for (const alias of [...mapAliasToNodeTypes.keys()].sort()) {
const nodeTypes = mapAliasToNodeTypes.get(alias);
nodeTypes.sort();
if (!(alias in aliasDescriptions)) {
throw new Error(
'Missing alias descriptions of "' +
alias +
", which covers " +
nodeTypes.join(",")
);
}
readme.push("#### " + alias);
readme.push("");
readme.push(aliasDescriptions[alias]);
readme.push("```javascript");
readme.push("t.is" + alias + "(node);");
readme.push("```");
readme.push("");
readme.push("Covered nodes: ");
for (const nodeType of nodeTypes) {
readme.push("- [`" + nodeType + "`](#" + nodeType.toLowerCase() + ")");
}
readme.push("");
}
process.stdout.write(readme.join("\n"));
import * as t from "../../lib/index.js";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";
const NODE_PREFIX = "BabelNode";
let code = `// NOTE: This file is autogenerated. Do not modify.
// See packages/babel-types/scripts/generators/flow.js for script used.
declare class ${NODE_PREFIX}Comment {
value: string;
start: number;
end: number;
loc: ${NODE_PREFIX}SourceLocation;
}
declare class ${NODE_PREFIX}CommentBlock extends ${NODE_PREFIX}Comment {
type: "CommentBlock";
}
declare class ${NODE_PREFIX}CommentLine extends ${NODE_PREFIX}Comment {
type: "CommentLine";
}
declare class ${NODE_PREFIX}SourceLocation {
start: {
line: number;
column: number;
};
end: {
line: number;
column: number;
};
}
declare class ${NODE_PREFIX} {
leadingComments?: Array<${NODE_PREFIX}Comment>;
innerComments?: Array<${NODE_PREFIX}Comment>;
trailingComments?: Array<${NODE_PREFIX}Comment>;
start: ?number;
end: ?number;
loc: ?${NODE_PREFIX}SourceLocation;
extra?: { [string]: mixed };
}\n\n`;
//
const lines = [];
for (const type in t.NODE_FIELDS) {
const fields = t.NODE_FIELDS[type];
const struct = ['type: "' + type + '";'];
const args = [];
const builderNames = t.BUILDER_KEYS[type];
Object.keys(t.NODE_FIELDS[type])
.sort((fieldA, fieldB) => {
const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(fieldName => {
const field = fields[fieldName];
let suffix = "";
if (field.optional || field.default != null) suffix += "?";
let typeAnnotation = "any";
const validate = field.validate;
if (validate) {
typeAnnotation = stringifyValidator(validate, NODE_PREFIX);
}
if (typeAnnotation) {
suffix += ": " + typeAnnotation;
}
if (builderNames.includes(fieldName)) {
args.push(t.toBindingIdentifierName(fieldName) + suffix);
}
if (t.isValidIdentifier(fieldName)) {
struct.push(fieldName + suffix + ";");
}
});
code += `declare class ${NODE_PREFIX}${type} extends ${NODE_PREFIX} {
${struct.join("\n ").trim()}
}\n\n`;
// Flow chokes on super() and import() :/
if (type !== "Super" && type !== "Import") {
lines.push(
`declare export function ${toFunctionName(type)}(${args.join(
", "
)}): ${NODE_PREFIX}${type};`
);
} else {
const functionName = toFunctionName(type);
lines.push(
`declare function _${functionName}(${args.join(
", "
)}): ${NODE_PREFIX}${type};`,
`declare export { _${functionName} as ${functionName} }`
);
}
}
for (const typeName of t.TYPES) {
const isDeprecated = !!t.DEPRECATED_KEYS[typeName];
const realName = isDeprecated ? t.DEPRECATED_KEYS[typeName] : typeName;
let decl = `declare export function is${typeName}(node: ?Object, opts?: ?Object): boolean`;
if (t.NODE_FIELDS[realName]) {
decl += ` %checks (node instanceof ${NODE_PREFIX}${realName})`;
}
lines.push(decl);
lines.push(
`declare export function assert${typeName}(node: ?Object, opts?: ?Object): void`
);
}
lines.push(
`declare export var VISITOR_KEYS: { [type: string]: string[] }`,
// assert/
`declare export function assertNode(obj: any): void`,
// builders/
// eslint-disable-next-line max-len
`declare export function createTypeAnnotationBasedOnTypeof(type: 'string' | 'number' | 'undefined' | 'boolean' | 'function' | 'object' | 'symbol'): ${NODE_PREFIX}TypeAnnotation`,
// eslint-disable-next-line max-len
`declare export function createUnionTypeAnnotation(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
// eslint-disable-next-line max-len
`declare export function createFlowUnionType(types: Array<${NODE_PREFIX}FlowType>): ${NODE_PREFIX}UnionTypeAnnotation`,
// this smells like "internal API"
// eslint-disable-next-line max-len
`declare export function buildChildren(node: { children: Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment | ${NODE_PREFIX}JSXEmptyExpression> }): Array<${NODE_PREFIX}JSXText | ${NODE_PREFIX}JSXExpressionContainer | ${NODE_PREFIX}JSXSpreadChild | ${NODE_PREFIX}JSXElement | ${NODE_PREFIX}JSXFragment>`,
// clone/
`declare export function clone<T>(n: T): T;`,
`declare export function cloneDeep<T>(n: T): T;`,
`declare export function cloneDeepWithoutLoc<T>(n: T): T;`,
`declare export function cloneNode<T>(n: T, deep?: boolean, withoutLoc?: boolean): T;`,
`declare export function cloneWithoutLoc<T>(n: T): T;`,
// comments/
`declare type CommentTypeShorthand = 'leading' | 'inner' | 'trailing'`,
// eslint-disable-next-line max-len
`declare export function addComment<T: BabelNode>(node: T, type: CommentTypeShorthand, content: string, line?: boolean): T`,
// eslint-disable-next-line max-len
`declare export function addComments<T: BabelNode>(node: T, type: CommentTypeShorthand, comments: Array<Comment>): T`,
`declare export function inheritInnerComments(node: BabelNode, parent: BabelNode): void`,
`declare export function inheritLeadingComments(node: BabelNode, parent: BabelNode): void`,
`declare export function inheritsComments<T: BabelNode>(node: T, parent: BabelNode): void`,
`declare export function inheritTrailingComments(node: BabelNode, parent: BabelNode): void`,
`declare export function removeComments<T: BabelNode>(node: T): T`,
// converters/
`declare export function ensureBlock(node: ${NODE_PREFIX}, key: string): ${NODE_PREFIX}BlockStatement`,
`declare export function toBindingIdentifierName(name?: ?string): string`,
// eslint-disable-next-line max-len
`declare export function toBlock(node: ${NODE_PREFIX}Statement | ${NODE_PREFIX}Expression, parent?: ${NODE_PREFIX}Function | null): ${NODE_PREFIX}BlockStatement`,
// eslint-disable-next-line max-len
`declare export function toComputedKey(node: ${NODE_PREFIX}Method | ${NODE_PREFIX}Property, key?: ${NODE_PREFIX}Expression | ${NODE_PREFIX}Identifier): ${NODE_PREFIX}Expression`,
// eslint-disable-next-line max-len
`declare export function toExpression(node: ${NODE_PREFIX}ExpressionStatement | ${NODE_PREFIX}Expression | ${NODE_PREFIX}Class | ${NODE_PREFIX}Function): ${NODE_PREFIX}Expression`,
`declare export function toIdentifier(name?: ?string): string`,
// eslint-disable-next-line max-len
`declare export function toKeyAlias(node: ${NODE_PREFIX}Method | ${NODE_PREFIX}Property, key?: ${NODE_PREFIX}): string`,
// toSequenceExpression relies on types that aren't declared in flow
// eslint-disable-next-line max-len
`declare export function toStatement(node: ${NODE_PREFIX}Statement | ${NODE_PREFIX}Class | ${NODE_PREFIX}Function | ${NODE_PREFIX}AssignmentExpression, ignore?: boolean): ${NODE_PREFIX}Statement | void`,
`declare export function valueToNode(value: any): ${NODE_PREFIX}Expression`,
// modifications/
// eslint-disable-next-line max-len
`declare export function removeTypeDuplicates(types: Array<${NODE_PREFIX}FlowType>): Array<${NODE_PREFIX}FlowType>`,
// eslint-disable-next-line max-len
`declare export function appendToMemberExpression(member: ${NODE_PREFIX}MemberExpression, append: ${NODE_PREFIX}, computed?: boolean): ${NODE_PREFIX}MemberExpression`,
// eslint-disable-next-line max-len
`declare export function inherits<T: BabelNode>(child: T, parent: ${NODE_PREFIX} | null | void): T`,
// eslint-disable-next-line max-len
`declare export function prependToMemberExpression(member: ${NODE_PREFIX}MemberExpression, prepend: ${NODE_PREFIX}Expression): ${NODE_PREFIX}MemberExpression`,
`declare export function removeProperties<T>(n: T, opts: ?{}): void;`,
`declare export function removePropertiesDeep<T>(n: T, opts: ?{}): T;`,
// retrievers/
// eslint-disable-next-line max-len
`declare export var getBindingIdentifiers: {
(node: ${NODE_PREFIX}, duplicates?: boolean, outerOnly?: boolean): { [key: string]: ${NODE_PREFIX}Identifier | Array<${NODE_PREFIX}Identifier> },
keys: { [type: string]: string[] }
}`,
// eslint-disable-next-line max-len
`declare export function getOuterBindingIdentifiers(node: BabelNode, duplicates?: boolean): { [key: string]: ${NODE_PREFIX}Identifier | Array<${NODE_PREFIX}Identifier> }`,
// traverse/
`declare type TraversalAncestors = Array<{
node: BabelNode,
key: string,
index?: number,
}>;
declare type TraversalHandler<T> = (BabelNode, TraversalAncestors, T) => void;
declare type TraversalHandlers<T> = {
enter?: TraversalHandler<T>,
exit?: TraversalHandler<T>,
};`.replace(/(^|\n) {2}/g, "$1"),
// eslint-disable-next-line
`declare export function traverse<T>(n: BabelNode, TraversalHandler<T> | TraversalHandlers<T>, state?: T): void;`,
`declare export function traverseFast<T>(n: BabelNode, h: TraversalHandler<T>, state?: T): void;`,
// utils/
// cleanJSXElementLiteralChild is not exported
// inherit is not exported
`declare export function shallowEqual(actual: Object, expected: Object): boolean`,
// validators/
// eslint-disable-next-line max-len
`declare export function buildMatchMemberExpression(match: string, allowPartial?: boolean): (?BabelNode) => boolean`,
`declare export function is(type: string, n: BabelNode, opts: Object): boolean;`,
`declare export function isBinding(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
`declare export function isBlockScoped(node: BabelNode): boolean`,
`declare export function isImmutable(node: BabelNode): boolean`,
`declare export function isLet(node: BabelNode): boolean`,
`declare export function isNode(node: ?Object): boolean`,
`declare export function isNodesEquivalent(a: any, b: any): boolean`,
`declare export function isPlaceholderType(placeholderType: string, targetType: string): boolean`,
`declare export function isReferenced(node: BabelNode, parent: BabelNode, grandparent?: BabelNode): boolean`,
`declare export function isScope(node: BabelNode, parent: BabelNode): boolean`,
`declare export function isSpecifierDefault(specifier: BabelNodeModuleSpecifier): boolean`,
`declare export function isType(nodetype: ?string, targetType: string): boolean`,
`declare export function isValidES3Identifier(name: string): boolean`,
`declare export function isValidES3Identifier(name: string): boolean`,
`declare export function isValidIdentifier(name: string): boolean`,
`declare export function isVar(node: BabelNode): boolean`,
// eslint-disable-next-line max-len
`declare export function matchesPattern(node: ?BabelNode, match: string | Array<string>, allowPartial?: boolean): boolean`,
`declare export function validate(n: BabelNode, key: string, value: mixed): void;`
);
for (const type in t.FLIPPED_ALIAS_KEYS) {
const types = t.FLIPPED_ALIAS_KEYS[type];
code += `type ${NODE_PREFIX}${type} = ${types
.map(type => `${NODE_PREFIX}${type}`)
.join(" | ")};\n`;
}
code += `\ndeclare module "@babel/types" {
${lines.join("\n").replace(/\n/g, "\n ").trim()}
}\n`;
//
process.stdout.write(code);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment