diff --git a/README.md b/README.md
index d2e99b4..102187f 100644
--- a/README.md
+++ b/README.md
@@ -77,8 +77,10 @@ type OnigurumaToEsOptions = {
global?: boolean;
hasIndices?: boolean;
maxRecursionDepth?: number | null;
+ overrides?: {
+ allowOrphanBackrefs?: boolean;
+ };
target?: 'auto' | 'ES2025' | 'ES2024' | 'ES2018';
- tmGrammar?: boolean;
verbose?: boolean;
};
```
@@ -215,6 +217,12 @@ Since recursion isn't infinite-depth like in Oniguruma, use of recursion also re
Using a high limit has a small impact on performance. Generally, this is only a problem if the regex has an existing issue with runaway backtracking that recursion exacerbates. Higher limits have no effect on regexes that don't use recursion, so you should feel free to increase this if helpful.
+### `overrides`
+
+Advanced options that take precedence over standard error checking and flags.
+
+- `allowOrphanBackrefs`: Useful with TextMate grammar processors that merge backreferences across `begin` and `end` patterns.
+
### `target`
One of `'auto'` *(default)*, `'ES2025'`, `'ES2024'`, or `'ES2018'`.
@@ -235,12 +243,6 @@ JavaScript version used for generated regexes. Using `auto` detects the best val
- Generated regexes might use features that require Node.js 23 or a 2024-era browser (except Safari, which lacks support for flag groups).
-### `tmGrammar`
-
-*Default: `false`.*
-
-Leave disabled unless the regex will be used in a TextMate grammar processor that merges backreferences across `begin` and `end` patterns.
-
### `verbose`
*Default: `false`.*
@@ -940,7 +942,7 @@ The following features don't yet have any support, and throw errors. They're all
- Grapheme boundaries: \y
, \Y
.
- Grapheme boundary options (flags y{g}
, y{w}
).
- Whole-pattern options: don't capture (?C)
, ignore-care is ASCII (?I)
, find longest (?L)
.
-- Absent repeater (?~…)
, expression (?~|…|…)
, and range cutter (?~|…)
.
+- Absent repeater (?\~…)
, expression (?\~|…|…)
, and range cutter (?\~|…)
.
- Conditionals: (?(…)…)
, (?(…)…|…)
.
- Code point sequences: \x{H H …H}
, \o{O O …O}
.
- Additional, extremely rare ways to specify characters.
diff --git a/demo/demo.css b/demo/demo.css
index b2a6081..821b95c 100644
--- a/demo/demo.css
+++ b/demo/demo.css
@@ -111,8 +111,8 @@ label .tip-lg {
}
label .tip-xl {
- width: 320px;
- margin-left: -160px;
+ width: 300px;
+ margin-left: -150px;
}
label .tip :is(code, kbd) {
diff --git a/demo/demo.js b/demo/demo.js
index 9a83ef6..6c92f1d 100644
--- a/demo/demo.js
+++ b/demo/demo.js
@@ -20,6 +20,9 @@ const state = {
global: getValue('option-global'),
hasIndices: getValue('option-hasIndices'),
maxRecursionDepth: getValue('option-maxRecursionDepth'),
+ overrides: {
+ allowOrphanBackrefs: getValue('option-allowOrphanBackrefs'),
+ },
target: getValue('option-target'),
verbose: getValue('option-verbose'),
},
@@ -218,3 +221,8 @@ function setOption(option, value) {
state.opts[option] = value;
showTranspiled();
}
+
+function setOverride(option, value) {
+ state.opts.overrides[option] = value;
+ showTranspiled();
+}
diff --git a/demo/index.html b/demo/index.html
index 352132b..8304ff9 100644
--- a/demo/index.html
+++ b/demo/index.html
@@ -118,9 +118,9 @@
diff --git a/src/index.js b/src/index.js index c14b876..48d9ce2 100644 --- a/src/index.js +++ b/src/index.js @@ -28,8 +28,10 @@ import {recursion} from 'regex-recursion'; global?: boolean; hasIndices?: boolean; maxRecursionDepth?: number | null; + overrides?: { + allowOrphanBackrefs?: boolean; + }; target?: keyof Target; - tmGrammar?: boolean; verbose?: boolean; }} OnigurumaToEsOptions */ @@ -48,7 +50,7 @@ function toDetails(pattern, options) { const opts = getOptions(options); const tokenized = tokenize(pattern, opts.flags); const onigurumaAst = parse(tokenized, { - skipBackrefValidation: opts.tmGrammar, + skipBackrefValidation: opts.overrides.allowOrphanBackrefs, verbose: opts.verbose, }); const regexAst = transform(onigurumaAst, { diff --git a/src/options.js b/src/options.js index 7c21255..5e9bbf5 100644 --- a/src/options.js +++ b/src/options.js @@ -49,12 +49,16 @@ function getOptions(options) { // your environment. Later targets allow faster processing, simpler generated source, and // support for additional features. target: 'auto', - // Leave disabled unless the regex will be used in a TextMate grammar processor that merges - // backreferences across `begin` and `end` patterns. - tmGrammar: false, // Disables optimizations that simplify the pattern when it doesn't change the meaning. verbose: false, ...options, + // Advanced options that take precedence over standard error checking and flags. + overrides: { + // Useful with TextMate grammar processors that merge backreferences across `begin` and `end` + // patterns. + allowOrphanBackrefs: false, + ...(options?.overrides), + }, }; if (opts.target === 'auto') { opts.target = (envSupportsDuplicateNames && envSupportsFlagGroups) ? diff --git a/src/transform.js b/src/transform.js index 05f801b..a6b186a 100644 --- a/src/transform.js +++ b/src/transform.js @@ -505,7 +505,7 @@ const ThirdPassVisitor = { Backreference({node, replaceWith}, state) { if (node.orphan) { state.highestOrphanBackref = Math.max(state.highestOrphanBackref, node.ref); - // Don't renumber; used with option `tmGrammar` + // Don't renumber; used with `allowOrphanBackrefs` return; } const reffedNodes = state.reffedNodesByReferencer.get(node); @@ -566,7 +566,7 @@ const ThirdPassVisitor = { // exist within `end`. This presents a dilemma since both Oniguruma and JS (with flag u/v) // error for backrefs to undefined captures. So adding captures to the end is a solution that // doesn't change what the regex matches, and lets invalid numbered backrefs through. Note: - // Orphan backrefs are only allowed if the `tmGrammar` option is used + // Orphan backrefs are only allowed if `allowOrphanBackrefs` is enabled const numCapsNeeded = Math.max(state.highestOrphanBackref - state.numCapturesToLeft, 0); for (let i = 0; i < numCapsNeeded; i++) { const emptyCapture = createCapturingGroup();