1 | 'use strict';
|
2 |
|
3 | const path = require('node:path');
|
4 | const pkgDir = require('pkg-dir');
|
5 | const resolveFrom = require('resolve-from');
|
6 | const pkg = require('./package');
|
7 |
|
8 | exports.loadAvaHelper = (filename, overrides) => {
|
9 | const rootDirectory = pkgDir.sync(filename);
|
10 | if (!rootDirectory) {
|
11 | return undefined;
|
12 | }
|
13 |
|
14 | const avaHelperPath = resolveFrom.silent(rootDirectory, 'ava/eslint-plugin-helper');
|
15 | if (!avaHelperPath) {
|
16 | return undefined;
|
17 | }
|
18 |
|
19 | const avaHelper = require(avaHelperPath);
|
20 | return avaHelper.load(rootDirectory, overrides);
|
21 | };
|
22 |
|
23 | const functionExpressions = new Set([
|
24 | 'FunctionExpression',
|
25 | 'ArrowFunctionExpression',
|
26 | ]);
|
27 |
|
28 | exports.getRootNode = node => {
|
29 | if (node.object.type === 'MemberExpression') {
|
30 | return exports.getRootNode(node.object);
|
31 | }
|
32 |
|
33 | return node;
|
34 | };
|
35 |
|
36 | exports.getNameOfRootNodeObject = node => exports.getRootNode(node).object.name;
|
37 |
|
38 | exports.isPropertyUnderContext = node => exports.getRootNode(node).property.name === 'context';
|
39 |
|
40 | exports.isFunctionExpression = node => node && functionExpressions.has(node.type);
|
41 |
|
42 | function getTestModifiers(node) {
|
43 | if (node.type === 'CallExpression') {
|
44 | return getTestModifiers(node.callee);
|
45 | }
|
46 |
|
47 | if (node.type === 'MemberExpression') {
|
48 | return [...getTestModifiers(node.object), node.property];
|
49 | }
|
50 |
|
51 | return [];
|
52 | }
|
53 |
|
54 | exports.getTestModifiers = getTestModifiers;
|
55 |
|
56 | exports.getTestModifier = (node, module_) => getTestModifiers(node).find(property => property.name === module_);
|
57 |
|
58 | exports.removeTestModifier = parameters => {
|
59 | const modifier = parameters.modifier.trim();
|
60 | const range = [...exports.getTestModifier(parameters.node, modifier).range];
|
61 | const replacementRegExp = new RegExp(`\\.|${modifier}`, 'g');
|
62 | const source = parameters.context.getSourceCode().getText();
|
63 | let dotPosition = range[0] - 1;
|
64 | while (source.charAt(dotPosition) !== '.') {
|
65 | dotPosition -= 1;
|
66 | }
|
67 |
|
68 | let snippet = source.slice(dotPosition, range[1]);
|
69 | snippet = snippet.replace(replacementRegExp, '');
|
70 | return [[dotPosition, range[1]], snippet];
|
71 | };
|
72 |
|
73 | const getMembers = node => {
|
74 | const {name} = node.property;
|
75 |
|
76 | if (node.object.type === 'MemberExpression') {
|
77 | return [...getMembers(node.object), name];
|
78 | }
|
79 |
|
80 | return [name];
|
81 | };
|
82 |
|
83 | exports.getMembers = getMembers;
|
84 |
|
85 | const repoUrl = 'https://github.com/avajs/eslint-plugin-ava';
|
86 |
|
87 | const getDocumentationUrl = (filename, commitHash = `v${pkg.version}`) => {
|
88 | const ruleName = path.basename(filename, '.js');
|
89 | return `${repoUrl}/blob/${commitHash}/docs/rules/${ruleName}.md`;
|
90 | };
|
91 |
|
92 | exports.getDocsUrl = getDocumentationUrl;
|
93 |
|
94 | const assertionMethodsNumberArguments = new Map([
|
95 | ['assert', 1],
|
96 | ['deepEqual', 2],
|
97 | ['fail', 0],
|
98 | ['false', 1],
|
99 | ['falsy', 1],
|
100 | ['ifError', 1],
|
101 | ['is', 2],
|
102 | ['like', 2],
|
103 | ['not', 2],
|
104 | ['notDeepEqual', 2],
|
105 | ['notRegex', 2],
|
106 | ['notThrows', 1],
|
107 | ['notThrowsAsync', 1],
|
108 | ['pass', 0],
|
109 | ['regex', 2],
|
110 | ['snapshot', 1],
|
111 | ['throws', 1],
|
112 | ['throwsAsync', 1],
|
113 | ['true', 1],
|
114 | ['truthy', 1],
|
115 | ['try', 1],
|
116 | ]);
|
117 |
|
118 | const assertionMethodNames = [...assertionMethodsNumberArguments.keys()];
|
119 |
|
120 | exports.assertionMethodsNumArguments = assertionMethodsNumberArguments;
|
121 | exports.assertionMethods = new Set(assertionMethodNames);
|
122 | exports.executionMethods = new Set([...assertionMethodNames, 'end', 'plan', 'log', 'teardown', 'timeout']);
|