Skip to content

fix: not throw error when namedGroup is not enabled #41

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Setting this option to `true` enables support for [named capture groups](https:/

```js
rewritePattern('(?<name>.)\k<name>', '', {
'namedGroups': true
'namedGroup': true
});
// → '(.)\1'
```
Expand All @@ -115,7 +115,7 @@ the name of the group, and its index.

```js
rewritePattern('(?<name>.)\k<name>', '', {
'namedGroups': true,
'namedGroup': true,
onNamedGroup(name, index) {
console.log(name, index);
// → 'name', 1
Expand Down
8 changes: 8 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ console.log(processedPattern);

// throws
new RegExp(processedPattern, 'u');

console.log(rewritePattern('(?<name>.)\\k<name>', '', {
'namedGroup': true
}))

console.log(rewritePattern('(?<name>.)\\k<name>', '', {
'namedGroup': false
}))
12 changes: 8 additions & 4 deletions rewrite-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ const processCharacterClass = (characterClassItem, regenerateOptions) => {
};

const updateNamedReference = (item, index) => {
delete item.name;
item.matchIndex = index;
if (config.namedGroup) {
delete item.name;
item.matchIndex = index;
}
};

const assertNoUnmatchedReferences = (groups) => {
Expand Down Expand Up @@ -224,7 +226,7 @@ const processTerm = (item, regenerateOptions, groups) => {
if (item.behavior == 'normal') {
groups.lastIndex++;
}
if (item.name && config.namedGroup) {
if (item.name) {
const name = item.name.value;

if (groups.names[name]) {
Expand All @@ -234,7 +236,9 @@ const processTerm = (item, regenerateOptions, groups) => {
}

const index = groups.lastIndex;
delete item.name;
if (config.namedGroup) {
delete item.name;
}

groups.names[name] = index;
if (groups.onNamedGroup) {
Expand Down
32 changes: 32 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,38 @@ describe('namedGroup', () => {
});
assert.strictEqual(expected, transpiled);
})

it('should not throw when namedGroup is not enabled', () => {
let transpiled;
const expected = '(?<name>.)\\k<name>';
assert.doesNotThrow(() => {
transpiled = rewritePattern('(?<name>.)\\k<name>', '');
});
assert.strictEqual(expected, transpiled);
})

it('should not throw when namedGroup is true', () => {
let transpiled;
const expected = '(.)\\1';
assert.doesNotThrow(() => {
transpiled = rewritePattern('(?<name>.)\\k<name>', '', {
namedGroup: true
});
});
assert.strictEqual(expected, transpiled);
})

it('should not throw when namedGroup is false', () => {
let transpiled;
const expected = '(?<name>.)\\k<name>';
assert.doesNotThrow(() => {
transpiled = rewritePattern('(?<name>.)\\k<name>', '', {
namedGroup: false
});
});
assert.strictEqual(expected, transpiled);
})

});

const lookbehindFixtures = [
Expand Down