Skip to content

Commit

Permalink
fix: s modifier should have precedence over flag (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Oct 11, 2024
1 parent 51de91b commit 60a4444
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions rewrite-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ const processTerm = (item, regenerateOptions, groups) => {
if (config.transform.unicodeFlag) {
update(
item,
getUnicodeDotSet(config.flags.dotAll || config.modifiersData.s).toString(regenerateOptions)
getUnicodeDotSet(config.isDotAllMode).toString(regenerateOptions)
);
} else if (config.transform.dotAllFlag || config.modifiersData.s) {
} else if ((config.modifiersData.s != null ? config.modifiersData.s : config.transform.dotAllFlag)) {
// TODO: consider changing this at the regenerate level.
update(item, '[^]');
}
Expand Down Expand Up @@ -797,6 +797,9 @@ const config = {
},
get useUnicodeFlag() {
return (this.flags.unicode || this.flags.unicodeSets) && !this.transform.unicodeFlag;
},
get isDotAllMode() {
return (this.modifiersData.s !== undefined ? this.modifiersData.s : this.flags.dotAll);
}
};

Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,29 @@ const modifiersFixtures = [
'expected': '([^](?:.))',
'expectedFlags': ''
},
{
'pattern': '(.(?-s:.))',
'flags': 's',
'options': { dotAllFlag: 'transform', modifiers: 'transform' },
'expected': '([^](?:.))',
'expectedFlags': ''
},
{
'pattern': '(.(?-s:.))',
'flags': 'su',
'options': { dotAllFlag: 'transform', modifiers: 'transform' },
'expected': '([^](?:.))',
'expectedFlags': 'u'
},
{
'pattern': '(.(?-s:.))',
'flags': 'su',
'options': { dotAllFlag: 'transform', modifiers: 'transform', unicodeFlag: 'transform' },
'expected': '((?:[\\0-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])(?:(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])))',
'expectedFlags': '',
'matches': ['\na'],
'nonMatches': ['a\n']
},
// +ims
{
'pattern': '(?ims:^[a-z])',
Expand Down

0 comments on commit 60a4444

Please sign in to comment.