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

Add tests for proposal-duplicate-named-capturing-groups #3625

Merged
merged 4 commits into from
Aug 9, 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
4 changes: 4 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ regexp-v-flag
# https://github.com/tc39/proposal-decorators
decorators

# Duplicate named capturing groups
# https://github.com/tc39/proposal-duplicate-named-capturing-groups
regexp-duplicate-named-groups

## Standard language features
#
# Language features that have been included in a published version of the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Enumeration order of the groups object with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/


let regexp = /(?<y>a)(?<x>a)|(?<x>b)(?<y>b)/;

assert.compareArray(
Object.keys(regexp.exec("aa").groups),
["y", "x"],
"property enumeration order of the groups object is based on source order, not match order"
);

assert.compareArray(
Object.keys(regexp.exec("bb").groups),
["y", "x"],
"property enumeration order of the groups object is based on source order, not match order"
);
Comment on lines +14 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like for these messages to be distinct, but I can't come up with clear replacements that are.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: match indices with duplicate named capture groups
esid: sec-makematchindicesindexpairarray
features: [regexp-duplicate-named-groups, regexp-match-indices]
includes: [compareArray.js]
---*/

let indices = "..ab".match(/(?<x>a)|(?<x>b)/d).indices;
assert.compareArray(indices.groups.x, [2, 3]);

indices = "..ba".match(/(?<x>a)|(?<x>b)/d).indices;
assert.compareArray(indices.groups.x, [2, 3]);
17 changes: 17 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names-replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: String.prototype.replace behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
---*/

assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/, "[$<x>]"), "[a]b");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/, "[$<x>]"), "[b]a");

assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/, "[$<x>][$1][$2]"), "[a][a][]b");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/, "[$<x>][$1][$2]"), "[b][][b]a");

assert.sameValue("ab".replace(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[a][b]");
assert.sameValue("ba".replace(/(?<x>a)|(?<x>b)/g, "[$<x>]"), "[b][a]");
22 changes: 22 additions & 0 deletions test/built-ins/RegExp/named-groups/duplicate-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Matching behavior with duplicate named capture groups
esid: prod-GroupSpecifier
features: [regexp-duplicate-named-groups]
includes: [compareArray.js]
---*/

assert.compareArray(["b", "b"], "bab".match(/(?<x>a)|(?<x>b)/));
assert.compareArray(["b", "b"], "bab".match(/(?<x>b)|(?<x>a)/));

assert.compareArray(["aa", "aa", undefined], "aa".match(/(?:(?<x>a)|(?<x>b))\k<x>/));
assert.compareArray(["bb", undefined, "bb"], "bb".match(/(?:(?<x>a)|(?<x>b))\k<x>/));

let matchResult = "aabb".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/);
assert.compareArray(["aabb", undefined, "bb"], matchResult);
assert.sameValue(matchResult.groups.x, "bb");
bakkot marked this conversation as resolved.
Show resolved Hide resolved

let notMatched = "abab".match(/(?:(?:(?<x>a)|(?<x>b))\k<x>){2}/);
assert.sameValue(notMatched, null);
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: GroupSpecifiers must be unique.
description: GroupSpecifiers within one alternative must be unique.
info: |
It is a Syntax Error if Pattern contains multiple GroupSpecifiers
whose enclosed RegExpIdentifierNames have the same StringValue.
It is a Syntax Error if |Pattern| contains two distinct |GroupSpecifier|s
_x_ and _y_ for which CapturingGroupName(_x_) is the same as
CapturingGroupName(_y_) and such that CanBothParticipate(_x_, _y_) is *true*.
esid: sec-patterns-static-semantics-early-errors
negative:
phase: parse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: GroupSpecifiers must be unique.
description: GroupSpecifiers within one alternative must be unique.
info: |
It is a Syntax Error if Pattern contains multiple GroupSpecifiers
whose enclosed RegExpIdentifierNames have the same StringValue.
It is a Syntax Error if |Pattern| contains two distinct |GroupSpecifier|s
_x_ and _y_ for which CapturingGroupName(_x_) is the same as
CapturingGroupName(_y_) and such that CanBothParticipate(_x_, _y_) is *true*.
esid: sec-patterns-static-semantics-early-errors
negative:
phase: parse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: GroupSpecifiers must be unique.
description: GroupSpecifiers within one alternative must be unique.
info: |
It is a Syntax Error if Pattern contains multiple GroupSpecifiers
whose enclosed RegExpIdentifierNames have the same StringValue.
It is a Syntax Error if |Pattern| contains two distinct |GroupSpecifier|s
_x_ and _y_ for which CapturingGroupName(_x_) is the same as
CapturingGroupName(_y_) and such that CanBothParticipate(_x_, _y_) is *true*.
esid: sec-patterns-static-semantics-early-errors
negative:
phase: parse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: GroupSpecifiers must be unique.
description: GroupSpecifiers within one alternative must be unique.
info: |
It is a Syntax Error if Pattern contains multiple GroupSpecifiers
whose enclosed RegExpIdentifierNames have the same StringValue.
It is a Syntax Error if |Pattern| contains two distinct |GroupSpecifier|s
_x_ and _y_ for which CapturingGroupName(_x_) is the same as
CapturingGroupName(_y_) and such that CanBothParticipate(_x_, _y_) is *true*.
esid: sec-patterns-static-semantics-early-errors
negative:
phase: parse
Expand Down