-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: checks validity of an identifier
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters