Skip to content

Commit 9cd3fdf

Browse files
futpibsindresorhus
andauthored
use-test: Ignore type import
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 4bc45cd commit 9cd3fdf

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

docs/rules/use-test.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Translations: [Français](https://github.com/avajs/ava-docs/blob/main/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/main/docs/recipes/typescript.md#typing-tcontext)).
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/main/docs/recipes/typescript.md#typing-tcontext)). Type-only imports (`import type ... from 'ava'`) are not linted.
77

88
### Fail
99

@@ -28,6 +28,7 @@ const test = require('foo');
2828

2929
```ts
3030
import anyTest from 'ava';
31+
import type {TestInterface} from 'ava';
3132

3233
const test = anyTest as TestInterface<{foo: string}>;
3334
```

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"resolve-from": "^5.0.0"
4141
},
4242
"devDependencies": {
43+
"@typescript-eslint/parser": "^5.9.0",
4344
"ava": "^3.15.0",
4445
"c8": "^7.7.3",
4546
"chalk": "^4.1.1",

rules/use-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const create = context => {
3131
const isTypeScript = ext === '.ts' || ext === '.tsx';
3232

3333
return {
34-
ImportDeclaration: node => {
34+
'ImportDeclaration[importKind!="type"]': node => {
3535
if (node.source.value === 'ava') {
3636
const {name} = node.specifiers[0].local;
3737
if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {

test/use-test.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ const ruleTester = avaRuleTester(test, {
1313
},
1414
});
1515

16+
const typescriptRuleTester = avaRuleTester(test, {
17+
parser: require.resolve('@typescript-eslint/parser'),
18+
});
19+
1620
const errors = [{}];
1721

18-
ruleTester.run('use-test', rule, {
22+
const commonTestCases = {
1923
valid: [
2024
{code: 'var test = require(\'ava\');', filename: 'file.js'},
2125
{code: 'let test = require(\'ava\');', filename: 'file.js'},
@@ -124,4 +128,16 @@ ruleTester.run('use-test', rule, {
124128
filename: 'file.tsx',
125129
},
126130
],
127-
});
131+
};
132+
133+
const typescriptTestCases = {
134+
valid: [
135+
{code: 'import type {Macro} from \'ava\';', filename: 'file.ts'},
136+
{code: 'import type {Macro} from \'ava\';', filename: 'file.tsx'},
137+
],
138+
invalid: [],
139+
};
140+
141+
ruleTester.run('use-test', rule, commonTestCases);
142+
typescriptRuleTester.run('use-test', rule, commonTestCases);
143+
typescriptRuleTester.run('use-test', rule, typescriptTestCases);

0 commit comments

Comments
 (0)