CodeQL queries 1.25
Skip to end of metadata
Go to start of metadata

Name: Overwritten property

Description: If an object literal has two properties with the same name, the second property overwrites the first one, which makes the code hard to understand and error-prone.

ID: js/overwritten-property

Kind: problem

Severity: error

Precision: very-high

Query: UniquePropertyNames.ql
/**
 * @name Overwritten property
 * @description If an object literal has two properties with the same name,
 *              the second property overwrites the first one,
 *              which makes the code hard to understand and error-prone.
 * @kind problem
 * @problem.severity error
 * @id js/overwritten-property
 * @tags reliability
 *       correctness
 *       external/cwe/cwe-563
 * @precision very-high
 */

import Expressions.Clones

/**
 * Holds if `p` is the `i`th property of object expression `o`, and its
 * kind is `kind`.
 *
 * The kind is an integer value indicating whether `p` is a value property (0),
 * a getter (1) or a setter (2).
 */
predicate hasProperty(ObjectExpr o, Property p, string name, int kind, int i) {
  p = o.getProperty(i) and
  name = p.getName() and
  kind = p.getKind()
}

/**
 * Holds if property `p` appears before property `q` in the same object literal and
 * both have the same name and kind, but are not structurally identical.
 */
predicate overwrittenBy(Property p, Property q) {
  exists(ObjectExpr o, string name, int i, int j, int kind |
    hasProperty(o, p, name, kind, i) and
    hasProperty(o, q, name, kind, j) and
    i < j
  ) and
  // exclude structurally identical properties: they are flagged by
  // the DuplicateProperty query
  not p.getInit().(DuplicatePropertyInitDetector).same(q.getInit())
}

from Property p, Property q
where
  overwrittenBy(p, q) and
  // ensure that `q` is the last property with the same name as `p`
  not overwrittenBy(q, _)
select p, "This property is overwritten by $@ in the same object literal.", q, "another property"

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. In particular, if the last definition assigns a different value from earlier definitions, the earlier value is lost, which is most likely unintentional and should be avoided.

Recommendation

Rename the properties to have different names.

Example

In the following example, the object literal assigned to point has two definitions of property x. The object's use in function dist suggests that the second definition should define a property y instead, and should be renamed.

function dist(p) {
	return Math.sqrt(p.x*p.x+p.y*p.y);
}

var point = {
	x: 3,
	x: 4
};
console.log(dist(point));

References
  • Ecma International, ECMAScript Language Definition, 5.1 Edition, Annex C. ECMA, 2011.
  • Ecma International, ECMAScript Language Definition, 6th Edition, Annex E. ECMA, 2015.
  • Common Weakness Enumeration: CWE-563.