/
no-this-alias.test.ts
135 lines (128 loc) · 2.53 KB
/
no-this-alias.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import rule from '../../src/rules/no-this-alias';
import { RuleTester } from '../RuleTester';
const idError = {
messageId: 'thisAssignment' as const,
type: AST_NODE_TYPES.Identifier,
};
const destructureError = {
messageId: 'thisDestructure' as const,
type: AST_NODE_TYPES.ObjectPattern,
};
const arrayDestructureError = {
messageId: 'thisDestructure' as const,
type: AST_NODE_TYPES.ArrayPattern,
};
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});
ruleTester.run('no-this-alias', rule, {
valid: [
'const self = foo(this);',
{
code: `
const { props, state } = this;
const { length } = this;
const { length, toString } = this;
const [foo] = this;
const [foo, bar] = this;
`,
options: [
{
allowDestructuring: true,
},
],
},
{
code: 'const self = this;',
options: [
{
allowedNames: ['self'],
},
],
},
// https://github.com/bradzacher/eslint-plugin-typescript/issues/281
`
declare module 'foo' {
declare const aVar: string;
}
`,
],
invalid: [
{
code: 'const self = this;',
options: [
{
allowDestructuring: true,
},
],
errors: [idError],
},
{
code: 'const self = this;',
errors: [idError],
},
{
code: `
let that;
that = this;
`,
errors: [idError],
},
{
code: 'const { props, state } = this;',
options: [
{
allowDestructuring: false,
},
],
errors: [destructureError],
},
{
code: `
var unscoped = this;
function testFunction() {
let inFunction = this;
}
const testLambda = () => {
const inLambda = this;
};
`,
errors: [idError, idError, idError],
},
{
code: `
class TestClass {
constructor() {
const inConstructor = this;
const asThis: this = this;
const asString = 'this';
const asArray = [this];
const asArrayString = ['this'];
}
public act(scope: this = this) {
const inMemberFunction = this;
const { act } = this;
const { act, constructor } = this;
const [foo] = this;
const [foo, bar] = this;
}
}
`,
options: [
{
allowDestructuring: false,
},
],
errors: [
idError,
idError,
idError,
destructureError,
destructureError,
arrayDestructureError,
arrayDestructureError,
],
},
],
});