← 返回首页
Duplicate property — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Duplicate property

ID: js/duplicate-property Kind: problem Security severity: Severity: warning Precision: very-high Tags: - quality - maintainability - readability - external/cwe/cwe-563 Query suites: - javascript-code-quality.qls - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

In ECMAScript 2015 and above, as well as ECMAScript 5 non-strict mode, an object literal may define the same property multiple times, with later definitions overwriting earlier ones. If all definitions assign the same value to the property, this will not to lead to problems at runtime, but it makes the code harder to read and maintain.

Recommendation

Eliminate the spurious redefinition.

Example

In the following example, the object literal passed to method css has two definitions of property backgroundColor, both setting it to value "orange".

$(".alert").css({ backgroundColor: "orange", fontWeight: "bold", backgroundColor: "orange" });

The second definition is spurious and should be removed:

$(".alert").css({ backgroundColor: "orange", fontWeight: "bold" });

References