1
1
import path from 'path' ;
2
- import { ESLintUtils , type TSESLint } from '@typescript-eslint/utils' ;
2
+ import { version as rawTypeScriptESLintPluginVersion } from '@typescript-eslint/eslint-plugin/package.json' ;
3
+ import { TSESLint } from '@typescript-eslint/utils' ;
3
4
import dedent from 'dedent' ;
4
5
import type { MessageIds , Options } from '../unbound-method' ;
5
6
@@ -9,15 +10,35 @@ function getFixturesRootDir(): string {
9
10
10
11
const rootPath = getFixturesRootDir ( ) ;
11
12
12
- const ruleTester = new ESLintUtils . RuleTester ( {
13
- parser : '@typescript-eslint/parser' ,
13
+ const ruleTester = new TSESLint . RuleTester ( {
14
+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
14
15
parserOptions : {
15
16
sourceType : 'module' ,
16
17
tsconfigRootDir : rootPath ,
17
18
project : './tsconfig.json' ,
18
19
} ,
19
20
} ) ;
20
21
22
+ const fixtureFilename = path . join ( rootPath , 'file.ts' ) ;
23
+
24
+ const withFixtureFilename = <
25
+ T extends Array <
26
+ | ( TSESLint . ValidTestCase < Options > | string )
27
+ | TSESLint . InvalidTestCase < MessageIds , Options >
28
+ > ,
29
+ > (
30
+ cases : T ,
31
+ ) : T extends Array < TSESLint . InvalidTestCase < MessageIds , Options > >
32
+ ? Array < TSESLint . InvalidTestCase < MessageIds , Options > >
33
+ : Array < TSESLint . ValidTestCase < Options > > => {
34
+ // @ts -expect-error this is fine, and will go away later once we upgrade
35
+ return cases . map ( code => {
36
+ const test = typeof code === 'string' ? { code } : code ;
37
+
38
+ return { filename : fixtureFilename , ...test } ;
39
+ } ) ;
40
+ } ;
41
+
21
42
const ConsoleClassAndVariableCode = dedent `
22
43
class Console {
23
44
log(str) {
@@ -164,8 +185,8 @@ describe('error handling', () => {
164
185
} ) ;
165
186
166
187
describe ( 'when @typescript-eslint/eslint-plugin is not available' , ( ) => {
167
- const ruleTester = new ESLintUtils . RuleTester ( {
168
- parser : '@typescript-eslint/parser' ,
188
+ const ruleTester = new TSESLint . RuleTester ( {
189
+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
169
190
parserOptions : {
170
191
sourceType : 'module' ,
171
192
tsconfigRootDir : rootPath ,
@@ -177,16 +198,18 @@ describe('error handling', () => {
177
198
'unbound-method jest edition without type service' ,
178
199
requireRule ( true ) ,
179
200
{
180
- valid : validTestCases . concat ( invalidTestCases . map ( ( { code } ) => code ) ) ,
201
+ valid : withFixtureFilename (
202
+ validTestCases . concat ( invalidTestCases . map ( ( { code } ) => code ) ) ,
203
+ ) ,
181
204
invalid : [ ] ,
182
205
} ,
183
206
) ;
184
207
} ) ;
185
208
} ) ;
186
209
187
210
ruleTester . run ( 'unbound-method jest edition' , requireRule ( false ) , {
188
- valid : validTestCases ,
189
- invalid : invalidTestCases ,
211
+ valid : withFixtureFilename ( validTestCases ) ,
212
+ invalid : withFixtureFilename ( invalidTestCases ) ,
190
213
} ) ;
191
214
192
215
function addContainsMethodsClass ( code : string ) : string {
@@ -225,11 +248,14 @@ function addContainsMethodsClassInvalid(
225
248
}
226
249
227
250
ruleTester . run ( 'unbound-method' , requireRule ( false ) , {
228
- valid : [
251
+ valid : withFixtureFilename ( [
229
252
'Promise.resolve().then(console.log);' ,
230
253
"['1', '2', '3'].map(Number.parseInt);" ,
231
254
'[5.2, 7.1, 3.6].map(Math.floor);' ,
232
255
'const x = console.log;' ,
256
+ ...( parseInt ( rawTypeScriptESLintPluginVersion . split ( '.' ) [ 0 ] , 10 ) >= 6
257
+ ? [ 'const x = Object.defineProperty;' ]
258
+ : [ ] ) ,
233
259
...[
234
260
'instance.bound();' ,
235
261
'instance.unbound();' ,
@@ -455,8 +481,8 @@ class OtherClass extends BaseClass {
455
481
const oc = new OtherClass();
456
482
oc.superLogThis();
457
483
` ,
458
- ] ,
459
- invalid : [
484
+ ] ) ,
485
+ invalid : withFixtureFilename ( [
460
486
{
461
487
code : `
462
488
class Console {
@@ -762,5 +788,59 @@ class OtherClass extends BaseClass {
762
788
} ,
763
789
] ,
764
790
} ,
765
- ] ,
791
+ {
792
+ code : `
793
+ const values = {
794
+ a() {},
795
+ b: () => {},
796
+ };
797
+
798
+ const { a, b } = values;
799
+ ` ,
800
+ errors : [
801
+ {
802
+ line : 7 ,
803
+ column : 9 ,
804
+ endColumn : 10 ,
805
+ messageId : 'unboundWithoutThisAnnotation' ,
806
+ } ,
807
+ ] ,
808
+ } ,
809
+ {
810
+ code : `
811
+ const values = {
812
+ a() {},
813
+ b: () => {},
814
+ };
815
+
816
+ const { a: c } = values;
817
+ ` ,
818
+ errors : [
819
+ {
820
+ line : 7 ,
821
+ column : 9 ,
822
+ endColumn : 10 ,
823
+ messageId : 'unboundWithoutThisAnnotation' ,
824
+ } ,
825
+ ] ,
826
+ } ,
827
+ {
828
+ code : `
829
+ const values = {
830
+ a() {},
831
+ b: () => {},
832
+ };
833
+
834
+ const { b, a } = values;
835
+ ` ,
836
+ errors : [
837
+ {
838
+ line : 7 ,
839
+ column : 12 ,
840
+ endColumn : 13 ,
841
+ messageId : 'unboundWithoutThisAnnotation' ,
842
+ } ,
843
+ ] ,
844
+ } ,
845
+ ] ) ,
766
846
} ) ;
0 commit comments