forked from avajs/eslint-plugin-ava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
177 lines (143 loc) · 4.25 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
const fs = require('fs');
const path = require('path');
const isPlainObject = require('is-plain-object');
const esmRequire = require('esm')(module, {
cjs: false,
force: true,
mode: 'all'
});
const pkg = require('./package');
const functionExpressions = [
'FunctionExpression',
'ArrowFunctionExpression'
];
const defaultFiles = [
'test.js',
'test-*.js',
'test/**/*.js',
'**/__tests__/**/*.js',
'**/*.test.js'
];
exports.nameOfRootObject = node => {
if (node.object.type === 'MemberExpression') {
return exports.nameOfRootObject(node.object);
}
return node.object.name;
};
exports.isInContext = node => {
if (node.object.type === 'MemberExpression') {
return exports.isInContext(node.object);
}
return node.property.name === 'context';
};
const NO_SUCH_FILE = Symbol('no ava.config.js file');
const MISSING_DEFAULT_EXPORT = Symbol('missing default export');
// Based on https://github.com/avajs/ava/blob/v1.0.0-beta.6/lib/load-config.js,
// except where AVA would exit with an exception, this function returns an empty
// config object.
exports.getAvaConfig = packageFilepath => {
const defaultResult = {};
if (!packageFilepath) {
return defaultResult;
}
try {
const {ava: packageConf = defaultResult} = JSON.parse(fs.readFileSync(packageFilepath, 'utf8'));
const projectDir = path.dirname(packageFilepath);
let fileConf;
try {
({default: fileConf = MISSING_DEFAULT_EXPORT} = esmRequire(path.join(projectDir, 'ava.config.js')));
} catch (error) {
if (error && error.code === 'MODULE_NOT_FOUND') {
fileConf = NO_SUCH_FILE;
} else {
return defaultResult;
}
}
if (fileConf === MISSING_DEFAULT_EXPORT) {
return defaultResult;
}
if (fileConf === NO_SUCH_FILE) {
return packageConf;
}
if (Object.keys(packageConf).length > 0) {
return defaultResult;
}
if (typeof fileConf === 'function') {
fileConf = fileConf({projectDir});
}
return !fileConf || typeof fileConf.then === 'function' || !isPlainObject(fileConf) ?
defaultResult :
fileConf;
} catch (_) {
return defaultResult;
}
};
exports.isFunctionExpression = node => node && functionExpressions.includes(node.type);
function getTestModifiers(node) {
if (node.type === 'CallExpression') {
return getTestModifiers(node.callee);
}
if (node.type === 'MemberExpression') {
return getTestModifiers(node.object).concat(node.property);
}
return [];
}
exports.getTestModifiers = getTestModifiers;
exports.getTestModifier = (node, mod) => {
return getTestModifiers(node).find(property => property.name === mod);
};
exports.removeTestModifier = params => {
const modifier = params.modifier.trim();
const range = exports.getTestModifier(params.node, modifier).range.slice();
const replacementRegExp = new RegExp(`\\.|${modifier}`, 'g');
const source = params.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).concat(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 assertionMethodsNumArguments = new Map([
['assert', 1],
['deepEqual', 2],
['fail', 0],
['false', 1],
['falsy', 1],
['ifError', 1],
['is', 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]
]);
const assertionMethodNames = [...assertionMethodsNumArguments.keys()];
exports.assertionMethodsNumArguments = assertionMethodsNumArguments;
exports.defaultFiles = defaultFiles;
exports.assertionMethods = new Set(assertionMethodNames);
exports.executionMethods = new Set(assertionMethodNames.concat(['end', 'plan', 'log']));