1 | 'use strict';
|
2 |
|
3 | const path = require('node:path');
|
4 | const {isDeepStrictEqual} = require('node:util');
|
5 | const espurify = require('espurify');
|
6 | const util = require('../util');
|
7 |
|
8 | const avaVariableDeclaratorInitAst = {
|
9 | type: 'CallExpression',
|
10 | callee: {
|
11 | type: 'Identifier',
|
12 | name: 'require',
|
13 | },
|
14 | arguments: [
|
15 | {
|
16 | type: 'Literal',
|
17 | value: 'ava',
|
18 | },
|
19 | ],
|
20 | };
|
21 |
|
22 | function report(context, node) {
|
23 | context.report({
|
24 | node,
|
25 | message: 'AVA should be imported as `test`.',
|
26 | });
|
27 | }
|
28 |
|
29 | const create = context => {
|
30 | const extension = path.extname(context.getFilename());
|
31 | const isTypeScript = extension === '.ts' || extension === '.tsx';
|
32 |
|
33 | return {
|
34 | 'ImportDeclaration[importKind!="type"]'(node) {
|
35 | if (node.source.value === 'ava') {
|
36 | const {name} = node.specifiers[0].local;
|
37 | if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {
|
38 | report(context, node);
|
39 | }
|
40 | }
|
41 | },
|
42 | VariableDeclarator(node) {
|
43 | if (node.init && isDeepStrictEqual(espurify(node.init), avaVariableDeclaratorInitAst)) {
|
44 | const {name} = node.id;
|
45 | if (name !== 'test' && (!isTypeScript || name !== 'anyTest')) {
|
46 | report(context, node);
|
47 | }
|
48 | }
|
49 | },
|
50 | };
|
51 | };
|
52 |
|
53 | module.exports = {
|
54 | create,
|
55 | meta: {
|
56 | type: 'suggestion',
|
57 | docs: {
|
58 | description: 'Ensure that AVA is imported with `test` as the variable name.',
|
59 | url: util.getDocsUrl(__filename),
|
60 | },
|
61 | schema: [],
|
62 | },
|
63 | };
|