Skip to content

Commit 524594d

Browse files
authored
Allow importing AVA as anyTest in TypeScript files
1 parent 9fcec4d commit 524594d

File tree

3 files changed

+119
-24
lines changed

3 files changed

+119
-24
lines changed

docs/rules/use-test.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/use-test.md)
44

55
The convention is to import AVA and assign it to a variable named `test`. Most rules in `eslint-plugin-ava` are based on that assumption.
6+
In a TypeScript file (`.ts` or `.tsx`) AVA can be assigned to a variable named `anyTest` in order to define the types of `t.context` (see [Typing t.context](https://github.com/avajs/ava/blob/master/docs/recipes/typescript.md#typing-tcontext)).
67

78
### Fail
89

@@ -24,3 +25,9 @@ import test from 'ava';
2425
var test = require('foo');
2526
const test = require('foo');
2627
```
28+
29+
```ts
30+
import anyTest from 'ava';
31+
32+
const test = anyTest as TestInterface<{foo: string}>;
33+
```

rules/use-test.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const path = require('path');
23
const espurify = require('espurify');
34
const deepStrictEqual = require('deep-strict-equal');
45
const util = require('../util');
@@ -24,18 +25,29 @@ function report(context, node) {
2425
});
2526
}
2627

27-
const create = context => ({
28-
ImportDeclaration: node => {
29-
if (node.source.value === 'ava' && node.specifiers[0].local.name !== 'test') {
30-
report(context, node);
31-
}
32-
},
33-
VariableDeclarator: node => {
34-
if (node.id.name !== 'test' && node.init && deepStrictEqual(espurify(node.init), avaVariableDeclaratorInitAst)) {
35-
report(context, node);
28+
const create = context => {
29+
const ext = path.extname(context.getFilename());
30+
const isTypeScript = ext === '.ts' || ext === '.tsx';
31+
32+
return {
33+
ImportDeclaration: node => {
34+
if (node.source.value === 'ava') {
35+
const {name} = node.specifiers[0].local;
36+
if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {
37+
report(context, node);
38+
}
39+
}
40+
},
41+
VariableDeclarator: node => {
42+
if (node.init && deepStrictEqual(espurify(node.init), avaVariableDeclaratorInitAst)) {
43+
const {name} = node.id;
44+
if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {
45+
report(context, node);
46+
}
47+
}
3648
}
37-
}
38-
});
49+
};
50+
};
3951

4052
module.exports = {
4153
create,

test/use-test.js

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,111 @@ const errors = [{ruleId: 'use-test'}];
1515

1616
ruleTester.run('use-test', rule, {
1717
valid: [
18-
'var test = require(\'ava\');',
19-
'let test = require(\'ava\');',
20-
'const test = require(\'ava\');',
21-
'const a = 1, test = require(\'ava\'), b = 2;',
22-
'const test = require(\'foo\');',
23-
'import test from \'ava\';',
24-
'import test, {} from \'ava\';',
25-
'import test from \'foo\';'
18+
{code: 'var test = require(\'ava\');', filename: 'file.js'},
19+
{code: 'let test = require(\'ava\');', filename: 'file.js'},
20+
{code: 'const test = require(\'ava\');', filename: 'file.js'},
21+
{code: 'const a = 1, test = require(\'ava\'), b = 2;', filename: 'file.js'},
22+
{code: 'const test = require(\'foo\');', filename: 'file.js'},
23+
{code: 'import test from \'ava\';', filename: 'file.js'},
24+
{code: 'import test, {} from \'ava\';', filename: 'file.js'},
25+
{code: 'import test from \'foo\';', filename: 'file.js'},
26+
{code: 'var anyTest = require(\'ava\');', filename: 'file.ts'},
27+
{code: 'let anyTest = require(\'ava\');', filename: 'file.ts'},
28+
{code: 'const anyTest = require(\'ava\');', filename: 'file.ts'},
29+
{code: 'const a = 1, anyTest = require(\'ava\'), b = 2;', filename: 'file.ts'},
30+
{code: 'const anyTest = require(\'foo\');', filename: 'file.ts'},
31+
{code: 'import anyTest from \'ava\';', filename: 'file.ts'},
32+
{code: 'import anyTest, {} from \'ava\';', filename: 'file.ts'},
33+
{code: 'import anyTest from \'foo\';', filename: 'file.ts'},
34+
{code: 'var anyTest = require(\'ava\');', filename: 'file.tsx'},
35+
{code: 'let anyTest = require(\'ava\');', filename: 'file.tsx'},
36+
{code: 'const anyTest = require(\'ava\');', filename: 'file.tsx'},
37+
{code: 'const a = 1, anyTest = require(\'ava\'), b = 2;', filename: 'file.tsx'},
38+
{code: 'const anyTest = require(\'foo\');', filename: 'file.tsx'},
39+
{code: 'import anyTest from \'ava\';', filename: 'file.tsx'},
40+
{code: 'import anyTest, {} from \'ava\';', filename: 'file.tsx'},
41+
{code: 'import anyTest from \'foo\';', filename: 'file.tsx'}
2642
],
2743
invalid: [
2844
{
2945
code: 'var ava = require(\'ava\');',
30-
errors
46+
errors,
47+
filename: 'file.ts'
3148
},
3249
{
3350
code: 'let ava = require(\'ava\');',
34-
errors
51+
errors,
52+
filename: 'file.ts'
3553
},
3654
{
3755
code: 'const ava = require(\'ava\');',
38-
errors
56+
errors,
57+
filename: 'file.ts'
3958
},
4059
{
4160
code: 'const a = 1, ava = require(\'ava\'), b = 2;',
42-
errors
61+
errors,
62+
filename: 'file.ts'
4363
},
4464
{
4565
code: 'import ava from \'ava\';',
46-
errors
66+
errors,
67+
filename: 'file.ts'
68+
},
69+
{
70+
code: 'var anyTest = require(\'ava\');',
71+
errors,
72+
filename: 'file.js'
73+
},
74+
{
75+
code: 'var ava = require(\'ava\');',
76+
errors,
77+
filename: 'file.ts'
78+
},
79+
{
80+
code: 'let ava = require(\'ava\');',
81+
errors,
82+
filename: 'file.ts'
83+
},
84+
{
85+
code: 'const ava = require(\'ava\');',
86+
errors,
87+
filename: 'file.ts'
88+
},
89+
{
90+
code: 'const a = 1, ava = require(\'ava\'), b = 2;',
91+
errors,
92+
filename: 'file.ts'
93+
},
94+
{
95+
code: 'import ava from \'ava\';',
96+
errors,
97+
filename: 'file.ts'
98+
},
99+
{
100+
code: 'var ava = require(\'ava\');',
101+
errors,
102+
filename: 'file.tsx'
103+
},
104+
{
105+
code: 'let ava = require(\'ava\');',
106+
errors,
107+
filename: 'file.tsx'
108+
},
109+
{
110+
code: 'const ava = require(\'ava\');',
111+
errors,
112+
filename: 'file.tsx'
113+
},
114+
{
115+
code: 'const a = 1, ava = require(\'ava\'), b = 2;',
116+
errors,
117+
filename: 'file.tsx'
118+
},
119+
{
120+
code: 'import ava from \'ava\';',
121+
errors,
122+
filename: 'file.tsx'
47123
}
48124
]
49125
});

0 commit comments

Comments
 (0)