Skip to content

Commit

Permalink
Fix intersection with directly nested classes with multiple elements …
Browse files Browse the repository at this point in the history
…in non-verbose mode
  • Loading branch information
slevithan committed Nov 22, 2024
1 parent 65e254d commit d74c76e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,27 @@ function genCharacter({value}, state) {
}

function genCharacterClass({negate, parent, elements}, state, gen) {
const genClass = () => `[${negate ? '^' : ''}${elements.map(gen).join('')}]`;
if (!state.inCharClass) {
// For the outermost char class, set state
state.inCharClass = true;
const result = genClass();
state.inCharClass = false;
return result;
}
const firstType = elements[0].type;
if (
!negate &&
( // Allows many nested classes to work with `target` ES2018 which doesn't support nesting
(!state.useFlagV || !state.verbose) &&
parent.type === AstTypes.CharacterClass &&
elements[0].type !== AstTypes.CharacterClassIntersection
firstType !== AstTypes.CharacterClassIntersection
) ||
( !state.verbose &&
parent.type === AstTypes.CharacterClassIntersection &&
elements.length === 1 &&
elements[0].type !== AstTypes.CharacterClassRange
firstType !== AstTypes.CharacterClass &&
firstType !== AstTypes.CharacterClassRange
)
) {
// Remove unnecessary nesting; unwrap kids into the parent char class. Some basic char class
Expand All @@ -296,15 +306,7 @@ function genCharacterClass({negate, parent, elements}, state, gen) {
if (!state.useFlagV && parent.type === AstTypes.CharacterClass) {
throw new Error('Use of nested character class requires min target ES2024');
}
const genClass = () => `[${negate ? '^' : ''}${elements.map(gen).join('')}]`;
if (state.inCharClass) {
return genClass();
}
// For the outermost char class, set state
state.inCharClass = true;
const result = genClass();
state.inCharClass = false;
return result;
return genClass();
}

function genCharacterClassRange(node, state) {
Expand Down

0 comments on commit d74c76e

Please sign in to comment.