Duplicate Class Member
ID |
javascript.no_dupe_class_members |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
dead-code, reliability |
Description
Reports class declarations that contain two methods (or static methods, getters, or setters) with the same name in the same slot. The later definition silently shadows the earlier one — JavaScript does not warn — so calls bind to whichever happens to come last.
Rationale
-
The duplicate is almost always a copy-paste leftover or an unfinished refactor.
-
The bug is invisible until someone removes the second definition and the original behaviour reappears.
-
Static and instance members with the same name are not a duplicate — they live in different slots.
-
Getters and setters can share a name with each other (that’s how accessor pairs are written) but shadow regular methods of the same name; the rule treats them as separate slots.
// Bad - the first foo() is dead
class C {
foo() { return 1; }
foo() { return 2; }
}
Remediation
Rename one of the methods, or remove the duplicate.
// Good
class C {
fooLegacy() { return 1; }
foo() { return 2; }
}