Skip to content

Commit

Permalink
always enable modifiers parsing (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Oct 30, 2024
1 parent 5cfd6b9 commit f745fbe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,7 @@ These options can be set to `false` or `'transform'`. When using `'transform'`,
// → '(?:(?![f-h])[\s\S])' (to be used without /u)
```

#### Experimental regular expression features

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.

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`.

- `modifiers` - [Inline `m`/`s`/`i` modifiers](https://github.com/tc39/proposal-regexp-modifiers)
- `modifiers` - [Inline `i`/`m`/`s` modifiers](https://github.com/tc39/proposal-regexp-modifiers)

```js
rewritePattern('(?i:[a-z])[a-z]', '', {
Expand All @@ -155,6 +149,12 @@ Once these features become stable (when the proposals are accepted as part of EC
// → '(?:[a-zA-Z])([a-z])'
```

#### Experimental regular expression features

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.

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`.

#### Miscellaneous options

- `onNamedGroup`
Expand Down
4 changes: 2 additions & 2 deletions rewrite-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ const validateOptions = (options) => {
throw new Error(`.${key} must be false (default) or 'transform'.`);
}
break;
// todo: remove modifiers: 'parse' in regexpu-core v7
case 'modifiers':
if (value != null && value !== false && value !== 'parse' && value !== 'transform') {
throw new Error(`.${key} must be false (default), 'parse' or 'transform'.`);
Expand Down Expand Up @@ -867,9 +868,8 @@ const rewritePattern = (pattern, flags, options) => {
config.modifiersData.m = undefined;

const regjsparserFeatures = {
'modifiers': Boolean(options && options.modifiers),

// Enable every stable RegExp feature by default
'modifiers': true,
'unicodePropertyEscape': true,
'unicodeSet': true,
'namedGroups': true,
Expand Down
17 changes: 15 additions & 2 deletions tests/fixtures/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const modifiersFixtures = [
{
'pattern': '(?i:[\\q{ab|cd|abc}--\\q{abc}--\\q{cd}])',
'flags': 'v',
'options': { unicodeSetsFlag: 'transform', modifiers: 'parse' },
'options': { unicodeSetsFlag: 'transform', modifiers: false },
'expected': '(?i:(?:ab))',
'expectedFlags': 'u',
}, {
Expand Down Expand Up @@ -200,7 +200,7 @@ const modifiersFixtures = [
{
'pattern': '(?-i:[\\q{ab|cd|abc}--\\q{abc}--\\q{cd}])',
'flags': 'iv',
'options': { unicodeSetsFlag: 'transform', modifiers: 'parse' },
'options': { unicodeSetsFlag: 'transform', modifiers: false },
'expected': '(?-i:(?:ab))',
'expectedFlags': 'iu',
}, {
Expand Down Expand Up @@ -314,6 +314,13 @@ const modifiersFixtures = [
'expected': '(?:(?:^|(?<=[\\n\\r\\u2028\\u2029]))[A-Za-z])',
'expectedFlags': '',
},
{
'pattern': '(?ims:^[a-z])',
'flags': '',
'expected': '(?ims:^[a-z])',
'expectedFlags': '',
'options': { 'modifiers': false }
},
// -ims
{
'pattern': '(?-ims:^[a-z].)(^[a-z].)',
Expand All @@ -326,6 +333,12 @@ const modifiersFixtures = [
'expected': '(?:^[a-z].)(^[a-z].)',
'expectedFlags': '',
},
{
'pattern': '(?-ims:^[a-z].)(^[a-z].)',
'expected': '(?-ims:^[a-z].)(^[a-z].)',
'expectedFlags': '',
'options': { 'modifiers': false }
},
].filter(Boolean);

exports.modifiersFixtures = modifiersFixtures;
6 changes: 0 additions & 6 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,5 @@ describe('modifiers', () => {
}
});
}

it('No `modifiers:"transform"`', () => {
assert.throws(() => {
rewritePattern('(?i:a)', '');
});
})
});

0 comments on commit f745fbe

Please sign in to comment.