From 65e254deac95717877e89dca2a5432fcd2196625 Mon Sep 17 00:00:00 2001 From: Steven Levithan Date: Fri, 22 Nov 2024 14:17:59 +0100 Subject: [PATCH] Fix escaping in nested classes in verbose mode --- README.md | 4 ++-- src/generate.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e4b33bc..aed9380 100644 --- a/README.md +++ b/README.md @@ -145,8 +145,8 @@ One of `'default'` *(default)* or `'strict'`. Sets the level of emulation rigor/strictness. -- `default`: Permits a few close approximations in order to support additional features. -- `strict`: Throw if the pattern can't be emulated with identical behavior (even in rare edge cases) for the given `target`. +- **Default:** Permits a few close approximations in order to support additional features. +- **Strict:** Error if the pattern can't be emulated with identical behavior (even in rare edge cases) for the given `target`.
More details diff --git a/src/generate.js b/src/generate.js index 92a9902..a4a5904 100644 --- a/src/generate.js +++ b/src/generate.js @@ -296,8 +296,13 @@ 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 = `[${negate ? '^' : ''}${elements.map(gen).join('')}]`; + const result = genClass(); state.inCharClass = false; return result; }