From b0abc2dfe2dc378e6b7a22e5e63e7816a94b61fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 25 Jun 2020 23:56:01 +0200 Subject: [PATCH] Enable the `BABEL_TYPES_8_BREAKING` flag in `@babel/types` (#11172) --- packages/babel-types/src/definitions/core.js | 99 +++---------------- .../babel-types/src/definitions/es2015.js | 28 +----- .../src/definitions/experimental.js | 37 ++----- packages/babel-types/src/definitions/utils.js | 2 +- 4 files changed, 31 insertions(+), 135 deletions(-) diff --git a/packages/babel-types/src/definitions/core.js b/packages/babel-types/src/definitions/core.js index 38948a63dbc5..f612c8cfaaf6 100644 --- a/packages/babel-types/src/definitions/core.js +++ b/packages/babel-types/src/definitions/core.js @@ -30,7 +30,6 @@ defineType("ArrayExpression", { assertNodeOrValueType("null", "Expression", "SpreadElement"), ), ), - default: !process.env.BABEL_TYPES_8_BREAKING ? [] : undefined, }, }, visitor: ["elements"], @@ -41,10 +40,6 @@ defineType("AssignmentExpression", { fields: { operator: { validate: (function () { - if (!process.env.BABEL_TYPES_8_BREAKING) { - return assertValueType("string"); - } - const identifier = assertOneOf(...ASSIGNMENT_OPERATORS); const pattern = assertOneOf("="); @@ -55,14 +50,12 @@ defineType("AssignmentExpression", { })(), }, left: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertNodeType("LVal") - : assertNodeType( - "Identifier", - "MemberExpression", - "ArrayPattern", - "ObjectPattern", - ), + validate: assertNodeType( + "Identifier", + "MemberExpression", + "ArrayPattern", + "ObjectPattern", + ), }, right: { validate: assertNodeType("Expression"), @@ -181,14 +174,6 @@ defineType("CallExpression", { ), ), }, - ...(!process.env.BABEL_TYPES_8_BREAKING - ? { - optional: { - validate: assertOneOf(true, false), - optional: true, - }, - } - : {}), typeArguments: { validate: assertNodeType("TypeParameterInstantiation"), optional: true, @@ -303,15 +288,13 @@ defineType("ForInStatement", { ], fields: { left: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertNodeType("VariableDeclaration", "LVal") - : assertNodeType( - "VariableDeclaration", - "Identifier", - "MemberExpression", - "ArrayPattern", - "ObjectPattern", - ), + validate: assertNodeType( + "VariableDeclaration", + "Identifier", + "MemberExpression", + "ArrayPattern", + "ObjectPattern", + ), }, right: { validate: assertNodeType("Expression"), @@ -413,8 +396,6 @@ defineType("FunctionDeclaration", { "Declaration", ], validate: (function () { - if (!process.env.BABEL_TYPES_8_BREAKING) return () => {}; - const identifier = assertNodeType("Identifier"); return function (parent, key, node) { @@ -473,8 +454,6 @@ defineType("Identifier", { assertValueType("string"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (!isValidIdentifier(val, false)) { throw new TypeError(`"${val}" is not a valid identifier name`); } @@ -489,8 +468,6 @@ defineType("Identifier", { }, }, validate(parent, key, node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - const match = /\.(\w+)$/.exec(key); if (!match) return; @@ -605,8 +582,6 @@ defineType("RegExpLiteral", { assertValueType("string"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - const invalid = /[^gimsuy]/.exec(val); if (invalid) { throw new TypeError(`"${invalid[0]}" is not a valid RegExp flag`); @@ -662,14 +637,6 @@ defineType("MemberExpression", { computed: { default: false, }, - ...(!process.env.BABEL_TYPES_8_BREAKING - ? { - optional: { - validate: assertOneOf(true, false), - optional: true, - }, - } - : {}), }, }); @@ -732,7 +699,6 @@ defineType("ObjectMethod", { ...functionTypeAnnotationCommon, kind: { validate: assertOneOf("method", "get", "set"), - ...(!process.env.BABEL_TYPES_8_BREAKING ? { default: "method" } : {}), }, computed: { default: false, @@ -791,13 +757,7 @@ defineType("ObjectMethod", { }); defineType("ObjectProperty", { - builder: [ - "key", - "value", - "computed", - "shorthand", - ...(!process.env.BABEL_TYPES_8_BREAKING ? ["decorators"] : []), - ], + builder: ["key", "value", "computed", "shorthand"], fields: { computed: { default: false, @@ -835,8 +795,6 @@ defineType("ObjectProperty", { assertValueType("boolean"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (val && node.computed) { throw new TypeError( "Property shorthand of ObjectProperty cannot be true if computed is true", @@ -845,15 +803,6 @@ defineType("ObjectProperty", { }, { type: "boolean" }, ), - function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - - if (val && !is("Identifier", node.key)) { - throw new TypeError( - "Property shorthand of ObjectProperty cannot be true if key is not an Identifier", - ); - } - }, ), default: false, }, @@ -872,8 +821,6 @@ defineType("ObjectProperty", { const expression = assertNodeType("Expression"); return function (parent, key, node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - const validator = is("ObjectPattern", parent) ? pattern : expression; validator(node, "value", node.value); }; @@ -888,14 +835,10 @@ defineType("RestElement", { fields: { ...patternLikeCommon, argument: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertNodeType("LVal") - : assertNodeType("Identifier", "Pattern", "MemberExpression"), + validate: assertNodeType("Identifier", "Pattern", "MemberExpression"), }, }, validate(parent, key) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - const match = /(\w+)\[(\d+)\]/.exec(key); if (!match) throw new Error("Internal Babel error: malformed key."); @@ -995,8 +938,6 @@ defineType("TryStatement", { assertNodeType("BlockStatement"), Object.assign( function (node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - // This validator isn't put at the top level because we can run it // even if this node doesn't have a parent. @@ -1047,9 +988,7 @@ defineType("UpdateExpression", { default: false, }, argument: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertNodeType("Expression") - : assertNodeType("Identifier", "MemberExpression"), + validate: assertNodeType("Identifier", "MemberExpression"), }, operator: { validate: assertOneOf(...UPDATE_OPERATORS), @@ -1079,8 +1018,6 @@ defineType("VariableDeclaration", { }, }, validate(parent, key, node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (!is("ForXStatement", parent, { left: node })) return; if (node.declarations.length !== 1) { throw new TypeError( @@ -1095,10 +1032,6 @@ defineType("VariableDeclarator", { fields: { id: { validate: (function () { - if (!process.env.BABEL_TYPES_8_BREAKING) { - return assertNodeType("LVal"); - } - const normal = assertNodeType( "Identifier", "ArrayPattern", diff --git a/packages/babel-types/src/definitions/es2015.js b/packages/babel-types/src/definitions/es2015.js index cebf5966e915..7223a7085d14 100644 --- a/packages/babel-types/src/definitions/es2015.js +++ b/packages/babel-types/src/definitions/es2015.js @@ -239,8 +239,6 @@ defineType("ClassDeclaration", { const identifier = assertNodeType("Identifier"); return function (parent, key, node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (!is("ExportDefaultDeclaration", parent)) { identifier(node, "id", node.id); } @@ -298,8 +296,6 @@ defineType("ExportNamedDeclaration", { assertNodeType("Declaration"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - // This validator isn't put at the top level because we can run it // even if this node doesn't have a parent. @@ -308,19 +304,13 @@ defineType("ExportNamedDeclaration", { "Only declaration or specifiers is allowed on ExportNamedDeclaration", ); } + + if (val && node.source) { + throw new TypeError("Cannot export a declaration from a source"); + } }, { oneOfNodeTypes: ["Declaration"] }, ), - function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - - // This validator isn't put at the top level because we can run it - // even if this node doesn't have a parent. - - if (val && node.source) { - throw new TypeError("Cannot export a declaration from a source"); - } - }, ), }, specifiers: { @@ -336,8 +326,6 @@ defineType("ExportNamedDeclaration", { ); const sourceless = assertNodeType("ExportSpecifier"); - if (!process.env.BABEL_TYPES_8_BREAKING) return sourced; - return function (node, key, val) { const validator = node.source ? sourced : sourceless; validator(node, key, val); @@ -381,10 +369,6 @@ defineType("ForOfStatement", { fields: { left: { validate: (function () { - if (!process.env.BABEL_TYPES_8_BREAKING) { - return assertNodeType("VariableDeclaration", "LVal"); - } - const declaration = assertNodeType("VariableDeclaration"); const lval = assertNodeType( "Identifier", @@ -489,8 +473,6 @@ defineType("MetaProperty", { assertNodeType("Identifier"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - let property; switch (val.name) { case "function": @@ -727,8 +709,6 @@ defineType("YieldExpression", { assertValueType("boolean"), Object.assign( function (node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (val && !node.argument) { throw new TypeError( "Property delegate of YieldExpression cannot be true if there is no argument", diff --git a/packages/babel-types/src/definitions/experimental.js b/packages/babel-types/src/definitions/experimental.js index 50c4a6a0666a..3d777fc85bd9 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -29,27 +29,14 @@ defineType("AwaitExpression", { defineType("BindExpression", { visitor: ["object", "callee"], aliases: ["Expression"], - fields: !process.env.BABEL_TYPES_8_BREAKING - ? { - object: { - validate: Object.assign(() => {}, { - oneOfNodeTypes: ["Expression"], - }), - }, - callee: { - validate: Object.assign(() => {}, { - oneOfNodeTypes: ["Expression"], - }), - }, - } - : { - object: { - validate: assertNodeType("Expression"), - }, - callee: { - validate: assertNodeType("Expression"), - }, - }, + fields: { + object: { + validate: assertNodeType("Expression"), + }, + callee: { + validate: assertNodeType("Expression"), + }, + }, }); defineType("ClassProperty", { @@ -121,9 +108,7 @@ defineType("OptionalMemberExpression", { default: false, }, optional: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertValueType("boolean") - : chain(assertValueType("boolean"), assertOptionalChainStart()), + validate: chain(assertValueType("boolean"), assertOptionalChainStart()), }, }, }); @@ -169,9 +154,7 @@ defineType("OptionalCallExpression", { ), }, optional: { - validate: !process.env.BABEL_TYPES_8_BREAKING - ? assertValueType("boolean") - : chain(assertValueType("boolean"), assertOptionalChainStart()), + validate: chain(assertValueType("boolean"), assertOptionalChainStart()), }, typeArguments: { validate: assertNodeType("TypeParameterInstantiation"), diff --git a/packages/babel-types/src/definitions/utils.js b/packages/babel-types/src/definitions/utils.js index 9404fdbab6c9..a9280b991189 100644 --- a/packages/babel-types/src/definitions/utils.js +++ b/packages/babel-types/src/definitions/utils.js @@ -73,7 +73,7 @@ export function assertEach(callback: Validator): Validator { const subkey = `${key}[${i}]`; const v = val[i]; callback(node, subkey, v); - if (process.env.BABEL_TYPES_8_BREAKING) validateChild(node, subkey, v); + validateChild(node, subkey, v); } } validator.each = callback;