From 1454f8375473c2a63983d9fafb524af5f87a7117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 26 Feb 2020 01:54:19 +0100 Subject: [PATCH] Enable the BABEL_TYPES_8_BREAKING flag in @babel/types --- packages/babel-types/src/definitions/core.js | 92 ++++--------------- .../babel-types/src/definitions/es2015.js | 16 ---- .../src/definitions/experimental.js | 18 ++-- packages/babel-types/src/definitions/utils.js | 2 +- 4 files changed, 25 insertions(+), 103 deletions(-) diff --git a/packages/babel-types/src/definitions/core.js b/packages/babel-types/src/definitions/core.js index 9461e36d3cc2..12833e76354b 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"), @@ -170,14 +163,6 @@ defineType("CallExpression", { ), ), }, - ...(!process.env.BABEL_TYPES_8_BREAKING - ? { - optional: { - validate: assertOneOf(true, false), - optional: true, - }, - } - : {}), typeArguments: { validate: assertNodeType("TypeParameterInstantiation"), optional: true, @@ -283,15 +268,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"), @@ -393,8 +376,6 @@ defineType("FunctionDeclaration", { "Declaration", ], validate: (function() { - if (!process.env.BABEL_TYPES_8_BREAKING) return () => {}; - const identifier = assertNodeType("Identifier"); return function(parent, key, node) { @@ -450,8 +431,6 @@ defineType("Identifier", { ...patternLikeCommon, name: { validate: chain(assertValueType("string"), function(node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - if (!esutils.keyword.isIdentifierNameES6(val)) { throw new TypeError(`"${val}" is not a valid identifier name`); } @@ -463,8 +442,6 @@ defineType("Identifier", { }, }, validate(parent, key, node) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - const match = /\.(\w+)$/.exec(key); if (!match) return; @@ -575,8 +552,6 @@ defineType("RegExpLiteral", { }, flags: { validate: chain(assertValueType("string"), 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`); @@ -626,14 +601,6 @@ defineType("MemberExpression", { computed: { default: false, }, - ...(!process.env.BABEL_TYPES_8_BREAKING - ? { - optional: { - validate: assertOneOf(true, false), - optional: true, - }, - } - : {}), }, }); @@ -696,7 +663,6 @@ defineType("ObjectMethod", { ...functionTypeAnnotationCommon, kind: { validate: assertOneOf("method", "get", "set"), - ...(!process.env.BABEL_TYPES_8_BREAKING ? { default: "method" } : {}), }, computed: { default: false, @@ -747,13 +713,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, @@ -782,8 +742,6 @@ defineType("ObjectProperty", { validate: chain( assertValueType("boolean"), 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", @@ -791,8 +749,6 @@ defineType("ObjectProperty", { } }, 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", @@ -817,8 +773,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); }; @@ -833,14 +787,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."); @@ -937,8 +887,6 @@ defineType("TryStatement", { fields: { block: { validate: chain(assertNodeType("BlockStatement"), 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. @@ -984,9 +932,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), @@ -1016,8 +962,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( @@ -1032,10 +976,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 853af1b12e65..cae45dc8dc40 100644 --- a/packages/babel-types/src/definitions/es2015.js +++ b/packages/babel-types/src/definitions/es2015.js @@ -190,8 +190,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); } @@ -248,8 +246,6 @@ defineType("ExportNamedDeclaration", { validate: chain( assertNodeType("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. @@ -260,8 +256,6 @@ defineType("ExportNamedDeclaration", { } }, 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. @@ -284,8 +278,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); @@ -329,10 +321,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", @@ -433,8 +421,6 @@ defineType("MetaProperty", { fields: { meta: { validate: chain(assertNodeType("Identifier"), function(node, key, val) { - if (!process.env.BABEL_TYPES_8_BREAKING) return; - let property; switch (val.name) { case "function": @@ -664,8 +650,6 @@ defineType("YieldExpression", { fields: { delegate: { validate: chain(assertValueType("boolean"), 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 a77350105a5b..4b60b370ba5e 100644 --- a/packages/babel-types/src/definitions/experimental.js +++ b/packages/babel-types/src/definitions/experimental.js @@ -26,16 +26,14 @@ defineType("AwaitExpression", { defineType("BindExpression", { visitor: ["object", "callee"], aliases: ["Expression"], - fields: !process.env.BABEL_TYPES_8_BREAKING - ? {} - : { - object: { - validate: assertNodeType("Expression"), - }, - callee: { - validate: assertNodeType("Expression"), - }, - }, + fields: { + object: { + validate: assertNodeType("Expression"), + }, + callee: { + validate: assertNodeType("Expression"), + }, + }, }); defineType("ClassProperty", { diff --git a/packages/babel-types/src/definitions/utils.js b/packages/babel-types/src/definitions/utils.js index cc3a347cca4a..2c17feda3052 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;