Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix atomicGroupOffsets option being ignored #92

Merged
merged 1 commit into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/redos-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,15 @@ describe('RedosDetector', () => {
'`atomicGroupOffsets` cannot be used with `downgradePattern: true`.'
);
});

it('supports `atomicGroupOffsets`', () => {
expect(
isSafePattern('(a?)a?$', {
atomicGroupOffsets: new Set([0]),
downgradePattern: false,
}).trails
).toHaveLength(0);
});
});

describe('isSafePattern', () => {
Expand Down
11 changes: 7 additions & 4 deletions src/redos-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export type IsSafeConfig = {
* The offsets of groups which should be considered atomic.
* This is an advanced option you probably never want to use.
*
* It exists because sometimes when a patten is downgraded, some of
* It exists because sometimes when a pattern is downgraded, some of
* the groups in the downgrade can be considered atomic.
*/
readonly atomicGroupOffsets?: ReadonlySet<number>;
Expand All @@ -130,7 +130,7 @@ export type IsSafeConfig = {
*
* An exception may be thrown if the pattern needed to be downgraded.
*/
readonly downgradePattern?: false;
readonly downgradePattern: false;
}
| {
readonly atomicGroupOffsets?: undefined;
Expand All @@ -142,7 +142,7 @@ export type IsSafeConfig = {
*
* You can downgrade the pattern yourself with `downgradePattern`.
*/
readonly downgradePattern: true;
readonly downgradePattern?: true;
}
);

Expand Down Expand Up @@ -292,7 +292,10 @@ export function isSafePattern(
const { pattern, atomicGroupOffsets }: PatternWithAtomicGroupOffsets =
downgradePattern
? downgradePatternFn({ pattern: inputPattern, unicode })
: { atomicGroupOffsets: new Set(), pattern: inputPattern };
: {
atomicGroupOffsets: new Set(atomicGroupOffsetsInput || []),
pattern: inputPattern,
};

const patternDowngraded = downgradePattern && inputPattern !== pattern;

Expand Down