eslint-plugin-jest 22.15.0 → 22.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -40
- package/docs/rules/prefer-to-be-null.md +7 -2
- package/docs/rules/prefer-to-be-undefined.md +7 -2
- package/docs/rules/prefer-to-contain.md +8 -10
- package/docs/rules/prefer-to-have-length.md +7 -3
- package/docs/rules/require-top-level-describe.md +52 -0
- package/lib/__tests__/rules.test.js +5 -4
- package/lib/index.js +2 -3
- package/lib/rules/consistent-test-it.js +7 -7
- package/lib/rules/expect-expect.js +3 -3
- package/lib/rules/lowercase-name.js +3 -3
- package/lib/rules/no-alias-methods.js +18 -14
- package/lib/rules/no-commented-out-tests.js +2 -2
- package/lib/rules/no-disabled-tests.js +4 -4
- package/lib/rules/no-duplicate-hooks.js +5 -5
- package/lib/rules/no-empty-title.js +8 -14
- package/lib/rules/no-expect-resolves.js +3 -3
- package/lib/rules/no-export.js +3 -3
- package/lib/rules/no-focused-tests.js +4 -4
- package/lib/rules/no-hooks.js +3 -3
- package/lib/rules/no-identical-title.js +9 -9
- package/lib/rules/no-if.js +5 -5
- package/lib/rules/no-jasmine-globals.js +4 -4
- package/lib/rules/no-jest-import.js +2 -2
- package/lib/rules/no-large-snapshots.js +25 -18
- package/lib/rules/no-mocks-import.js +18 -7
- package/lib/rules/no-standalone-expect.js +8 -8
- package/lib/rules/no-test-callback.js +5 -5
- package/lib/rules/no-test-prefixes.js +4 -4
- package/lib/rules/no-test-return-statement.js +4 -4
- package/lib/rules/no-truthy-falsy.js +31 -19
- package/lib/rules/no-try-expect.js +5 -5
- package/lib/rules/prefer-called-with.js +23 -11
- package/lib/rules/prefer-expect-assertions.js +45 -34
- package/lib/rules/prefer-inline-snapshots.js +2 -2
- package/lib/rules/prefer-spy-on.js +4 -4
- package/lib/rules/prefer-strict-equal.js +10 -12
- package/lib/rules/prefer-to-be-null.js +34 -16
- package/lib/rules/prefer-to-be-undefined.js +34 -16
- package/lib/rules/prefer-to-contain.js +112 -51
- package/lib/rules/prefer-to-have-length.js +47 -14
- package/lib/rules/prefer-todo.js +13 -17
- package/lib/rules/require-top-level-describe.js +66 -0
- package/lib/rules/require-tothrow-message.js +20 -15
- package/lib/rules/utils.js +486 -0
- package/lib/rules/valid-describe.js +36 -44
- package/lib/rules/valid-expect-in-promise.js +71 -66
- package/lib/rules/valid-expect.js +140 -173
- package/package.json +6 -5
- package/lib/rules/tsUtils.js +0 -250
- package/lib/rules/util.js +0 -100
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getNodeName = getNodeName;
|
|
7
|
+
exports.scopeHasLocalReference = exports.isDescribe = exports.isTestCase = exports.isHook = exports.isFunction = exports.TestCaseProperty = exports.DescribeProperty = exports.HookName = exports.TestCaseName = exports.DescribeAlias = exports.parseExpectCall = exports.isParsedEqualityMatcherCall = exports.ModifierName = exports.isExpectMember = exports.isExpectCall = exports.getAccessorValue = exports.isSupportedAccessor = exports.hasOnlyOneArgument = exports.getStringValue = exports.isStringNode = exports.isTemplateLiteral = exports.followTypeAssertionChain = exports.createRule = void 0;
|
|
8
|
+
|
|
9
|
+
var _path = require("path");
|
|
10
|
+
|
|
11
|
+
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
12
|
+
|
|
13
|
+
var _package = require("../../package.json");
|
|
14
|
+
|
|
15
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
16
|
+
|
|
17
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
18
|
+
|
|
19
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
+
|
|
21
|
+
const REPO_URL = 'https://github.com/jest-community/eslint-plugin-jest';
|
|
22
|
+
|
|
23
|
+
const createRule = _experimentalUtils.ESLintUtils.RuleCreator(name => {
|
|
24
|
+
const ruleName = (0, _path.basename)(name, '.ts');
|
|
25
|
+
return `${REPO_URL}/blob/v${_package.version}/docs/rules/${ruleName}.md`;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
exports.createRule = createRule;
|
|
29
|
+
|
|
30
|
+
const isTypeCastExpression = node => node.type === _experimentalUtils.AST_NODE_TYPES.TSAsExpression || node.type === _experimentalUtils.AST_NODE_TYPES.TSTypeAssertion;
|
|
31
|
+
|
|
32
|
+
const followTypeAssertionChain = expression => isTypeCastExpression(expression) ? followTypeAssertionChain(expression.expression) : expression;
|
|
33
|
+
/**
|
|
34
|
+
* A `Literal` with a `value` of type `string`.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
exports.followTypeAssertionChain = followTypeAssertionChain;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Checks if the given `node` is a `StringLiteral`.
|
|
42
|
+
*
|
|
43
|
+
* If a `value` is provided & the `node` is a `StringLiteral`,
|
|
44
|
+
* the `value` will be compared to that of the `StringLiteral`.
|
|
45
|
+
*
|
|
46
|
+
* @param {Node} node
|
|
47
|
+
* @param {V?} value
|
|
48
|
+
*
|
|
49
|
+
* @return {node is StringLiteral<V>}
|
|
50
|
+
*
|
|
51
|
+
* @template {V}.
|
|
52
|
+
*/
|
|
53
|
+
const isStringLiteral = (node, value) => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'string' && (value === undefined || node.value === value);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Checks if the given `node` is a `TemplateLiteral`.
|
|
57
|
+
*
|
|
58
|
+
* Complex `TemplateLiteral`s are not considered specific, and so will return `false`.
|
|
59
|
+
*
|
|
60
|
+
* If a `value` is provided & the `node` is a `TemplateLiteral`,
|
|
61
|
+
* the `value` will be compared to that of the `TemplateLiteral`.
|
|
62
|
+
*
|
|
63
|
+
* @param {Node} node
|
|
64
|
+
* @param {V?} value
|
|
65
|
+
*
|
|
66
|
+
* @return {node is TemplateLiteral<V>}
|
|
67
|
+
*
|
|
68
|
+
* @template V
|
|
69
|
+
*/
|
|
70
|
+
const isTemplateLiteral = (node, value) => node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral && (value === undefined || node.quasis.length === 1 && // bail out if not simple
|
|
71
|
+
node.quasis[0].value.raw === value);
|
|
72
|
+
|
|
73
|
+
exports.isTemplateLiteral = isTemplateLiteral;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Checks if the given `node` is a {@link StringNode}.
|
|
77
|
+
*
|
|
78
|
+
* @param {Node} node
|
|
79
|
+
* @param {V?} specifics
|
|
80
|
+
*
|
|
81
|
+
* @return {node is StringNode}
|
|
82
|
+
*
|
|
83
|
+
* @template V
|
|
84
|
+
*/
|
|
85
|
+
const isStringNode = (node, specifics) => isStringLiteral(node, specifics) || isTemplateLiteral(node, specifics);
|
|
86
|
+
/**
|
|
87
|
+
* Gets the value of the given `StringNode`.
|
|
88
|
+
*
|
|
89
|
+
* If the `node` is a `TemplateLiteral`, the `raw` value is used;
|
|
90
|
+
* otherwise, `value` is returned instead.
|
|
91
|
+
*
|
|
92
|
+
* @param {StringNode<S>} node
|
|
93
|
+
*
|
|
94
|
+
* @return {S}
|
|
95
|
+
*
|
|
96
|
+
* @template S
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
exports.isStringNode = isStringNode;
|
|
101
|
+
|
|
102
|
+
const getStringValue = node => isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;
|
|
103
|
+
/**
|
|
104
|
+
* Represents a `MemberExpression` with a "known" `property`.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
exports.getStringValue = getStringValue;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Guards that the given `call` has only one `argument`.
|
|
112
|
+
*
|
|
113
|
+
* @param {CallExpression} call
|
|
114
|
+
*
|
|
115
|
+
* @return {call is CallExpressionWithSingleArgument}
|
|
116
|
+
*/
|
|
117
|
+
const hasOnlyOneArgument = call => call.arguments && call.arguments.length === 1;
|
|
118
|
+
/**
|
|
119
|
+
* An `Identifier` with a known `name` value - i.e `expect`.
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
exports.hasOnlyOneArgument = hasOnlyOneArgument;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Checks if the given `node` is an `Identifier`.
|
|
127
|
+
*
|
|
128
|
+
* If a `name` is provided, & the `node` is an `Identifier`,
|
|
129
|
+
* the `name` will be compared to that of the `identifier`.
|
|
130
|
+
*
|
|
131
|
+
* @param {Node} node
|
|
132
|
+
* @param {V?} name
|
|
133
|
+
*
|
|
134
|
+
* @return {node is KnownIdentifier<Name>}
|
|
135
|
+
*
|
|
136
|
+
* @template V
|
|
137
|
+
*/
|
|
138
|
+
const isIdentifier = (node, name) => node.type === _experimentalUtils.AST_NODE_TYPES.Identifier && (name === undefined || node.name === name);
|
|
139
|
+
/**
|
|
140
|
+
* Checks if the given `node` is a "supported accessor".
|
|
141
|
+
*
|
|
142
|
+
* This means that it's a node can be used to access properties,
|
|
143
|
+
* and who's "value" can be statically determined.
|
|
144
|
+
*
|
|
145
|
+
* `MemberExpression` nodes most commonly contain accessors,
|
|
146
|
+
* but it's possible for other nodes to contain them.
|
|
147
|
+
*
|
|
148
|
+
* If a `value` is provided & the `node` is an `AccessorNode`,
|
|
149
|
+
* the `value` will be compared to that of the `AccessorNode`.
|
|
150
|
+
*
|
|
151
|
+
* Note that `value` here refers to the normalised value.
|
|
152
|
+
* The property that holds the value is not always called `name`.
|
|
153
|
+
*
|
|
154
|
+
* @param {Node} node
|
|
155
|
+
* @param {V?} value
|
|
156
|
+
*
|
|
157
|
+
* @return {node is AccessorNode<V>}
|
|
158
|
+
*
|
|
159
|
+
* @template V
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStringNode(node, value);
|
|
164
|
+
/**
|
|
165
|
+
* Gets the value of the given `AccessorNode`,
|
|
166
|
+
* account for the different node types.
|
|
167
|
+
*
|
|
168
|
+
* @param {AccessorNode<S>} accessor
|
|
169
|
+
*
|
|
170
|
+
* @return {S}
|
|
171
|
+
*
|
|
172
|
+
* @template S
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
exports.isSupportedAccessor = isSupportedAccessor;
|
|
177
|
+
|
|
178
|
+
const getAccessorValue = accessor => accessor.type === _experimentalUtils.AST_NODE_TYPES.Identifier ? accessor.name : getStringValue(accessor);
|
|
179
|
+
|
|
180
|
+
exports.getAccessorValue = getAccessorValue;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Checks if the given `node` is a valid `ExpectCall`.
|
|
184
|
+
*
|
|
185
|
+
* In order to be an `ExpectCall`, the `node` must:
|
|
186
|
+
* * be a `CallExpression`,
|
|
187
|
+
* * have an accessor named 'expect',
|
|
188
|
+
* * have a `parent`.
|
|
189
|
+
*
|
|
190
|
+
* @param {Node} node
|
|
191
|
+
*
|
|
192
|
+
* @return {node is ExpectCall}
|
|
193
|
+
*/
|
|
194
|
+
const isExpectCall = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && isSupportedAccessor(node.callee, 'expect') && node.parent !== undefined;
|
|
195
|
+
|
|
196
|
+
exports.isExpectCall = isExpectCall;
|
|
197
|
+
|
|
198
|
+
const isExpectMember = (node, name) => node.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && isSupportedAccessor(node.property, name);
|
|
199
|
+
/**
|
|
200
|
+
* Represents all the jest matchers.
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
exports.isExpectMember = isExpectMember;
|
|
205
|
+
let ModifierName;
|
|
206
|
+
exports.ModifierName = ModifierName;
|
|
207
|
+
|
|
208
|
+
(function (ModifierName) {
|
|
209
|
+
ModifierName["not"] = "not";
|
|
210
|
+
ModifierName["rejects"] = "rejects";
|
|
211
|
+
ModifierName["resolves"] = "resolves";
|
|
212
|
+
})(ModifierName || (exports.ModifierName = ModifierName = {}));
|
|
213
|
+
|
|
214
|
+
var EqualityMatcher;
|
|
215
|
+
|
|
216
|
+
(function (EqualityMatcher) {
|
|
217
|
+
EqualityMatcher["toBe"] = "toBe";
|
|
218
|
+
EqualityMatcher["toEqual"] = "toEqual";
|
|
219
|
+
EqualityMatcher["toStrictEqual"] = "toStrictEqual";
|
|
220
|
+
})(EqualityMatcher || (EqualityMatcher = {}));
|
|
221
|
+
|
|
222
|
+
const isParsedEqualityMatcherCall = matcher => EqualityMatcher.hasOwnProperty(matcher.name) && matcher.arguments !== null && matcher.arguments.length === 1;
|
|
223
|
+
/**
|
|
224
|
+
* Represents a parsed expect matcher, such as `toBe`, `toContain`, and so on.
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
exports.isParsedEqualityMatcherCall = isParsedEqualityMatcherCall;
|
|
229
|
+
|
|
230
|
+
const parseExpectMember = expectMember => ({
|
|
231
|
+
name: getAccessorValue(expectMember.property),
|
|
232
|
+
node: expectMember
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const reparseAsMatcher = parsedMember => _objectSpread({}, parsedMember, {
|
|
236
|
+
/**
|
|
237
|
+
* The arguments being passed to this `Matcher`, if any.
|
|
238
|
+
*
|
|
239
|
+
* If this matcher isn't called, this will be `null`.
|
|
240
|
+
*/
|
|
241
|
+
arguments: parsedMember.node.parent && parsedMember.node.parent.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? parsedMember.node.parent.arguments : null
|
|
242
|
+
});
|
|
243
|
+
/**
|
|
244
|
+
* Re-parses the given `parsedMember` as a `ParsedExpectModifier`.
|
|
245
|
+
*
|
|
246
|
+
* If the given `parsedMember` does not have a `name` of a valid `Modifier`,
|
|
247
|
+
* an exception will be thrown.
|
|
248
|
+
*
|
|
249
|
+
* @param {ParsedExpectMember<ModifierName>} parsedMember
|
|
250
|
+
*
|
|
251
|
+
* @return {ParsedExpectModifier}
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
const reparseMemberAsModifier = parsedMember => {
|
|
256
|
+
if (isSpecificMember(parsedMember, ModifierName.not)) {
|
|
257
|
+
return parsedMember;
|
|
258
|
+
}
|
|
259
|
+
/* istanbul ignore if */
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
if (!isSpecificMember(parsedMember, ModifierName.resolves) && !isSpecificMember(parsedMember, ModifierName.rejects)) {
|
|
263
|
+
// ts doesn't think that the ModifierName.not check is the direct inverse as the above two checks
|
|
264
|
+
// todo: impossible at runtime, but can't be typed w/o negation support
|
|
265
|
+
throw new Error(`modifier name must be either "${ModifierName.resolves}" or "${ModifierName.rejects}" (got "${parsedMember.name}")`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const negation = parsedMember.node.parent && isExpectMember(parsedMember.node.parent, ModifierName.not) ? parsedMember.node.parent : undefined;
|
|
269
|
+
return _objectSpread({}, parsedMember, {
|
|
270
|
+
negation
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const isSpecificMember = (member, specific) => member.name === specific;
|
|
275
|
+
/**
|
|
276
|
+
* Checks if the given `ParsedExpectMember` should be re-parsed as an `ParsedExpectModifier`.
|
|
277
|
+
*
|
|
278
|
+
* @param {ParsedExpectMember} member
|
|
279
|
+
*
|
|
280
|
+
* @return {member is ParsedExpectMember<ModifierName>}
|
|
281
|
+
*/
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
const shouldBeParsedExpectModifier = member => ModifierName.hasOwnProperty(member.name);
|
|
285
|
+
|
|
286
|
+
const parseExpectCall = expect => {
|
|
287
|
+
const expectation = {
|
|
288
|
+
expect
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
if (!isExpectMember(expect.parent)) {
|
|
292
|
+
return expectation;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const parsedMember = parseExpectMember(expect.parent);
|
|
296
|
+
|
|
297
|
+
if (!shouldBeParsedExpectModifier(parsedMember)) {
|
|
298
|
+
expectation.matcher = reparseAsMatcher(parsedMember);
|
|
299
|
+
return expectation;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const modifier = expectation.modifier = reparseMemberAsModifier(parsedMember);
|
|
303
|
+
const memberNode = modifier.negation || modifier.node;
|
|
304
|
+
|
|
305
|
+
if (!memberNode.parent || !isExpectMember(memberNode.parent)) {
|
|
306
|
+
return expectation;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
expectation.matcher = reparseAsMatcher(parseExpectMember(memberNode.parent));
|
|
310
|
+
return expectation;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
exports.parseExpectCall = parseExpectCall;
|
|
314
|
+
let DescribeAlias;
|
|
315
|
+
exports.DescribeAlias = DescribeAlias;
|
|
316
|
+
|
|
317
|
+
(function (DescribeAlias) {
|
|
318
|
+
DescribeAlias["describe"] = "describe";
|
|
319
|
+
DescribeAlias["fdescribe"] = "fdescribe";
|
|
320
|
+
DescribeAlias["xdescribe"] = "xdescribe";
|
|
321
|
+
})(DescribeAlias || (exports.DescribeAlias = DescribeAlias = {}));
|
|
322
|
+
|
|
323
|
+
let TestCaseName;
|
|
324
|
+
exports.TestCaseName = TestCaseName;
|
|
325
|
+
|
|
326
|
+
(function (TestCaseName) {
|
|
327
|
+
TestCaseName["fit"] = "fit";
|
|
328
|
+
TestCaseName["it"] = "it";
|
|
329
|
+
TestCaseName["test"] = "test";
|
|
330
|
+
TestCaseName["xit"] = "xit";
|
|
331
|
+
TestCaseName["xtest"] = "xtest";
|
|
332
|
+
})(TestCaseName || (exports.TestCaseName = TestCaseName = {}));
|
|
333
|
+
|
|
334
|
+
let HookName;
|
|
335
|
+
exports.HookName = HookName;
|
|
336
|
+
|
|
337
|
+
(function (HookName) {
|
|
338
|
+
HookName["beforeAll"] = "beforeAll";
|
|
339
|
+
HookName["beforeEach"] = "beforeEach";
|
|
340
|
+
HookName["afterAll"] = "afterAll";
|
|
341
|
+
HookName["afterEach"] = "afterEach";
|
|
342
|
+
})(HookName || (exports.HookName = HookName = {}));
|
|
343
|
+
|
|
344
|
+
let DescribeProperty;
|
|
345
|
+
exports.DescribeProperty = DescribeProperty;
|
|
346
|
+
|
|
347
|
+
(function (DescribeProperty) {
|
|
348
|
+
DescribeProperty["each"] = "each";
|
|
349
|
+
DescribeProperty["only"] = "only";
|
|
350
|
+
DescribeProperty["skip"] = "skip";
|
|
351
|
+
})(DescribeProperty || (exports.DescribeProperty = DescribeProperty = {}));
|
|
352
|
+
|
|
353
|
+
let TestCaseProperty;
|
|
354
|
+
exports.TestCaseProperty = TestCaseProperty;
|
|
355
|
+
|
|
356
|
+
(function (TestCaseProperty) {
|
|
357
|
+
TestCaseProperty["each"] = "each";
|
|
358
|
+
TestCaseProperty["only"] = "only";
|
|
359
|
+
TestCaseProperty["skip"] = "skip";
|
|
360
|
+
TestCaseProperty["todo"] = "todo";
|
|
361
|
+
})(TestCaseProperty || (exports.TestCaseProperty = TestCaseProperty = {}));
|
|
362
|
+
|
|
363
|
+
function getNodeName(node) {
|
|
364
|
+
function joinNames(a, b) {
|
|
365
|
+
return a && b ? `${a}.${b}` : null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
switch (node.type) {
|
|
369
|
+
case _experimentalUtils.AST_NODE_TYPES.Identifier:
|
|
370
|
+
return node.name;
|
|
371
|
+
|
|
372
|
+
case _experimentalUtils.AST_NODE_TYPES.Literal:
|
|
373
|
+
return `${node.value}`;
|
|
374
|
+
|
|
375
|
+
case _experimentalUtils.AST_NODE_TYPES.TemplateLiteral:
|
|
376
|
+
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
|
|
377
|
+
break;
|
|
378
|
+
|
|
379
|
+
case _experimentalUtils.AST_NODE_TYPES.MemberExpression:
|
|
380
|
+
return joinNames(getNodeName(node.object), getNodeName(node.property));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const isFunction = node => node.type === _experimentalUtils.AST_NODE_TYPES.FunctionExpression || node.type === _experimentalUtils.AST_NODE_TYPES.ArrowFunctionExpression;
|
|
387
|
+
|
|
388
|
+
exports.isFunction = isFunction;
|
|
389
|
+
|
|
390
|
+
const isHook = node => {
|
|
391
|
+
return node.callee.type === _experimentalUtils.AST_NODE_TYPES.Identifier && HookName.hasOwnProperty(node.callee.name);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
exports.isHook = isHook;
|
|
395
|
+
|
|
396
|
+
const isTestCase = node => {
|
|
397
|
+
return node.callee.type === _experimentalUtils.AST_NODE_TYPES.Identifier && TestCaseName.hasOwnProperty(node.callee.name) || node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && node.callee.object.type === _experimentalUtils.AST_NODE_TYPES.Identifier && TestCaseName.hasOwnProperty(node.callee.object.name) && node.callee.property.type === _experimentalUtils.AST_NODE_TYPES.Identifier && TestCaseProperty.hasOwnProperty(node.callee.property.name);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
exports.isTestCase = isTestCase;
|
|
401
|
+
|
|
402
|
+
const isDescribe = node => {
|
|
403
|
+
return node.callee.type === _experimentalUtils.AST_NODE_TYPES.Identifier && DescribeAlias.hasOwnProperty(node.callee.name) || node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && node.callee.object.type === _experimentalUtils.AST_NODE_TYPES.Identifier && DescribeAlias.hasOwnProperty(node.callee.object.name) && node.callee.property.type === _experimentalUtils.AST_NODE_TYPES.Identifier && DescribeProperty.hasOwnProperty(node.callee.property.name);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
exports.isDescribe = isDescribe;
|
|
407
|
+
|
|
408
|
+
const collectReferences = scope => {
|
|
409
|
+
const locals = new Set();
|
|
410
|
+
const unresolved = new Set();
|
|
411
|
+
let currentScope = scope;
|
|
412
|
+
|
|
413
|
+
while (currentScope !== null) {
|
|
414
|
+
var _iteratorNormalCompletion = true;
|
|
415
|
+
var _didIteratorError = false;
|
|
416
|
+
var _iteratorError = undefined;
|
|
417
|
+
|
|
418
|
+
try {
|
|
419
|
+
for (var _iterator = currentScope.variables[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
420
|
+
const ref = _step.value;
|
|
421
|
+
const isReferenceDefined = ref.defs.some(def => {
|
|
422
|
+
return def.type !== 'ImplicitGlobalVariable';
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
if (isReferenceDefined) {
|
|
426
|
+
locals.add(ref.name);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
} catch (err) {
|
|
430
|
+
_didIteratorError = true;
|
|
431
|
+
_iteratorError = err;
|
|
432
|
+
} finally {
|
|
433
|
+
try {
|
|
434
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
435
|
+
_iterator.return();
|
|
436
|
+
}
|
|
437
|
+
} finally {
|
|
438
|
+
if (_didIteratorError) {
|
|
439
|
+
throw _iteratorError;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
var _iteratorNormalCompletion2 = true;
|
|
445
|
+
var _didIteratorError2 = false;
|
|
446
|
+
var _iteratorError2 = undefined;
|
|
447
|
+
|
|
448
|
+
try {
|
|
449
|
+
for (var _iterator2 = currentScope.through[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
|
450
|
+
const ref = _step2.value;
|
|
451
|
+
unresolved.add(ref.identifier.name);
|
|
452
|
+
}
|
|
453
|
+
} catch (err) {
|
|
454
|
+
_didIteratorError2 = true;
|
|
455
|
+
_iteratorError2 = err;
|
|
456
|
+
} finally {
|
|
457
|
+
try {
|
|
458
|
+
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
459
|
+
_iterator2.return();
|
|
460
|
+
}
|
|
461
|
+
} finally {
|
|
462
|
+
if (_didIteratorError2) {
|
|
463
|
+
throw _iteratorError2;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
currentScope = currentScope.upper;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return {
|
|
472
|
+
locals,
|
|
473
|
+
unresolved
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const scopeHasLocalReference = (scope, referenceName) => {
|
|
478
|
+
const references = collectReferences(scope);
|
|
479
|
+
return (// referenceName was found as a local variable or function declaration.
|
|
480
|
+
references.locals.has(referenceName) || // referenceName was not found as an unresolved reference,
|
|
481
|
+
// meaning it is likely not an implicit global reference.
|
|
482
|
+
!references.unresolved.has(referenceName)
|
|
483
|
+
);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
exports.scopeHasLocalReference = scopeHasLocalReference;
|
|
@@ -7,7 +7,7 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _utils = require("./utils");
|
|
11
11
|
|
|
12
12
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
|
13
13
|
|
|
@@ -17,12 +17,6 @@ function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d =
|
|
|
17
17
|
|
|
18
18
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
19
19
|
|
|
20
|
-
const isAsync = node => node.async;
|
|
21
|
-
|
|
22
|
-
const isString = node => node.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof node.value === 'string' || node.type === _experimentalUtils.AST_NODE_TYPES.TemplateLiteral;
|
|
23
|
-
|
|
24
|
-
const hasParams = node => node.params.length > 0;
|
|
25
|
-
|
|
26
20
|
const paramsLocation = params => {
|
|
27
21
|
const _params = _slicedToArray(params, 1),
|
|
28
22
|
first = _params[0];
|
|
@@ -34,15 +28,15 @@ const paramsLocation = params => {
|
|
|
34
28
|
};
|
|
35
29
|
};
|
|
36
30
|
|
|
37
|
-
const isDescribeEach = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression &&
|
|
31
|
+
const isDescribeEach = node => node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'each');
|
|
38
32
|
|
|
39
|
-
var _default = (0,
|
|
33
|
+
var _default = (0, _utils.createRule)({
|
|
40
34
|
name: __filename,
|
|
41
35
|
meta: {
|
|
42
36
|
type: 'problem',
|
|
43
37
|
docs: {
|
|
44
|
-
description: 'Using an improper `describe()` callback function can lead to unexpected test errors.',
|
|
45
38
|
category: 'Possible Errors',
|
|
39
|
+
description: 'Using an improper `describe()` callback function can lead to unexpected test errors.',
|
|
46
40
|
recommended: 'warn'
|
|
47
41
|
},
|
|
48
42
|
messages: {
|
|
@@ -60,7 +54,7 @@ var _default = (0, _tsUtils.createRule)({
|
|
|
60
54
|
create(context) {
|
|
61
55
|
return {
|
|
62
56
|
CallExpression(node) {
|
|
63
|
-
if (!(0,
|
|
57
|
+
if (!(0, _utils.isDescribe)(node) || isDescribeEach(node)) {
|
|
64
58
|
return;
|
|
65
59
|
}
|
|
66
60
|
|
|
@@ -71,20 +65,18 @@ var _default = (0, _tsUtils.createRule)({
|
|
|
71
65
|
});
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
const _node$arguments = _slicedToArray(node.arguments,
|
|
75
|
-
name = _node$arguments[0]
|
|
68
|
+
const _node$arguments = _slicedToArray(node.arguments, 2),
|
|
69
|
+
name = _node$arguments[0],
|
|
70
|
+
callback = _node$arguments[1];
|
|
76
71
|
|
|
77
|
-
|
|
78
|
-
callbackFunction = _node$arguments2[1];
|
|
79
|
-
|
|
80
|
-
if (!isString(name)) {
|
|
72
|
+
if (!(0, _utils.isStringNode)(name)) {
|
|
81
73
|
context.report({
|
|
82
74
|
messageId: 'firstArgumentMustBeName',
|
|
83
75
|
loc: paramsLocation(node.arguments)
|
|
84
76
|
});
|
|
85
77
|
}
|
|
86
78
|
|
|
87
|
-
if (!
|
|
79
|
+
if (!callback) {
|
|
88
80
|
context.report({
|
|
89
81
|
messageId: 'nameAndCallback',
|
|
90
82
|
loc: paramsLocation(node.arguments)
|
|
@@ -92,38 +84,38 @@ var _default = (0, _tsUtils.createRule)({
|
|
|
92
84
|
return;
|
|
93
85
|
}
|
|
94
86
|
|
|
95
|
-
if ((0,
|
|
96
|
-
if (isAsync(callbackFunction)) {
|
|
97
|
-
context.report({
|
|
98
|
-
messageId: 'noAsyncDescribeCallback',
|
|
99
|
-
node: callbackFunction
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (hasParams(callbackFunction)) {
|
|
104
|
-
context.report({
|
|
105
|
-
messageId: 'unexpectedDescribeArgument',
|
|
106
|
-
loc: paramsLocation(callbackFunction.params)
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (callbackFunction.body && callbackFunction.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
111
|
-
callbackFunction.body.body.forEach(node => {
|
|
112
|
-
if (node.type === 'ReturnStatement') {
|
|
113
|
-
context.report({
|
|
114
|
-
messageId: 'unexpectedReturnInDescribe',
|
|
115
|
-
node
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
} else {
|
|
87
|
+
if (!(0, _utils.isFunction)(callback)) {
|
|
121
88
|
context.report({
|
|
122
89
|
messageId: 'secondArgumentMustBeFunction',
|
|
123
90
|
loc: paramsLocation(node.arguments)
|
|
124
91
|
});
|
|
125
92
|
return;
|
|
126
93
|
}
|
|
94
|
+
|
|
95
|
+
if (callback.async) {
|
|
96
|
+
context.report({
|
|
97
|
+
messageId: 'noAsyncDescribeCallback',
|
|
98
|
+
node: callback
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (callback.params.length) {
|
|
103
|
+
context.report({
|
|
104
|
+
messageId: 'unexpectedDescribeArgument',
|
|
105
|
+
loc: paramsLocation(callback.params)
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (callback.body && callback.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement) {
|
|
110
|
+
callback.body.body.forEach(node => {
|
|
111
|
+
if (node.type === _experimentalUtils.AST_NODE_TYPES.ReturnStatement) {
|
|
112
|
+
context.report({
|
|
113
|
+
messageId: 'unexpectedReturnInDescribe',
|
|
114
|
+
node
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
127
119
|
}
|
|
128
120
|
|
|
129
121
|
};
|