-
-
Notifications
You must be signed in to change notification settings - Fork 603
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
5 changed files
with
8,555 additions
and
6 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 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; | ||
}; |
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
Oops, something went wrong.