Skip to content

Commit

Permalink
fix: checks validity of an identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
anu-007 committed Mar 20, 2018
1 parent b6a30ad commit 05accf8
Show file tree
Hide file tree
Showing 5 changed files with 8,555 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/utils/ast-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const hashtable = require("./hashtable");
const generateIdentifierRegex = require("./generate-identifier-regex");

function safeTraverse(obj, paths) {
let val = obj;
Expand Down Expand Up @@ -149,8 +150,8 @@ function createIdentifierOrLiteral(j, val) {
return createExternalRegExp(j, val);
} else {
// Use identifier instead
// TODO: Check if literalVal is a valid Identifier!
return j.identifier(literalVal);
if (!literalVal.match(generateIdentifierRegex()))
return j.identifier(literalVal);
}
}
return j.literal(literalVal);
Expand Down
55 changes: 55 additions & 0 deletions lib/utils/generate-identifier-regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const regenerate = require("regenerate");
const template = require("lodash.template");

// Which Unicode version should be used?
const version = "10.0.0";

// Set up a shorthand function to import Unicode data.
const get = function(what) {
return require(`unicode-${ version }/${ what }/code-points.js`);
};

// Get the Unicode properties needed to construct the ES6 regex.
const ID_Start = get("Binary_Property/ID_Start");
const ID_Continue = get("Binary_Property/ID_Continue");
const Other_ID_Start = get("Binary_Property/Other_ID_Start");

const compileRegex = template("/^(?!(?:<%= reservedWords %>)$)" +
"(?:<%= identifierStart %>)(?:<%= identifierPart %>)*$/");

module.exports = function generateES6Regex() {
const identifierStart = regenerate(ID_Start)
.add(
"$",
"_"
);
const identifierPart = regenerate(ID_Continue)
.add(Other_ID_Start)
.add(
"$",
"_",
"\u200C",
"\u200D"
);

const reservedWords = [
// https://mathiasbynens.be/notes/reserved-keywords#ecmascript-6
"do", "if", "in", "for", "let", "new", "try", "var", "case", "else",
"enum", "eval", "null", "this", "true", "void", "with", "await", "break",
"catch", "class", "const", "false", "super", "throw", "while", "yield",
"delete", "export", "import", "public", "return", "static", "switch",
"typeof", "default", "extends", "finally", "package", "private",
"continue", "debugger", "function", "arguments", "interface", "protected",
"implements", "instanceof",
// These aren’t strictly reserved words, but they kind of behave as if
// they were.
//'NaN', 'Infinity', 'undefined'
];

const regex = compileRegex({
reservedWords: reservedWords.join("|"),
identifierStart: identifierStart.toString(),
identifierPart: identifierPart.toString()
});
return regex;
};
11 changes: 7 additions & 4 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@
"listr": "^0.13.0",
"loader-utils": "^1.1.0",
"lodash": "^4.17.5",
"lodash.template": "^4.4.0",
"log-symbols": "^2.2.0",
"mkdirp": "^0.5.1",
"p-each-series": "^1.0.0",
"p-lazy": "^1.0.0",
"prettier": "^1.5.3",
"regenerate": "^1.3.3",
"resolve-cwd": "^2.0.0",
"supports-color": "^5.3.0",
"v8-compile-cache": "^1.1.2",
Expand Down Expand Up @@ -129,6 +131,7 @@
"nyc": "^11.4.1",
"prettier-eslint-cli": "^4.7.1",
"schema-utils": "^0.4.5",
"unicode-10.0.0": "^0.7.5",
"webpack": "^4.1.1",
"webpack-dev-server": "^3.0.0"
}
Expand Down
Loading

0 comments on commit 05accf8

Please sign in to comment.