forked from eslint-community/eslint-plugin-eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-only-tests.js
112 lines (102 loc) · 3.97 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
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const utils = require('../utils');
/**
* Checks if the given token is a comma token or not.
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is a comma token.
*/
function isCommaToken (token) {
return token.value === ',' && token.type === 'Punctuator';
}
/**
* Checks if the given token is an opening brace token or not.
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is an opening brace token.
*/
function isOpeningBraceToken (token) {
return token.value === '{' && token.type === 'Punctuator';
}
/**
* Checks if the given token is a closing brace token or not.
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is a closing brace token.
*/
function isClosingBraceToken (token) {
return token.value === '}' && token.type === 'Punctuator';
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow the test case property `only`',
category: 'Tests',
recommended: false,
},
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`.',
},
hasSuggestions: true,
},
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.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.getSourceCode();
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' });
}
}
}
},
};
},
};