throw a TypeError if identifier validation fails (#10621)
* throw a TypeError if Identifier validation fails * Relax identifier validation, reserved words are ok
This commit is contained in:
parent
d08702c9d2
commit
c7d8b8a377
@ -1,5 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import isValidIdentifier from "../validators/isValidIdentifier";
|
import isIdentifierName from "../validators/isIdentifierName";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BINARY_OPERATORS,
|
BINARY_OPERATORS,
|
||||||
@ -406,8 +406,8 @@ defineType("Identifier", {
|
|||||||
...patternLikeCommon,
|
...patternLikeCommon,
|
||||||
name: {
|
name: {
|
||||||
validate: chain(function(node, key, val) {
|
validate: chain(function(node, key, val) {
|
||||||
if (!isValidIdentifier(val)) {
|
if (!isIdentifierName(val)) {
|
||||||
// throw new TypeError(`"${val}" is not a valid identifer name`);
|
throw new TypeError(`"${val}" is not a valid identifer name`);
|
||||||
}
|
}
|
||||||
}, assertValueType("string")),
|
}, assertValueType("string")),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -109,6 +109,7 @@ export {
|
|||||||
default as isValidES3Identifier,
|
default as isValidES3Identifier,
|
||||||
} from "./validators/isValidES3Identifier";
|
} from "./validators/isValidES3Identifier";
|
||||||
export { default as isValidIdentifier } from "./validators/isValidIdentifier";
|
export { default as isValidIdentifier } from "./validators/isValidIdentifier";
|
||||||
|
export { default as isIdentifierName } from "./validators/isIdentifierName";
|
||||||
export { default as isVar } from "./validators/isVar";
|
export { default as isVar } from "./validators/isVar";
|
||||||
export { default as matchesPattern } from "./validators/matchesPattern";
|
export { default as matchesPattern } from "./validators/matchesPattern";
|
||||||
export { default as validate } from "./validators/validate";
|
export { default as validate } from "./validators/validate";
|
||||||
|
|||||||
9
packages/babel-types/src/validators/isIdentifierName.js
Normal file
9
packages/babel-types/src/validators/isIdentifierName.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// @flow
|
||||||
|
import esutils from "esutils";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the input `name` is a valid identifier name.
|
||||||
|
*/
|
||||||
|
export default function isIdentifierName(name: string): boolean {
|
||||||
|
return esutils.keyword.isIdentifierNameES6(name);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user