Skip to content

Commit

Permalink
Fix compiling v set notation to u with unicode properties (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
  • Loading branch information
3 people committed Sep 23, 2023
1 parent 91ee342 commit c24e0cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rewrite-pattern.js
Expand Up @@ -456,7 +456,7 @@ const computeCharacterClass = (characterClassItem, regenerateOptions) => {
data.transformed =
data.transformed ||
config.transform.unicodePropertyEscapes ||
(config.transform.unicodeSetsFlag && nestedData.maybeIncludesStrings);
(config.transform.unicodeSetsFlag && (nestedData.maybeIncludesStrings || characterClassItem.kind !== "union"));
break;
case 'characterClass':
const handler = item.negative ? handleNegative : handlePositive;
Expand Down Expand Up @@ -819,7 +819,7 @@ const rewritePattern = (pattern, flags, options) => {
config.transform.unicodeSetsFlag = config.flags.unicodeSets && transform(options, 'unicodeSetsFlag');

// unicodeFlag: 'transform' implies unicodePropertyEscapes: 'transform'
config.transform.unicodePropertyEscapes = config.flags.unicode && (
config.transform.unicodePropertyEscapes = (config.flags.unicode || config.flags.unicodeSets) && (
transform(options, 'unicodeFlag') || transform(options, 'unicodePropertyEscapes')
);
config.transform.namedGroups = transform(options, 'namedGroups');
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/unicode-set.js
Expand Up @@ -343,6 +343,23 @@ const unicodeSetFixtures = [
pattern: '[\\p{ASCII}&&\\p{Control}]',
expected: '[\\0-\\x1F\\x7F]',
},
{
pattern: '\\P{ASCII}',
expected: '[\\x80-\\u{10FFFF}]',
options: { unicodeSetsFlag: "transform", unicodePropertyEscapes: "transform" }
},
{
pattern: '[\\p{ASCII}\\p{Decimal_Number}]',
expected: '[\\p{ASCII}\\p{Decimal_Number}]'
},
{
pattern: '[\\p{Lowercase_Letter}]',
expected: '[\\p{Lowercase_Letter}]'
},
{
pattern: '^[\\p{Script=Arabic}&&\\p{Number}]$',
expected: '^[\\u0660-\\u0669\\u06F0-\\u06F9\\u{10E60}-\\u{10E7E}]$'
},
{
pattern: '.',
flags: 'sv',
Expand Down
2 changes: 0 additions & 2 deletions tests/tests.js
Expand Up @@ -427,8 +427,6 @@ describe('character classes', () => {
}
});



describe('unicodeSets (v) flag', () => {
// Re-use the unicode fixtures but replacing the input pattern's `u` flag with `v` flag
for (const fixture of unicodeFixtures) {
Expand Down

0 comments on commit c24e0cc

Please sign in to comment.