Skip to content

Commit

Permalink
throw a TypeError if identifier validation fails (babel#10621)
Browse files Browse the repository at this point in the history
* throw a TypeError if Identifier validation fails

* Relax identifier validation, reserved words are ok
  • Loading branch information
dentrado authored and nicolo-ribaudo committed Nov 2, 2019
1 parent d08702c commit c7d8b8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/babel-types/src/definitions/core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import isValidIdentifier from "../validators/isValidIdentifier";
import isIdentifierName from "../validators/isIdentifierName";

import {
BINARY_OPERATORS,
Expand Down Expand Up @@ -406,8 +406,8 @@ defineType("Identifier", {
...patternLikeCommon,
name: {
validate: chain(function(node, key, val) {
if (!isValidIdentifier(val)) {
// throw new TypeError(`"${val}" is not a valid identifer name`);
if (!isIdentifierName(val)) {
throw new TypeError(`"${val}" is not a valid identifer name`);
}
}, assertValueType("string")),
},
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export {
default as isValidES3Identifier,
} from "./validators/isValidES3Identifier";
export { default as isValidIdentifier } from "./validators/isValidIdentifier";
export { default as isIdentifierName } from "./validators/isIdentifierName";
export { default as isVar } from "./validators/isVar";
export { default as matchesPattern } from "./validators/matchesPattern";
export { default as validate } from "./validators/validate";
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-types/src/validators/isIdentifierName.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c7d8b8a

Please sign in to comment.