forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-only-eslint-tests.js
75 lines (68 loc) · 2.45 KB
/
no-only-eslint-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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Usage of only: true in ESLint tests',
category: 'Possible Errors',
},
fixable: 'code',
messages: {noOnlyInESLintTest: 'You cannot use only: true in an ESLint test.'},
schema: [] // no options
},
create: function(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
function checkForOnlyInTestCases(testCaseObjects) {
for (const testCase of testCaseObjects) {
if (!testCase || !testCase.properties) {
continue;
}
const onlyKeyProp = testCase.properties.find(prop => {
return prop.key.name === 'only';
});
if (onlyKeyProp) {
context.report({
node: onlyKeyProp,
messageId: 'noOnlyInESLintTest',
fix(fixer) {
let nextNode = sourceCode.getTokenAfter(onlyKeyProp);
// To delete the property, the trailing comma, and then the
// resulting new line, we find the next node after the comma and
// delete up to that. That ensures that when we remove the
// property we also remove the emtpy line.
if (nextNode?.value === ',') {
nextNode = sourceCode.getTokenAfter(nextNode);
}
return [
fixer.removeRange([onlyKeyProp.range[0], nextNode.range[0]]),
];
}
});
}
}
}
return {
// match ruleTester.run('foo', rule, {...})
'CallExpression[callee.object.name=\'ruleTester\'][callee.property.name=\'run\']'(node) {
// first argument = string name
// second argument = rule itself
// third argument = the object containing the test cases - what we want!
const tests = node.arguments[2];
if (!tests || !tests.properties) {
return;
}
for (const testProperty of tests.properties) {
// Iterate over the "valid" and "invalid" rules
// Here .value = the array of test cases, and .elements gets us each
// object (individual test case) within that array.
checkForOnlyInTestCases(testProperty.value.elements);
}
}
};
}
};