forked from daren.hsu/line_push
update
This commit is contained in:
+122
-64
@@ -1,4 +1,4 @@
|
||||
# regexpu-core [](https://travis-ci.org/mathiasbynens/regexpu-core) [](https://codecov.io/gh/mathiasbynens/regexpu-core)
|
||||
# regexpu-core [](https://github.com/mathiasbynens/regexpu-core/actions?query=workflow%3Arun-checks) [](https://www.npmjs.com/package/regexpu-core)
|
||||
|
||||
_regexpu_ is a source code transpiler that enables the use of ES2015 Unicode regular expressions in JavaScript-of-today (ES5).
|
||||
|
||||
@@ -51,89 +51,147 @@ rewritePattern('foo.bar', 'u');
|
||||
|
||||
The optional `options` argument recognizes the following properties:
|
||||
|
||||
#### `dotAllFlag` (default: `false`)
|
||||
#### Stable regular expression features
|
||||
|
||||
Setting this option to `true` enables support for [the `s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
|
||||
These options can be set to `false` or `'transform'`. When using `'transform'`, the corresponding features are compiled to older syntax that can run in older browsers. When using `false` (the default), they are not compiled and they can be relied upon to compile more modern features.
|
||||
|
||||
```js
|
||||
rewritePattern('.');
|
||||
// → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
||||
- `unicodeFlag` - The `u` flag, enabling support for Unicode code point escapes in the form `\u{...}`.
|
||||
|
||||
rewritePattern('.', '', {
|
||||
'dotAllFlag': true
|
||||
});
|
||||
// → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
||||
```js
|
||||
rewritePattern('\\u{ab}', '', {
|
||||
unicodeFlag: 'transform'
|
||||
});
|
||||
// → '\\u{ab}'
|
||||
|
||||
rewritePattern('.', 's', {
|
||||
'dotAllFlag': true
|
||||
});
|
||||
// → '[\\0-\\uFFFF]'
|
||||
rewritePattern('\\u{ab}', 'u', {
|
||||
unicodeFlag: 'transform'
|
||||
});
|
||||
// → '\\xAB'
|
||||
```
|
||||
|
||||
rewritePattern('.', 'su', {
|
||||
'dotAllFlag': true
|
||||
});
|
||||
// → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
|
||||
```
|
||||
- `dotAllFlag` - The [`s` (`dotAll`) flag](https://github.com/mathiasbynens/es-regexp-dotall-flag).
|
||||
|
||||
#### `unicodePropertyEscape` (default: `false`)
|
||||
```js
|
||||
rewritePattern('.', '', {
|
||||
dotAllFlag: 'transform'
|
||||
});
|
||||
// → '[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF]'
|
||||
|
||||
Setting this option to `true` enables [support for Unicode property escapes](property-escapes.md):
|
||||
rewritePattern('.', 's', {
|
||||
dotAllFlag: 'transform'
|
||||
});
|
||||
// → '[\\0-\\uFFFF]'
|
||||
|
||||
```js
|
||||
rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
||||
'unicodePropertyEscape': true
|
||||
});
|
||||
// → '(?:\\uD811[\\uDC00-\\uDE46])'
|
||||
```
|
||||
rewritePattern('.', 'su', {
|
||||
dotAllFlag: 'transform'
|
||||
});
|
||||
// → '(?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])'
|
||||
```
|
||||
|
||||
#### `lookbehind` (default: `false`)
|
||||
- `unicodePropertyEscapes` - [Unicode property escapes](property-escapes.md).
|
||||
|
||||
Setting this option to `true` enables support for [lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind).
|
||||
By default they are compiled to Unicode code point escapes of the form `\u{...}`. If the `unicodeFlag` option is set to `'transform'` they often result in larger output, although there are cases (such as `\p{Lu}`) where it actually _decreases_ the output size.
|
||||
|
||||
```js
|
||||
rewritePattern('(?<=.)a', '', {
|
||||
'lookbehind': true
|
||||
});
|
||||
// → '(?<=[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uFFFF])a'
|
||||
```
|
||||
```js
|
||||
rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
||||
unicodePropertyEscapes: 'transform'
|
||||
});
|
||||
// → '[\\u{14400}-\\u{14646}]'
|
||||
|
||||
#### `namedGroup` (default: `false`)
|
||||
rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
||||
unicodeFlag: 'transform',
|
||||
unicodePropertyEscapes: 'transform'
|
||||
});
|
||||
// → '(?:\\uD811[\\uDC00-\\uDE46])'
|
||||
```
|
||||
|
||||
Setting this option to `true` enables support for [named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
|
||||
- `namedGroups` - [Named capture groups](https://github.com/tc39/proposal-regexp-named-groups).
|
||||
|
||||
```js
|
||||
rewritePattern('(?<name>.)\k<name>', '', {
|
||||
'namedGroups': true
|
||||
});
|
||||
// → '(.)\1'
|
||||
```
|
||||
```js
|
||||
rewritePattern('(?<name>.)\\k<name>', '', {
|
||||
namedGroups: 'transform'
|
||||
});
|
||||
// → '(.)\1'
|
||||
```
|
||||
|
||||
#### `onNamedGroup`
|
||||
#### Experimental regular expression features
|
||||
|
||||
This option is a function that gets called when a named capture group is found. It receives two parameters:
|
||||
the name of the group, and its index.
|
||||
These options can be set to `false`, `'parse'` and `'transform'`. When using `'transform'`, the corresponding features are compiled to older syntax that can run in older browsers. When using `'parse'`, they are parsed and left as-is in the output pattern. When using `false` (the default), they result in a syntax error if used.
|
||||
|
||||
```js
|
||||
rewritePattern('(?<name>.)\k<name>', '', {
|
||||
'namedGroups': true,
|
||||
onNamedGroup(name, index) {
|
||||
console.log(name, index);
|
||||
// → 'name', 1
|
||||
}
|
||||
});
|
||||
```
|
||||
Once these features become stable (when the proposals are accepted as part of ECMAScript), they will be parsed by default and thus `'parse'` will behave like `false`.
|
||||
|
||||
#### `useUnicodeFlag` (default: `false`)
|
||||
- `unicodeSetsFlag` - [The `v` (`unicodeSets`) flag](https://github.com/tc39/proposal-regexp-set-notation)
|
||||
|
||||
Setting this option to `true` enables the use of Unicode code point escapes of the form `\u{…}`. Note that in regular expressions, such escape sequences only work correctly when the ES2015 `u` flag is set. Enabling this setting often results in more compact output, although there are cases (such as `\p{Lu}`) where it actually _increases_ the output size.
|
||||
```js
|
||||
rewritePattern('[\\p{Emoji}&&\\p{ASCII}]', 'u', {
|
||||
unicodeSetsFlag: 'transform'
|
||||
});
|
||||
// → '[#\*0-9]'
|
||||
```
|
||||
|
||||
By default, patterns with the `v` flag are transformed to patterns with the `u` flag. If you want to downlevel them more you can set the `unicodeFlag: 'transform'` option.
|
||||
|
||||
```js
|
||||
rewritePattern('[^[a-h]&&[f-z]]', 'v', {
|
||||
unicodeSetsFlag: 'transform'
|
||||
});
|
||||
// → '[^f-h]' (to be used with /u)
|
||||
```
|
||||
|
||||
```js
|
||||
rewritePattern('[^[a-h]&&[f-z]]', 'v', {
|
||||
unicodeSetsFlag: 'transform',
|
||||
unicodeFlag: 'transform'
|
||||
});
|
||||
// → '(?:(?![f-h])[\s\S])' (to be used without /u)
|
||||
```
|
||||
|
||||
|
||||
#### Miscellaneous options
|
||||
|
||||
- `onNamedGroup`
|
||||
|
||||
This option is a function that gets called when a named capture group is found. It receives two parameters:
|
||||
the name of the group, and its index.
|
||||
|
||||
```js
|
||||
rewritePattern('(?<name>.)\\k<name>', '', {
|
||||
onNamedGroup(name, index) {
|
||||
console.log(name, index);
|
||||
// → 'name', 1
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Caveats
|
||||
|
||||
- [Lookbehind assertions](https://github.com/tc39/proposal-regexp-lookbehind) cannot be transformed to older syntax.
|
||||
- When using `namedGroups: 'transform'`, _regexpu-core_ only takes care of the _syntax_: you will still need a runtime wrapper around the regular expression to populate the `.groups` property of `RegExp.prototype.match()`'s result. If you are using _regexpu-core_ via Babel, it's handled automatically.
|
||||
|
||||
## For maintainers
|
||||
|
||||
### How to publish a new release
|
||||
|
||||
1. On the `main` branch, bump the version number in `package.json`:
|
||||
|
||||
```sh
|
||||
npm version patch -m 'Release v%s'
|
||||
```
|
||||
|
||||
Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
||||
|
||||
Note that this produces a Git commit + tag.
|
||||
|
||||
1. Push the release commit and tag:
|
||||
|
||||
```sh
|
||||
git push && git push --tags
|
||||
```
|
||||
|
||||
Our CI then automatically publishes the new release to npm.
|
||||
|
||||
1. Once the release has been published to npm, update [`regexpu`](https://github.com/mathiasbynens/regexpu) to make use of it, and [cut a new release of `regexpu` as well](https://github.com/mathiasbynens/regexpu#how-to-publish-a-new-release).
|
||||
|
||||
```js
|
||||
rewritePattern('\\p{Script_Extensions=Anatolian_Hieroglyphs}', 'u', {
|
||||
'unicodePropertyEscape': true,
|
||||
'useUnicodeFlag': true
|
||||
});
|
||||
// → '[\\u{14400}-\\u{14646}]'
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
|
||||
+79
-335
@@ -16,9 +16,6 @@ module.exports = new Map([
|
||||
[0x1CB, 0x1CA],
|
||||
[0x1F1, 0x1F2],
|
||||
[0x1F2, 0x1F1],
|
||||
[0x26A, 0xA7AE],
|
||||
[0x282, 0xA7C5],
|
||||
[0x29D, 0xA7B2],
|
||||
[0x345, 0x1FBE],
|
||||
[0x392, 0x3D0],
|
||||
[0x395, 0x3F5],
|
||||
@@ -52,239 +49,19 @@ module.exports = new Map([
|
||||
[0x421, 0x1C83],
|
||||
[0x422, 0x1C85],
|
||||
[0x42A, 0x1C86],
|
||||
[0x432, 0x1C80],
|
||||
[0x434, 0x1C81],
|
||||
[0x43E, 0x1C82],
|
||||
[0x441, 0x1C83],
|
||||
[0x442, [
|
||||
0x1C84,
|
||||
0x1C85
|
||||
]],
|
||||
[0x44A, 0x1C86],
|
||||
[0x462, 0x1C87],
|
||||
[0x463, 0x1C87],
|
||||
[0x10D0, 0x1C90],
|
||||
[0x10D1, 0x1C91],
|
||||
[0x10D2, 0x1C92],
|
||||
[0x10D3, 0x1C93],
|
||||
[0x10D4, 0x1C94],
|
||||
[0x10D5, 0x1C95],
|
||||
[0x10D6, 0x1C96],
|
||||
[0x10D7, 0x1C97],
|
||||
[0x10D8, 0x1C98],
|
||||
[0x10D9, 0x1C99],
|
||||
[0x10DA, 0x1C9A],
|
||||
[0x10DB, 0x1C9B],
|
||||
[0x10DC, 0x1C9C],
|
||||
[0x10DD, 0x1C9D],
|
||||
[0x10DE, 0x1C9E],
|
||||
[0x10DF, 0x1C9F],
|
||||
[0x10E0, 0x1CA0],
|
||||
[0x10E1, 0x1CA1],
|
||||
[0x10E2, 0x1CA2],
|
||||
[0x10E3, 0x1CA3],
|
||||
[0x10E4, 0x1CA4],
|
||||
[0x10E5, 0x1CA5],
|
||||
[0x10E6, 0x1CA6],
|
||||
[0x10E7, 0x1CA7],
|
||||
[0x10E8, 0x1CA8],
|
||||
[0x10E9, 0x1CA9],
|
||||
[0x10EA, 0x1CAA],
|
||||
[0x10EB, 0x1CAB],
|
||||
[0x10EC, 0x1CAC],
|
||||
[0x10ED, 0x1CAD],
|
||||
[0x10EE, 0x1CAE],
|
||||
[0x10EF, 0x1CAF],
|
||||
[0x10F0, 0x1CB0],
|
||||
[0x10F1, 0x1CB1],
|
||||
[0x10F2, 0x1CB2],
|
||||
[0x10F3, 0x1CB3],
|
||||
[0x10F4, 0x1CB4],
|
||||
[0x10F5, 0x1CB5],
|
||||
[0x10F6, 0x1CB6],
|
||||
[0x10F7, 0x1CB7],
|
||||
[0x10F8, 0x1CB8],
|
||||
[0x10F9, 0x1CB9],
|
||||
[0x10FA, 0x1CBA],
|
||||
[0x10FD, 0x1CBD],
|
||||
[0x10FE, 0x1CBE],
|
||||
[0x10FF, 0x1CBF],
|
||||
[0x13A0, 0xAB70],
|
||||
[0x13A1, 0xAB71],
|
||||
[0x13A2, 0xAB72],
|
||||
[0x13A3, 0xAB73],
|
||||
[0x13A4, 0xAB74],
|
||||
[0x13A5, 0xAB75],
|
||||
[0x13A6, 0xAB76],
|
||||
[0x13A7, 0xAB77],
|
||||
[0x13A8, 0xAB78],
|
||||
[0x13A9, 0xAB79],
|
||||
[0x13AA, 0xAB7A],
|
||||
[0x13AB, 0xAB7B],
|
||||
[0x13AC, 0xAB7C],
|
||||
[0x13AD, 0xAB7D],
|
||||
[0x13AE, 0xAB7E],
|
||||
[0x13AF, 0xAB7F],
|
||||
[0x13B0, 0xAB80],
|
||||
[0x13B1, 0xAB81],
|
||||
[0x13B2, 0xAB82],
|
||||
[0x13B3, 0xAB83],
|
||||
[0x13B4, 0xAB84],
|
||||
[0x13B5, 0xAB85],
|
||||
[0x13B6, 0xAB86],
|
||||
[0x13B7, 0xAB87],
|
||||
[0x13B8, 0xAB88],
|
||||
[0x13B9, 0xAB89],
|
||||
[0x13BA, 0xAB8A],
|
||||
[0x13BB, 0xAB8B],
|
||||
[0x13BC, 0xAB8C],
|
||||
[0x13BD, 0xAB8D],
|
||||
[0x13BE, 0xAB8E],
|
||||
[0x13BF, 0xAB8F],
|
||||
[0x13C0, 0xAB90],
|
||||
[0x13C1, 0xAB91],
|
||||
[0x13C2, 0xAB92],
|
||||
[0x13C3, 0xAB93],
|
||||
[0x13C4, 0xAB94],
|
||||
[0x13C5, 0xAB95],
|
||||
[0x13C6, 0xAB96],
|
||||
[0x13C7, 0xAB97],
|
||||
[0x13C8, 0xAB98],
|
||||
[0x13C9, 0xAB99],
|
||||
[0x13CA, 0xAB9A],
|
||||
[0x13CB, 0xAB9B],
|
||||
[0x13CC, 0xAB9C],
|
||||
[0x13CD, 0xAB9D],
|
||||
[0x13CE, 0xAB9E],
|
||||
[0x13CF, 0xAB9F],
|
||||
[0x13D0, 0xABA0],
|
||||
[0x13D1, 0xABA1],
|
||||
[0x13D2, 0xABA2],
|
||||
[0x13D3, 0xABA3],
|
||||
[0x13D4, 0xABA4],
|
||||
[0x13D5, 0xABA5],
|
||||
[0x13D6, 0xABA6],
|
||||
[0x13D7, 0xABA7],
|
||||
[0x13D8, 0xABA8],
|
||||
[0x13D9, 0xABA9],
|
||||
[0x13DA, 0xABAA],
|
||||
[0x13DB, 0xABAB],
|
||||
[0x13DC, 0xABAC],
|
||||
[0x13DD, 0xABAD],
|
||||
[0x13DE, 0xABAE],
|
||||
[0x13DF, 0xABAF],
|
||||
[0x13E0, 0xABB0],
|
||||
[0x13E1, 0xABB1],
|
||||
[0x13E2, 0xABB2],
|
||||
[0x13E3, 0xABB3],
|
||||
[0x13E4, 0xABB4],
|
||||
[0x13E5, 0xABB5],
|
||||
[0x13E6, 0xABB6],
|
||||
[0x13E7, 0xABB7],
|
||||
[0x13E8, 0xABB8],
|
||||
[0x13E9, 0xABB9],
|
||||
[0x13EA, 0xABBA],
|
||||
[0x13EB, 0xABBB],
|
||||
[0x13EC, 0xABBC],
|
||||
[0x13ED, 0xABBD],
|
||||
[0x13EE, 0xABBE],
|
||||
[0x13EF, 0xABBF],
|
||||
[0x13F0, 0x13F8],
|
||||
[0x13F1, 0x13F9],
|
||||
[0x13F2, 0x13FA],
|
||||
[0x13F3, 0x13FB],
|
||||
[0x13F4, 0x13FC],
|
||||
[0x13F5, 0x13FD],
|
||||
[0x13F8, 0x13F0],
|
||||
[0x13F9, 0x13F1],
|
||||
[0x13FA, 0x13F2],
|
||||
[0x13FB, 0x13F3],
|
||||
[0x13FC, 0x13F4],
|
||||
[0x13FD, 0x13F5],
|
||||
[0x1C80, [
|
||||
0x412,
|
||||
0x432
|
||||
]],
|
||||
[0x1C81, [
|
||||
0x414,
|
||||
0x434
|
||||
]],
|
||||
[0x1C82, [
|
||||
0x41E,
|
||||
0x43E
|
||||
]],
|
||||
[0x1C83, [
|
||||
0x421,
|
||||
0x441
|
||||
]],
|
||||
[0x1C84, [
|
||||
0x1C85,
|
||||
0x442
|
||||
]],
|
||||
[0x1C80, 0x412],
|
||||
[0x1C81, 0x414],
|
||||
[0x1C82, 0x41E],
|
||||
[0x1C83, 0x421],
|
||||
[0x1C84, 0x1C85],
|
||||
[0x1C85, [
|
||||
0x422,
|
||||
0x1C84,
|
||||
0x442
|
||||
0x1C84
|
||||
]],
|
||||
[0x1C86, [
|
||||
0x42A,
|
||||
0x44A
|
||||
]],
|
||||
[0x1C87, [
|
||||
0x462,
|
||||
0x463
|
||||
]],
|
||||
[0x1C88, [
|
||||
0xA64A,
|
||||
0xA64B
|
||||
]],
|
||||
[0x1C90, 0x10D0],
|
||||
[0x1C91, 0x10D1],
|
||||
[0x1C92, 0x10D2],
|
||||
[0x1C93, 0x10D3],
|
||||
[0x1C94, 0x10D4],
|
||||
[0x1C95, 0x10D5],
|
||||
[0x1C96, 0x10D6],
|
||||
[0x1C97, 0x10D7],
|
||||
[0x1C98, 0x10D8],
|
||||
[0x1C99, 0x10D9],
|
||||
[0x1C9A, 0x10DA],
|
||||
[0x1C9B, 0x10DB],
|
||||
[0x1C9C, 0x10DC],
|
||||
[0x1C9D, 0x10DD],
|
||||
[0x1C9E, 0x10DE],
|
||||
[0x1C9F, 0x10DF],
|
||||
[0x1CA0, 0x10E0],
|
||||
[0x1CA1, 0x10E1],
|
||||
[0x1CA2, 0x10E2],
|
||||
[0x1CA3, 0x10E3],
|
||||
[0x1CA4, 0x10E4],
|
||||
[0x1CA5, 0x10E5],
|
||||
[0x1CA6, 0x10E6],
|
||||
[0x1CA7, 0x10E7],
|
||||
[0x1CA8, 0x10E8],
|
||||
[0x1CA9, 0x10E9],
|
||||
[0x1CAA, 0x10EA],
|
||||
[0x1CAB, 0x10EB],
|
||||
[0x1CAC, 0x10EC],
|
||||
[0x1CAD, 0x10ED],
|
||||
[0x1CAE, 0x10EE],
|
||||
[0x1CAF, 0x10EF],
|
||||
[0x1CB0, 0x10F0],
|
||||
[0x1CB1, 0x10F1],
|
||||
[0x1CB2, 0x10F2],
|
||||
[0x1CB3, 0x10F3],
|
||||
[0x1CB4, 0x10F4],
|
||||
[0x1CB5, 0x10F5],
|
||||
[0x1CB6, 0x10F6],
|
||||
[0x1CB7, 0x10F7],
|
||||
[0x1CB8, 0x10F8],
|
||||
[0x1CB9, 0x10F9],
|
||||
[0x1CBA, 0x10FA],
|
||||
[0x1CBD, 0x10FD],
|
||||
[0x1CBE, 0x10FE],
|
||||
[0x1CBF, 0x10FF],
|
||||
[0x1D8E, 0xA7C6],
|
||||
[0x1C86, 0x42A],
|
||||
[0x1C87, 0x462],
|
||||
[0x1C88, 0xA64A],
|
||||
[0x1E60, 0x1E9B],
|
||||
[0x1E9B, 0x1E60],
|
||||
[0x1E9E, 0xDF],
|
||||
@@ -356,109 +133,6 @@ module.exports = new Map([
|
||||
0xE5
|
||||
]],
|
||||
[0xA64A, 0x1C88],
|
||||
[0xA64B, 0x1C88],
|
||||
[0xA794, 0xA7C4],
|
||||
[0xA7AE, 0x26A],
|
||||
[0xA7B2, 0x29D],
|
||||
[0xA7B3, 0xAB53],
|
||||
[0xA7B4, 0xA7B5],
|
||||
[0xA7B5, 0xA7B4],
|
||||
[0xA7B6, 0xA7B7],
|
||||
[0xA7B7, 0xA7B6],
|
||||
[0xA7B8, 0xA7B9],
|
||||
[0xA7B9, 0xA7B8],
|
||||
[0xA7BA, 0xA7BB],
|
||||
[0xA7BB, 0xA7BA],
|
||||
[0xA7BC, 0xA7BD],
|
||||
[0xA7BD, 0xA7BC],
|
||||
[0xA7BE, 0xA7BF],
|
||||
[0xA7BF, 0xA7BE],
|
||||
[0xA7C2, 0xA7C3],
|
||||
[0xA7C3, 0xA7C2],
|
||||
[0xA7C4, 0xA794],
|
||||
[0xA7C5, 0x282],
|
||||
[0xA7C6, 0x1D8E],
|
||||
[0xAB53, 0xA7B3],
|
||||
[0xAB70, 0x13A0],
|
||||
[0xAB71, 0x13A1],
|
||||
[0xAB72, 0x13A2],
|
||||
[0xAB73, 0x13A3],
|
||||
[0xAB74, 0x13A4],
|
||||
[0xAB75, 0x13A5],
|
||||
[0xAB76, 0x13A6],
|
||||
[0xAB77, 0x13A7],
|
||||
[0xAB78, 0x13A8],
|
||||
[0xAB79, 0x13A9],
|
||||
[0xAB7A, 0x13AA],
|
||||
[0xAB7B, 0x13AB],
|
||||
[0xAB7C, 0x13AC],
|
||||
[0xAB7D, 0x13AD],
|
||||
[0xAB7E, 0x13AE],
|
||||
[0xAB7F, 0x13AF],
|
||||
[0xAB80, 0x13B0],
|
||||
[0xAB81, 0x13B1],
|
||||
[0xAB82, 0x13B2],
|
||||
[0xAB83, 0x13B3],
|
||||
[0xAB84, 0x13B4],
|
||||
[0xAB85, 0x13B5],
|
||||
[0xAB86, 0x13B6],
|
||||
[0xAB87, 0x13B7],
|
||||
[0xAB88, 0x13B8],
|
||||
[0xAB89, 0x13B9],
|
||||
[0xAB8A, 0x13BA],
|
||||
[0xAB8B, 0x13BB],
|
||||
[0xAB8C, 0x13BC],
|
||||
[0xAB8D, 0x13BD],
|
||||
[0xAB8E, 0x13BE],
|
||||
[0xAB8F, 0x13BF],
|
||||
[0xAB90, 0x13C0],
|
||||
[0xAB91, 0x13C1],
|
||||
[0xAB92, 0x13C2],
|
||||
[0xAB93, 0x13C3],
|
||||
[0xAB94, 0x13C4],
|
||||
[0xAB95, 0x13C5],
|
||||
[0xAB96, 0x13C6],
|
||||
[0xAB97, 0x13C7],
|
||||
[0xAB98, 0x13C8],
|
||||
[0xAB99, 0x13C9],
|
||||
[0xAB9A, 0x13CA],
|
||||
[0xAB9B, 0x13CB],
|
||||
[0xAB9C, 0x13CC],
|
||||
[0xAB9D, 0x13CD],
|
||||
[0xAB9E, 0x13CE],
|
||||
[0xAB9F, 0x13CF],
|
||||
[0xABA0, 0x13D0],
|
||||
[0xABA1, 0x13D1],
|
||||
[0xABA2, 0x13D2],
|
||||
[0xABA3, 0x13D3],
|
||||
[0xABA4, 0x13D4],
|
||||
[0xABA5, 0x13D5],
|
||||
[0xABA6, 0x13D6],
|
||||
[0xABA7, 0x13D7],
|
||||
[0xABA8, 0x13D8],
|
||||
[0xABA9, 0x13D9],
|
||||
[0xABAA, 0x13DA],
|
||||
[0xABAB, 0x13DB],
|
||||
[0xABAC, 0x13DC],
|
||||
[0xABAD, 0x13DD],
|
||||
[0xABAE, 0x13DE],
|
||||
[0xABAF, 0x13DF],
|
||||
[0xABB0, 0x13E0],
|
||||
[0xABB1, 0x13E1],
|
||||
[0xABB2, 0x13E2],
|
||||
[0xABB3, 0x13E3],
|
||||
[0xABB4, 0x13E4],
|
||||
[0xABB5, 0x13E5],
|
||||
[0xABB6, 0x13E6],
|
||||
[0xABB7, 0x13E7],
|
||||
[0xABB8, 0x13E8],
|
||||
[0xABB9, 0x13E9],
|
||||
[0xABBA, 0x13EA],
|
||||
[0xABBB, 0x13EB],
|
||||
[0xABBC, 0x13EC],
|
||||
[0xABBD, 0x13ED],
|
||||
[0xABBE, 0x13EE],
|
||||
[0xABBF, 0x13EF],
|
||||
[0x10400, 0x10428],
|
||||
[0x10401, 0x10429],
|
||||
[0x10402, 0x1042A],
|
||||
@@ -611,6 +285,76 @@ module.exports = new Map([
|
||||
[0x104F9, 0x104D1],
|
||||
[0x104FA, 0x104D2],
|
||||
[0x104FB, 0x104D3],
|
||||
[0x10570, 0x10597],
|
||||
[0x10571, 0x10598],
|
||||
[0x10572, 0x10599],
|
||||
[0x10573, 0x1059A],
|
||||
[0x10574, 0x1059B],
|
||||
[0x10575, 0x1059C],
|
||||
[0x10576, 0x1059D],
|
||||
[0x10577, 0x1059E],
|
||||
[0x10578, 0x1059F],
|
||||
[0x10579, 0x105A0],
|
||||
[0x1057A, 0x105A1],
|
||||
[0x1057C, 0x105A3],
|
||||
[0x1057D, 0x105A4],
|
||||
[0x1057E, 0x105A5],
|
||||
[0x1057F, 0x105A6],
|
||||
[0x10580, 0x105A7],
|
||||
[0x10581, 0x105A8],
|
||||
[0x10582, 0x105A9],
|
||||
[0x10583, 0x105AA],
|
||||
[0x10584, 0x105AB],
|
||||
[0x10585, 0x105AC],
|
||||
[0x10586, 0x105AD],
|
||||
[0x10587, 0x105AE],
|
||||
[0x10588, 0x105AF],
|
||||
[0x10589, 0x105B0],
|
||||
[0x1058A, 0x105B1],
|
||||
[0x1058C, 0x105B3],
|
||||
[0x1058D, 0x105B4],
|
||||
[0x1058E, 0x105B5],
|
||||
[0x1058F, 0x105B6],
|
||||
[0x10590, 0x105B7],
|
||||
[0x10591, 0x105B8],
|
||||
[0x10592, 0x105B9],
|
||||
[0x10594, 0x105BB],
|
||||
[0x10595, 0x105BC],
|
||||
[0x10597, 0x10570],
|
||||
[0x10598, 0x10571],
|
||||
[0x10599, 0x10572],
|
||||
[0x1059A, 0x10573],
|
||||
[0x1059B, 0x10574],
|
||||
[0x1059C, 0x10575],
|
||||
[0x1059D, 0x10576],
|
||||
[0x1059E, 0x10577],
|
||||
[0x1059F, 0x10578],
|
||||
[0x105A0, 0x10579],
|
||||
[0x105A1, 0x1057A],
|
||||
[0x105A3, 0x1057C],
|
||||
[0x105A4, 0x1057D],
|
||||
[0x105A5, 0x1057E],
|
||||
[0x105A6, 0x1057F],
|
||||
[0x105A7, 0x10580],
|
||||
[0x105A8, 0x10581],
|
||||
[0x105A9, 0x10582],
|
||||
[0x105AA, 0x10583],
|
||||
[0x105AB, 0x10584],
|
||||
[0x105AC, 0x10585],
|
||||
[0x105AD, 0x10586],
|
||||
[0x105AE, 0x10587],
|
||||
[0x105AF, 0x10588],
|
||||
[0x105B0, 0x10589],
|
||||
[0x105B1, 0x1058A],
|
||||
[0x105B3, 0x1058C],
|
||||
[0x105B4, 0x1058D],
|
||||
[0x105B5, 0x1058E],
|
||||
[0x105B6, 0x1058F],
|
||||
[0x105B7, 0x10590],
|
||||
[0x105B8, 0x10591],
|
||||
[0x105B9, 0x10592],
|
||||
[0x105BB, 0x10594],
|
||||
[0x105BC, 0x10595],
|
||||
[0x10C80, 0x10CC0],
|
||||
[0x10C81, 0x10CC1],
|
||||
[0x10C82, 0x10CC2],
|
||||
|
||||
+24
-24
@@ -1,32 +1,32 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"regexpu-core@4.7.0",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"regexpu-core@5.1.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "regexpu-core@4.7.0",
|
||||
"_id": "regexpu-core@4.7.0",
|
||||
"_from": "regexpu-core@5.1.0",
|
||||
"_id": "regexpu-core@5.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-TQ4KXRnIn6tz6tjnrXEkD/sshygKH/j5KzK86X8MkeHyZ8qst/LZ89j3X4/8HEIfHANTFIP/AbXakeRhWIl5YQ==",
|
||||
"_integrity": "sha512-bb6hk+xWd2PEOkj5It46A16zFMs2mv86Iwpdu94la4S3sJ7C973h2dHpYKwIBGaWSO7cIRJ+UX0IeMaWcO4qwA==",
|
||||
"_location": "/regexpu-core",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "regexpu-core@4.7.0",
|
||||
"raw": "regexpu-core@5.1.0",
|
||||
"name": "regexpu-core",
|
||||
"escapedName": "regexpu-core",
|
||||
"rawSpec": "4.7.0",
|
||||
"rawSpec": "5.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "4.7.0"
|
||||
"fetchSpec": "5.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@babel/helper-create-regexp-features-plugin"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-4.7.0.tgz",
|
||||
"_spec": "4.7.0",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-5.1.0.tgz",
|
||||
"_spec": "5.1.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Mathias Bynens",
|
||||
"url": "https://mathiasbynens.be/"
|
||||
@@ -35,22 +35,22 @@
|
||||
"url": "https://github.com/mathiasbynens/regexpu-core/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"regenerate": "^1.4.0",
|
||||
"regenerate-unicode-properties": "^8.2.0",
|
||||
"regjsgen": "^0.5.1",
|
||||
"regjsparser": "^0.6.4",
|
||||
"unicode-match-property-ecmascript": "^1.0.4",
|
||||
"unicode-match-property-value-ecmascript": "^1.2.0"
|
||||
"regenerate": "^1.4.2",
|
||||
"regenerate-unicode-properties": "^10.0.1",
|
||||
"regjsgen": "^0.6.0",
|
||||
"regjsparser": "^0.8.2",
|
||||
"unicode-match-property-ecmascript": "^2.0.0",
|
||||
"unicode-match-property-value-ecmascript": "^2.0.0"
|
||||
},
|
||||
"description": "regexpu’s core functionality (i.e. `rewritePattern(pattern, flag)`), capable of translating ES6 Unicode regular expressions to ES5.",
|
||||
"devDependencies": {
|
||||
"codecov": "^3.6.5",
|
||||
"@unicode/unicode-14.0.0": "^1.2.1",
|
||||
"codecov": "^3.8.3",
|
||||
"istanbul": "^0.4.5",
|
||||
"jsesc": "^2.5.2",
|
||||
"lodash": "^4.17.15",
|
||||
"mocha": "^7.1.0",
|
||||
"regexpu-fixtures": "^2.1.3",
|
||||
"unicode-13.0.0": "^0.8.0"
|
||||
"jsesc": "^3.0.2",
|
||||
"lodash": "^4.17.21",
|
||||
"mocha": "^9.1.1",
|
||||
"regexpu-fixtures": "2.1.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
@@ -93,5 +93,5 @@
|
||||
"cover": "istanbul cover --report html node_modules/.bin/_mocha tests -- -u exports -R spec",
|
||||
"test": "mocha tests"
|
||||
},
|
||||
"version": "4.7.0"
|
||||
"version": "5.1.0"
|
||||
}
|
||||
|
||||
+476
-84
@@ -8,12 +8,24 @@ const unicodeMatchPropertyValue = require('unicode-match-property-value-ecmascri
|
||||
const iuMappings = require('./data/iu-mappings.js');
|
||||
const ESCAPE_SETS = require('./data/character-class-escape-sets.js');
|
||||
|
||||
function flatMap(array, callback) {
|
||||
const result = [];
|
||||
array.forEach(item => {
|
||||
const res = callback(item);
|
||||
if (Array.isArray(res)) {
|
||||
result.push.apply(result, res);
|
||||
} else {
|
||||
result.push(res);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
const SPECIAL_CHARS = new Set('\\^$.*+?()[]{}|'.split(''));
|
||||
|
||||
// Prepare a Regenerate set containing all code points, used for negative
|
||||
// character classes (if any).
|
||||
const UNICODE_SET = regenerate().addRange(0x0, 0x10FFFF);
|
||||
// Without the `u` flag, the range stops at 0xFFFF.
|
||||
// https://mths.be/es6#sec-pattern-semantics
|
||||
const BMP_SET = regenerate().addRange(0x0, 0xFFFF);
|
||||
|
||||
// Prepare a Regenerate set containing all code points that are supposed to be
|
||||
// matched by `/./u`. https://mths.be/es6#sec-atom
|
||||
@@ -62,8 +74,13 @@ const handleLoneUnicodePropertyNameOrValue = (value) => {
|
||||
const category = unicodeMatchPropertyValue(property, value);
|
||||
return getUnicodePropertyValueSet(property, category);
|
||||
} catch (exception) {}
|
||||
// It’s not a `General_Category` value, so check if it’s a binary
|
||||
// property. Note: `unicodeMatchProperty` throws on invalid properties.
|
||||
// It’s not a `General_Category` value, so check if it’s a property
|
||||
// of strings.
|
||||
try {
|
||||
return getUnicodePropertyValueSet('Property_of_Strings', value);
|
||||
} catch (exception) {}
|
||||
// Lastly, check if it’s a binary property of single code points.
|
||||
// Note: `unicodeMatchProperty` throws on invalid properties.
|
||||
const property = unicodeMatchProperty(value);
|
||||
return getUnicodePropertyValueSet(property);
|
||||
};
|
||||
@@ -81,9 +98,32 @@ const getUnicodePropertyEscapeSet = (value, isNegative) => {
|
||||
set = getUnicodePropertyValueSet(property, value);
|
||||
}
|
||||
if (isNegative) {
|
||||
return UNICODE_SET.clone().remove(set);
|
||||
if (set.strings) {
|
||||
throw new Error('Cannot negate Unicode property of strings');
|
||||
}
|
||||
return {
|
||||
characters: UNICODE_SET.clone().remove(set.characters),
|
||||
strings: new Set()
|
||||
};
|
||||
}
|
||||
return set.clone();
|
||||
return {
|
||||
characters: set.characters.clone(),
|
||||
strings: set.strings
|
||||
// We need to escape strings like *️⃣ to make sure that they can be safelu used in unions
|
||||
? new Set(set.strings.map(str => SPECIAL_CHARS.has(str[0]) ? `\\${str}` : str))
|
||||
: new Set()
|
||||
};
|
||||
};
|
||||
|
||||
const getUnicodePropertyEscapeCharacterClassData = (property, isNegative) => {
|
||||
const set = getUnicodePropertyEscapeSet(property, isNegative);
|
||||
const data = getCharacterClassEmptyData();
|
||||
data.singleChars = set.characters;
|
||||
if (set.strings.size > 0) {
|
||||
data.longStrings = set.strings;
|
||||
data.maybeIncludesStrings = true;
|
||||
}
|
||||
return data;
|
||||
};
|
||||
|
||||
// Given a range of code points, add any case-folded code points in that range
|
||||
@@ -98,6 +138,16 @@ regenerate.prototype.iuAddRange = function(min, max) {
|
||||
} while (++min <= max);
|
||||
return $this;
|
||||
};
|
||||
regenerate.prototype.iuRemoveRange = function(min, max) {
|
||||
const $this = this;
|
||||
do {
|
||||
const folded = caseFold(min);
|
||||
if (folded) {
|
||||
$this.remove(folded);
|
||||
}
|
||||
} while (++min <= max);
|
||||
return $this;
|
||||
};
|
||||
|
||||
const update = (item, pattern) => {
|
||||
let tree = parse(pattern, config.useUnicodeFlag ? 'u' : '');
|
||||
@@ -128,36 +178,248 @@ const caseFold = (codePoint) => {
|
||||
return iuMappings.get(codePoint) || false;
|
||||
};
|
||||
|
||||
const processCharacterClass = (characterClassItem, regenerateOptions) => {
|
||||
let set = regenerate();
|
||||
const buildHandler = (action) => {
|
||||
switch (action) {
|
||||
case 'union':
|
||||
return {
|
||||
single: (data, cp) => {
|
||||
data.singleChars.add(cp);
|
||||
},
|
||||
regSet: (data, set2) => {
|
||||
data.singleChars.add(set2);
|
||||
},
|
||||
range: (data, start, end) => {
|
||||
data.singleChars.addRange(start, end);
|
||||
},
|
||||
iuRange: (data, start, end) => {
|
||||
data.singleChars.iuAddRange(start, end);
|
||||
},
|
||||
nested: (data, nestedData) => {
|
||||
data.singleChars.add(nestedData.singleChars);
|
||||
for (const str of nestedData.longStrings) data.longStrings.add(str);
|
||||
if (nestedData.maybeIncludesStrings) data.maybeIncludesStrings = true;
|
||||
}
|
||||
};
|
||||
case 'union-negative': {
|
||||
const regSet = (data, set2) => {
|
||||
data.singleChars = UNICODE_SET.clone().remove(set2).add(data.singleChars);
|
||||
};
|
||||
return {
|
||||
single: (data, cp) => {
|
||||
const unicode = UNICODE_SET.clone();
|
||||
data.singleChars = data.singleChars.contains(cp) ? unicode : unicode.remove(cp);
|
||||
},
|
||||
regSet: regSet,
|
||||
range: (data, start, end) => {
|
||||
data.singleChars = UNICODE_SET.clone().removeRange(start, end).add(data.singleChars);
|
||||
},
|
||||
iuRange: (data, start, end) => {
|
||||
data.singleChars = UNICODE_SET.clone().iuRemoveRange(start, end).add(data.singleChars);
|
||||
},
|
||||
nested: (data, nestedData) => {
|
||||
regSet(data, nestedData.singleChars);
|
||||
if (nestedData.maybeIncludesStrings) throw new Error('ASSERTION ERROR');
|
||||
}
|
||||
};
|
||||
}
|
||||
case 'intersection': {
|
||||
const regSet = (data, set2) => {
|
||||
if (data.first) data.singleChars = set2;
|
||||
else data.singleChars.intersection(set2);
|
||||
};
|
||||
return {
|
||||
single: (data, cp) => {
|
||||
data.singleChars = data.first || data.singleChars.contains(cp) ? regenerate(cp) : regenerate();
|
||||
data.longStrings.clear();
|
||||
data.maybeIncludesStrings = false;
|
||||
},
|
||||
regSet: (data, set) => {
|
||||
regSet(data, set);
|
||||
data.longStrings.clear();
|
||||
data.maybeIncludesStrings = false;
|
||||
},
|
||||
range: (data, start, end) => {
|
||||
if (data.first) data.singleChars.addRange(start, end);
|
||||
else data.singleChars.intersection(regenerate().addRange(start, end));
|
||||
data.longStrings.clear();
|
||||
data.maybeIncludesStrings = false;
|
||||
},
|
||||
iuRange: (data, start, end) => {
|
||||
if (data.first) data.singleChars.iuAddRange(start, end);
|
||||
else data.singleChars.intersection(regenerate().iuAddRange(start, end));
|
||||
data.longStrings.clear();
|
||||
data.maybeIncludesStrings = false;
|
||||
},
|
||||
nested: (data, nestedData) => {
|
||||
regSet(data, nestedData.singleChars);
|
||||
|
||||
if (data.first) {
|
||||
data.longStrings = nestedData.longStrings;
|
||||
data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
|
||||
} else {
|
||||
for (const str of data.longStrings) {
|
||||
if (!nestedData.longStrings.has(str)) data.longStrings.delete(str);
|
||||
}
|
||||
if (!nestedData.maybeIncludesStrings) data.maybeIncludesStrings = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
case 'subtraction': {
|
||||
const regSet = (data, set2) => {
|
||||
if (data.first) data.singleChars.add(set2);
|
||||
else data.singleChars.remove(set2);
|
||||
};
|
||||
return {
|
||||
single: (data, cp) => {
|
||||
if (data.first) data.singleChars.add(cp);
|
||||
else data.singleChars.remove(cp);
|
||||
},
|
||||
regSet: regSet,
|
||||
range: (data, start, end) => {
|
||||
if (data.first) data.singleChars.addRange(start, end);
|
||||
else data.singleChars.removeRange(start, end);
|
||||
},
|
||||
iuRange: (data, start, end) => {
|
||||
if (data.first) data.singleChars.iuAddRange(start, end);
|
||||
else data.singleChars.iuRemoveRange(start, end);
|
||||
},
|
||||
nested: (data, nestedData) => {
|
||||
regSet(data, nestedData.singleChars);
|
||||
|
||||
if (data.first) {
|
||||
data.longStrings = nestedData.longStrings;
|
||||
data.maybeIncludesStrings = nestedData.maybeIncludesStrings;
|
||||
} else {
|
||||
for (const str of data.longStrings) {
|
||||
if (nestedData.longStrings.has(str)) data.longStrings.delete(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
// The `default` clause is only here as a safeguard; it should never be
|
||||
// reached. Code coverage tools should ignore it.
|
||||
/* istanbul ignore next */
|
||||
default:
|
||||
throw new Error(`Unknown set action: ${ characterClassItem.kind }`);
|
||||
}
|
||||
};
|
||||
|
||||
const getCharacterClassEmptyData = () => ({
|
||||
transformed: config.transform.unicodeFlag,
|
||||
singleChars: regenerate(),
|
||||
longStrings: new Set(),
|
||||
hasEmptyString: false,
|
||||
first: true,
|
||||
maybeIncludesStrings: false
|
||||
});
|
||||
|
||||
const maybeFold = (codePoint) => {
|
||||
if (config.flags.ignoreCase && config.transform.unicodeFlag) {
|
||||
const folded = caseFold(codePoint);
|
||||
if (folded) {
|
||||
return [codePoint, folded];
|
||||
}
|
||||
}
|
||||
return [codePoint];
|
||||
};
|
||||
|
||||
const computeClassStrings = (classStrings, regenerateOptions) => {
|
||||
let data = getCharacterClassEmptyData();
|
||||
|
||||
for (const string of classStrings.strings) {
|
||||
if (string.characters.length === 1) {
|
||||
maybeFold(string.characters[0].codePoint).forEach((cp) => {
|
||||
data.singleChars.add(cp);
|
||||
});
|
||||
} else {
|
||||
let stringifiedString;
|
||||
if (config.flags.ignoreCase && config.transform.unicodeFlag) {
|
||||
stringifiedString = '';
|
||||
for (const ch of string.characters) {
|
||||
let set = regenerate(ch.codePoint);
|
||||
const folded = caseFold(ch.codePoint);
|
||||
if (folded) set.add(folded);
|
||||
stringifiedString += set.toString(regenerateOptions);
|
||||
}
|
||||
} else {
|
||||
stringifiedString = string.characters.map(ch => generate(ch)).join('')
|
||||
}
|
||||
|
||||
data.longStrings.add(stringifiedString);
|
||||
data.maybeIncludesStrings = true;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
const computeCharacterClass = (characterClassItem, regenerateOptions) => {
|
||||
let data = getCharacterClassEmptyData();
|
||||
|
||||
let handlePositive;
|
||||
let handleNegative;
|
||||
|
||||
switch (characterClassItem.kind) {
|
||||
case 'union':
|
||||
handlePositive = buildHandler('union');
|
||||
handleNegative = buildHandler('union-negative');
|
||||
break;
|
||||
case 'intersection':
|
||||
handlePositive = buildHandler('intersection');
|
||||
handleNegative = buildHandler('subtraction');
|
||||
break;
|
||||
case 'subtraction':
|
||||
handlePositive = buildHandler('subtraction');
|
||||
handleNegative = buildHandler('intersection');
|
||||
break;
|
||||
// The `default` clause is only here as a safeguard; it should never be
|
||||
// reached. Code coverage tools should ignore it.
|
||||
/* istanbul ignore next */
|
||||
default:
|
||||
throw new Error(`Unknown character class kind: ${ characterClassItem.kind }`);
|
||||
}
|
||||
|
||||
for (const item of characterClassItem.body) {
|
||||
switch (item.type) {
|
||||
case 'value':
|
||||
set.add(item.codePoint);
|
||||
if (config.ignoreCase && config.unicode && !config.useUnicodeFlag) {
|
||||
const folded = caseFold(item.codePoint);
|
||||
if (folded) {
|
||||
set.add(folded);
|
||||
}
|
||||
}
|
||||
maybeFold(item.codePoint).forEach((cp) => {
|
||||
handlePositive.single(data, cp);
|
||||
});
|
||||
break;
|
||||
case 'characterClassRange':
|
||||
const min = item.min.codePoint;
|
||||
const max = item.max.codePoint;
|
||||
set.addRange(min, max);
|
||||
if (config.ignoreCase && config.unicode && !config.useUnicodeFlag) {
|
||||
set.iuAddRange(min, max);
|
||||
handlePositive.range(data, min, max);
|
||||
if (config.flags.ignoreCase && config.transform.unicodeFlag) {
|
||||
handlePositive.iuRange(data, min, max);
|
||||
}
|
||||
break;
|
||||
case 'characterClassEscape':
|
||||
set.add(getCharacterClassEscapeSet(
|
||||
handlePositive.regSet(data, getCharacterClassEscapeSet(
|
||||
item.value,
|
||||
config.unicode,
|
||||
config.ignoreCase
|
||||
config.flags.unicode,
|
||||
config.flags.ignoreCase
|
||||
));
|
||||
break;
|
||||
case 'unicodePropertyEscape':
|
||||
set.add(getUnicodePropertyEscapeSet(item.value, item.negative));
|
||||
const nestedData = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
|
||||
handlePositive.nested(data, nestedData);
|
||||
data.transformed =
|
||||
data.transformed ||
|
||||
config.transform.unicodePropertyEscapes ||
|
||||
(config.transform.unicodeSetsFlag && nestedData.maybeIncludesStrings);
|
||||
break;
|
||||
case 'characterClass':
|
||||
const handler = item.negative ? handleNegative : handlePositive;
|
||||
const res = computeCharacterClass(item, regenerateOptions);
|
||||
handler.nested(data, res);
|
||||
data.transformed = true;
|
||||
break;
|
||||
case 'classStrings':
|
||||
handlePositive.nested(data, computeClassStrings(item, regenerateOptions));
|
||||
data.transformed = true;
|
||||
break;
|
||||
// The `default` clause is only here as a safeguard; it should never be
|
||||
// reached. Code coverage tools should ignore it.
|
||||
@@ -165,17 +427,45 @@ const processCharacterClass = (characterClassItem, regenerateOptions) => {
|
||||
default:
|
||||
throw new Error(`Unknown term type: ${ item.type }`);
|
||||
}
|
||||
}
|
||||
if (characterClassItem.negative) {
|
||||
set = (config.unicode ? UNICODE_SET : BMP_SET).clone().remove(set);
|
||||
}
|
||||
update(characterClassItem, set.toString(regenerateOptions));
|
||||
return characterClassItem;
|
||||
};
|
||||
|
||||
const updateNamedReference = (item, index) => {
|
||||
delete item.name;
|
||||
item.matchIndex = index;
|
||||
data.first = false;
|
||||
}
|
||||
|
||||
if (characterClassItem.negative && data.maybeIncludesStrings) {
|
||||
throw new SyntaxError('Cannot negate set containing strings');
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
const processCharacterClass = (
|
||||
characterClassItem,
|
||||
regenerateOptions,
|
||||
computed = computeCharacterClass(characterClassItem, regenerateOptions)
|
||||
) => {
|
||||
const negative = characterClassItem.negative;
|
||||
const { singleChars, transformed, longStrings } = computed;
|
||||
if (transformed) {
|
||||
const setStr = singleChars.toString(regenerateOptions);
|
||||
|
||||
if (negative) {
|
||||
if (config.useUnicodeFlag) {
|
||||
update(characterClassItem, `[^${setStr[0] === '[' ? setStr.slice(1, -1) : setStr}]`)
|
||||
} else {
|
||||
update(characterClassItem, `(?!${setStr})[\\s\\S]`)
|
||||
}
|
||||
} else {
|
||||
const hasEmptyString = longStrings.has('');
|
||||
const pieces = Array.from(longStrings).sort((a, b) => b.length - a.length);
|
||||
|
||||
if (setStr !== '[]' || longStrings.size === 0) {
|
||||
pieces.splice(pieces.length - (hasEmptyString ? 1 : 0), 0, setStr);
|
||||
}
|
||||
|
||||
update(characterClassItem, pieces.join('|'));
|
||||
}
|
||||
}
|
||||
return characterClassItem;
|
||||
};
|
||||
|
||||
const assertNoUnmatchedReferences = (groups) => {
|
||||
@@ -188,12 +478,12 @@ const assertNoUnmatchedReferences = (groups) => {
|
||||
const processTerm = (item, regenerateOptions, groups) => {
|
||||
switch (item.type) {
|
||||
case 'dot':
|
||||
if (config.unicode) {
|
||||
if (config.transform.unicodeFlag) {
|
||||
update(
|
||||
item,
|
||||
getUnicodeDotSet(config.dotAll).toString(regenerateOptions)
|
||||
getUnicodeDotSet(config.flags.dotAll).toString(regenerateOptions)
|
||||
);
|
||||
} else if (config.dotAll) {
|
||||
} else if (config.transform.dotAllFlag) {
|
||||
// TODO: consider changing this at the regenerate level.
|
||||
update(item, '[\\s\\S]');
|
||||
}
|
||||
@@ -202,64 +492,90 @@ const processTerm = (item, regenerateOptions, groups) => {
|
||||
item = processCharacterClass(item, regenerateOptions);
|
||||
break;
|
||||
case 'unicodePropertyEscape':
|
||||
if (config.unicodePropertyEscape) {
|
||||
const data = getUnicodePropertyEscapeCharacterClassData(item.value, item.negative);
|
||||
if (data.maybeIncludesStrings) {
|
||||
if (!config.flags.unicodeSets) {
|
||||
throw new Error(
|
||||
'Properties of strings are only supported when using the unicodeSets (v) flag.'
|
||||
);
|
||||
}
|
||||
if (config.transform.unicodeSetsFlag) {
|
||||
data.transformed = true;
|
||||
item = processCharacterClass(item, regenerateOptions, data);
|
||||
}
|
||||
} else if (config.transform.unicodePropertyEscapes) {
|
||||
update(
|
||||
item,
|
||||
getUnicodePropertyEscapeSet(item.value, item.negative)
|
||||
.toString(regenerateOptions)
|
||||
data.singleChars.toString(regenerateOptions)
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'characterClassEscape':
|
||||
update(
|
||||
item,
|
||||
getCharacterClassEscapeSet(
|
||||
item.value,
|
||||
config.unicode,
|
||||
config.ignoreCase
|
||||
).toString(regenerateOptions)
|
||||
);
|
||||
if (config.transform.unicodeFlag) {
|
||||
update(
|
||||
item,
|
||||
getCharacterClassEscapeSet(
|
||||
item.value,
|
||||
/* config.transform.unicodeFlag implies config.flags.unicode */ true,
|
||||
config.flags.ignoreCase
|
||||
).toString(regenerateOptions)
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'group':
|
||||
if (item.behavior == 'normal') {
|
||||
groups.lastIndex++;
|
||||
}
|
||||
if (item.name && config.namedGroup) {
|
||||
if (item.name && config.transform.namedGroups) {
|
||||
const name = item.name.value;
|
||||
|
||||
if (groups.names[name]) {
|
||||
if (groups.namesConflicts[name]) {
|
||||
throw new Error(
|
||||
`Multiple groups with the same name (${ name }) are not allowed.`
|
||||
`Group '${ name }' has already been defined in this context.`
|
||||
);
|
||||
}
|
||||
groups.namesConflicts[name] = true;
|
||||
|
||||
const index = groups.lastIndex;
|
||||
delete item.name;
|
||||
|
||||
groups.names[name] = index;
|
||||
if (!groups.names[name]) {
|
||||
groups.names[name] = [];
|
||||
}
|
||||
groups.names[name].push(index);
|
||||
|
||||
if (groups.onNamedGroup) {
|
||||
groups.onNamedGroup.call(null, name, index);
|
||||
}
|
||||
|
||||
if (groups.unmatchedReferences[name]) {
|
||||
groups.unmatchedReferences[name].forEach(reference => {
|
||||
updateNamedReference(reference, index);
|
||||
});
|
||||
delete groups.unmatchedReferences[name];
|
||||
}
|
||||
}
|
||||
/* falls through */
|
||||
case 'alternative':
|
||||
case 'disjunction':
|
||||
case 'quantifier':
|
||||
item.body = item.body.map(term => {
|
||||
return processTerm(term, regenerateOptions, groups);
|
||||
});
|
||||
break;
|
||||
case 'disjunction':
|
||||
const outerNamesConflicts = groups.namesConflicts;
|
||||
item.body = item.body.map(term => {
|
||||
groups.namesConflicts = Object.create(outerNamesConflicts);
|
||||
return processTerm(term, regenerateOptions, groups);
|
||||
});
|
||||
break;
|
||||
case 'alternative':
|
||||
item.body = flatMap(item.body, term => {
|
||||
const res = processTerm(term, regenerateOptions, groups);
|
||||
// Alternatives cannot contain alternatives; flatten them.
|
||||
return res.type === 'alternative' ? res.body : res;
|
||||
});
|
||||
break;
|
||||
case 'value':
|
||||
const codePoint = item.codePoint;
|
||||
const set = regenerate(codePoint);
|
||||
if (config.ignoreCase && config.unicode && !config.useUnicodeFlag) {
|
||||
if (config.flags.ignoreCase && config.transform.unicodeFlag) {
|
||||
const folded = caseFold(codePoint);
|
||||
if (folded) {
|
||||
set.add(folded);
|
||||
@@ -270,17 +586,32 @@ const processTerm = (item, regenerateOptions, groups) => {
|
||||
case 'reference':
|
||||
if (item.name) {
|
||||
const name = item.name.value;
|
||||
const index = groups.names[name];
|
||||
if (index) {
|
||||
updateNamedReference(item, index);
|
||||
break;
|
||||
const indexes = groups.names[name];
|
||||
if (indexes) {
|
||||
const body = indexes.map(index => ({
|
||||
'type': 'reference',
|
||||
'matchIndex': index,
|
||||
'raw': '\\' + index,
|
||||
}));
|
||||
if (body.length === 1) {
|
||||
return body[0];
|
||||
}
|
||||
return {
|
||||
'type': 'alternative',
|
||||
'body': body,
|
||||
'raw': body.map(term => term.raw).join(''),
|
||||
};
|
||||
}
|
||||
|
||||
if (!groups.unmatchedReferences[name]) {
|
||||
groups.unmatchedReferences[name] = [];
|
||||
}
|
||||
// Keep track of references used before the corresponding group.
|
||||
groups.unmatchedReferences[name].push(item);
|
||||
// This named reference comes before the group where it’s defined,
|
||||
// so it’s always an empty match.
|
||||
groups.unmatchedReferences[name] = true;
|
||||
return {
|
||||
'type': 'group',
|
||||
'behavior': 'ignore',
|
||||
'body': [],
|
||||
'raw': '(?:)',
|
||||
};
|
||||
}
|
||||
break;
|
||||
case 'anchor':
|
||||
@@ -298,36 +629,97 @@ const processTerm = (item, regenerateOptions, groups) => {
|
||||
};
|
||||
|
||||
const config = {
|
||||
'ignoreCase': false,
|
||||
'unicode': false,
|
||||
'dotAll': false,
|
||||
'useUnicodeFlag': false,
|
||||
'unicodePropertyEscape': false,
|
||||
'namedGroup': false
|
||||
'flags': {
|
||||
'ignoreCase': false,
|
||||
'unicode': false,
|
||||
'unicodeSets': false,
|
||||
'dotAll': false,
|
||||
},
|
||||
'transform': {
|
||||
'dotAllFlag': false,
|
||||
'unicodeFlag': false,
|
||||
'unicodeSetsFlag': false,
|
||||
'unicodePropertyEscapes': false,
|
||||
'namedGroups': false,
|
||||
},
|
||||
get useUnicodeFlag() {
|
||||
return (this.flags.unicode || this.flags.unicodeSets) && !this.transform.unicodeFlag;
|
||||
}
|
||||
};
|
||||
|
||||
const validateOptions = (options) => {
|
||||
if (!options) return;
|
||||
|
||||
for (const key of Object.keys(options)) {
|
||||
const value = options[key];
|
||||
switch (key) {
|
||||
case 'dotAllFlag':
|
||||
case 'unicodeFlag':
|
||||
case 'unicodePropertyEscapes':
|
||||
case 'namedGroups':
|
||||
if (value != null && value !== false && value !== 'transform') {
|
||||
throw new Error(`.${key} must be false (default) or 'transform'.`);
|
||||
}
|
||||
break;
|
||||
case 'unicodeSetsFlag':
|
||||
if (value != null && value !== false && value !== 'parse' && value !== 'transform') {
|
||||
throw new Error(`.${key} must be false (default), 'parse' or 'transform'.`);
|
||||
}
|
||||
break;
|
||||
case 'onNamedGroup':
|
||||
if (value != null && typeof value !== 'function') {
|
||||
throw new Error('.onNamedGroup must be a function.');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error(`.${key} is not a valid regexpu-core option.`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const hasFlag = (flags, flag) => flags ? flags.includes(flag) : false;
|
||||
const transform = (options, name) => options ? options[name] === 'transform' : false;
|
||||
|
||||
const rewritePattern = (pattern, flags, options) => {
|
||||
config.unicode = flags && flags.includes('u');
|
||||
validateOptions(options);
|
||||
|
||||
config.flags.unicode = hasFlag(flags, 'u');
|
||||
config.flags.unicodeSets = hasFlag(flags, 'v');
|
||||
config.flags.ignoreCase = hasFlag(flags, 'i');
|
||||
config.flags.dotAll = hasFlag(flags, 's');
|
||||
|
||||
config.transform.dotAllFlag = config.flags.dotAll && transform(options, 'dotAllFlag');
|
||||
config.transform.unicodeFlag = (config.flags.unicode || config.flags.unicodeSets) && transform(options, 'unicodeFlag');
|
||||
config.transform.unicodeSetsFlag = config.flags.unicodeSets && transform(options, 'unicodeSetsFlag');
|
||||
|
||||
// unicodeFlag: 'transform' implies unicodePropertyEscapes: 'transform'
|
||||
config.transform.unicodePropertyEscapes = config.flags.unicode && (
|
||||
transform(options, 'unicodeFlag') || transform(options, 'unicodePropertyEscapes')
|
||||
);
|
||||
config.transform.namedGroups = transform(options, 'namedGroups');
|
||||
|
||||
const regjsparserFeatures = {
|
||||
'unicodePropertyEscape': config.unicode,
|
||||
'unicodeSet': Boolean(options && options.unicodeSetsFlag),
|
||||
|
||||
// Enable every stable RegExp feature by default
|
||||
'unicodePropertyEscape': true,
|
||||
'namedGroups': true,
|
||||
'lookbehind': options && options.lookbehind
|
||||
'lookbehind': true,
|
||||
};
|
||||
config.ignoreCase = flags && flags.includes('i');
|
||||
const supportDotAllFlag = options && options.dotAllFlag;
|
||||
config.dotAll = supportDotAllFlag && flags && flags.includes('s');
|
||||
config.namedGroup = options && options.namedGroup;
|
||||
config.useUnicodeFlag = options && options.useUnicodeFlag;
|
||||
config.unicodePropertyEscape = options && options.unicodePropertyEscape;
|
||||
|
||||
const regenerateOptions = {
|
||||
'hasUnicodeFlag': config.useUnicodeFlag,
|
||||
'bmpOnly': !config.unicode
|
||||
'bmpOnly': !config.flags.unicode
|
||||
};
|
||||
|
||||
const groups = {
|
||||
'onNamedGroup': options && options.onNamedGroup,
|
||||
'lastIndex': 0,
|
||||
'names': Object.create(null), // { [name]: index }
|
||||
'unmatchedReferences': Object.create(null) // { [name]: Array<reference> }
|
||||
'names': Object.create(null), // { [name]: Array<index> }
|
||||
'namesConflicts': Object.create(null), // { [name]: true }
|
||||
'unmatchedReferences': Object.create(null) // { [name]: true }
|
||||
};
|
||||
|
||||
const tree = parse(pattern, flags, regjsparserFeatures);
|
||||
// Note: `processTerm` mutates `tree` and `groups`.
|
||||
processTerm(tree, regenerateOptions, groups);
|
||||
|
||||
Reference in New Issue
Block a user