-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathno-only-tests.js
99 lines (92 loc) · 3.55 KB
/
no-only-tests.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
'use strict';
const utils = require('../utils');
const {
isCommaToken,
isOpeningBraceToken,
isClosingBraceToken,
} = require('@eslint-community/eslint-utils');
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow the test case property `only`',
category: 'Tests',
recommended: true,
url: 'https://github.com/eslint-community/eslint-plugin-eslint-plugin/tree/HEAD/docs/rules/no-only-tests.md',
},
hasSuggestions: true,
schema: [],
messages: {
foundOnly:
'The test case property `only` can be used during development, but should not be checked-in, since it prevents all the tests from running.',
removeOnly: 'Remove `only`.',
},
},
create(context) {
return {
Program(ast) {
for (const testRun of utils.getTestInfo(context, ast)) {
for (const test of [...testRun.valid, ...testRun.invalid]) {
if (test.type === 'ObjectExpression') {
// Test case object: { code: 'const x = 123;', ... }
const onlyProperty = test.properties.find(
(property) =>
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.key.name === 'only' &&
property.value.type === 'Literal' &&
property.value.value,
);
if (onlyProperty) {
context.report({
node: onlyProperty,
messageId: 'foundOnly',
suggest: [
{
messageId: 'removeOnly',
*fix(fixer) {
const sourceCode =
context.sourceCode || context.getSourceCode(); // TODO: just use context.sourceCode when dropping eslint < v9
const tokenBefore =
sourceCode.getTokenBefore(onlyProperty);
const tokenAfter =
sourceCode.getTokenAfter(onlyProperty);
if (
(isCommaToken(tokenBefore) &&
isCommaToken(tokenAfter)) || // In middle of properties
(isOpeningBraceToken(tokenBefore) &&
isCommaToken(tokenAfter)) // At beginning of properties
) {
yield fixer.remove(tokenAfter); // Remove extra comma.
}
if (
isCommaToken(tokenBefore) &&
isClosingBraceToken(tokenAfter)
) {
// At end of properties
yield fixer.remove(tokenBefore); // Remove extra comma.
}
yield fixer.remove(onlyProperty);
},
},
],
});
}
} else if (
test.type === 'CallExpression' &&
test.callee.type === 'MemberExpression' &&
test.callee.object.type === 'Identifier' &&
test.callee.object.name === 'RuleTester' &&
test.callee.property.type === 'Identifier' &&
test.callee.property.name === 'only'
) {
// RuleTester.only('const x = 123;');
context.report({ node: test.callee, messageId: 'foundOnly' });
}
}
}
},
};
},
};