Skip to content

Commit

Permalink
Enable the BABEL_TYPES_8_BREAKING flag in @babel/types (#11172)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 27, 2020
1 parent 69b51e5 commit 3d8aa02
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 141 deletions.
129 changes: 19 additions & 110 deletions packages/babel-types/src/definitions/core.js
Expand Up @@ -33,7 +33,6 @@ defineType("ArrayExpression", {
assertNodeOrValueType("null", "Expression", "SpreadElement"),
),
),
default: !process.env.BABEL_TYPES_8_BREAKING ? [] : undefined,
},
},
visitor: ["elements"],
Expand All @@ -44,10 +43,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("=");

Expand All @@ -58,14 +53,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"),
Expand Down Expand Up @@ -184,14 +177,6 @@ defineType("CallExpression", {
),
),
},
...(!process.env.BABEL_TYPES_8_BREAKING
? {
optional: {
validate: assertOneOf(true, false),
optional: true,
},
}
: {}),
typeArguments: {
validate: assertNodeType("TypeParameterInstantiation"),
optional: true,
Expand Down Expand Up @@ -283,11 +268,7 @@ defineType("File", {
validate: assertNodeType("Program"),
},
comments: {
validate: !process.env.BABEL_TYPES_8_BREAKING
? Object.assign(() => {}, {
each: { oneOfNodeTypes: ["CommentBlock", "CommentLine"] },
})
: assertEach(assertNodeType("CommentBlock", "CommentLine")),
validate: assertEach(assertNodeType("CommentBlock", "CommentLine")),
optional: true,
},
tokens: {
Expand All @@ -310,15 +291,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"),
Expand Down Expand Up @@ -420,8 +399,6 @@ defineType("FunctionDeclaration", {
"Declaration",
],
validate: (function () {
if (!process.env.BABEL_TYPES_8_BREAKING) return () => {};

const identifier = assertNodeType("Identifier");

return function (parent, key, node) {
Expand Down Expand Up @@ -480,8 +457,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`);
}
Expand All @@ -496,8 +471,6 @@ defineType("Identifier", {
},
},
validate(parent, key, node) {
if (!process.env.BABEL_TYPES_8_BREAKING) return;

const match = /\.(\w+)$/.exec(key);
if (!match) return;

Expand Down Expand Up @@ -612,8 +585,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`);
Expand Down Expand Up @@ -669,14 +640,6 @@ defineType("MemberExpression", {
computed: {
default: false,
},
...(!process.env.BABEL_TYPES_8_BREAKING
? {
optional: {
validate: assertOneOf(true, false),
optional: true,
},
}
: {}),
},
});

Expand Down Expand Up @@ -739,7 +702,6 @@ defineType("ObjectMethod", {
...functionTypeAnnotationCommon,
kind: {
validate: assertOneOf("method", "get", "set"),
...(!process.env.BABEL_TYPES_8_BREAKING ? { default: "method" } : {}),
},
computed: {
default: false,
Expand Down Expand Up @@ -798,13 +760,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,
Expand Down Expand Up @@ -842,8 +798,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",
Expand All @@ -852,15 +806,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,
},
Expand All @@ -879,8 +824,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);
};
Expand All @@ -895,14 +838,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.");

Expand Down Expand Up @@ -1002,8 +941,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.

Expand Down Expand Up @@ -1054,9 +991,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),
Expand Down Expand Up @@ -1086,8 +1021,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(
Expand All @@ -1102,10 +1035,6 @@ defineType("VariableDeclarator", {
fields: {
id: {
validate: (function () {
if (!process.env.BABEL_TYPES_8_BREAKING) {
return assertNodeType("LVal");
}

const normal = assertNodeType(
"Identifier",
"ArrayPattern",
Expand Down Expand Up @@ -1380,8 +1309,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);
}
Expand Down Expand Up @@ -1439,8 +1366,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.

Expand All @@ -1453,8 +1378,6 @@ defineType("ExportNamedDeclaration", {
{ 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.

Expand All @@ -1477,8 +1400,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);
Expand Down Expand Up @@ -1522,10 +1443,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",
Expand Down Expand Up @@ -1630,8 +1547,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":
Expand Down Expand Up @@ -1868,8 +1783,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",
Expand Down Expand Up @@ -1952,9 +1865,7 @@ defineType("OptionalMemberExpression", {
default: false,
},
optional: {
validate: !process.env.BABEL_TYPES_8_BREAKING
? assertValueType("boolean")
: chain(assertValueType("boolean"), assertOptionalChainStart()),
validate: chain(assertValueType("boolean"), assertOptionalChainStart()),
},
},
});
Expand All @@ -1976,9 +1887,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"),
Expand Down
29 changes: 8 additions & 21 deletions packages/babel-types/src/definitions/experimental.js
Expand Up @@ -16,27 +16,14 @@ defineType("ArgumentPlaceholder", {});
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", {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/src/definitions/utils.js
Expand Up @@ -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;
Expand Down
9 changes: 0 additions & 9 deletions packages/babel-types/test/regressions.js

This file was deleted.

0 comments on commit 3d8aa02

Please sign in to comment.