
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
112 lines
2.6 KiB
Plaintext
112 lines
2.6 KiB
Plaintext
== Why is this an issue?
|
|
Getters and setters provide a way to enforce encapsulation by providing methods that give controlled access to class fields. However, in classes with multiple fields, it is not unusual that copy and paste is used to quickly create the needed getters and setters, which can result in the wrong field being accessed by a getter or setter.
|
|
|
|
This rule raises an issue in the following cases:
|
|
|
|
* A setter does not update the field with the corresponding name (if it exists).
|
|
* A getter:
|
|
** does not return any value
|
|
** does not access the field with the corresponding name (if it exists).
|
|
|
|
Underscore prefixes for fields are supported, so `setX()` can assign a value to ``++_x++``.
|
|
|
|
The following type of getters and setters are supported:
|
|
|
|
* `getX()` and `setX()`
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
class A {
|
|
#y: number = 0;
|
|
setY(val: number) { // Noncompliant: field '#y' is not updated
|
|
}
|
|
}
|
|
----
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
class A {
|
|
#y: number = 0;
|
|
setY(val: number) {
|
|
this.#y = val;
|
|
}
|
|
}
|
|
----
|
|
* `get x()` and `set x()`
|
|
|
|
[source,javascript,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
class A {
|
|
_x: number = 0;
|
|
#y: number = 0;
|
|
|
|
get x() { // Noncompliant: field '_x' is not used in the return value
|
|
return this.#y;
|
|
}
|
|
|
|
get y() { // Noncompliant: method may not return any value
|
|
if (condition) {
|
|
return #y;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
[source,javascript,diff-id=2,diff-type=compliant]
|
|
----
|
|
class A {
|
|
_x: number = 0;
|
|
#y: number = 0;
|
|
|
|
get x() {
|
|
return this._x;
|
|
}
|
|
get y() {
|
|
if (condition) {
|
|
return #y;
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
----
|
|
* getters and setters defined with `Object.defineProperty()`
|
|
|
|
[source,javascript,diff-id=3,diff-type=noncompliant]
|
|
----
|
|
let x = 0;
|
|
let y = 0;
|
|
Object.defineProperty(o, 'x', {
|
|
get() { // Noncompliant: variable 'x' is not used in the return value
|
|
return y;
|
|
}
|
|
});
|
|
----
|
|
[source,javascript,diff-id=3,diff-type=compliant]
|
|
----
|
|
let x = 0;
|
|
let y = 0;
|
|
Object.defineProperty(o, 'x', {
|
|
get() {
|
|
return x;
|
|
}
|
|
});
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get[get]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set[set]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields[Private class features]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|