Skip to content

Commit

Permalink
Don't add flag y for unsupported \G when using allowUnhandledGAnchors
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Dec 19, 2024
1 parent 38699b2 commit 3ee05b2
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ Using a high limit has a small impact on performance. Generally, this is only a
Advanced pattern options that override standard error checking and flags when enabled.

- `allowOrphanBackrefs`: Useful with TextMate grammars that merge backreferences across patterns.
- `allowUnhandledGAnchors`: Applies flag `y` for unsupported uses of `\G`, rather than erroring.
- Oniguruma-To-ES uses a variety of strategies to accurately emulate many common uses of `\G`. When using this option, if a `\G` is found that doesn't have a known emulation strategy, the `\G` is simply removed and JavaScript's `y` (`sticky`) flag is added. This might lead to some false positives and negatives, but is useful for non-critical matching (like syntax highlighting) when having some mismatches is better than not working.
- `allowUnhandledGAnchors`: Removes unsupported uses of `\G`, rather than erroring.
- Oniguruma-To-ES uses a variety of strategies to accurately emulate many common uses of `\G`. When using this option, if a `\G` is found that doesn't have a known emulation strategy, the `\G` is simply removed. This might lead to some false positive matches, but is useful for non-critical matching (like syntax highlighting) when having some mismatches is better than not working.
- `asciiWordBoundaries`: Use ASCII-based `\b` and `\B`, which increases search performance of generated regexes.
- `captureGroup`: Oniguruma option `ONIG_OPTION_CAPTURE_GROUP`. Unnamed captures and numbered calls allowed when using named capture.

Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ <h2>Try it</h2>
<label>
<input type="checkbox" id="option-allowUnhandledGAnchors" onchange="setRule('allowUnhandledGAnchors', this.checked)">
<code>allowUnhandledGAnchors</code>
<span class="tip tip-xl">Applies flag <code>y</code> for unsupported uses of <code>\G</code>, rather than erroring</span>
<span class="tip tip-lg">Removes unsupported uses of <code>\G</code>, rather than erroring</span>
</label>
</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion spec/match-search-start.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Assertion: Search start', () => {
];
patterns.forEach(pattern => {
expect(() => toDetails(pattern)).toThrow();
expect(toRegExp(pattern, {rules: {allowUnhandledGAnchors: true}}).sticky).toBe(true);
expect(() => toDetails(pattern, {rules: {allowUnhandledGAnchors: true}})).not.toThrow();
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getOptions(options) {
rules: {
// Useful with TextMate grammars that merge backreferences across patterns.
allowOrphanBackrefs: false,
// Applies flag `y` for unsupported uses of `\G`, rather than erroring.
// Removes unsupported uses of `\G`, rather than erroring.
allowUnhandledGAnchors: false,
// Use ASCII-based `\b` and `\B`, which increases search performance of generated regexes.
asciiWordBoundaries: false,
Expand Down
5 changes: 3 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ const FirstPassVisitor = {
// string-terminating line feed
replaceWith(parseFragment(r`(?<=\A|\n(?!\z))`));
} else if (kind === AstAssertionKinds.search_start) {
if (!supportedGNodes.has(node) && !allowUnhandledGAnchors) {
if (supportedGNodes.has(node)) {
ast.flags.sticky = true;
} else if (!allowUnhandledGAnchors) {
throw new Error(r`Uses "\G" in a way that's unsupported`);
}
ast.flags.sticky = true;
remove();
} else if (kind === AstAssertionKinds.string_end_newline) {
replaceWith(parseFragment(r`(?=\n?\z)`));
Expand Down

0 comments on commit 3ee05b2

Please sign in to comment.