Skip to content

Commit c7d8b8a

Browse files
dentradonicolo-ribaudo
authored andcommitted
throw a TypeError if identifier validation fails (#10621)
* throw a TypeError if Identifier validation fails * Relax identifier validation, reserved words are ok
1 parent d08702c commit c7d8b8a

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

packages/babel-types/src/definitions/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import isValidIdentifier from "../validators/isValidIdentifier";
2+
import isIdentifierName from "../validators/isIdentifierName";
33

44
import {
55
BINARY_OPERATORS,
@@ -406,8 +406,8 @@ defineType("Identifier", {
406406
...patternLikeCommon,
407407
name: {
408408
validate: chain(function(node, key, val) {
409-
if (!isValidIdentifier(val)) {
410-
// throw new TypeError(`"${val}" is not a valid identifer name`);
409+
if (!isIdentifierName(val)) {
410+
throw new TypeError(`"${val}" is not a valid identifer name`);
411411
}
412412
}, assertValueType("string")),
413413
},

packages/babel-types/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export {
109109
default as isValidES3Identifier,
110110
} from "./validators/isValidES3Identifier";
111111
export { default as isValidIdentifier } from "./validators/isValidIdentifier";
112+
export { default as isIdentifierName } from "./validators/isIdentifierName";
112113
export { default as isVar } from "./validators/isVar";
113114
export { default as matchesPattern } from "./validators/matchesPattern";
114115
export { default as validate } from "./validators/validate";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @flow
2+
import esutils from "esutils";
3+
4+
/**
5+
* Check if the input `name` is a valid identifier name.
6+
*/
7+
export default function isIdentifierName(name: string): boolean {
8+
return esutils.keyword.isIdentifierNameES6(name);
9+
}

0 commit comments

Comments
 (0)