Skip to content

Commit

Permalink
Backcompat: Don't throw on doubly degated P with flag i, since it's n…
Browse files Browse the repository at this point in the history
…ot always problematic and new option unicodeSetsPlugin allows handling it fully if needed (closes #10)
  • Loading branch information
slevithan committed Aug 2, 2024
1 parent 4145d25 commit 3884779
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 6 additions & 4 deletions spec/backcompat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('backcompat', () => {
expect(() => regex({disable: {v: true}})`[\d-a]`).toThrow();
});

it('should allow unescaped { and } in character classes when part of a valid token', () => {
expect('a').toMatch(regex({disable: {v: true}})`^[\u{61}]$`);
expect('a').toMatch(regex({disable: {v: true}})`^[\p{L}]$`);
expect('a').toMatch(regex({disable: {v: true}})`^[^\P{L}]$`);
});

it('should allow escaping characters in character classes when escapes are permitted by flag v but invalid without it', () => {
expect('&').toMatch(regex({disable: {v: true}})`^[\&]$`);
expect('!').toMatch(regex({disable: {v: true}})`^[\!]$`);
Expand Down Expand Up @@ -69,8 +75,4 @@ describe('backcompat', () => {
expect(() => regex({disable: {v: true}})`[^[]]`).toThrow();
expect(() => regex({disable: {v: true}})`[]]`).toThrow();
});

it('should throw for doubly negated sets with flag i when flag v unavailable', () => {
expect(() => regex({disable: {v: true}, flags: 'i'})`[^\P{Ll}]`).toThrow();
});
});
8 changes: 2 additions & 6 deletions src/backcompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ Assumes flag u and doesn't worry about syntax errors that are caught by it.
@param {string} flags
@returns {string}
*/
export function backcompatPlugin(expression, flags) {
export function backcompatPlugin(expression) {
const unescapedLiteralHyphenMsg = 'Invalid unescaped "-" in character class';
let inCharClass = false;
let inNegatedCharClass = false;
let result = '';
for (const {0: m, groups: {dp, vOnlyEscape}} of expression.matchAll(token)) {
if (m[0] === '[') {
Expand All @@ -32,21 +31,18 @@ export function backcompatPlugin(expression, flags) {
throw new Error(unescapedLiteralHyphenMsg);
}
inCharClass = true;
inNegatedCharClass = m[1] === '^';
} else if (m.endsWith(']')) {
if (m[0] === '-') {
throw new Error(unescapedLiteralHyphenMsg);
}
inCharClass = inNegatedCharClass = false;
inCharClass = false;
} else if (inCharClass) {
if (m === '&&' || m === '--') {
throw new Error(`Invalid set operator "${m}" when flag v not supported`);
} else if (dp) {
throw new Error(`Invalid double punctuator "${m}", reserved by flag v`);
} else if ('(){}/|'.includes(m)) {
throw new Error(`Invalid unescaped "${m}" in character class`);
} else if (inNegatedCharClass && m.startsWith('\\P') && flags.includes('i')) {
throw new Error('Negated \\P in negated character class with flag i works differently with flag v');
} else if (vOnlyEscape) {
// Remove the escaping backslash to emulate flag v rules, since this character is allowed
// to be escaped within character classes with flag v but not with flag u
Expand Down

0 comments on commit 3884779

Please sign in to comment.