Skip to content

Commit f44783c

Browse files
armano2bradzacher
authored andcommitted
test(eslint-plugin): add test cases for eslint rules (typescript-eslint#240)
* fix(eslint-plugin): add test cases for eslint rules * fix(eslint-plugin): fix merge conflicts * chore(eslint-plugin): fix formatting
1 parent c680cbc commit f44783c

File tree

4 files changed

+266
-37
lines changed

4 files changed

+266
-37
lines changed

packages/eslint-plugin/tests/RuleTester.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ interface ValidTestCase<TOptions extends Readonly<any[]>> {
1212
settings?: Record<string, any>;
1313
parser?: string;
1414
globals?: Record<string, boolean>;
15+
env?: {
16+
browser?: boolean;
17+
};
1518
}
1619

1720
interface InvalidTestCase<
Lines changed: 246 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
import rule from 'eslint/lib/rules/no-redeclare';
2+
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
23
import { RuleTester } from '../RuleTester';
34

45
const ruleTester = new RuleTester({
56
parserOptions: {
6-
ecmaVersion: 6,
7-
sourceType: 'module',
8-
ecmaFeatures: {}
7+
ecmaVersion: 6
98
},
109
parser: '@typescript-eslint/parser'
1110
});
1211

1312
ruleTester.run('no-redeclare', rule, {
1413
valid: [
14+
'var a = 3; var b = function() { var a = 10; };',
15+
'var a = 3; a = 10;',
16+
{
17+
code: 'if (true) {\n let b = 2;\n} else { \nlet b = 3;\n}',
18+
parserOptions: {
19+
ecmaVersion: 6
20+
}
21+
},
22+
'var Object = 0;',
23+
{ code: 'var Object = 0;', options: [{ builtinGlobals: false }] },
24+
{
25+
code: 'var Object = 0;',
26+
options: [{ builtinGlobals: true }],
27+
parserOptions: { sourceType: 'module' }
28+
},
29+
{
30+
code: 'var Object = 0;',
31+
options: [{ builtinGlobals: true }],
32+
parserOptions: { ecmaFeatures: { globalReturn: true } }
33+
},
34+
{ code: 'var top = 0;', env: { browser: true } },
35+
{ code: 'var top = 0;', options: [{ builtinGlobals: true }] },
36+
{
37+
code: 'var top = 0;',
38+
options: [{ builtinGlobals: true }],
39+
parserOptions: { ecmaFeatures: { globalReturn: true } },
40+
env: { browser: true }
41+
},
42+
{
43+
code: 'var top = 0;',
44+
options: [{ builtinGlobals: true }],
45+
parserOptions: { sourceType: 'module' },
46+
env: { browser: true }
47+
},
48+
{
49+
code: 'var self = 1',
50+
options: [{ builtinGlobals: true }],
51+
env: { browser: false }
52+
},
1553
// https://github.com/eslint/typescript-eslint-parser/issues/443
1654
`
1755
const Foo = 1;
@@ -22,7 +60,211 @@ type Foo = 1;
2260
function foo({ bar }: { bar: string }) {
2361
console.log(bar);
2462
}
63+
`,
64+
`
65+
type AST<T extends ParserOptions> = TSESTree.Program &
66+
(T['range'] extends true ? { range: [number, number] } : {}) &
67+
(T['tokens'] extends true ? { tokens: TSESTree.Token[] } : {}) &
68+
(T['comment'] extends true ? { comments: TSESTree.Comment[] } : {});
69+
interface ParseAndGenerateServicesResult<T extends ParserOptions> {
70+
ast: AST<T>;
71+
services: ParserServices;
72+
}
73+
`,
74+
`
75+
function A<T>() {}
76+
interface B<T> {}
77+
type C<T> = Array<T>
78+
class D<T> {}
2579
`
2680
],
27-
invalid: []
81+
invalid: [
82+
{
83+
code: 'var a = 3; var a = 10;',
84+
parserOptions: { ecmaVersion: 6 },
85+
errors: [
86+
{
87+
message: "'a' is already defined.",
88+
type: AST_NODE_TYPES.Identifier
89+
} as any
90+
]
91+
},
92+
{
93+
code: 'switch(foo) { case a: var b = 3;\ncase b: var b = 4}',
94+
errors: [
95+
{
96+
message: "'b' is already defined.",
97+
type: AST_NODE_TYPES.Identifier
98+
} as any
99+
]
100+
},
101+
{
102+
code: 'var a = 3; var a = 10;',
103+
errors: [
104+
{
105+
message: "'a' is already defined.",
106+
type: AST_NODE_TYPES.Identifier
107+
} as any
108+
]
109+
},
110+
{
111+
code: 'var a = {}; var a = [];',
112+
errors: [
113+
{
114+
message: "'a' is already defined.",
115+
type: AST_NODE_TYPES.Identifier
116+
} as any
117+
]
118+
},
119+
{
120+
code: 'var a; function a() {}',
121+
errors: [
122+
{
123+
message: "'a' is already defined.",
124+
type: AST_NODE_TYPES.Identifier
125+
} as any
126+
]
127+
},
128+
{
129+
code: 'function a() {} function a() {}',
130+
errors: [
131+
{
132+
message: "'a' is already defined.",
133+
type: AST_NODE_TYPES.Identifier
134+
} as any
135+
]
136+
},
137+
{
138+
code: 'var a = function() { }; var a = function() { }',
139+
errors: [
140+
{
141+
message: "'a' is already defined.",
142+
type: AST_NODE_TYPES.Identifier
143+
} as any
144+
]
145+
},
146+
{
147+
code: 'var a = function() { }; var a = new Date();',
148+
errors: [
149+
{
150+
message: "'a' is already defined.",
151+
type: AST_NODE_TYPES.Identifier
152+
} as any
153+
]
154+
},
155+
{
156+
code: 'var a = 3; var a = 10; var a = 15;',
157+
errors: [
158+
{
159+
message: "'a' is already defined.",
160+
type: AST_NODE_TYPES.Identifier
161+
} as any,
162+
{
163+
message: "'a' is already defined.",
164+
type: AST_NODE_TYPES.Identifier
165+
} as any
166+
]
167+
},
168+
{
169+
code: 'var a; var a;',
170+
parserOptions: { sourceType: 'module' },
171+
errors: [
172+
{
173+
message: "'a' is already defined.",
174+
type: AST_NODE_TYPES.Identifier
175+
} as any
176+
]
177+
},
178+
{
179+
code: 'export var a; var a;',
180+
parserOptions: { sourceType: 'module' },
181+
errors: [
182+
{
183+
message: "'a' is already defined.",
184+
type: AST_NODE_TYPES.Identifier
185+
} as any
186+
]
187+
},
188+
{
189+
code: 'var Object = 0;',
190+
options: [{ builtinGlobals: true }],
191+
errors: [
192+
{
193+
message: "'Object' is already defined.",
194+
type: AST_NODE_TYPES.Identifier
195+
} as any
196+
]
197+
},
198+
{
199+
code: 'var top = 0;',
200+
options: [{ builtinGlobals: true }],
201+
errors: [
202+
{
203+
message: "'top' is already defined.",
204+
type: AST_NODE_TYPES.Identifier
205+
} as any
206+
],
207+
env: { browser: true }
208+
},
209+
{
210+
code: 'var a; var {a = 0, b: Object = 0} = {};',
211+
options: [{ builtinGlobals: true }],
212+
parserOptions: { ecmaVersion: 6 },
213+
errors: [
214+
{
215+
message: "'a' is already defined.",
216+
type: AST_NODE_TYPES.Identifier
217+
} as any,
218+
{
219+
message: "'Object' is already defined.",
220+
type: AST_NODE_TYPES.Identifier
221+
} as any
222+
]
223+
},
224+
{
225+
code: 'var a; var {a = 0, b: Object = 0} = {};',
226+
options: [{ builtinGlobals: true }],
227+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
228+
errors: [
229+
{
230+
message: "'a' is already defined.",
231+
type: AST_NODE_TYPES.Identifier
232+
} as any
233+
]
234+
},
235+
{
236+
code: 'var a; var {a = 0, b: Object = 0} = {};',
237+
options: [{ builtinGlobals: true }],
238+
parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } },
239+
errors: [
240+
{
241+
message: "'a' is already defined.",
242+
type: AST_NODE_TYPES.Identifier
243+
} as any
244+
]
245+
},
246+
{
247+
code: 'var a; var {a = 0, b: Object = 0} = {};',
248+
options: [{ builtinGlobals: false }],
249+
parserOptions: { ecmaVersion: 6 },
250+
errors: [
251+
{
252+
message: "'a' is already defined.",
253+
type: AST_NODE_TYPES.Identifier
254+
} as any
255+
]
256+
},
257+
258+
// Notifications of readonly are moved from no-undef: https://github.com/eslint/eslint/issues/4504
259+
{
260+
code: '/*global b:false*/ var b = 1;',
261+
options: [{ builtinGlobals: true }],
262+
errors: [
263+
{
264+
message: "'b' is already defined.",
265+
type: AST_NODE_TYPES.Identifier
266+
} as any
267+
]
268+
}
269+
]
28270
});

packages/eslint-plugin/tests/eslint-rules/no-use-before-define.test.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/eslint-plugin/tests/rules/no-use-before-define.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,22 @@ export namespace Third {
214214
`,
215215
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
216216
parser: '@typescript-eslint/parser'
217-
}
217+
},
218+
// https://github.com/eslint/typescript-eslint-parser/issues/550
219+
`
220+
function test(file: Blob) {
221+
const slice: typeof file.slice =
222+
file.slice || (file as any).webkitSlice || (file as any).mozSlice
223+
return slice
224+
}
225+
`,
226+
// https://github.com/eslint/typescript-eslint-parser/issues/435
227+
`
228+
interface Foo {
229+
bar: string
230+
}
231+
const bar = 'blah'
232+
`
218233
],
219234
invalid: [
220235
{
@@ -813,7 +828,7 @@ function foo() {
813828
var bar = 1;
814829
}
815830
var bar;
816-
`,
831+
`,
817832
parserOptions,
818833
options: [{ variables: false }],
819834
errors: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy