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