Skip to content

Commit e30eafa

Browse files
Alex Zherdevsindresorhus
Alex Zherdev
andcommitted
Support named imports and destructured requires (#232)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 6705aa6 commit e30eafa

File tree

2 files changed

+200
-6
lines changed

2 files changed

+200
-6
lines changed

create-ava-rule.js

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const enhance = require('enhance-visitors');
44
const deepStrictEqual = require('deep-strict-equal');
55
const util = require('./util');
66

7-
const avaImportDeclarationAst = {
7+
const avaImportDeclarationAsts = [{
88
type: 'ImportDeclaration',
99
specifiers: [
1010
{
@@ -19,9 +19,66 @@ const avaImportDeclarationAst = {
1919
type: 'Literal',
2020
value: 'ava'
2121
}
22-
};
22+
}, {
23+
type: 'ImportDeclaration',
24+
specifiers: [
25+
{
26+
type: 'ImportSpecifier',
27+
imported: {
28+
type: 'Identifier',
29+
name: 'serial'
30+
},
31+
local: {
32+
type: 'Identifier',
33+
name: 'test'
34+
}
35+
}
36+
],
37+
source: {
38+
type: 'Literal',
39+
value: 'ava'
40+
}
41+
}, {
42+
type: 'ImportDeclaration',
43+
specifiers: [
44+
{
45+
type: 'ImportSpecifier',
46+
imported: {
47+
type: 'Identifier',
48+
name: 'serial'
49+
},
50+
local: {
51+
type: 'Identifier',
52+
name: 'test'
53+
}
54+
}
55+
],
56+
source: {
57+
type: 'Literal',
58+
value: 'ava'
59+
}
60+
}, {
61+
type: 'ImportDeclaration',
62+
specifiers: [
63+
{
64+
type: 'ImportSpecifier',
65+
imported: {
66+
type: 'Identifier',
67+
name: 'serial'
68+
},
69+
local: {
70+
type: 'Identifier',
71+
name: 'serial'
72+
}
73+
}
74+
],
75+
source: {
76+
type: 'Literal',
77+
value: 'ava'
78+
}
79+
}];
2380

24-
const avaVariableDeclaratorAst = {
81+
const avaVariableDeclaratorAsts = [{
2582
type: 'VariableDeclarator',
2683
id: {
2784
type: 'Identifier',
@@ -40,7 +97,73 @@ const avaVariableDeclaratorAst = {
4097
}
4198
]
4299
}
43-
};
100+
}, {
101+
type: 'VariableDeclarator',
102+
id: {
103+
type: 'ObjectPattern',
104+
properties: [{
105+
type: 'Property',
106+
key: {
107+
type: 'Identifier',
108+
name: 'serial'
109+
},
110+
value: {
111+
type: 'Identifier',
112+
name: 'serial'
113+
},
114+
kind: 'init',
115+
method: false,
116+
shorthand: true,
117+
computed: false
118+
}]
119+
},
120+
init: {
121+
type: 'CallExpression',
122+
callee: {
123+
type: 'Identifier',
124+
name: 'require'
125+
},
126+
arguments: [
127+
{
128+
type: 'Literal',
129+
value: 'ava'
130+
}
131+
]
132+
}
133+
}, {
134+
type: 'VariableDeclarator',
135+
id: {
136+
type: 'ObjectPattern',
137+
properties: [{
138+
type: 'Property',
139+
key: {
140+
type: 'Identifier',
141+
name: 'serial'
142+
},
143+
value: {
144+
type: 'Identifier',
145+
name: 'test'
146+
},
147+
kind: 'init',
148+
method: false,
149+
shorthand: false,
150+
computed: false
151+
}]
152+
},
153+
init: {
154+
type: 'CallExpression',
155+
callee: {
156+
type: 'Identifier',
157+
name: 'require'
158+
},
159+
arguments: [
160+
{
161+
type: 'Literal',
162+
value: 'ava'
163+
}
164+
]
165+
}
166+
}];
44167

45168
function isTestFunctionCall(node) {
46169
if (node.type === 'Identifier') {
@@ -65,12 +188,12 @@ module.exports = () => {
65188
/* eslint quote-props: [2, "as-needed"] */
66189
const predefinedRules = {
67190
ImportDeclaration: node => {
68-
if (!isTestFile && deepStrictEqual(espurify(node), avaImportDeclarationAst)) {
191+
if (!isTestFile && avaImportDeclarationAsts.some(ast => deepStrictEqual(espurify(node), ast))) {
69192
isTestFile = true;
70193
}
71194
},
72195
VariableDeclarator: node => {
73-
if (!isTestFile && deepStrictEqual(espurify(node), avaVariableDeclaratorAst)) {
196+
if (!isTestFile && avaVariableDeclaratorAsts.some(ast => deepStrictEqual(espurify(node), ast))) {
74197
isTestFile = true;
75198
}
76199
},

test/create-ava-rule.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import test from 'ava';
2+
import avaRuleTester from 'eslint-ava-rule-tester';
3+
import {visitIf} from 'enhance-visitors';
4+
import createAvaRule from '../create-ava-rule';
5+
6+
const rule = {
7+
create: context => {
8+
const ava = createAvaRule();
9+
10+
return ava.merge({
11+
'Program:exit': node => {
12+
if (!ava.isInTestFile()) {
13+
context.report({node, message: 'not a test file'});
14+
}
15+
}
16+
});
17+
}
18+
};
19+
20+
const ruleTester = avaRuleTester(test, {
21+
env: {
22+
es6: true,
23+
},
24+
parserOptions: {
25+
sourceType: 'module'
26+
}
27+
});
28+
29+
const errors = [
30+
{
31+
message: 'not a test file'
32+
}
33+
];
34+
35+
ruleTester.run('rule-fixture', rule, {
36+
valid: [
37+
'const test = require(\'ava\');',
38+
'const {serial} = require(\'ava\');',
39+
'const {serial: test} = require(\'ava\');',
40+
'import test from \'ava\';',
41+
'import {serial} from \'ava\';',
42+
'import {serial as test} from \'ava\';'
43+
],
44+
45+
invalid: [
46+
{
47+
code: 'const test2 = require(\'ava\');',
48+
errors
49+
},
50+
{
51+
code: 'const {serial2} = require(\'ava\');',
52+
errors
53+
},
54+
{
55+
code: 'const {serial2: test} = require(\'ava\');',
56+
errors
57+
},
58+
{
59+
code: 'import test2 from \'ava\';',
60+
errors
61+
},
62+
{
63+
code: 'import {serial2} from \'ava\';',
64+
errors
65+
},
66+
{
67+
code: 'import {serial2 as test} from \'ava\';',
68+
errors
69+
}
70+
]
71+
});

0 commit comments

Comments
 (0)