-
Notifications
You must be signed in to change notification settings - Fork 780
/
Copy pathgenerate-identifier-regex.js
43 lines (37 loc) · 1.39 KB
/
generate-identifier-regex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Based on https://gist.github.com/mathiasbynens/6334847 by @mathias
var regenerate = require('regenerate');
// Which Unicode version should be used?
var pkg = require('../package.json');
var dependencies = Object.keys(pkg.devDependencies);
var unicodeDep = dependencies.find((name) => /^unicode-\d/.test(name));
var version = unicodeDep.match(/[^\d]+(.+)$/)[1];
// Shorthand function
var get = function(what) {
return require(unicodeDep + '/' + what + '/code-points.js');
};
var generateRegex = function() { // ES 6
// https://mathiasbynens.be/notes/javascript-identifiers-es6
var identifierStart = regenerate(get('Binary_Property/ID_Start'))
.add('$', '_')
.removeRange(0x0, 0x7F); // remove ASCII symbols (Esprima-specific)
var identifierPart = regenerate(get('Binary_Property/ID_Continue'))
.add(get('Binary_Property/Other_ID_Start'))
.add('\u200C', '\u200D')
.add('$', '_')
.removeRange(0x0, 0x7F); // remove ASCII symbols (Esprima-specific)
return {
'NonAsciiIdentifierStart': '/' + identifierStart + '/',
'NonAsciiIdentifierPart': '/' + identifierPart + '/'
};
};
var result = generateRegex();
console.log(
'// Unicode v%s NonAsciiIdentifierStart:\n%s\n',
version,
result.NonAsciiIdentifierStart
);
console.log(
'// Unicode v%s NonAsciiIdentifierPart:\n%s',
version,
result.NonAsciiIdentifierPart
);