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 28, 2018
1 parent 0de8a4e commit 39261d0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 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("./generateIdentifierRegex");

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/generateIdentifierRegex.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 UNICODE_VERSION = "10.0.0";

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

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

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

module.exports = function generateES6Regex() {
const identifierStart = regenerate(idStart)
.add(
"$",
"_"
);
const identifierPart = regenerate(idContinue)
.add(otherIdStart)
.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;
};
6 changes: 6 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"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 @@ -131,6 +132,7 @@
"nyc": "^11.6.0",
"prettier-eslint-cli": "^4.7.1",
"schema-utils": "^0.4.5",
"unicode-10.0.0": "^0.7.5",
"webpack": "^4.2.0",
"webpack-dev-server": "^3.0.0"
},
Expand Down

0 comments on commit 39261d0

Please sign in to comment.