forked from avajs/eslint-plugin-ava
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
128 lines (102 loc) · 3.15 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
123
124
125
126
127
128
'use strict';
const path = require('path');
const pkgDir = require('pkg-dir');
const resolveFrom = require('resolve-from');
const pkg = require('./package');
exports.loadAvaHelper = (filename, overrides) => {
const rootDir = pkgDir.sync(filename);
if (!rootDir) {
return undefined;
}
const avaHelperPath = resolveFrom.silent(rootDir, 'ava/eslint-plugin-helper');
if (!avaHelperPath) {
return undefined;
}
const avaHelper = require(avaHelperPath);
return avaHelper.load(rootDir, 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 => {
return exports.getRootNode(node).object.name;
};
exports.isPropertyUnderContext = node => {
return 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, mod) => {
return getTestModifiers(node).find(property => property.name === mod);
};
exports.removeTestModifier = parameters => {
const modifier = parameters.modifier.trim();
const range = exports.getTestModifier(parameters.node, modifier).range.slice();
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 getDocsUrl = (filename, commitHash) => {
const ruleName = path.basename(filename, '.js');
commitHash = commitHash || `v${pkg.version}`;
return `${repoUrl}/blob/${commitHash}/docs/rules/${ruleName}.md`;
};
exports.getDocsUrl = getDocsUrl;
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']);