diff --git a/benchmark/babel-types/builders/multiple.mjs b/benchmark/babel-types/builders/multiple.mjs new file mode 100644 index 000000000000..7ad2597026dd --- /dev/null +++ b/benchmark/babel-types/builders/multiple.mjs @@ -0,0 +1,26 @@ +import { Benchmark, baselineTypes, currentTypes } from "../../util.mjs"; + +const bench = new Benchmark(); + +function benchCases(implementations) { + for (const version in implementations) { + const t = implementations[version]; + const func = t.stringLiteral; + const func2 = t.memberExpression; + const func3 = t.assignmentExpression; + const func4 = t.binaryExpression; + + const id = t.identifier("id"); + const id2 = t.identifier("id2"); + bench.add(`${version} builder`, () => { + func("bar"); + func2(id, id2, /*computed?*/ false /*, optional? missing*/); + func3("=", id, id2); + func4("+", id, id2); + }); + } +} + +benchCases({ baselineTypes, currentTypes }); + +bench.run(); diff --git a/packages/babel-types/scripts/generators/builders.js b/packages/babel-types/scripts/generators/builders.js index 90002caa27c3..1dd7f8874a0b 100644 --- a/packages/babel-types/scripts/generators/builders.js +++ b/packages/babel-types/scripts/generators/builders.js @@ -6,13 +6,10 @@ import { } from "../../lib/index.js"; import formatBuilderName from "../utils/formatBuilderName.js"; import stringifyValidator from "../utils/stringifyValidator.js"; +// eslint-disable-next-line import/no-extraneous-dependencies +import { IS_BABEL_8 } from "$repo-utils"; -// env vars from the cli are always strings, so !!ENV_VAR returns true for "false" -function bool(value) { - return value && value !== "false" && value !== "0"; -} - -if (!bool(process.env.BABEL_8_BREAKING)) { +if (!IS_BABEL_8()) { // eslint-disable-next-line no-var var lowerFirst = function (string) { return string[0].toLowerCase() + string.slice(1); @@ -100,9 +97,14 @@ function generateLowercaseBuilders() { * This file is auto-generated! Do not modify it directly. * To re-generate run 'make build' */ -import validateNode from "../validateNode.ts"; +import * as _validate from "../../validators/validate.ts"; import type * as t from "../../index.ts"; import deprecationWarning from "../../utils/deprecationWarning.ts"; +import * as utils from "../../definitions/utils.ts"; + +const { validateInternal: validate } = _validate; +const { NODE_FIELDS } = utils; + `; const reservedNames = new Set(["super", "import"]); @@ -136,7 +138,23 @@ import deprecationWarning from "../../utils/deprecationWarning.ts"; .join("\n")}\n }`; if (builderNames.length > 0) { - output += `\n return validateNode(${nodeObjectExpression});`; + output += `\n const node:t.${type} = ${nodeObjectExpression};`; + output += `\n const defs = NODE_FIELDS.${type};`; + + fieldNames.forEach(fieldName => { + const field = NODE_FIELDS[type][fieldName]; + if (field && builderNames.includes(fieldName)) { + const argName = toBindingIdentifierName(fieldName); + output += `\n validate(defs.${fieldName}, node, "${fieldName}", ${argName}${ + JSON.stringify( + stringifyValidator(field.validate, "#node#") + ).includes("#node#") + ? ", 1" + : "" + });`; + } + }); + output += `\n return node;`; } else { output += `\n return ${nodeObjectExpression};`; } @@ -146,7 +164,7 @@ import deprecationWarning from "../../utils/deprecationWarning.ts"; output += `export { ${formattedBuilderNameLocal} as ${formattedBuilderName} };\n`; } - if (!bool(process.env.BABEL_8_BREAKING)) { + if (!IS_BABEL_8()) { // This is needed for backwards compatibility. // JSXIdentifier -> jSXIdentifier if (/^[A-Z]{2}/.test(type)) { @@ -168,7 +186,7 @@ function ${type}(${generateBuilderArgs(newType).join(", ")}) { } export { ${type} as ${formattedBuilderName} };\n`; - if (!bool(process.env.BABEL_8_BREAKING)) { + if (!IS_BABEL_8()) { // This is needed for backwards compatibility. // JSXIdentifier -> jSXIdentifier if (/^[A-Z]{2}/.test(type)) { @@ -191,7 +209,7 @@ function generateUppercaseBuilders() { * conflict with AST types. TypeScript reads the uppercase.d.ts file instead. */ - export {\n`; + export {\n`; Object.keys(BUILDER_KEYS).forEach(type => { const formattedBuilderName = formatBuilderName(type); diff --git a/packages/babel-types/src/builders/generated/index.ts b/packages/babel-types/src/builders/generated/index.ts index 6859ed2f1aa2..a29b38b9c971 100644 --- a/packages/babel-types/src/builders/generated/index.ts +++ b/packages/babel-types/src/builders/generated/index.ts @@ -2,28 +2,41 @@ * This file is auto-generated! Do not modify it directly. * To re-generate run 'make build' */ -import validateNode from "../validateNode.ts"; +import * as _validate from "../../validators/validate.ts"; import type * as t from "../../index.ts"; import deprecationWarning from "../../utils/deprecationWarning.ts"; +import * as utils from "../../definitions/utils.ts"; + +const { validateInternal: validate } = _validate; +const { NODE_FIELDS } = utils; + export function arrayExpression( elements: Array = [], ): t.ArrayExpression { - return validateNode({ + const node: t.ArrayExpression = { type: "ArrayExpression", elements, - }); + }; + const defs = NODE_FIELDS.ArrayExpression; + validate(defs.elements, node, "elements", elements, 1); + return node; } export function assignmentExpression( operator: string, left: t.LVal | t.OptionalMemberExpression, right: t.Expression, ): t.AssignmentExpression { - return validateNode({ + const node: t.AssignmentExpression = { type: "AssignmentExpression", operator, left, right, - }); + }; + const defs = NODE_FIELDS.AssignmentExpression; + validate(defs.operator, node, "operator", operator); + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function binaryExpression( operator: @@ -53,58 +66,83 @@ export function binaryExpression( left: t.Expression | t.PrivateName, right: t.Expression, ): t.BinaryExpression { - return validateNode({ + const node: t.BinaryExpression = { type: "BinaryExpression", operator, left, right, - }); + }; + const defs = NODE_FIELDS.BinaryExpression; + validate(defs.operator, node, "operator", operator); + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function interpreterDirective(value: string): t.InterpreterDirective { - return validateNode({ + const node: t.InterpreterDirective = { type: "InterpreterDirective", value, - }); + }; + const defs = NODE_FIELDS.InterpreterDirective; + validate(defs.value, node, "value", value); + return node; } export function directive(value: t.DirectiveLiteral): t.Directive { - return validateNode({ + const node: t.Directive = { type: "Directive", value, - }); + }; + const defs = NODE_FIELDS.Directive; + validate(defs.value, node, "value", value, 1); + return node; } export function directiveLiteral(value: string): t.DirectiveLiteral { - return validateNode({ + const node: t.DirectiveLiteral = { type: "DirectiveLiteral", value, - }); + }; + const defs = NODE_FIELDS.DirectiveLiteral; + validate(defs.value, node, "value", value); + return node; } export function blockStatement( body: Array, directives: Array = [], ): t.BlockStatement { - return validateNode({ + const node: t.BlockStatement = { type: "BlockStatement", body, directives, - }); + }; + const defs = NODE_FIELDS.BlockStatement; + validate(defs.body, node, "body", body, 1); + validate(defs.directives, node, "directives", directives, 1); + return node; } export function breakStatement( label: t.Identifier | null = null, ): t.BreakStatement { - return validateNode({ + const node: t.BreakStatement = { type: "BreakStatement", label, - }); + }; + const defs = NODE_FIELDS.BreakStatement; + validate(defs.label, node, "label", label, 1); + return node; } export function callExpression( callee: t.Expression | t.Super | t.V8IntrinsicIdentifier, _arguments: Array, ): t.CallExpression { - return validateNode({ + const node: t.CallExpression = { type: "CallExpression", callee, arguments: _arguments, - }); + }; + const defs = NODE_FIELDS.CallExpression; + validate(defs.callee, node, "callee", callee, 1); + validate(defs.arguments, node, "arguments", _arguments, 1); + return node; } export function catchClause( param: @@ -115,31 +153,43 @@ export function catchClause( | undefined = null, body: t.BlockStatement, ): t.CatchClause { - return validateNode({ + const node: t.CatchClause = { type: "CatchClause", param, body, - }); + }; + const defs = NODE_FIELDS.CatchClause; + validate(defs.param, node, "param", param, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function conditionalExpression( test: t.Expression, consequent: t.Expression, alternate: t.Expression, ): t.ConditionalExpression { - return validateNode({ + const node: t.ConditionalExpression = { type: "ConditionalExpression", test, consequent, alternate, - }); + }; + const defs = NODE_FIELDS.ConditionalExpression; + validate(defs.test, node, "test", test, 1); + validate(defs.consequent, node, "consequent", consequent, 1); + validate(defs.alternate, node, "alternate", alternate, 1); + return node; } export function continueStatement( label: t.Identifier | null = null, ): t.ContinueStatement { - return validateNode({ + const node: t.ContinueStatement = { type: "ContinueStatement", label, - }); + }; + const defs = NODE_FIELDS.ContinueStatement; + validate(defs.label, node, "label", label, 1); + return node; } export function debuggerStatement(): t.DebuggerStatement { return { @@ -150,11 +200,15 @@ export function doWhileStatement( test: t.Expression, body: t.Statement, ): t.DoWhileStatement { - return validateNode({ + const node: t.DoWhileStatement = { type: "DoWhileStatement", test, body, - }); + }; + const defs = NODE_FIELDS.DoWhileStatement; + validate(defs.test, node, "test", test, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function emptyStatement(): t.EmptyStatement { return { @@ -164,34 +218,47 @@ export function emptyStatement(): t.EmptyStatement { export function expressionStatement( expression: t.Expression, ): t.ExpressionStatement { - return validateNode({ + const node: t.ExpressionStatement = { type: "ExpressionStatement", expression, - }); + }; + const defs = NODE_FIELDS.ExpressionStatement; + validate(defs.expression, node, "expression", expression, 1); + return node; } export function file( program: t.Program, comments: Array | null = null, tokens: Array | null = null, ): t.File { - return validateNode({ + const node: t.File = { type: "File", program, comments, tokens, - }); + }; + const defs = NODE_FIELDS.File; + validate(defs.program, node, "program", program, 1); + validate(defs.comments, node, "comments", comments, 1); + validate(defs.tokens, node, "tokens", tokens); + return node; } export function forInStatement( left: t.VariableDeclaration | t.LVal, right: t.Expression, body: t.Statement, ): t.ForInStatement { - return validateNode({ + const node: t.ForInStatement = { type: "ForInStatement", left, right, body, - }); + }; + const defs = NODE_FIELDS.ForInStatement; + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function forStatement( init: t.VariableDeclaration | t.Expression | null | undefined = null, @@ -199,13 +266,19 @@ export function forStatement( update: t.Expression | null | undefined = null, body: t.Statement, ): t.ForStatement { - return validateNode({ + const node: t.ForStatement = { type: "ForStatement", init, test, update, body, - }); + }; + const defs = NODE_FIELDS.ForStatement; + validate(defs.init, node, "init", init, 1); + validate(defs.test, node, "test", test, 1); + validate(defs.update, node, "update", update, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function functionDeclaration( id: t.Identifier | null | undefined = null, @@ -214,14 +287,21 @@ export function functionDeclaration( generator: boolean = false, async: boolean = false, ): t.FunctionDeclaration { - return validateNode({ + const node: t.FunctionDeclaration = { type: "FunctionDeclaration", id, params, body, generator, async, - }); + }; + const defs = NODE_FIELDS.FunctionDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.generator, node, "generator", generator); + validate(defs.async, node, "async", async); + return node; } export function functionExpression( id: t.Identifier | null | undefined = null, @@ -230,54 +310,79 @@ export function functionExpression( generator: boolean = false, async: boolean = false, ): t.FunctionExpression { - return validateNode({ + const node: t.FunctionExpression = { type: "FunctionExpression", id, params, body, generator, async, - }); + }; + const defs = NODE_FIELDS.FunctionExpression; + validate(defs.id, node, "id", id, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.generator, node, "generator", generator); + validate(defs.async, node, "async", async); + return node; } export function identifier(name: string): t.Identifier { - return validateNode({ + const node: t.Identifier = { type: "Identifier", name, - }); + }; + const defs = NODE_FIELDS.Identifier; + validate(defs.name, node, "name", name); + return node; } export function ifStatement( test: t.Expression, consequent: t.Statement, alternate: t.Statement | null = null, ): t.IfStatement { - return validateNode({ + const node: t.IfStatement = { type: "IfStatement", test, consequent, alternate, - }); + }; + const defs = NODE_FIELDS.IfStatement; + validate(defs.test, node, "test", test, 1); + validate(defs.consequent, node, "consequent", consequent, 1); + validate(defs.alternate, node, "alternate", alternate, 1); + return node; } export function labeledStatement( label: t.Identifier, body: t.Statement, ): t.LabeledStatement { - return validateNode({ + const node: t.LabeledStatement = { type: "LabeledStatement", label, body, - }); + }; + const defs = NODE_FIELDS.LabeledStatement; + validate(defs.label, node, "label", label, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function stringLiteral(value: string): t.StringLiteral { - return validateNode({ + const node: t.StringLiteral = { type: "StringLiteral", value, - }); + }; + const defs = NODE_FIELDS.StringLiteral; + validate(defs.value, node, "value", value); + return node; } export function numericLiteral(value: number): t.NumericLiteral { - return validateNode({ + const node: t.NumericLiteral = { type: "NumericLiteral", value, - }); + }; + const defs = NODE_FIELDS.NumericLiteral; + validate(defs.value, node, "value", value); + return node; } export function nullLiteral(): t.NullLiteral { return { @@ -285,32 +390,44 @@ export function nullLiteral(): t.NullLiteral { }; } export function booleanLiteral(value: boolean): t.BooleanLiteral { - return validateNode({ + const node: t.BooleanLiteral = { type: "BooleanLiteral", value, - }); + }; + const defs = NODE_FIELDS.BooleanLiteral; + validate(defs.value, node, "value", value); + return node; } export function regExpLiteral( pattern: string, flags: string = "", ): t.RegExpLiteral { - return validateNode({ + const node: t.RegExpLiteral = { type: "RegExpLiteral", pattern, flags, - }); + }; + const defs = NODE_FIELDS.RegExpLiteral; + validate(defs.pattern, node, "pattern", pattern); + validate(defs.flags, node, "flags", flags); + return node; } export function logicalExpression( operator: "||" | "&&" | "??", left: t.Expression, right: t.Expression, ): t.LogicalExpression { - return validateNode({ + const node: t.LogicalExpression = { type: "LogicalExpression", operator, left, right, - }); + }; + const defs = NODE_FIELDS.LogicalExpression; + validate(defs.operator, node, "operator", operator); + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function memberExpression( object: t.Expression | t.Super, @@ -318,23 +435,33 @@ export function memberExpression( computed: boolean = false, optional: boolean | null = null, ): t.MemberExpression { - return validateNode({ + const node: t.MemberExpression = { type: "MemberExpression", object, property, computed, optional, - }); + }; + const defs = NODE_FIELDS.MemberExpression; + validate(defs.object, node, "object", object, 1); + validate(defs.property, node, "property", property, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.optional, node, "optional", optional); + return node; } export function newExpression( callee: t.Expression | t.Super | t.V8IntrinsicIdentifier, _arguments: Array, ): t.NewExpression { - return validateNode({ + const node: t.NewExpression = { type: "NewExpression", callee, arguments: _arguments, - }); + }; + const defs = NODE_FIELDS.NewExpression; + validate(defs.callee, node, "callee", callee, 1); + validate(defs.arguments, node, "arguments", _arguments, 1); + return node; } export function program( body: Array, @@ -342,21 +469,30 @@ export function program( sourceType: "script" | "module" = "script", interpreter: t.InterpreterDirective | null = null, ): t.Program { - return validateNode({ + const node: t.Program = { type: "Program", body, directives, sourceType, interpreter, - }); + }; + const defs = NODE_FIELDS.Program; + validate(defs.body, node, "body", body, 1); + validate(defs.directives, node, "directives", directives, 1); + validate(defs.sourceType, node, "sourceType", sourceType); + validate(defs.interpreter, node, "interpreter", interpreter, 1); + return node; } export function objectExpression( properties: Array, ): t.ObjectExpression { - return validateNode({ + const node: t.ObjectExpression = { type: "ObjectExpression", properties, - }); + }; + const defs = NODE_FIELDS.ObjectExpression; + validate(defs.properties, node, "properties", properties, 1); + return node; } export function objectMethod( kind: "method" | "get" | "set" | undefined = "method", @@ -372,7 +508,7 @@ export function objectMethod( generator: boolean = false, async: boolean = false, ): t.ObjectMethod { - return validateNode({ + const node: t.ObjectMethod = { type: "ObjectMethod", kind, key, @@ -381,7 +517,16 @@ export function objectMethod( computed, generator, async, - }); + }; + const defs = NODE_FIELDS.ObjectMethod; + validate(defs.kind, node, "kind", kind); + validate(defs.key, node, "key", key, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.generator, node, "generator", generator); + validate(defs.async, node, "async", async); + return node; } export function objectProperty( key: @@ -397,64 +542,91 @@ export function objectProperty( shorthand: boolean = false, decorators: Array | null = null, ): t.ObjectProperty { - return validateNode({ + const node: t.ObjectProperty = { type: "ObjectProperty", key, value, computed, shorthand, decorators, - }); + }; + const defs = NODE_FIELDS.ObjectProperty; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.shorthand, node, "shorthand", shorthand); + validate(defs.decorators, node, "decorators", decorators, 1); + return node; } export function restElement(argument: t.LVal): t.RestElement { - return validateNode({ + const node: t.RestElement = { type: "RestElement", argument, - }); + }; + const defs = NODE_FIELDS.RestElement; + validate(defs.argument, node, "argument", argument, 1); + return node; } export function returnStatement( argument: t.Expression | null = null, ): t.ReturnStatement { - return validateNode({ + const node: t.ReturnStatement = { type: "ReturnStatement", argument, - }); + }; + const defs = NODE_FIELDS.ReturnStatement; + validate(defs.argument, node, "argument", argument, 1); + return node; } export function sequenceExpression( expressions: Array, ): t.SequenceExpression { - return validateNode({ + const node: t.SequenceExpression = { type: "SequenceExpression", expressions, - }); + }; + const defs = NODE_FIELDS.SequenceExpression; + validate(defs.expressions, node, "expressions", expressions, 1); + return node; } export function parenthesizedExpression( expression: t.Expression, ): t.ParenthesizedExpression { - return validateNode({ + const node: t.ParenthesizedExpression = { type: "ParenthesizedExpression", expression, - }); + }; + const defs = NODE_FIELDS.ParenthesizedExpression; + validate(defs.expression, node, "expression", expression, 1); + return node; } export function switchCase( test: t.Expression | null | undefined = null, consequent: Array, ): t.SwitchCase { - return validateNode({ + const node: t.SwitchCase = { type: "SwitchCase", test, consequent, - }); + }; + const defs = NODE_FIELDS.SwitchCase; + validate(defs.test, node, "test", test, 1); + validate(defs.consequent, node, "consequent", consequent, 1); + return node; } export function switchStatement( discriminant: t.Expression, cases: Array, ): t.SwitchStatement { - return validateNode({ + const node: t.SwitchStatement = { type: "SwitchStatement", discriminant, cases, - }); + }; + const defs = NODE_FIELDS.SwitchStatement; + validate(defs.discriminant, node, "discriminant", discriminant, 1); + validate(defs.cases, node, "cases", cases, 1); + return node; } export function thisExpression(): t.ThisExpression { return { @@ -462,86 +634,120 @@ export function thisExpression(): t.ThisExpression { }; } export function throwStatement(argument: t.Expression): t.ThrowStatement { - return validateNode({ + const node: t.ThrowStatement = { type: "ThrowStatement", argument, - }); + }; + const defs = NODE_FIELDS.ThrowStatement; + validate(defs.argument, node, "argument", argument, 1); + return node; } export function tryStatement( block: t.BlockStatement, handler: t.CatchClause | null = null, finalizer: t.BlockStatement | null = null, ): t.TryStatement { - return validateNode({ + const node: t.TryStatement = { type: "TryStatement", block, handler, finalizer, - }); + }; + const defs = NODE_FIELDS.TryStatement; + validate(defs.block, node, "block", block, 1); + validate(defs.handler, node, "handler", handler, 1); + validate(defs.finalizer, node, "finalizer", finalizer, 1); + return node; } export function unaryExpression( operator: "void" | "throw" | "delete" | "!" | "+" | "-" | "~" | "typeof", argument: t.Expression, prefix: boolean = true, ): t.UnaryExpression { - return validateNode({ + const node: t.UnaryExpression = { type: "UnaryExpression", operator, argument, prefix, - }); + }; + const defs = NODE_FIELDS.UnaryExpression; + validate(defs.operator, node, "operator", operator); + validate(defs.argument, node, "argument", argument, 1); + validate(defs.prefix, node, "prefix", prefix); + return node; } export function updateExpression( operator: "++" | "--", argument: t.Expression, prefix: boolean = false, ): t.UpdateExpression { - return validateNode({ + const node: t.UpdateExpression = { type: "UpdateExpression", operator, argument, prefix, - }); + }; + const defs = NODE_FIELDS.UpdateExpression; + validate(defs.operator, node, "operator", operator); + validate(defs.argument, node, "argument", argument, 1); + validate(defs.prefix, node, "prefix", prefix); + return node; } export function variableDeclaration( kind: "var" | "let" | "const" | "using" | "await using", declarations: Array, ): t.VariableDeclaration { - return validateNode({ + const node: t.VariableDeclaration = { type: "VariableDeclaration", kind, declarations, - }); + }; + const defs = NODE_FIELDS.VariableDeclaration; + validate(defs.kind, node, "kind", kind); + validate(defs.declarations, node, "declarations", declarations, 1); + return node; } export function variableDeclarator( id: t.LVal, init: t.Expression | null = null, ): t.VariableDeclarator { - return validateNode({ + const node: t.VariableDeclarator = { type: "VariableDeclarator", id, init, - }); + }; + const defs = NODE_FIELDS.VariableDeclarator; + validate(defs.id, node, "id", id, 1); + validate(defs.init, node, "init", init, 1); + return node; } export function whileStatement( test: t.Expression, body: t.Statement, ): t.WhileStatement { - return validateNode({ + const node: t.WhileStatement = { type: "WhileStatement", test, body, - }); + }; + const defs = NODE_FIELDS.WhileStatement; + validate(defs.test, node, "test", test, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function withStatement( object: t.Expression, body: t.Statement, ): t.WithStatement { - return validateNode({ + const node: t.WithStatement = { type: "WithStatement", object, body, - }); + }; + const defs = NODE_FIELDS.WithStatement; + validate(defs.object, node, "object", object, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function assignmentPattern( left: @@ -555,32 +761,44 @@ export function assignmentPattern( | t.TSNonNullExpression, right: t.Expression, ): t.AssignmentPattern { - return validateNode({ + const node: t.AssignmentPattern = { type: "AssignmentPattern", left, right, - }); + }; + const defs = NODE_FIELDS.AssignmentPattern; + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function arrayPattern( elements: Array, ): t.ArrayPattern { - return validateNode({ + const node: t.ArrayPattern = { type: "ArrayPattern", elements, - }); + }; + const defs = NODE_FIELDS.ArrayPattern; + validate(defs.elements, node, "elements", elements, 1); + return node; } export function arrowFunctionExpression( params: Array, body: t.BlockStatement | t.Expression, async: boolean = false, ): t.ArrowFunctionExpression { - return validateNode({ + const node: t.ArrowFunctionExpression = { type: "ArrowFunctionExpression", params, body, async, expression: null, - }); + }; + const defs = NODE_FIELDS.ArrowFunctionExpression; + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.async, node, "async", async); + return node; } export function classBody( body: Array< @@ -594,10 +812,13 @@ export function classBody( | t.StaticBlock >, ): t.ClassBody { - return validateNode({ + const node: t.ClassBody = { type: "ClassBody", body, - }); + }; + const defs = NODE_FIELDS.ClassBody; + validate(defs.body, node, "body", body, 1); + return node; } export function classExpression( id: t.Identifier | null | undefined = null, @@ -605,13 +826,19 @@ export function classExpression( body: t.ClassBody, decorators: Array | null = null, ): t.ClassExpression { - return validateNode({ + const node: t.ClassExpression = { type: "ClassExpression", id, superClass, body, decorators, - }); + }; + const defs = NODE_FIELDS.ClassExpression; + validate(defs.id, node, "id", id, 1); + validate(defs.superClass, node, "superClass", superClass, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.decorators, node, "decorators", decorators, 1); + return node; } export function classDeclaration( id: t.Identifier | null | undefined = null, @@ -619,21 +846,30 @@ export function classDeclaration( body: t.ClassBody, decorators: Array | null = null, ): t.ClassDeclaration { - return validateNode({ + const node: t.ClassDeclaration = { type: "ClassDeclaration", id, superClass, body, decorators, - }); + }; + const defs = NODE_FIELDS.ClassDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.superClass, node, "superClass", superClass, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.decorators, node, "decorators", decorators, 1); + return node; } export function exportAllDeclaration( source: t.StringLiteral, ): t.ExportAllDeclaration { - return validateNode({ + const node: t.ExportAllDeclaration = { type: "ExportAllDeclaration", source, - }); + }; + const defs = NODE_FIELDS.ExportAllDeclaration; + validate(defs.source, node, "source", source, 1); + return node; } export function exportDefaultDeclaration( declaration: @@ -642,10 +878,13 @@ export function exportDefaultDeclaration( | t.ClassDeclaration | t.Expression, ): t.ExportDefaultDeclaration { - return validateNode({ + const node: t.ExportDefaultDeclaration = { type: "ExportDefaultDeclaration", declaration, - }); + }; + const defs = NODE_FIELDS.ExportDefaultDeclaration; + validate(defs.declaration, node, "declaration", declaration, 1); + return node; } export function exportNamedDeclaration( declaration: t.Declaration | null = null, @@ -654,22 +893,31 @@ export function exportNamedDeclaration( > = [], source: t.StringLiteral | null = null, ): t.ExportNamedDeclaration { - return validateNode({ + const node: t.ExportNamedDeclaration = { type: "ExportNamedDeclaration", declaration, specifiers, source, - }); + }; + const defs = NODE_FIELDS.ExportNamedDeclaration; + validate(defs.declaration, node, "declaration", declaration, 1); + validate(defs.specifiers, node, "specifiers", specifiers, 1); + validate(defs.source, node, "source", source, 1); + return node; } export function exportSpecifier( local: t.Identifier, exported: t.Identifier | t.StringLiteral, ): t.ExportSpecifier { - return validateNode({ + const node: t.ExportSpecifier = { type: "ExportSpecifier", local, exported, - }); + }; + const defs = NODE_FIELDS.ExportSpecifier; + validate(defs.local, node, "local", local, 1); + validate(defs.exported, node, "exported", exported, 1); + return node; } export function forOfStatement( left: t.VariableDeclaration | t.LVal, @@ -677,13 +925,19 @@ export function forOfStatement( body: t.Statement, _await: boolean = false, ): t.ForOfStatement { - return validateNode({ + const node: t.ForOfStatement = { type: "ForOfStatement", left, right, body, await: _await, - }); + }; + const defs = NODE_FIELDS.ForOfStatement; + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.await, node, "await", _await); + return node; } export function importDeclaration( specifiers: Array< @@ -691,57 +945,79 @@ export function importDeclaration( >, source: t.StringLiteral, ): t.ImportDeclaration { - return validateNode({ + const node: t.ImportDeclaration = { type: "ImportDeclaration", specifiers, source, - }); + }; + const defs = NODE_FIELDS.ImportDeclaration; + validate(defs.specifiers, node, "specifiers", specifiers, 1); + validate(defs.source, node, "source", source, 1); + return node; } export function importDefaultSpecifier( local: t.Identifier, ): t.ImportDefaultSpecifier { - return validateNode({ + const node: t.ImportDefaultSpecifier = { type: "ImportDefaultSpecifier", local, - }); + }; + const defs = NODE_FIELDS.ImportDefaultSpecifier; + validate(defs.local, node, "local", local, 1); + return node; } export function importNamespaceSpecifier( local: t.Identifier, ): t.ImportNamespaceSpecifier { - return validateNode({ + const node: t.ImportNamespaceSpecifier = { type: "ImportNamespaceSpecifier", local, - }); + }; + const defs = NODE_FIELDS.ImportNamespaceSpecifier; + validate(defs.local, node, "local", local, 1); + return node; } export function importSpecifier( local: t.Identifier, imported: t.Identifier | t.StringLiteral, ): t.ImportSpecifier { - return validateNode({ + const node: t.ImportSpecifier = { type: "ImportSpecifier", local, imported, - }); + }; + const defs = NODE_FIELDS.ImportSpecifier; + validate(defs.local, node, "local", local, 1); + validate(defs.imported, node, "imported", imported, 1); + return node; } export function importExpression( source: t.Expression, options: t.Expression | null = null, ): t.ImportExpression { - return validateNode({ + const node: t.ImportExpression = { type: "ImportExpression", source, options, - }); + }; + const defs = NODE_FIELDS.ImportExpression; + validate(defs.source, node, "source", source, 1); + validate(defs.options, node, "options", options, 1); + return node; } export function metaProperty( meta: t.Identifier, property: t.Identifier, ): t.MetaProperty { - return validateNode({ + const node: t.MetaProperty = { type: "MetaProperty", meta, property, - }); + }; + const defs = NODE_FIELDS.MetaProperty; + validate(defs.meta, node, "meta", meta, 1); + validate(defs.property, node, "property", property, 1); + return node; } export function classMethod( kind: "get" | "set" | "method" | "constructor" | undefined = "method", @@ -760,7 +1036,7 @@ export function classMethod( generator: boolean = false, async: boolean = false, ): t.ClassMethod { - return validateNode({ + const node: t.ClassMethod = { type: "ClassMethod", kind, key, @@ -770,21 +1046,37 @@ export function classMethod( static: _static, generator, async, - }); + }; + const defs = NODE_FIELDS.ClassMethod; + validate(defs.kind, node, "kind", kind); + validate(defs.key, node, "key", key, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.static, node, "static", _static); + validate(defs.generator, node, "generator", generator); + validate(defs.async, node, "async", async); + return node; } export function objectPattern( properties: Array, ): t.ObjectPattern { - return validateNode({ + const node: t.ObjectPattern = { type: "ObjectPattern", properties, - }); + }; + const defs = NODE_FIELDS.ObjectPattern; + validate(defs.properties, node, "properties", properties, 1); + return node; } export function spreadElement(argument: t.Expression): t.SpreadElement { - return validateNode({ + const node: t.SpreadElement = { type: "SpreadElement", argument, - }); + }; + const defs = NODE_FIELDS.SpreadElement; + validate(defs.argument, node, "argument", argument, 1); + return node; } function _super(): t.Super { return { @@ -796,47 +1088,66 @@ export function taggedTemplateExpression( tag: t.Expression, quasi: t.TemplateLiteral, ): t.TaggedTemplateExpression { - return validateNode({ + const node: t.TaggedTemplateExpression = { type: "TaggedTemplateExpression", tag, quasi, - }); + }; + const defs = NODE_FIELDS.TaggedTemplateExpression; + validate(defs.tag, node, "tag", tag, 1); + validate(defs.quasi, node, "quasi", quasi, 1); + return node; } export function templateElement( value: { raw: string; cooked?: string }, tail: boolean = false, ): t.TemplateElement { - return validateNode({ + const node: t.TemplateElement = { type: "TemplateElement", value, tail, - }); + }; + const defs = NODE_FIELDS.TemplateElement; + validate(defs.value, node, "value", value); + validate(defs.tail, node, "tail", tail); + return node; } export function templateLiteral( quasis: Array, expressions: Array, ): t.TemplateLiteral { - return validateNode({ + const node: t.TemplateLiteral = { type: "TemplateLiteral", quasis, expressions, - }); + }; + const defs = NODE_FIELDS.TemplateLiteral; + validate(defs.quasis, node, "quasis", quasis, 1); + validate(defs.expressions, node, "expressions", expressions, 1); + return node; } export function yieldExpression( argument: t.Expression | null = null, delegate: boolean = false, ): t.YieldExpression { - return validateNode({ + const node: t.YieldExpression = { type: "YieldExpression", argument, delegate, - }); + }; + const defs = NODE_FIELDS.YieldExpression; + validate(defs.argument, node, "argument", argument, 1); + validate(defs.delegate, node, "delegate", delegate); + return node; } export function awaitExpression(argument: t.Expression): t.AwaitExpression { - return validateNode({ + const node: t.AwaitExpression = { type: "AwaitExpression", argument, - }); + }; + const defs = NODE_FIELDS.AwaitExpression; + validate(defs.argument, node, "argument", argument, 1); + return node; } function _import(): t.Import { return { @@ -845,18 +1156,24 @@ function _import(): t.Import { } export { _import as import }; export function bigIntLiteral(value: string): t.BigIntLiteral { - return validateNode({ + const node: t.BigIntLiteral = { type: "BigIntLiteral", value, - }); + }; + const defs = NODE_FIELDS.BigIntLiteral; + validate(defs.value, node, "value", value); + return node; } export function exportNamespaceSpecifier( exported: t.Identifier, ): t.ExportNamespaceSpecifier { - return validateNode({ + const node: t.ExportNamespaceSpecifier = { type: "ExportNamespaceSpecifier", exported, - }); + }; + const defs = NODE_FIELDS.ExportNamespaceSpecifier; + validate(defs.exported, node, "exported", exported, 1); + return node; } export function optionalMemberExpression( object: t.Expression, @@ -864,25 +1181,36 @@ export function optionalMemberExpression( computed: boolean | undefined = false, optional: boolean, ): t.OptionalMemberExpression { - return validateNode({ + const node: t.OptionalMemberExpression = { type: "OptionalMemberExpression", object, property, computed, optional, - }); + }; + const defs = NODE_FIELDS.OptionalMemberExpression; + validate(defs.object, node, "object", object, 1); + validate(defs.property, node, "property", property, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.optional, node, "optional", optional); + return node; } export function optionalCallExpression( callee: t.Expression, _arguments: Array, optional: boolean, ): t.OptionalCallExpression { - return validateNode({ + const node: t.OptionalCallExpression = { type: "OptionalCallExpression", callee, arguments: _arguments, optional, - }); + }; + const defs = NODE_FIELDS.OptionalCallExpression; + validate(defs.callee, node, "callee", callee, 1); + validate(defs.arguments, node, "arguments", _arguments, 1); + validate(defs.optional, node, "optional", optional); + return node; } export function classProperty( key: @@ -897,7 +1225,7 @@ export function classProperty( computed: boolean = false, _static: boolean = false, ): t.ClassProperty { - return validateNode({ + const node: t.ClassProperty = { type: "ClassProperty", key, value, @@ -905,7 +1233,15 @@ export function classProperty( decorators, computed, static: _static, - }); + }; + const defs = NODE_FIELDS.ClassProperty; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + validate(defs.decorators, node, "decorators", decorators, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.static, node, "static", _static); + return node; } export function classAccessorProperty( key: @@ -921,7 +1257,7 @@ export function classAccessorProperty( computed: boolean = false, _static: boolean = false, ): t.ClassAccessorProperty { - return validateNode({ + const node: t.ClassAccessorProperty = { type: "ClassAccessorProperty", key, value, @@ -929,7 +1265,15 @@ export function classAccessorProperty( decorators, computed, static: _static, - }); + }; + const defs = NODE_FIELDS.ClassAccessorProperty; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + validate(defs.decorators, node, "decorators", decorators, 1); + validate(defs.computed, node, "computed", computed); + validate(defs.static, node, "static", _static); + return node; } export function classPrivateProperty( key: t.PrivateName, @@ -937,13 +1281,19 @@ export function classPrivateProperty( decorators: Array | null = null, _static: boolean = false, ): t.ClassPrivateProperty { - return validateNode({ + const node: t.ClassPrivateProperty = { type: "ClassPrivateProperty", key, value, decorators, static: _static, - }); + }; + const defs = NODE_FIELDS.ClassPrivateProperty; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.decorators, node, "decorators", decorators, 1); + validate(defs.static, node, "static", _static); + return node; } export function classPrivateMethod( kind: "get" | "set" | "method" | undefined = "method", @@ -954,26 +1304,39 @@ export function classPrivateMethod( body: t.BlockStatement, _static: boolean = false, ): t.ClassPrivateMethod { - return validateNode({ + const node: t.ClassPrivateMethod = { type: "ClassPrivateMethod", kind, key, params, body, static: _static, - }); + }; + const defs = NODE_FIELDS.ClassPrivateMethod; + validate(defs.kind, node, "kind", kind); + validate(defs.key, node, "key", key, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.static, node, "static", _static); + return node; } export function privateName(id: t.Identifier): t.PrivateName { - return validateNode({ + const node: t.PrivateName = { type: "PrivateName", id, - }); + }; + const defs = NODE_FIELDS.PrivateName; + validate(defs.id, node, "id", id, 1); + return node; } export function staticBlock(body: Array): t.StaticBlock { - return validateNode({ + const node: t.StaticBlock = { type: "StaticBlock", body, - }); + }; + const defs = NODE_FIELDS.StaticBlock; + validate(defs.body, node, "body", body, 1); + return node; } export function anyTypeAnnotation(): t.AnyTypeAnnotation { return { @@ -983,10 +1346,13 @@ export function anyTypeAnnotation(): t.AnyTypeAnnotation { export function arrayTypeAnnotation( elementType: t.FlowType, ): t.ArrayTypeAnnotation { - return validateNode({ + const node: t.ArrayTypeAnnotation = { type: "ArrayTypeAnnotation", elementType, - }); + }; + const defs = NODE_FIELDS.ArrayTypeAnnotation; + validate(defs.elementType, node, "elementType", elementType, 1); + return node; } export function booleanTypeAnnotation(): t.BooleanTypeAnnotation { return { @@ -996,10 +1362,13 @@ export function booleanTypeAnnotation(): t.BooleanTypeAnnotation { export function booleanLiteralTypeAnnotation( value: boolean, ): t.BooleanLiteralTypeAnnotation { - return validateNode({ + const node: t.BooleanLiteralTypeAnnotation = { type: "BooleanLiteralTypeAnnotation", value, - }); + }; + const defs = NODE_FIELDS.BooleanLiteralTypeAnnotation; + validate(defs.value, node, "value", value); + return node; } export function nullLiteralTypeAnnotation(): t.NullLiteralTypeAnnotation { return { @@ -1010,11 +1379,15 @@ export function classImplements( id: t.Identifier, typeParameters: t.TypeParameterInstantiation | null = null, ): t.ClassImplements { - return validateNode({ + const node: t.ClassImplements = { type: "ClassImplements", id, typeParameters, - }); + }; + const defs = NODE_FIELDS.ClassImplements; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export function declareClass( id: t.Identifier, @@ -1022,19 +1395,28 @@ export function declareClass( _extends: Array | null | undefined = null, body: t.ObjectTypeAnnotation, ): t.DeclareClass { - return validateNode({ + const node: t.DeclareClass = { type: "DeclareClass", id, typeParameters, extends: _extends, body, - }); + }; + const defs = NODE_FIELDS.DeclareClass; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.extends, node, "extends", _extends, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function declareFunction(id: t.Identifier): t.DeclareFunction { - return validateNode({ + const node: t.DeclareFunction = { type: "DeclareFunction", id, - }); + }; + const defs = NODE_FIELDS.DeclareFunction; + validate(defs.id, node, "id", id, 1); + return node; } export function declareInterface( id: t.Identifier, @@ -1042,63 +1424,90 @@ export function declareInterface( _extends: Array | null | undefined = null, body: t.ObjectTypeAnnotation, ): t.DeclareInterface { - return validateNode({ + const node: t.DeclareInterface = { type: "DeclareInterface", id, typeParameters, extends: _extends, body, - }); + }; + const defs = NODE_FIELDS.DeclareInterface; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.extends, node, "extends", _extends, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function declareModule( id: t.Identifier | t.StringLiteral, body: t.BlockStatement, kind: "CommonJS" | "ES" | null = null, ): t.DeclareModule { - return validateNode({ + const node: t.DeclareModule = { type: "DeclareModule", id, body, kind, - }); + }; + const defs = NODE_FIELDS.DeclareModule; + validate(defs.id, node, "id", id, 1); + validate(defs.body, node, "body", body, 1); + validate(defs.kind, node, "kind", kind); + return node; } export function declareModuleExports( typeAnnotation: t.TypeAnnotation, ): t.DeclareModuleExports { - return validateNode({ + const node: t.DeclareModuleExports = { type: "DeclareModuleExports", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.DeclareModuleExports; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export function declareTypeAlias( id: t.Identifier, typeParameters: t.TypeParameterDeclaration | null | undefined = null, right: t.FlowType, ): t.DeclareTypeAlias { - return validateNode({ + const node: t.DeclareTypeAlias = { type: "DeclareTypeAlias", id, typeParameters, right, - }); + }; + const defs = NODE_FIELDS.DeclareTypeAlias; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function declareOpaqueType( id: t.Identifier, typeParameters: t.TypeParameterDeclaration | null = null, supertype: t.FlowType | null = null, ): t.DeclareOpaqueType { - return validateNode({ + const node: t.DeclareOpaqueType = { type: "DeclareOpaqueType", id, typeParameters, supertype, - }); + }; + const defs = NODE_FIELDS.DeclareOpaqueType; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.supertype, node, "supertype", supertype, 1); + return node; } export function declareVariable(id: t.Identifier): t.DeclareVariable { - return validateNode({ + const node: t.DeclareVariable = { type: "DeclareVariable", id, - }); + }; + const defs = NODE_FIELDS.DeclareVariable; + validate(defs.id, node, "id", id, 1); + return node; } export function declareExportDeclaration( declaration: t.Flow | null = null, @@ -1107,26 +1516,37 @@ export function declareExportDeclaration( > | null = null, source: t.StringLiteral | null = null, ): t.DeclareExportDeclaration { - return validateNode({ + const node: t.DeclareExportDeclaration = { type: "DeclareExportDeclaration", declaration, specifiers, source, - }); + }; + const defs = NODE_FIELDS.DeclareExportDeclaration; + validate(defs.declaration, node, "declaration", declaration, 1); + validate(defs.specifiers, node, "specifiers", specifiers, 1); + validate(defs.source, node, "source", source, 1); + return node; } export function declareExportAllDeclaration( source: t.StringLiteral, ): t.DeclareExportAllDeclaration { - return validateNode({ + const node: t.DeclareExportAllDeclaration = { type: "DeclareExportAllDeclaration", source, - }); + }; + const defs = NODE_FIELDS.DeclareExportAllDeclaration; + validate(defs.source, node, "source", source, 1); + return node; } export function declaredPredicate(value: t.Flow): t.DeclaredPredicate { - return validateNode({ + const node: t.DeclaredPredicate = { type: "DeclaredPredicate", value, - }); + }; + const defs = NODE_FIELDS.DeclaredPredicate; + validate(defs.value, node, "value", value, 1); + return node; } export function existsTypeAnnotation(): t.ExistsTypeAnnotation { return { @@ -1139,33 +1559,47 @@ export function functionTypeAnnotation( rest: t.FunctionTypeParam | null | undefined = null, returnType: t.FlowType, ): t.FunctionTypeAnnotation { - return validateNode({ + const node: t.FunctionTypeAnnotation = { type: "FunctionTypeAnnotation", typeParameters, params, rest, returnType, - }); + }; + const defs = NODE_FIELDS.FunctionTypeAnnotation; + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.rest, node, "rest", rest, 1); + validate(defs.returnType, node, "returnType", returnType, 1); + return node; } export function functionTypeParam( name: t.Identifier | null | undefined = null, typeAnnotation: t.FlowType, ): t.FunctionTypeParam { - return validateNode({ + const node: t.FunctionTypeParam = { type: "FunctionTypeParam", name, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.FunctionTypeParam; + validate(defs.name, node, "name", name, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export function genericTypeAnnotation( id: t.Identifier | t.QualifiedTypeIdentifier, typeParameters: t.TypeParameterInstantiation | null = null, ): t.GenericTypeAnnotation { - return validateNode({ + const node: t.GenericTypeAnnotation = { type: "GenericTypeAnnotation", id, typeParameters, - }); + }; + const defs = NODE_FIELDS.GenericTypeAnnotation; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export function inferredPredicate(): t.InferredPredicate { return { @@ -1176,11 +1610,15 @@ export function interfaceExtends( id: t.Identifier | t.QualifiedTypeIdentifier, typeParameters: t.TypeParameterInstantiation | null = null, ): t.InterfaceExtends { - return validateNode({ + const node: t.InterfaceExtends = { type: "InterfaceExtends", id, typeParameters, - }); + }; + const defs = NODE_FIELDS.InterfaceExtends; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export function interfaceDeclaration( id: t.Identifier, @@ -1188,31 +1626,44 @@ export function interfaceDeclaration( _extends: Array | null | undefined = null, body: t.ObjectTypeAnnotation, ): t.InterfaceDeclaration { - return validateNode({ + const node: t.InterfaceDeclaration = { type: "InterfaceDeclaration", id, typeParameters, extends: _extends, body, - }); + }; + const defs = NODE_FIELDS.InterfaceDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.extends, node, "extends", _extends, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function interfaceTypeAnnotation( _extends: Array | null | undefined = null, body: t.ObjectTypeAnnotation, ): t.InterfaceTypeAnnotation { - return validateNode({ + const node: t.InterfaceTypeAnnotation = { type: "InterfaceTypeAnnotation", extends: _extends, body, - }); + }; + const defs = NODE_FIELDS.InterfaceTypeAnnotation; + validate(defs.extends, node, "extends", _extends, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function intersectionTypeAnnotation( types: Array, ): t.IntersectionTypeAnnotation { - return validateNode({ + const node: t.IntersectionTypeAnnotation = { type: "IntersectionTypeAnnotation", types, - }); + }; + const defs = NODE_FIELDS.IntersectionTypeAnnotation; + validate(defs.types, node, "types", types, 1); + return node; } export function mixedTypeAnnotation(): t.MixedTypeAnnotation { return { @@ -1227,18 +1678,24 @@ export function emptyTypeAnnotation(): t.EmptyTypeAnnotation { export function nullableTypeAnnotation( typeAnnotation: t.FlowType, ): t.NullableTypeAnnotation { - return validateNode({ + const node: t.NullableTypeAnnotation = { type: "NullableTypeAnnotation", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.NullableTypeAnnotation; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export function numberLiteralTypeAnnotation( value: number, ): t.NumberLiteralTypeAnnotation { - return validateNode({ + const node: t.NumberLiteralTypeAnnotation = { type: "NumberLiteralTypeAnnotation", value, - }); + }; + const defs = NODE_FIELDS.NumberLiteralTypeAnnotation; + validate(defs.value, node, "value", value); + return node; } export function numberTypeAnnotation(): t.NumberTypeAnnotation { return { @@ -1252,14 +1709,21 @@ export function objectTypeAnnotation( internalSlots: Array = [], exact: boolean = false, ): t.ObjectTypeAnnotation { - return validateNode({ + const node: t.ObjectTypeAnnotation = { type: "ObjectTypeAnnotation", properties, indexers, callProperties, internalSlots, exact, - }); + }; + const defs = NODE_FIELDS.ObjectTypeAnnotation; + validate(defs.properties, node, "properties", properties, 1); + validate(defs.indexers, node, "indexers", indexers, 1); + validate(defs.callProperties, node, "callProperties", callProperties, 1); + validate(defs.internalSlots, node, "internalSlots", internalSlots, 1); + validate(defs.exact, node, "exact", exact); + return node; } export function objectTypeInternalSlot( id: t.Identifier, @@ -1268,23 +1732,33 @@ export function objectTypeInternalSlot( _static: boolean, method: boolean, ): t.ObjectTypeInternalSlot { - return validateNode({ + const node: t.ObjectTypeInternalSlot = { type: "ObjectTypeInternalSlot", id, value, optional, static: _static, method, - }); + }; + const defs = NODE_FIELDS.ObjectTypeInternalSlot; + validate(defs.id, node, "id", id, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.optional, node, "optional", optional); + validate(defs.static, node, "static", _static); + validate(defs.method, node, "method", method); + return node; } export function objectTypeCallProperty( value: t.FlowType, ): t.ObjectTypeCallProperty { - return validateNode({ + const node: t.ObjectTypeCallProperty = { type: "ObjectTypeCallProperty", value, static: null, - }); + }; + const defs = NODE_FIELDS.ObjectTypeCallProperty; + validate(defs.value, node, "value", value, 1); + return node; } export function objectTypeIndexer( id: t.Identifier | null | undefined = null, @@ -1292,21 +1766,27 @@ export function objectTypeIndexer( value: t.FlowType, variance: t.Variance | null = null, ): t.ObjectTypeIndexer { - return validateNode({ + const node: t.ObjectTypeIndexer = { type: "ObjectTypeIndexer", id, key, value, variance, static: null, - }); + }; + const defs = NODE_FIELDS.ObjectTypeIndexer; + validate(defs.id, node, "id", id, 1); + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.variance, node, "variance", variance, 1); + return node; } export function objectTypeProperty( key: t.Identifier | t.StringLiteral, value: t.FlowType, variance: t.Variance | null = null, ): t.ObjectTypeProperty { - return validateNode({ + const node: t.ObjectTypeProperty = { type: "ObjectTypeProperty", key, value, @@ -1316,15 +1796,23 @@ export function objectTypeProperty( optional: null, proto: null, static: null, - }); + }; + const defs = NODE_FIELDS.ObjectTypeProperty; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + validate(defs.variance, node, "variance", variance, 1); + return node; } export function objectTypeSpreadProperty( argument: t.FlowType, ): t.ObjectTypeSpreadProperty { - return validateNode({ + const node: t.ObjectTypeSpreadProperty = { type: "ObjectTypeSpreadProperty", argument, - }); + }; + const defs = NODE_FIELDS.ObjectTypeSpreadProperty; + validate(defs.argument, node, "argument", argument, 1); + return node; } export function opaqueType( id: t.Identifier, @@ -1332,31 +1820,44 @@ export function opaqueType( supertype: t.FlowType | null | undefined = null, impltype: t.FlowType, ): t.OpaqueType { - return validateNode({ + const node: t.OpaqueType = { type: "OpaqueType", id, typeParameters, supertype, impltype, - }); + }; + const defs = NODE_FIELDS.OpaqueType; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.supertype, node, "supertype", supertype, 1); + validate(defs.impltype, node, "impltype", impltype, 1); + return node; } export function qualifiedTypeIdentifier( id: t.Identifier, qualification: t.Identifier | t.QualifiedTypeIdentifier, ): t.QualifiedTypeIdentifier { - return validateNode({ + const node: t.QualifiedTypeIdentifier = { type: "QualifiedTypeIdentifier", id, qualification, - }); + }; + const defs = NODE_FIELDS.QualifiedTypeIdentifier; + validate(defs.id, node, "id", id, 1); + validate(defs.qualification, node, "qualification", qualification, 1); + return node; } export function stringLiteralTypeAnnotation( value: string, ): t.StringLiteralTypeAnnotation { - return validateNode({ + const node: t.StringLiteralTypeAnnotation = { type: "StringLiteralTypeAnnotation", value, - }); + }; + const defs = NODE_FIELDS.StringLiteralTypeAnnotation; + validate(defs.value, node, "value", value); + return node; } export function stringTypeAnnotation(): t.StringTypeAnnotation { return { @@ -1376,89 +1877,124 @@ export function thisTypeAnnotation(): t.ThisTypeAnnotation { export function tupleTypeAnnotation( types: Array, ): t.TupleTypeAnnotation { - return validateNode({ + const node: t.TupleTypeAnnotation = { type: "TupleTypeAnnotation", types, - }); + }; + const defs = NODE_FIELDS.TupleTypeAnnotation; + validate(defs.types, node, "types", types, 1); + return node; } export function typeofTypeAnnotation( argument: t.FlowType, ): t.TypeofTypeAnnotation { - return validateNode({ + const node: t.TypeofTypeAnnotation = { type: "TypeofTypeAnnotation", argument, - }); + }; + const defs = NODE_FIELDS.TypeofTypeAnnotation; + validate(defs.argument, node, "argument", argument, 1); + return node; } export function typeAlias( id: t.Identifier, typeParameters: t.TypeParameterDeclaration | null | undefined = null, right: t.FlowType, ): t.TypeAlias { - return validateNode({ + const node: t.TypeAlias = { type: "TypeAlias", id, typeParameters, right, - }); + }; + const defs = NODE_FIELDS.TypeAlias; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.right, node, "right", right, 1); + return node; } export function typeAnnotation(typeAnnotation: t.FlowType): t.TypeAnnotation { - return validateNode({ + const node: t.TypeAnnotation = { type: "TypeAnnotation", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TypeAnnotation; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export function typeCastExpression( expression: t.Expression, typeAnnotation: t.TypeAnnotation, ): t.TypeCastExpression { - return validateNode({ + const node: t.TypeCastExpression = { type: "TypeCastExpression", expression, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TypeCastExpression; + validate(defs.expression, node, "expression", expression, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export function typeParameter( bound: t.TypeAnnotation | null = null, _default: t.FlowType | null = null, variance: t.Variance | null = null, ): t.TypeParameter { - return validateNode({ + const node: t.TypeParameter = { type: "TypeParameter", bound, default: _default, variance, name: null, - }); + }; + const defs = NODE_FIELDS.TypeParameter; + validate(defs.bound, node, "bound", bound, 1); + validate(defs.default, node, "default", _default, 1); + validate(defs.variance, node, "variance", variance, 1); + return node; } export function typeParameterDeclaration( params: Array, ): t.TypeParameterDeclaration { - return validateNode({ + const node: t.TypeParameterDeclaration = { type: "TypeParameterDeclaration", params, - }); + }; + const defs = NODE_FIELDS.TypeParameterDeclaration; + validate(defs.params, node, "params", params, 1); + return node; } export function typeParameterInstantiation( params: Array, ): t.TypeParameterInstantiation { - return validateNode({ + const node: t.TypeParameterInstantiation = { type: "TypeParameterInstantiation", params, - }); + }; + const defs = NODE_FIELDS.TypeParameterInstantiation; + validate(defs.params, node, "params", params, 1); + return node; } export function unionTypeAnnotation( types: Array, ): t.UnionTypeAnnotation { - return validateNode({ + const node: t.UnionTypeAnnotation = { type: "UnionTypeAnnotation", types, - }); + }; + const defs = NODE_FIELDS.UnionTypeAnnotation; + validate(defs.types, node, "types", types, 1); + return node; } export function variance(kind: "minus" | "plus"): t.Variance { - return validateNode({ + const node: t.Variance = { type: "Variance", kind, - }); + }; + const defs = NODE_FIELDS.Variance; + validate(defs.kind, node, "kind", kind); + return node; } export function voidTypeAnnotation(): t.VoidTypeAnnotation { return { @@ -1473,104 +2009,142 @@ export function enumDeclaration( | t.EnumStringBody | t.EnumSymbolBody, ): t.EnumDeclaration { - return validateNode({ + const node: t.EnumDeclaration = { type: "EnumDeclaration", id, body, - }); + }; + const defs = NODE_FIELDS.EnumDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.body, node, "body", body, 1); + return node; } export function enumBooleanBody( members: Array, ): t.EnumBooleanBody { - return validateNode({ + const node: t.EnumBooleanBody = { type: "EnumBooleanBody", members, explicitType: null, hasUnknownMembers: null, - }); + }; + const defs = NODE_FIELDS.EnumBooleanBody; + validate(defs.members, node, "members", members, 1); + return node; } export function enumNumberBody( members: Array, ): t.EnumNumberBody { - return validateNode({ + const node: t.EnumNumberBody = { type: "EnumNumberBody", members, explicitType: null, hasUnknownMembers: null, - }); + }; + const defs = NODE_FIELDS.EnumNumberBody; + validate(defs.members, node, "members", members, 1); + return node; } export function enumStringBody( members: Array, ): t.EnumStringBody { - return validateNode({ + const node: t.EnumStringBody = { type: "EnumStringBody", members, explicitType: null, hasUnknownMembers: null, - }); + }; + const defs = NODE_FIELDS.EnumStringBody; + validate(defs.members, node, "members", members, 1); + return node; } export function enumSymbolBody( members: Array, ): t.EnumSymbolBody { - return validateNode({ + const node: t.EnumSymbolBody = { type: "EnumSymbolBody", members, hasUnknownMembers: null, - }); + }; + const defs = NODE_FIELDS.EnumSymbolBody; + validate(defs.members, node, "members", members, 1); + return node; } export function enumBooleanMember(id: t.Identifier): t.EnumBooleanMember { - return validateNode({ + const node: t.EnumBooleanMember = { type: "EnumBooleanMember", id, init: null, - }); + }; + const defs = NODE_FIELDS.EnumBooleanMember; + validate(defs.id, node, "id", id, 1); + return node; } export function enumNumberMember( id: t.Identifier, init: t.NumericLiteral, ): t.EnumNumberMember { - return validateNode({ + const node: t.EnumNumberMember = { type: "EnumNumberMember", id, init, - }); + }; + const defs = NODE_FIELDS.EnumNumberMember; + validate(defs.id, node, "id", id, 1); + validate(defs.init, node, "init", init, 1); + return node; } export function enumStringMember( id: t.Identifier, init: t.StringLiteral, ): t.EnumStringMember { - return validateNode({ + const node: t.EnumStringMember = { type: "EnumStringMember", id, init, - }); + }; + const defs = NODE_FIELDS.EnumStringMember; + validate(defs.id, node, "id", id, 1); + validate(defs.init, node, "init", init, 1); + return node; } export function enumDefaultedMember(id: t.Identifier): t.EnumDefaultedMember { - return validateNode({ + const node: t.EnumDefaultedMember = { type: "EnumDefaultedMember", id, - }); + }; + const defs = NODE_FIELDS.EnumDefaultedMember; + validate(defs.id, node, "id", id, 1); + return node; } export function indexedAccessType( objectType: t.FlowType, indexType: t.FlowType, ): t.IndexedAccessType { - return validateNode({ + const node: t.IndexedAccessType = { type: "IndexedAccessType", objectType, indexType, - }); + }; + const defs = NODE_FIELDS.IndexedAccessType; + validate(defs.objectType, node, "objectType", objectType, 1); + validate(defs.indexType, node, "indexType", indexType, 1); + return node; } export function optionalIndexedAccessType( objectType: t.FlowType, indexType: t.FlowType, ): t.OptionalIndexedAccessType { - return validateNode({ + const node: t.OptionalIndexedAccessType = { type: "OptionalIndexedAccessType", objectType, indexType, optional: null, - }); + }; + const defs = NODE_FIELDS.OptionalIndexedAccessType; + validate(defs.objectType, node, "objectType", objectType, 1); + validate(defs.indexType, node, "indexType", indexType, 1); + return node; } export function jsxAttribute( name: t.JSXIdentifier | t.JSXNamespacedName, @@ -1581,20 +2155,27 @@ export function jsxAttribute( | t.JSXExpressionContainer | null = null, ): t.JSXAttribute { - return validateNode({ + const node: t.JSXAttribute = { type: "JSXAttribute", name, value, - }); + }; + const defs = NODE_FIELDS.JSXAttribute; + validate(defs.name, node, "name", name, 1); + validate(defs.value, node, "value", value, 1); + return node; } export { jsxAttribute as jSXAttribute }; export function jsxClosingElement( name: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName, ): t.JSXClosingElement { - return validateNode({ + const node: t.JSXClosingElement = { type: "JSXClosingElement", name, - }); + }; + const defs = NODE_FIELDS.JSXClosingElement; + validate(defs.name, node, "name", name, 1); + return node; } export { jsxClosingElement as jSXClosingElement }; export function jsxElement( @@ -1609,13 +2190,19 @@ export function jsxElement( >, selfClosing: boolean | null = null, ): t.JSXElement { - return validateNode({ + const node: t.JSXElement = { type: "JSXElement", openingElement, closingElement, children, selfClosing, - }); + }; + const defs = NODE_FIELDS.JSXElement; + validate(defs.openingElement, node, "openingElement", openingElement, 1); + validate(defs.closingElement, node, "closingElement", closingElement, 1); + validate(defs.children, node, "children", children, 1); + validate(defs.selfClosing, node, "selfClosing", selfClosing); + return node; } export { jsxElement as jSXElement }; export function jsxEmptyExpression(): t.JSXEmptyExpression { @@ -1627,46 +2214,63 @@ export { jsxEmptyExpression as jSXEmptyExpression }; export function jsxExpressionContainer( expression: t.Expression | t.JSXEmptyExpression, ): t.JSXExpressionContainer { - return validateNode({ + const node: t.JSXExpressionContainer = { type: "JSXExpressionContainer", expression, - }); + }; + const defs = NODE_FIELDS.JSXExpressionContainer; + validate(defs.expression, node, "expression", expression, 1); + return node; } export { jsxExpressionContainer as jSXExpressionContainer }; export function jsxSpreadChild(expression: t.Expression): t.JSXSpreadChild { - return validateNode({ + const node: t.JSXSpreadChild = { type: "JSXSpreadChild", expression, - }); + }; + const defs = NODE_FIELDS.JSXSpreadChild; + validate(defs.expression, node, "expression", expression, 1); + return node; } export { jsxSpreadChild as jSXSpreadChild }; export function jsxIdentifier(name: string): t.JSXIdentifier { - return validateNode({ + const node: t.JSXIdentifier = { type: "JSXIdentifier", name, - }); + }; + const defs = NODE_FIELDS.JSXIdentifier; + validate(defs.name, node, "name", name); + return node; } export { jsxIdentifier as jSXIdentifier }; export function jsxMemberExpression( object: t.JSXMemberExpression | t.JSXIdentifier, property: t.JSXIdentifier, ): t.JSXMemberExpression { - return validateNode({ + const node: t.JSXMemberExpression = { type: "JSXMemberExpression", object, property, - }); + }; + const defs = NODE_FIELDS.JSXMemberExpression; + validate(defs.object, node, "object", object, 1); + validate(defs.property, node, "property", property, 1); + return node; } export { jsxMemberExpression as jSXMemberExpression }; export function jsxNamespacedName( namespace: t.JSXIdentifier, name: t.JSXIdentifier, ): t.JSXNamespacedName { - return validateNode({ + const node: t.JSXNamespacedName = { type: "JSXNamespacedName", namespace, name, - }); + }; + const defs = NODE_FIELDS.JSXNamespacedName; + validate(defs.namespace, node, "namespace", namespace, 1); + validate(defs.name, node, "name", name, 1); + return node; } export { jsxNamespacedName as jSXNamespacedName }; export function jsxOpeningElement( @@ -1674,28 +2278,39 @@ export function jsxOpeningElement( attributes: Array, selfClosing: boolean = false, ): t.JSXOpeningElement { - return validateNode({ + const node: t.JSXOpeningElement = { type: "JSXOpeningElement", name, attributes, selfClosing, - }); + }; + const defs = NODE_FIELDS.JSXOpeningElement; + validate(defs.name, node, "name", name, 1); + validate(defs.attributes, node, "attributes", attributes, 1); + validate(defs.selfClosing, node, "selfClosing", selfClosing); + return node; } export { jsxOpeningElement as jSXOpeningElement }; export function jsxSpreadAttribute( argument: t.Expression, ): t.JSXSpreadAttribute { - return validateNode({ + const node: t.JSXSpreadAttribute = { type: "JSXSpreadAttribute", argument, - }); + }; + const defs = NODE_FIELDS.JSXSpreadAttribute; + validate(defs.argument, node, "argument", argument, 1); + return node; } export { jsxSpreadAttribute as jSXSpreadAttribute }; export function jsxText(value: string): t.JSXText { - return validateNode({ + const node: t.JSXText = { type: "JSXText", value, - }); + }; + const defs = NODE_FIELDS.JSXText; + validate(defs.value, node, "value", value); + return node; } export { jsxText as jSXText }; export function jsxFragment( @@ -1709,12 +2324,17 @@ export function jsxFragment( | t.JSXFragment >, ): t.JSXFragment { - return validateNode({ + const node: t.JSXFragment = { type: "JSXFragment", openingFragment, closingFragment, children, - }); + }; + const defs = NODE_FIELDS.JSXFragment; + validate(defs.openingFragment, node, "openingFragment", openingFragment, 1); + validate(defs.closingFragment, node, "closingFragment", closingFragment, 1); + validate(defs.children, node, "children", children, 1); + return node; } export { jsxFragment as jSXFragment }; export function jsxOpeningFragment(): t.JSXOpeningFragment { @@ -1746,17 +2366,24 @@ export function placeholder( | "Pattern", name: t.Identifier, ): t.Placeholder { - return validateNode({ + const node: t.Placeholder = { type: "Placeholder", expectedNode, name, - }); + }; + const defs = NODE_FIELDS.Placeholder; + validate(defs.expectedNode, node, "expectedNode", expectedNode); + validate(defs.name, node, "name", name, 1); + return node; } export function v8IntrinsicIdentifier(name: string): t.V8IntrinsicIdentifier { - return validateNode({ + const node: t.V8IntrinsicIdentifier = { type: "V8IntrinsicIdentifier", name, - }); + }; + const defs = NODE_FIELDS.V8IntrinsicIdentifier; + validate(defs.name, node, "name", name); + return node; } export function argumentPlaceholder(): t.ArgumentPlaceholder { return { @@ -1767,73 +2394,103 @@ export function bindExpression( object: t.Expression, callee: t.Expression, ): t.BindExpression { - return validateNode({ + const node: t.BindExpression = { type: "BindExpression", object, callee, - }); + }; + const defs = NODE_FIELDS.BindExpression; + validate(defs.object, node, "object", object, 1); + validate(defs.callee, node, "callee", callee, 1); + return node; } export function importAttribute( key: t.Identifier | t.StringLiteral, value: t.StringLiteral, ): t.ImportAttribute { - return validateNode({ + const node: t.ImportAttribute = { type: "ImportAttribute", key, value, - }); + }; + const defs = NODE_FIELDS.ImportAttribute; + validate(defs.key, node, "key", key, 1); + validate(defs.value, node, "value", value, 1); + return node; } export function decorator(expression: t.Expression): t.Decorator { - return validateNode({ + const node: t.Decorator = { type: "Decorator", expression, - }); + }; + const defs = NODE_FIELDS.Decorator; + validate(defs.expression, node, "expression", expression, 1); + return node; } export function doExpression( body: t.BlockStatement, async: boolean = false, ): t.DoExpression { - return validateNode({ + const node: t.DoExpression = { type: "DoExpression", body, async, - }); + }; + const defs = NODE_FIELDS.DoExpression; + validate(defs.body, node, "body", body, 1); + validate(defs.async, node, "async", async); + return node; } export function exportDefaultSpecifier( exported: t.Identifier, ): t.ExportDefaultSpecifier { - return validateNode({ + const node: t.ExportDefaultSpecifier = { type: "ExportDefaultSpecifier", exported, - }); + }; + const defs = NODE_FIELDS.ExportDefaultSpecifier; + validate(defs.exported, node, "exported", exported, 1); + return node; } export function recordExpression( properties: Array, ): t.RecordExpression { - return validateNode({ + const node: t.RecordExpression = { type: "RecordExpression", properties, - }); + }; + const defs = NODE_FIELDS.RecordExpression; + validate(defs.properties, node, "properties", properties, 1); + return node; } export function tupleExpression( elements: Array = [], ): t.TupleExpression { - return validateNode({ + const node: t.TupleExpression = { type: "TupleExpression", elements, - }); + }; + const defs = NODE_FIELDS.TupleExpression; + validate(defs.elements, node, "elements", elements, 1); + return node; } export function decimalLiteral(value: string): t.DecimalLiteral { - return validateNode({ + const node: t.DecimalLiteral = { type: "DecimalLiteral", value, - }); + }; + const defs = NODE_FIELDS.DecimalLiteral; + validate(defs.value, node, "value", value); + return node; } export function moduleExpression(body: t.Program): t.ModuleExpression { - return validateNode({ + const node: t.ModuleExpression = { type: "ModuleExpression", body, - }); + }; + const defs = NODE_FIELDS.ModuleExpression; + validate(defs.body, node, "body", body, 1); + return node; } export function topicReference(): t.TopicReference { return { @@ -1843,18 +2500,24 @@ export function topicReference(): t.TopicReference { export function pipelineTopicExpression( expression: t.Expression, ): t.PipelineTopicExpression { - return validateNode({ + const node: t.PipelineTopicExpression = { type: "PipelineTopicExpression", expression, - }); + }; + const defs = NODE_FIELDS.PipelineTopicExpression; + validate(defs.expression, node, "expression", expression, 1); + return node; } export function pipelineBareFunction( callee: t.Expression, ): t.PipelineBareFunction { - return validateNode({ + const node: t.PipelineBareFunction = { type: "PipelineBareFunction", callee, - }); + }; + const defs = NODE_FIELDS.PipelineBareFunction; + validate(defs.callee, node, "callee", callee, 1); + return node; } export function pipelinePrimaryTopicReference(): t.PipelinePrimaryTopicReference { return { @@ -1864,10 +2527,13 @@ export function pipelinePrimaryTopicReference(): t.PipelinePrimaryTopicReference export function tsParameterProperty( parameter: t.Identifier | t.AssignmentPattern, ): t.TSParameterProperty { - return validateNode({ + const node: t.TSParameterProperty = { type: "TSParameterProperty", parameter, - }); + }; + const defs = NODE_FIELDS.TSParameterProperty; + validate(defs.parameter, node, "parameter", parameter, 1); + return node; } export { tsParameterProperty as tSParameterProperty }; export function tsDeclareFunction( @@ -1880,13 +2546,19 @@ export function tsDeclareFunction( params: Array, returnType: t.TSTypeAnnotation | t.Noop | null = null, ): t.TSDeclareFunction { - return validateNode({ + const node: t.TSDeclareFunction = { type: "TSDeclareFunction", id, typeParameters, params, returnType, - }); + }; + const defs = NODE_FIELDS.TSDeclareFunction; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.returnType, node, "returnType", returnType, 1); + return node; } export { tsDeclareFunction as tSDeclareFunction }; export function tsDeclareMethod( @@ -1907,25 +2579,36 @@ export function tsDeclareMethod( >, returnType: t.TSTypeAnnotation | t.Noop | null = null, ): t.TSDeclareMethod { - return validateNode({ + const node: t.TSDeclareMethod = { type: "TSDeclareMethod", decorators, key, typeParameters, params, returnType, - }); + }; + const defs = NODE_FIELDS.TSDeclareMethod; + validate(defs.decorators, node, "decorators", decorators, 1); + validate(defs.key, node, "key", key, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.params, node, "params", params, 1); + validate(defs.returnType, node, "returnType", returnType, 1); + return node; } export { tsDeclareMethod as tSDeclareMethod }; export function tsQualifiedName( left: t.TSEntityName, right: t.Identifier, ): t.TSQualifiedName { - return validateNode({ + const node: t.TSQualifiedName = { type: "TSQualifiedName", left, right, - }); + }; + const defs = NODE_FIELDS.TSQualifiedName; + validate(defs.left, node, "left", left, 1); + validate(defs.right, node, "right", right, 1); + return node; } export { tsQualifiedName as tSQualifiedName }; export function tsCallSignatureDeclaration( @@ -1935,12 +2618,17 @@ export function tsCallSignatureDeclaration( >, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSCallSignatureDeclaration { - return validateNode({ + const node: t.TSCallSignatureDeclaration = { type: "TSCallSignatureDeclaration", typeParameters, parameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSCallSignatureDeclaration; + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsCallSignatureDeclaration as tSCallSignatureDeclaration }; export function tsConstructSignatureDeclaration( @@ -1950,24 +2638,33 @@ export function tsConstructSignatureDeclaration( >, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSConstructSignatureDeclaration { - return validateNode({ + const node: t.TSConstructSignatureDeclaration = { type: "TSConstructSignatureDeclaration", typeParameters, parameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSConstructSignatureDeclaration; + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsConstructSignatureDeclaration as tSConstructSignatureDeclaration }; export function tsPropertySignature( key: t.Expression, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSPropertySignature { - return validateNode({ + const node: t.TSPropertySignature = { type: "TSPropertySignature", key, typeAnnotation, kind: null, - }); + }; + const defs = NODE_FIELDS.TSPropertySignature; + validate(defs.key, node, "key", key, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsPropertySignature as tSPropertySignature }; export function tsMethodSignature( @@ -1978,25 +2675,35 @@ export function tsMethodSignature( >, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSMethodSignature { - return validateNode({ + const node: t.TSMethodSignature = { type: "TSMethodSignature", key, typeParameters, parameters, typeAnnotation, kind: null, - }); + }; + const defs = NODE_FIELDS.TSMethodSignature; + validate(defs.key, node, "key", key, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsMethodSignature as tSMethodSignature }; export function tsIndexSignature( parameters: Array, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSIndexSignature { - return validateNode({ + const node: t.TSIndexSignature = { type: "TSIndexSignature", parameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSIndexSignature; + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsIndexSignature as tSIndexSignature }; export function tsAnyKeyword(): t.TSAnyKeyword { @@ -2090,12 +2797,17 @@ export function tsFunctionType( >, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSFunctionType { - return validateNode({ + const node: t.TSFunctionType = { type: "TSFunctionType", typeParameters, parameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSFunctionType; + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsFunctionType as tSFunctionType }; export function tsConstructorType( @@ -2105,23 +2817,32 @@ export function tsConstructorType( >, typeAnnotation: t.TSTypeAnnotation | null = null, ): t.TSConstructorType { - return validateNode({ + const node: t.TSConstructorType = { type: "TSConstructorType", typeParameters, parameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSConstructorType; + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.parameters, node, "parameters", parameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsConstructorType as tSConstructorType }; export function tsTypeReference( typeName: t.TSEntityName, typeParameters: t.TSTypeParameterInstantiation | null = null, ): t.TSTypeReference { - return validateNode({ + const node: t.TSTypeReference = { type: "TSTypeReference", typeName, typeParameters, - }); + }; + const defs = NODE_FIELDS.TSTypeReference; + validate(defs.typeName, node, "typeName", typeName, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export { tsTypeReference as tSTypeReference }; export function tsTypePredicate( @@ -2129,62 +2850,86 @@ export function tsTypePredicate( typeAnnotation: t.TSTypeAnnotation | null = null, asserts: boolean | null = null, ): t.TSTypePredicate { - return validateNode({ + const node: t.TSTypePredicate = { type: "TSTypePredicate", parameterName, typeAnnotation, asserts, - }); + }; + const defs = NODE_FIELDS.TSTypePredicate; + validate(defs.parameterName, node, "parameterName", parameterName, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + validate(defs.asserts, node, "asserts", asserts); + return node; } export { tsTypePredicate as tSTypePredicate }; export function tsTypeQuery( exprName: t.TSEntityName | t.TSImportType, typeParameters: t.TSTypeParameterInstantiation | null = null, ): t.TSTypeQuery { - return validateNode({ + const node: t.TSTypeQuery = { type: "TSTypeQuery", exprName, typeParameters, - }); + }; + const defs = NODE_FIELDS.TSTypeQuery; + validate(defs.exprName, node, "exprName", exprName, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export { tsTypeQuery as tSTypeQuery }; export function tsTypeLiteral( members: Array, ): t.TSTypeLiteral { - return validateNode({ + const node: t.TSTypeLiteral = { type: "TSTypeLiteral", members, - }); + }; + const defs = NODE_FIELDS.TSTypeLiteral; + validate(defs.members, node, "members", members, 1); + return node; } export { tsTypeLiteral as tSTypeLiteral }; export function tsArrayType(elementType: t.TSType): t.TSArrayType { - return validateNode({ + const node: t.TSArrayType = { type: "TSArrayType", elementType, - }); + }; + const defs = NODE_FIELDS.TSArrayType; + validate(defs.elementType, node, "elementType", elementType, 1); + return node; } export { tsArrayType as tSArrayType }; export function tsTupleType( elementTypes: Array, ): t.TSTupleType { - return validateNode({ + const node: t.TSTupleType = { type: "TSTupleType", elementTypes, - }); + }; + const defs = NODE_FIELDS.TSTupleType; + validate(defs.elementTypes, node, "elementTypes", elementTypes, 1); + return node; } export { tsTupleType as tSTupleType }; export function tsOptionalType(typeAnnotation: t.TSType): t.TSOptionalType { - return validateNode({ + const node: t.TSOptionalType = { type: "TSOptionalType", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSOptionalType; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsOptionalType as tSOptionalType }; export function tsRestType(typeAnnotation: t.TSType): t.TSRestType { - return validateNode({ + const node: t.TSRestType = { type: "TSRestType", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSRestType; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsRestType as tSRestType }; export function tsNamedTupleMember( @@ -2192,28 +2937,39 @@ export function tsNamedTupleMember( elementType: t.TSType, optional: boolean = false, ): t.TSNamedTupleMember { - return validateNode({ + const node: t.TSNamedTupleMember = { type: "TSNamedTupleMember", label, elementType, optional, - }); + }; + const defs = NODE_FIELDS.TSNamedTupleMember; + validate(defs.label, node, "label", label, 1); + validate(defs.elementType, node, "elementType", elementType, 1); + validate(defs.optional, node, "optional", optional); + return node; } export { tsNamedTupleMember as tSNamedTupleMember }; export function tsUnionType(types: Array): t.TSUnionType { - return validateNode({ + const node: t.TSUnionType = { type: "TSUnionType", types, - }); + }; + const defs = NODE_FIELDS.TSUnionType; + validate(defs.types, node, "types", types, 1); + return node; } export { tsUnionType as tSUnionType }; export function tsIntersectionType( types: Array, ): t.TSIntersectionType { - return validateNode({ + const node: t.TSIntersectionType = { type: "TSIntersectionType", types, - }); + }; + const defs = NODE_FIELDS.TSIntersectionType; + validate(defs.types, node, "types", types, 1); + return node; } export { tsIntersectionType as tSIntersectionType }; export function tsConditionalType( @@ -2222,48 +2978,67 @@ export function tsConditionalType( trueType: t.TSType, falseType: t.TSType, ): t.TSConditionalType { - return validateNode({ + const node: t.TSConditionalType = { type: "TSConditionalType", checkType, extendsType, trueType, falseType, - }); + }; + const defs = NODE_FIELDS.TSConditionalType; + validate(defs.checkType, node, "checkType", checkType, 1); + validate(defs.extendsType, node, "extendsType", extendsType, 1); + validate(defs.trueType, node, "trueType", trueType, 1); + validate(defs.falseType, node, "falseType", falseType, 1); + return node; } export { tsConditionalType as tSConditionalType }; export function tsInferType(typeParameter: t.TSTypeParameter): t.TSInferType { - return validateNode({ + const node: t.TSInferType = { type: "TSInferType", typeParameter, - }); + }; + const defs = NODE_FIELDS.TSInferType; + validate(defs.typeParameter, node, "typeParameter", typeParameter, 1); + return node; } export { tsInferType as tSInferType }; export function tsParenthesizedType( typeAnnotation: t.TSType, ): t.TSParenthesizedType { - return validateNode({ + const node: t.TSParenthesizedType = { type: "TSParenthesizedType", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSParenthesizedType; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsParenthesizedType as tSParenthesizedType }; export function tsTypeOperator(typeAnnotation: t.TSType): t.TSTypeOperator { - return validateNode({ + const node: t.TSTypeOperator = { type: "TSTypeOperator", typeAnnotation, operator: null, - }); + }; + const defs = NODE_FIELDS.TSTypeOperator; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsTypeOperator as tSTypeOperator }; export function tsIndexedAccessType( objectType: t.TSType, indexType: t.TSType, ): t.TSIndexedAccessType { - return validateNode({ + const node: t.TSIndexedAccessType = { type: "TSIndexedAccessType", objectType, indexType, - }); + }; + const defs = NODE_FIELDS.TSIndexedAccessType; + validate(defs.objectType, node, "objectType", objectType, 1); + validate(defs.indexType, node, "indexType", indexType, 1); + return node; } export { tsIndexedAccessType as tSIndexedAccessType }; export function tsMappedType( @@ -2271,12 +3046,17 @@ export function tsMappedType( typeAnnotation: t.TSType | null = null, nameType: t.TSType | null = null, ): t.TSMappedType { - return validateNode({ + const node: t.TSMappedType = { type: "TSMappedType", typeParameter, typeAnnotation, nameType, - }); + }; + const defs = NODE_FIELDS.TSMappedType; + validate(defs.typeParameter, node, "typeParameter", typeParameter, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + validate(defs.nameType, node, "nameType", nameType, 1); + return node; } export { tsMappedType as tSMappedType }; export function tsLiteralType( @@ -2288,21 +3068,28 @@ export function tsLiteralType( | t.TemplateLiteral | t.UnaryExpression, ): t.TSLiteralType { - return validateNode({ + const node: t.TSLiteralType = { type: "TSLiteralType", literal, - }); + }; + const defs = NODE_FIELDS.TSLiteralType; + validate(defs.literal, node, "literal", literal, 1); + return node; } export { tsLiteralType as tSLiteralType }; export function tsExpressionWithTypeArguments( expression: t.TSEntityName, typeParameters: t.TSTypeParameterInstantiation | null = null, ): t.TSExpressionWithTypeArguments { - return validateNode({ + const node: t.TSExpressionWithTypeArguments = { type: "TSExpressionWithTypeArguments", expression, typeParameters, - }); + }; + const defs = NODE_FIELDS.TSExpressionWithTypeArguments; + validate(defs.expression, node, "expression", expression, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export { tsExpressionWithTypeArguments as tSExpressionWithTypeArguments }; export function tsInterfaceDeclaration( @@ -2311,22 +3098,31 @@ export function tsInterfaceDeclaration( _extends: Array | null | undefined = null, body: t.TSInterfaceBody, ): t.TSInterfaceDeclaration { - return validateNode({ + const node: t.TSInterfaceDeclaration = { type: "TSInterfaceDeclaration", id, typeParameters, extends: _extends, body, - }); + }; + const defs = NODE_FIELDS.TSInterfaceDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.extends, node, "extends", _extends, 1); + validate(defs.body, node, "body", body, 1); + return node; } export { tsInterfaceDeclaration as tSInterfaceDeclaration }; export function tsInterfaceBody( body: Array, ): t.TSInterfaceBody { - return validateNode({ + const node: t.TSInterfaceBody = { type: "TSInterfaceBody", body, - }); + }; + const defs = NODE_FIELDS.TSInterfaceBody; + validate(defs.body, node, "body", body, 1); + return node; } export { tsInterfaceBody as tSInterfaceBody }; export function tsTypeAliasDeclaration( @@ -2334,96 +3130,132 @@ export function tsTypeAliasDeclaration( typeParameters: t.TSTypeParameterDeclaration | null | undefined = null, typeAnnotation: t.TSType, ): t.TSTypeAliasDeclaration { - return validateNode({ + const node: t.TSTypeAliasDeclaration = { type: "TSTypeAliasDeclaration", id, typeParameters, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSTypeAliasDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsTypeAliasDeclaration as tSTypeAliasDeclaration }; export function tsInstantiationExpression( expression: t.Expression, typeParameters: t.TSTypeParameterInstantiation | null = null, ): t.TSInstantiationExpression { - return validateNode({ + const node: t.TSInstantiationExpression = { type: "TSInstantiationExpression", expression, typeParameters, - }); + }; + const defs = NODE_FIELDS.TSInstantiationExpression; + validate(defs.expression, node, "expression", expression, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export { tsInstantiationExpression as tSInstantiationExpression }; export function tsAsExpression( expression: t.Expression, typeAnnotation: t.TSType, ): t.TSAsExpression { - return validateNode({ + const node: t.TSAsExpression = { type: "TSAsExpression", expression, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSAsExpression; + validate(defs.expression, node, "expression", expression, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsAsExpression as tSAsExpression }; export function tsSatisfiesExpression( expression: t.Expression, typeAnnotation: t.TSType, ): t.TSSatisfiesExpression { - return validateNode({ + const node: t.TSSatisfiesExpression = { type: "TSSatisfiesExpression", expression, typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSSatisfiesExpression; + validate(defs.expression, node, "expression", expression, 1); + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsSatisfiesExpression as tSSatisfiesExpression }; export function tsTypeAssertion( typeAnnotation: t.TSType, expression: t.Expression, ): t.TSTypeAssertion { - return validateNode({ + const node: t.TSTypeAssertion = { type: "TSTypeAssertion", typeAnnotation, expression, - }); + }; + const defs = NODE_FIELDS.TSTypeAssertion; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + validate(defs.expression, node, "expression", expression, 1); + return node; } export { tsTypeAssertion as tSTypeAssertion }; export function tsEnumDeclaration( id: t.Identifier, members: Array, ): t.TSEnumDeclaration { - return validateNode({ + const node: t.TSEnumDeclaration = { type: "TSEnumDeclaration", id, members, - }); + }; + const defs = NODE_FIELDS.TSEnumDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.members, node, "members", members, 1); + return node; } export { tsEnumDeclaration as tSEnumDeclaration }; export function tsEnumMember( id: t.Identifier | t.StringLiteral, initializer: t.Expression | null = null, ): t.TSEnumMember { - return validateNode({ + const node: t.TSEnumMember = { type: "TSEnumMember", id, initializer, - }); + }; + const defs = NODE_FIELDS.TSEnumMember; + validate(defs.id, node, "id", id, 1); + validate(defs.initializer, node, "initializer", initializer, 1); + return node; } export { tsEnumMember as tSEnumMember }; export function tsModuleDeclaration( id: t.Identifier | t.StringLiteral, body: t.TSModuleBlock | t.TSModuleDeclaration, ): t.TSModuleDeclaration { - return validateNode({ + const node: t.TSModuleDeclaration = { type: "TSModuleDeclaration", id, body, - }); + }; + const defs = NODE_FIELDS.TSModuleDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.body, node, "body", body, 1); + return node; } export { tsModuleDeclaration as tSModuleDeclaration }; export function tsModuleBlock(body: Array): t.TSModuleBlock { - return validateNode({ + const node: t.TSModuleBlock = { type: "TSModuleBlock", body, - }); + }; + const defs = NODE_FIELDS.TSModuleBlock; + validate(defs.body, node, "body", body, 1); + return node; } export { tsModuleBlock as tSModuleBlock }; export function tsImportType( @@ -2431,85 +3263,115 @@ export function tsImportType( qualifier: t.TSEntityName | null = null, typeParameters: t.TSTypeParameterInstantiation | null = null, ): t.TSImportType { - return validateNode({ + const node: t.TSImportType = { type: "TSImportType", argument, qualifier, typeParameters, - }); + }; + const defs = NODE_FIELDS.TSImportType; + validate(defs.argument, node, "argument", argument, 1); + validate(defs.qualifier, node, "qualifier", qualifier, 1); + validate(defs.typeParameters, node, "typeParameters", typeParameters, 1); + return node; } export { tsImportType as tSImportType }; export function tsImportEqualsDeclaration( id: t.Identifier, moduleReference: t.TSEntityName | t.TSExternalModuleReference, ): t.TSImportEqualsDeclaration { - return validateNode({ + const node: t.TSImportEqualsDeclaration = { type: "TSImportEqualsDeclaration", id, moduleReference, isExport: null, - }); + }; + const defs = NODE_FIELDS.TSImportEqualsDeclaration; + validate(defs.id, node, "id", id, 1); + validate(defs.moduleReference, node, "moduleReference", moduleReference, 1); + return node; } export { tsImportEqualsDeclaration as tSImportEqualsDeclaration }; export function tsExternalModuleReference( expression: t.StringLiteral, ): t.TSExternalModuleReference { - return validateNode({ + const node: t.TSExternalModuleReference = { type: "TSExternalModuleReference", expression, - }); + }; + const defs = NODE_FIELDS.TSExternalModuleReference; + validate(defs.expression, node, "expression", expression, 1); + return node; } export { tsExternalModuleReference as tSExternalModuleReference }; export function tsNonNullExpression( expression: t.Expression, ): t.TSNonNullExpression { - return validateNode({ + const node: t.TSNonNullExpression = { type: "TSNonNullExpression", expression, - }); + }; + const defs = NODE_FIELDS.TSNonNullExpression; + validate(defs.expression, node, "expression", expression, 1); + return node; } export { tsNonNullExpression as tSNonNullExpression }; export function tsExportAssignment( expression: t.Expression, ): t.TSExportAssignment { - return validateNode({ + const node: t.TSExportAssignment = { type: "TSExportAssignment", expression, - }); + }; + const defs = NODE_FIELDS.TSExportAssignment; + validate(defs.expression, node, "expression", expression, 1); + return node; } export { tsExportAssignment as tSExportAssignment }; export function tsNamespaceExportDeclaration( id: t.Identifier, ): t.TSNamespaceExportDeclaration { - return validateNode({ + const node: t.TSNamespaceExportDeclaration = { type: "TSNamespaceExportDeclaration", id, - }); + }; + const defs = NODE_FIELDS.TSNamespaceExportDeclaration; + validate(defs.id, node, "id", id, 1); + return node; } export { tsNamespaceExportDeclaration as tSNamespaceExportDeclaration }; export function tsTypeAnnotation(typeAnnotation: t.TSType): t.TSTypeAnnotation { - return validateNode({ + const node: t.TSTypeAnnotation = { type: "TSTypeAnnotation", typeAnnotation, - }); + }; + const defs = NODE_FIELDS.TSTypeAnnotation; + validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1); + return node; } export { tsTypeAnnotation as tSTypeAnnotation }; export function tsTypeParameterInstantiation( params: Array, ): t.TSTypeParameterInstantiation { - return validateNode({ + const node: t.TSTypeParameterInstantiation = { type: "TSTypeParameterInstantiation", params, - }); + }; + const defs = NODE_FIELDS.TSTypeParameterInstantiation; + validate(defs.params, node, "params", params, 1); + return node; } export { tsTypeParameterInstantiation as tSTypeParameterInstantiation }; export function tsTypeParameterDeclaration( params: Array, ): t.TSTypeParameterDeclaration { - return validateNode({ + const node: t.TSTypeParameterDeclaration = { type: "TSTypeParameterDeclaration", params, - }); + }; + const defs = NODE_FIELDS.TSTypeParameterDeclaration; + validate(defs.params, node, "params", params, 1); + return node; } export { tsTypeParameterDeclaration as tSTypeParameterDeclaration }; export function tsTypeParameter( @@ -2517,12 +3379,17 @@ export function tsTypeParameter( _default: t.TSType | null | undefined = null, name: string, ): t.TSTypeParameter { - return validateNode({ + const node: t.TSTypeParameter = { type: "TSTypeParameter", constraint, default: _default, name, - }); + }; + const defs = NODE_FIELDS.TSTypeParameter; + validate(defs.constraint, node, "constraint", constraint, 1); + validate(defs.default, node, "default", _default, 1); + validate(defs.name, node, "name", name); + return node; } export { tsTypeParameter as tSTypeParameter }; /** @deprecated */ diff --git a/packages/babel-types/src/builders/validateNode.ts b/packages/babel-types/src/builders/validateNode.ts index 2409be690598..2c6126314959 100644 --- a/packages/babel-types/src/builders/validateNode.ts +++ b/packages/babel-types/src/builders/validateNode.ts @@ -1,12 +1,17 @@ -import validate from "../validators/validate.ts"; +import { validateInternal } from "../validators/validate.ts"; import type * as t from "../index.ts"; -import { BUILDER_KEYS } from "../index.ts"; +import { BUILDER_KEYS, NODE_FIELDS } from "../index.ts"; export default function validateNode(node: N) { + if (node == null || typeof node !== "object") return; + const fields = NODE_FIELDS[node.type]; + if (!fields) return; + // todo: because keys not in BUILDER_KEYS are not validated - this actually allows invalid nodes in some cases const keys = BUILDER_KEYS[node.type] as (keyof N & string)[]; for (const key of keys) { - validate(node, key, node[key]); + const field = fields[key]; + if (field != null) validateInternal(field, node, key, node[key]); } return node; } diff --git a/packages/babel-types/src/validators/validate.ts b/packages/babel-types/src/validators/validate.ts index 5c998b77e125..ff9cc9af4de4 100644 --- a/packages/babel-types/src/validators/validate.ts +++ b/packages/babel-types/src/validators/validate.ts @@ -8,7 +8,7 @@ import type * as t from "../index.ts"; export default function validate( node: t.Node | undefined | null, key: string, - val: any, + val: unknown, ): void { if (!node) return; @@ -20,10 +20,29 @@ export default function validate( validateChild(node, key, val); } +export function validateInternal( + field: FieldOptions, + node: t.Node | undefined | null, + key: string, + val: unknown, + maybeNode?: 1, +): void { + if (!field?.validate) return; + if (field.optional && val == null) return; + + field.validate(node, key, val); + + if (maybeNode) { + const type = (val as t.Node).type; + if (type == null) return; + NODE_PARENT_VALIDATIONS[type]?.(node, key, val); + } +} + export function validateField( node: t.Node | undefined | null, key: string, - val: any, + val: unknown, field: FieldOptions | undefined | null, ): void { if (!field?.validate) return; @@ -35,10 +54,9 @@ export function validateField( export function validateChild( node: t.Node | undefined | null, key: string, - val?: t.Node | undefined | null, + val?: unknown, ) { - if (val == null) return; - const validate = NODE_PARENT_VALIDATIONS[val.type]; - if (!validate) return; - validate(node, key, val); + const type = (val as t.Node)?.type; + if (type == null) return; + NODE_PARENT_VALIDATIONS[type]?.(node, key, val); }