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

Add @babel/helper-validator-identifier #11289

Merged
merged 5 commits into from Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion babel.config.js
Expand Up @@ -115,7 +115,10 @@ module.exports = function(api) {
].filter(Boolean),
overrides: [
{
test: "packages/babel-parser",
test: [
"packages/babel-parser",
"packages/babel-helper-validator-identifier",
],
plugins: [
"babel-plugin-transform-charcodes",
["@babel/transform-for-of", { assumeArray: true }],
Expand Down
Expand Up @@ -11,7 +11,6 @@
"dependencies": {
"@babel/helper-annotate-as-pure": "^7.8.3",
"@babel/helper-module-imports": "^7.8.3",
"@babel/types": "^7.8.3",
"esutils": "^2.0.0"
"@babel/types": "^7.8.3"
}
}
@@ -1,4 +1,3 @@
import esutils from "esutils";
import * as t from "@babel/types";
import { addNamed, addNamespace, isModule } from "@babel/helper-module-imports";
import annotateAsPure from "@babel/helper-annotate-as-pure";
Expand Down Expand Up @@ -472,7 +471,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
if (t.isJSXIdentifier(node)) {
if (node.name === "this" && t.isReferenced(node, parent)) {
return t.thisExpression();
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
} else if (t.isValidIdentifier(node.name, false)) {
node.type = "Identifier";
} else {
return t.stringLiteral(node.name);
Expand Down Expand Up @@ -521,7 +520,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
node.name = t.stringLiteral(
node.name.namespace.name + ":" + node.name.name.name,
);
} else if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
} else if (t.isValidIdentifier(node.name.name, false)) {
node.name.type = "Identifier";
} else {
node.name = t.stringLiteral(node.name.name);
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-helper-builder-react-jsx/package.json
Expand Up @@ -10,7 +10,6 @@
"main": "lib/index.js",
"dependencies": {
"@babel/helper-annotate-as-pure": "^7.8.3",
"@babel/types": "^7.8.3",
"esutils": "^2.0.0"
"@babel/types": "^7.8.3"
}
}
5 changes: 2 additions & 3 deletions packages/babel-helper-builder-react-jsx/src/index.js
@@ -1,4 +1,3 @@
import esutils from "esutils";
import * as t from "@babel/types";
import annotateAsPure from "@babel/helper-annotate-as-pure";

Expand Down Expand Up @@ -57,7 +56,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
if (t.isJSXIdentifier(node)) {
if (node.name === "this" && t.isReferenced(node, parent)) {
return t.thisExpression();
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
} else if (t.isValidIdentifier(node.name, false)) {
node.type = "Identifier";
} else {
return t.stringLiteral(node.name);
Expand Down Expand Up @@ -104,7 +103,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
node.name = t.stringLiteral(
node.name.namespace.name + ":" + node.name.name.name,
);
} else if (esutils.keyword.isIdentifierNameES6(node.name.name)) {
} else if (t.isValidIdentifier(node.name.name, false)) {
node.name.type = "Identifier";
} else {
node.name = t.stringLiteral(node.name.name);
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-helper-validator-identifier/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-helper-validator-identifier/README.md
@@ -0,0 +1,19 @@
# @babel/helper-validator-identifier

> Validate identifier/keywords name
See our website [@babel/helper-validator-identifier](https://babeljs.io/docs/en/next/babel-helper-validator-identifier.html) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/helper-validator-identifier
```

or using yarn:

```sh
yarn add @babel/helper-validator-identifier --dev
```
16 changes: 16 additions & 0 deletions packages/babel-helper-validator-identifier/package.json
@@ -0,0 +1,16 @@
{
"name": "@babel/helper-validator-identifier",
"version": "7.9.0",
"description": "Validate identifier/keywords name",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-helper-validator-identifier",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"dependencies": {},
"devDependencies": {
"charcodes": "^0.2.0",
"unicode-13.0.0": "^0.8.0"
}
}
102 changes: 102 additions & 0 deletions packages/babel-helper-validator-identifier/src/identifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/babel-helper-validator-identifier/src/index.js
@@ -0,0 +1,12 @@
export {
isIdentifierName,
isIdentifierChar,
isIdentifierStart,
} from "./identifier";
export {
isReservedWord,
isStrictBindOnlyReservedWord,
isStrictBindReservedWord,
isStrictReservedWord,
isKeyword,
} from "./keyword";
98 changes: 98 additions & 0 deletions packages/babel-helper-validator-identifier/src/keyword.js
@@ -0,0 +1,98 @@
// @flow

const reservedWords = {
keyword: [
"break",
"case",
"catch",
"continue",
"debugger",
"default",
"do",
"else",
"finally",
"for",
"function",
"if",
"return",
"switch",
"throw",
"try",
"var",
"const",
"while",
"with",
"new",
"this",
"super",
"class",
"extends",
"export",
"import",
"null",
"true",
"false",
"in",
"instanceof",
"typeof",
"void",
"delete",
],
strict: [
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
],
strictBind: ["eval", "arguments"],
};
const keywords = new Set(reservedWords.keyword);
const reservedWordsStrictSet = new Set(reservedWords.strict);
const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);

/**
* Checks if word is a reserved word in non-strict mode
*/
export const isReservedWord = (word: string, inModule: boolean): boolean => {
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
return (inModule && word === "await") || word === "enum";
};

/**
* Checks if word is a reserved word in non-binding strict mode
*
* Includes non-strict reserved words
*/
export function isStrictReservedWord(word: string, inModule: boolean): boolean {
return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
}

/**
* Checks if word is a reserved word in binding strict mode, but it is allowed as
* a normal identifier.
*/
export function isStrictBindOnlyReservedWord(word: string): boolean {
return reservedWordsStrictBindSet.has(word);
}

/**
* Checks if word is a reserved word in binding strict mode
*
* Includes non-strict reserved words and non-binding strict reserved words
*/
export function isStrictBindReservedWord(
word: string,
inModule: boolean,
): boolean {
return (
isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word)
);
}

export function isKeyword(word: string): boolean {
return keywords.has(word);
}
2 changes: 1 addition & 1 deletion packages/babel-highlight/package.json
Expand Up @@ -11,8 +11,8 @@
"repository": "https://github.com/babel/babel/tree/master/packages/babel-highlight",
"main": "lib/index.js",
"dependencies": {
"@babel/helper-validator-identifier": "^7.9.0",
"chalk": "^2.0.0",
"esutils": "^2.0.2",
"js-tokens": "^4.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-highlight/src/index.js
@@ -1,5 +1,5 @@
import jsTokens, { matchToToken } from "js-tokens";
import esutils from "esutils";
import { isReservedWord, isKeyword } from "@babel/helper-validator-identifier";
import Chalk from "chalk";

/**
Expand Down Expand Up @@ -43,7 +43,7 @@ function getTokenType(match) {
const token = matchToToken(match);

if (token.type === "name") {
if (esutils.keyword.isReservedWordES6(token.value)) {
if (isKeyword(token.value) || isReservedWord(token.value)) {
return "keyword";
}

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/package.json
Expand Up @@ -30,8 +30,8 @@
"devDependencies": {
"@babel/code-frame": "^7.8.3",
"@babel/helper-fixtures": "^7.8.6",
"charcodes": "^0.2.0",
"unicode-13.0.0": "^0.8.0"
"@babel/helper-validator-identifier": "^7.9.0",
"charcodes": "^0.2.0"
},
"bin": {
"parser": "./bin/babel-parser.js"
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/tokenizer/types.js
Expand Up @@ -163,6 +163,8 @@ export const types: { [name: string]: TokenType } = {
}),

// Keywords
// Don't forget to update packages/babel-helper-validator-identifier/src/keyword.js
// when new keywords are added
_break: createKeyword("break"),
_case: createKeyword("case", { beforeExpr }),
_catch: createKeyword("catch"),
Expand Down