Skip to content

Commit 627540f

Browse files
Allow duplicate function keys
Allow duplicate function keys when a modern language version (>= ES6) is selected. --------- Co-authored-by: vladimir.zhuravlev <[email protected]>
1 parent 7837398 commit 627540f

File tree

4 files changed

+82
-30
lines changed

4 files changed

+82
-30
lines changed

rhino/src/main/java/org/mozilla/javascript/Parser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,12 +3908,10 @@ private ObjectLiteral objectLiteral() throws IOException {
39083908

39093909
if (this.inUseStrictDirective
39103910
&& propertyName != null
3911-
&& !(pname instanceof ComputedPropertyKey)) {
3911+
&& !(pname instanceof ComputedPropertyKey)
3912+
&& compilerEnv.getLanguageVersion() < Context.VERSION_ES6) {
39123913
switch (entryKind) {
39133914
case PROP_ENTRY:
3914-
if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) {
3915-
break;
3916-
}
39173915
case METHOD_ENTRY:
39183916
if (getterNames.contains(propertyName)
39193917
|| setterNames.contains(propertyName)) {

rhino/src/test/java/org/mozilla/javascript/tests/DuplicateProperties.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.mozilla.javascript.tests;
2+
3+
import org.junit.Test;
4+
import org.mozilla.javascript.testutils.Utils;
5+
6+
public class DuplicatePropertiesTest {
7+
@Test
8+
public void duplicateProperty() {
9+
String script =
10+
"(function() {\n"
11+
+ "'use strict';\n"
12+
+ "const o = {foo: 1, foo: 2};\n"
13+
+ "return o.foo\n"
14+
+ "})();";
15+
16+
Utils.assertEvaluatorException_1_8(
17+
"Property \"foo\" already defined in this object literal", script);
18+
19+
Utils.assertWithAllModes_ES6(2, script);
20+
}
21+
22+
@Test
23+
public void duplicatePropertyFunction() {
24+
String script =
25+
"(function() {\n"
26+
+ "'use strict';\n"
27+
+ "var o = {\n"
28+
+ " foo: 1,\n"
29+
+ " foo() {return 'Hello';},\n"
30+
+ " foo: 2,\n"
31+
+ " foo() {return 'Hello2';},\n"
32+
+ "};\n"
33+
+ "return o.foo;\n"
34+
+ "})()();";
35+
36+
Utils.assertEvaluatorException_1_8(
37+
"Property \"foo\" already defined in this object literal", script);
38+
39+
Utils.assertWithAllModes_ES6("Hello2", script);
40+
}
41+
42+
@Test
43+
public void duplicatePropertyFunctionGetter() {
44+
String script =
45+
"(function() {\n"
46+
+ "'use strict';\n"
47+
+ "var o = {\n"
48+
+ " foo: 1,\n"
49+
+ " get foo() {return 'Hello2';},\n"
50+
+ " get foo() {return 'Hello2';},\n"
51+
+ "};\n"
52+
+ "return o.foo;\n"
53+
+ "})();";
54+
55+
Utils.assertEvaluatorException_1_8(
56+
"Property \"foo\" already defined in this object literal", script);
57+
58+
Utils.assertWithAllModes_ES6("Hello2", script);
59+
}
60+
61+
@Test
62+
public void duplicatePropertyFunctionSetter() {
63+
String script =
64+
"(function() {\n"
65+
+ "'use strict';\n"
66+
+ "var o = {\n"
67+
+ " foo: 1,\n"
68+
+ " set foo(val) {return val},\n"
69+
+ " set foo(val) {return val},\n"
70+
+ "};\n"
71+
+ "return o.foo = 'Hello2';\n"
72+
+ "})();";
73+
74+
Utils.assertEvaluatorException_1_8(
75+
"Property \"foo\" already defined in this object literal", script);
76+
77+
Utils.assertWithAllModes_ES6("Hello2", script);
78+
}
79+
}

tests/testsrc/test262.properties

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,7 +4686,7 @@ language/expressions/new 22/59 (37.29%)
46864686

46874687
~language/expressions/new.target
46884688

4689-
language/expressions/object 692/1170 (59.15%)
4689+
language/expressions/object 688/1170 (58.8%)
46904690
dstr/async-gen-meth-ary-init-iter-close.js {unsupported: [async-iteration, async]}
46914691
dstr/async-gen-meth-ary-init-iter-get-err.js {unsupported: [async-iteration]}
46924692
dstr/async-gen-meth-ary-init-iter-get-err-array-prototype.js {unsupported: [async-iteration]}
@@ -5358,10 +5358,6 @@ language/expressions/object 692/1170 (59.15%)
53585358
prop-def-id-eval-error.js non-strict
53595359
prop-def-id-eval-error-2.js non-strict
53605360
prop-dup-get-data.js strict
5361-
prop-dup-get-get.js strict
5362-
prop-dup-get-set-get.js strict
5363-
prop-dup-set-get-set.js strict
5364-
prop-dup-set-set.js strict
53655361
scope-gen-meth-body-lex-distinct.js non-strict
53665362
scope-gen-meth-param-rest-elem-var-close.js non-strict
53675363
scope-gen-meth-param-rest-elem-var-open.js non-strict

0 commit comments

Comments
 (0)