babel-types: avoid recreating validator closures (#5821)

This commit is contained in:
Justin Ridgewell 2017-06-05 12:38:30 -04:00 committed by Henry Zhu
parent c4fd05c0c2
commit 53e3f0dbdc
2 changed files with 37 additions and 17 deletions

View File

@ -308,7 +308,7 @@ defineType("Identifier", {
name: { name: {
validate(node, key, val) { validate(node, key, val) {
if (!t.isValidIdentifier(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"), validate: assertNodeType("Expression"),
}, },
property: { property: {
validate(node, key, val) { validate: (function () {
const expectedType = node.computed ? "Expression" : "Identifier"; const normal = assertNodeType("Identifier");
assertNodeType(expectedType)(node, key, val); const computed = assertNodeType("Expression");
},
return function (node, key, val) {
const validator = node.computed ? computed : normal;
validator(node, key, val);
};
}()),
}, },
computed: { computed: {
default: false, default: false,
@ -488,10 +493,15 @@ defineType("ObjectMethod", {
default: false, default: false,
}, },
key: { key: {
validate(node, key, val) { validate: (function () {
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; const normal = assertNodeType("Expression");
assertNodeType(...expectedTypes)(node, key, val); const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral");
},
return function (node, key, val) {
const validator = node.computed ? computed : normal;
validator(node, key, val);
};
}()),
}, },
decorators: { decorators: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("Decorator"))), validate: chain(assertValueType("array"), assertEach(assertNodeType("Decorator"))),
@ -520,10 +530,15 @@ defineType("ObjectProperty", {
default: false, default: false,
}, },
key: { key: {
validate(node, key, val) { validate: (function () {
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; const normal = assertNodeType("Identifier", "StringLiteral", "NumericLiteral");
assertNodeType(...expectedTypes)(node, key, val); const computed = assertNodeType("Expression");
},
return function (node, key, val) {
const validator = node.computed ? computed : normal;
validator(node, key, val);
};
}()),
}, },
value: { value: {
validate: assertNodeType("Expression", "Pattern", "RestElement"), validate: assertNodeType("Expression", "Pattern", "RestElement"),

View File

@ -271,10 +271,15 @@ defineType("ClassMethod", {
validate: assertValueType("boolean"), validate: assertValueType("boolean"),
}, },
key: { key: {
validate(node, key, val) { validate: (function () {
const expectedTypes = node.computed ? ["Expression"] : ["Identifier", "StringLiteral", "NumericLiteral"]; const normal = assertNodeType("Expression");
assertNodeType(...expectedTypes)(node, key, val); const computed = assertNodeType("Identifier", "StringLiteral", "NumericLiteral");
},
return function (node, key, val) {
const validator = node.computed ? computed : normal;
validator(node, key, val);
};
}()),
}, },
params: { params: {
validate: chain(assertValueType("array"), assertEach(assertNodeType("LVal"))), validate: chain(assertValueType("array"), assertEach(assertNodeType("LVal"))),