Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw a TypeError if identifier validation fails #10621

Merged
merged 2 commits into from Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/babel-types/src/definitions/core.js
@@ -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
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
@@ -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);
}