Skip to content

Commit af60cb4

Browse files
committed
New: Add support for duplicate object literal properties
1 parent 9f839b5 commit af60cb4

File tree

14 files changed

+1019
-10
lines changed

14 files changed

+1019
-10
lines changed

espree.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,12 +2445,13 @@ function parseObjectProperty() {
24452445

24462446
function parseObjectInitialiser() {
24472447
var marker = markerCreate(),
2448+
allowDuplicates = extra.ecmaFeatures.objectLiteralDuplicateProperties,
24482449
properties = [],
24492450
property,
24502451
name,
24512452
key,
24522453
kind,
2453-
map = {},
2454+
kindMap = {},
24542455
toString = String;
24552456

24562457
expect("{");
@@ -2459,34 +2460,43 @@ function parseObjectInitialiser() {
24592460
property = parseObjectProperty();
24602461

24612462
if (!property.computed) {
2463+
24622464
if (property.key.type === astNodeTypes.Identifier) {
24632465
name = property.key.name;
24642466
} else {
24652467
name = toString(property.key.value);
24662468
}
24672469

2468-
/*eslint-disable no-nested-ternary*/
2469-
kind = (property.kind === "init") ? PropertyKind.Data : (property.kind === "get") ? PropertyKind.Get : PropertyKind.Set;
2470-
/*eslint-enable no-nested-ternary*/
2470+
if (property.kind === "init") {
2471+
kind = PropertyKind.Data;
2472+
} else if (property.kind === "get") {
2473+
kind = PropertyKind.Get;
2474+
} else {
2475+
kind = PropertyKind.Set;
2476+
}
24712477

24722478
key = "$" + name;
2473-
if (Object.prototype.hasOwnProperty.call(map, key)) {
2474-
if (map[key] === PropertyKind.Data) {
2475-
if (strict && kind === PropertyKind.Data) {
2479+
if (Object.prototype.hasOwnProperty.call(kindMap, key)) {
2480+
if (kindMap[key] === PropertyKind.Data) {
2481+
if (kind === PropertyKind.Data && name === "__proto__" && allowDuplicates) {
2482+
// Duplicate '__proto__' literal properties are forbidden in ES 6
2483+
throwErrorTolerant({}, Messages.DuplicatePrototypeProperty);
2484+
} else if (kind === PropertyKind.Data && strict && !allowDuplicates) {
2485+
// Duplicate literal properties are only forbidden in ES 5 strict mode
24762486
throwErrorTolerant({}, Messages.StrictDuplicateProperty);
24772487
} else if (kind !== PropertyKind.Data) {
24782488
throwErrorTolerant({}, Messages.AccessorDataProperty);
24792489
}
24802490
} else {
24812491
if (kind === PropertyKind.Data) {
24822492
throwErrorTolerant({}, Messages.AccessorDataProperty);
2483-
} else if (map[key] & kind) {
2493+
} else if (kindMap[key] & kind) {
24842494
throwErrorTolerant({}, Messages.AccessorGetSet);
24852495
}
24862496
}
2487-
map[key] |= kind;
2497+
kindMap[key] |= kind;
24882498
} else {
2489-
map[key] = kind;
2499+
kindMap[key] = kind;
24902500
}
24912501
}
24922502

lib/features.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ module.exports = {
6666
// enable parsing of shorthand object literal properties
6767
objectLiteralShorthandProperties: false,
6868

69+
// Allow duplicate object literal properties (except '__proto__')
70+
objectLiteralDuplicateProperties: false,
71+
6972
// enable parsing of generators/yield
7073
generators: false,
7174

lib/messages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module.exports = {
6868
StrictOctalLiteral: "Octal literals are not allowed in strict mode.",
6969
StrictDelete: "Delete of an unqualified identifier in strict mode.",
7070
StrictDuplicateProperty: "Duplicate data property in object literal not allowed in strict mode",
71+
DuplicatePrototypeProperty: "Duplicate '__proto__' property in object literal are not allowed",
7172
AccessorDataProperty: "Object literal may not have data and accessor property with the same name",
7273
AccessorGetSet: "Object literal may not have multiple get/set accessors with the same name",
7374
StrictLHSAssignment: "Assignment to eval or arguments is not allowed in strict mode",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
objectLiteralDuplicateProperties: true,
3+
objectLiteralComputedProperties: true
4+
};

0 commit comments

Comments
 (0)