1
1
// Original test cases
2
2
// https://github.com/typescript-eslint/typescript-eslint/blob/78467fc1bde9bd2db1e08b3d19f151f4adaff8a9/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
3
3
/* eslint func-style: off, eslint-plugin/consistent-output: off -- respect original */
4
- import type {
5
- InvalidTestCase ,
6
- TestCaseError ,
7
- } from "@typescript-eslint/utils/dist/ts-eslint"
8
4
import * as path from "path"
5
+ import { RuleTester } from "eslint"
9
6
10
7
import rule from "../../../../../src/rules/@typescript-eslint/no-unnecessary-condition"
11
- import { getFixturesRootDir , noFormat , RuleTester } from "./RuleTester"
8
+
9
+ function getFixturesRootDir ( ) : string {
10
+ return path . join ( __dirname , "fixtures" )
11
+ }
12
12
13
13
const rootPath = getFixturesRootDir ( )
14
14
15
15
const ruleTester = new RuleTester ( {
16
- parser : "@typescript-eslint/parser" ,
16
+ parser : require . resolve ( "@typescript-eslint/parser" ) ,
17
17
parserOptions : {
18
18
tsconfigRootDir : rootPath ,
19
19
project : "./tsconfig.json" ,
20
20
} ,
21
21
} )
22
22
23
+ function withFileName <
24
+ TestCase extends RuleTester . ValidTestCase | RuleTester . InvalidTestCase ,
25
+ > ( list : ( string | TestCase ) [ ] ) : TestCase [ ] {
26
+ return list . map ( ( e ) => {
27
+ if ( typeof e === "string" ) {
28
+ return { code : e , filename : path . join ( rootPath , "file.ts" ) } as TestCase
29
+ }
30
+ if ( e . filename ) return e
31
+ return {
32
+ ...e ,
33
+ filename : e . parserOptions ?. tsconfigRootDir
34
+ ? path . join ( e . parserOptions . tsconfigRootDir , "file.ts" )
35
+ : path . join ( rootPath , "file.ts" ) ,
36
+ } as TestCase
37
+ } )
38
+ }
39
+
23
40
const ruleError = (
24
41
line : number ,
25
42
column : number ,
26
43
messageId : string ,
27
- ) : TestCaseError < string > => ( {
44
+ ) : {
45
+ messageId : string
46
+ line : number
47
+ column : number
48
+ } => ( {
28
49
messageId,
29
50
line,
30
51
column,
@@ -39,13 +60,13 @@ const t1 = b1 && b2;
39
60
const unnecessaryConditionTest = (
40
61
condition : string ,
41
62
messageId : string ,
42
- ) : InvalidTestCase < string , any > => ( {
63
+ ) : RuleTester . InvalidTestCase => ( {
43
64
code : necessaryConditionTest ( condition ) ,
44
65
errors : [ ruleError ( 4 , 12 , messageId ) ] ,
45
66
} )
46
67
47
68
ruleTester . run ( "no-unnecessary-conditionals" , rule as any , {
48
- valid : [
69
+ valid : withFileName ( [
49
70
`
50
71
declare const b1: boolean;
51
72
declare const b2: boolean;
@@ -519,8 +540,8 @@ if (x) {
519
540
tsconfigRootDir : path . join ( rootPath , "unstrict" ) ,
520
541
} ,
521
542
} ,
522
- ] ,
523
- invalid : [
543
+ ] ) ,
544
+ invalid : withFileName ( [
524
545
// Ensure that it's checking in all the right places
525
546
{
526
547
code : `
@@ -904,7 +925,7 @@ do {} while (true);
904
925
] ,
905
926
} ,
906
927
{
907
- code : noFormat `
928
+ code : `
908
929
let foo = { bar: true };
909
930
foo?.bar;
910
931
foo ?. bar;
954
975
] ,
955
976
} ,
956
977
{
957
- code : noFormat `
978
+ code : `
958
979
let foo = () => {};
959
980
foo?.();
960
981
foo ?. ();
@@ -1004,7 +1025,7 @@ foo
1004
1025
] ,
1005
1026
} ,
1006
1027
{
1007
- code : noFormat `
1028
+ code : `
1008
1029
let foo = () => {};
1009
1030
foo?.(bar);
1010
1031
foo ?. (bar);
@@ -1540,5 +1561,5 @@ if (x) {
1540
1561
} ,
1541
1562
] ,
1542
1563
} ,
1543
- ] ,
1564
+ ] ) ,
1544
1565
} )
0 commit comments