Skip to content

Commit

Permalink
Add @babel/helper-validator-identifier (#11289)
Browse files Browse the repository at this point in the history
* refactor: replace esutils.keywords.isIdentifierNameES6 by helper-validator-identifier

* refactor: replace esutils isReservedWordES6 by isKeyword || isReservedWord

* address review comments

* chore: specify both “main” and “exports”

* build helper-validator-identifier before babel-types
  • Loading branch information
JLHwung committed Mar 20, 2020
1 parent 693a5df commit e39b508
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 169 deletions.
12 changes: 11 additions & 1 deletion Gulpfile.js
Expand Up @@ -226,9 +226,19 @@ gulp.task("build-rollup", () => buildRollup(libBundles));
gulp.task("build-babel-standalone", () => buildRollup(standaloneBundle));

gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles));
gulp.task("build-babel-types", () =>
gulp.task("build-babel-types-deps", () =>
buildBabel(
/* exclude */ libBundles,
"packages/babel-helper-validator-identifier/src/**/*.js"
)
);
gulp.task("build-babel-types-self", () =>
buildBabel(/* exclude */ libBundles, "packages/babel-types/src/**/*.js")
);
gulp.task(
"build-babel-types",
gulp.series("build-babel-types-deps", "build-babel-types-self")
);
gulp.task("build", gulp.parallel("build-rollup", "build-babel"));

gulp.task("default", gulp.series("build"));
Expand Down
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
```
17 changes: 17 additions & 0 deletions packages/babel-helper-validator-identifier/package.json
@@ -0,0 +1,17 @@
{
"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",
"exports": "./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 function isReservedWord(word: string, inModule: boolean): boolean {
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

0 comments on commit e39b508

Please sign in to comment.