
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
77 lines
1.2 KiB
Plaintext
77 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Assigning ``++this++`` to a local variable is a way to reference parent context inside inner functions. In TypeScript when using arrow functions this happens automatically.
|
|
|
|
This rule raises an issue when ``++this++`` is assigned to a local variable.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
function Foo() {
|
|
let that = this; // Noncompliant
|
|
that.val = 0;
|
|
|
|
setInterval(function() {
|
|
that.val++;
|
|
}, 1000);
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
function Foo() {
|
|
this.val = 0;
|
|
|
|
setInterval(() => {
|
|
this.val++;
|
|
}, 1000);
|
|
}
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
This rule ignores ``++this++`` used for destructuring.
|
|
|
|
[source,javascript]
|
|
----
|
|
const { foo, bar } = this;
|
|
----
|
|
|
|
The rule also ignores alias references of ``++this++`` in generators.
|
|
|
|
[source,javascript]
|
|
----
|
|
const self = this;
|
|
return function* () {
|
|
let result = self.next();
|
|
while (!result.done) {
|
|
yield result.value;
|
|
result = self.next();
|
|
}
|
|
};
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Refactor this code to use an arrow function instead of assigning "this".
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Assignment statement
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|