81 lines
1.8 KiB
Plaintext

== Why is this an issue?
JavaScript allows duplicate property names in classes and object literals. The last occurrence will overwrite previous definitions. Therefore, having more than one occurrence will have no effect and may cause misunderstandings and bugs.
[source,javascript]
----
let data = {
"key": "value",
"1": "value",
"key": "value", // Noncompliant - duplicate of "key"
'key': "value", // Noncompliant - duplicate of "key"
key: "value", // Noncompliant - duplicate of "key"
\u006bey: "value", // Noncompliant - duplicate of "key"
"\u006bey": "value", // Noncompliant - duplicate of "key"
"\x6bey": "value", // Noncompliant - duplicate of "key"
1: "value" // Noncompliant - duplicate of "1"
}
class Foo {
bar() { }
bar() { } // Noncompliant - duplicate of "bar"
}
----
Defining a `class` with a duplicated `constructor` will generate an error.
[source,javascript]
----
class Class {
constructor() {
}
constructor(value) { // Noncompliant: A class may only have one constructor
}
}
----
Before ECMAScript 2015, using duplicate names generated an error in JavaScript strict mode code.
This rule will also report on duplicate properties in JSX.
[source,javascript]
----
function MyComponent(props) {
return <div prop={props.prop1} prop={props.prop2}> { /* Noncompliant, 'prop' is defined twice */ }
This is my component
</div>;
}
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions[Property definitions]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Duplicate name {0}.
or
No duplicate props allowed.
=== Highlighting
primary: the 2nd occurrence
secondar: the first occurrence
endif::env-github,rspecator-view[]