This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Whitelist option for
no-unbound-method
(#4472)
* Add tests for unbound method whitelist * Extend no-unbound-method parameters * Merge whitelist parameters into one * Handle whitelisting typeof expression * Handle method reference in whitelisted function call * Update rule description and examples * Fix rule description * Use is* type guard from tsutils * Change parameter types and parsing * Fix error with addint elements to set * Move handling of typeof to separate option * Fix linter issue * Use expression.text instead of expression.escapedText * Fix for edge case - Ignore case when parent of call expression is not an identifier * Fix linter issue
- Loading branch information
1 parent
5670c44
commit 39201ac
Showing
4 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class C { | ||
method(x: number) {} | ||
property: () => void; | ||
template(strs: TemplateStringsArray, x: any) {} | ||
} | ||
|
||
const c = new C(); | ||
[0].forEach(c.method); | ||
~~~~~~~~ [0] | ||
[0].forEach(x => c.method(x)); | ||
[0].forEach(c.property); | ||
|
||
c.template; | ||
~~~~~~~~~~ [0] | ||
c.template`foo${0}`; | ||
String.raw`${c.template}`; | ||
~~~~~~~~~~ [0] | ||
|
||
expect(c.method).toHaveBeenCalled(); | ||
typeof c.method; | ||
|
||
test(c.method); | ||
~~~~~~~~ [0] | ||
|
||
interface I { | ||
foo(): void; | ||
bar: () => void; | ||
} | ||
declare var i: I; | ||
i.foo; | ||
~~~~~ [0] | ||
i.bar; | ||
|
||
c.method === i.foo; | ||
|
||
// OK in condition | ||
c.method ? 1 : 2; | ||
1 ? c.method : c.method; | ||
~~~~~~~~ [0] | ||
~~~~~~~~ [0] | ||
if (c.method) {} | ||
while (c.method) {} | ||
do {} while (c.method); | ||
for (c.method; c.method; c.method) {} | ||
|
||
|
||
[0].forEach(c.method || i.foo); | ||
~~~~~~~~ [0] | ||
~~~~~ [0] | ||
[0].forEach(c.method.bind(c)); | ||
|
||
<button onClick={c.method}>Click me!</button>; | ||
~~~~~~~~ [0] | ||
|
||
class Validators { | ||
static required() { | ||
return null; | ||
} | ||
static compose(...args: Function[]) {} | ||
} | ||
|
||
Validators.compose(Validators.required); | ||
~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
(condition ? expectA : expectB)(c.method); | ||
~~~~~~~~ [0] | ||
(await someObject)(c.method); | ||
~~~~~~~~ [0] | ||
(await someMethod())(c.method); | ||
~~~~~~~~ [0] | ||
|
||
[0]: Avoid referencing unbound methods which may cause unintentional scoping of 'this'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"linterOptions": { | ||
"typeCheck": true | ||
}, | ||
"rules": { | ||
"no-unbound-method": [true, { "whitelist": ["expect"], "allow-typeof": true }] | ||
} | ||
} |