From 53e3f0dbdc4f1543d36db6d15c82800ee9def0ff Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Mon, 5 Jun 2017 12:38:30 -0400 Subject: [PATCH] babel-types: avoid recreating validator closures (#5821) --- packages/babel-types/src/definitions/core.js | 41 +++++++++++++------ .../babel-types/src/definitions/es2015.js | 13 ++++-- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/packages/babel-types/src/definitions/core.js b/packages/babel-types/src/definitions/core.js index d86cc05c20..02572c87b5 100644 --- a/packages/babel-types/src/definitions/core.js +++ b/packages/babel-types/src/definitions/core.js @@ -308,7 +308,7 @@ defineType("Identifier", { name: { validate(node, key, val) { if (!t.isValidIdentifier(val)) { - // todo + // throw new TypeError(`"${val}" is not a valid identifer name`); } }, }, @@ -424,10 +424,15 @@ defineType("MemberExpression", { validate: assertNodeType("Expression"), }, property: { - validate(node, key, val) { - const expectedType = node.computed ? "Expression" : "Identifier"; - assertNodeType(expectedType)(node, key, val); - }, + validate: (function () { + const normal = assertNodeType("Identifier"); + const computed = assertNodeType("Expression"); + + return function (node, key, val) { + const validator = node.computed ? computed : normal; + validator(node, key, val); + }; + }()), }, computed: { default: false, @@ -488,10 +493,15 @@ defineType("ObjectMethod", { default: false, }, key: { - validate(node, key, val) { - const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; - assertNodeType(...expectedTypes)(node, key, val); - }, + validate: (function () { + const normal = assertNodeType("Expression"); + const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral"); + + return function (node, key, val) { + const validator = node.computed ? computed : normal; + validator(node, key, val); + }; + }()), }, decorators: { validate: chain(assertValueType("array"), assertEach(assertNodeType("Decorator"))), @@ -520,10 +530,15 @@ defineType("ObjectProperty", { default: false, }, key: { - validate(node, key, val) { - const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; - assertNodeType(...expectedTypes)(node, key, val); - }, + validate: (function () { + const normal = assertNodeType("Identifier", "StringLiteral", "NumericLiteral"); + const computed = assertNodeType("Expression"); + + return function (node, key, val) { + const validator = node.computed ? computed : normal; + validator(node, key, val); + }; + }()), }, value: { validate: assertNodeType("Expression", "Pattern", "RestElement"), diff --git a/packages/babel-types/src/definitions/es2015.js b/packages/babel-types/src/definitions/es2015.js index cf7ef3e45b..db7d5fc0ce 100644 --- a/packages/babel-types/src/definitions/es2015.js +++ b/packages/babel-types/src/definitions/es2015.js @@ -271,10 +271,15 @@ defineType("ClassMethod", { validate: assertValueType("boolean"), }, key: { - validate(node, key, val) { - const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; - assertNodeType(...expectedTypes)(node, key, val); - }, + validate: (function () { + const normal = assertNodeType("Expression"); + const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral"); + + return function (node, key, val) { + const validator = node.computed ? computed : normal; + validator(node, key, val); + }; + }()), }, params: { validate: chain(assertValueType("array"), assertEach(assertNodeType("LVal"))),