-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathcheck-test-definitions.ts
More file actions
91 lines (78 loc) · 2.78 KB
/
check-test-definitions.ts
File metadata and controls
91 lines (78 loc) · 2.78 KB
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/types';
import {createRule} from './utils/ruleCreator.ts';
const TEST_NAME_REGEX = /^\[crbug.com\/\d+\]/;
function getTextValue(node: TSESTree.Node): string|undefined {
if (node.type === 'Literal') {
return node.value?.toString();
}
if (node.type === 'TemplateLiteral') {
if (node.quasis.length === 0) {
return;
}
return node.quasis[0].value.cooked ?? undefined;
}
return;
}
export default createRule({
name: 'check-test-definitions',
meta: {
type: 'problem',
docs: {
description: 'check test implementations',
category: 'Possible Errors',
},
messages: {
missingBugId:
'Skipped tests must have a CRBug included in the description: `it.skip(\'[crbug.com/BUGID]: testname\', async() => {})',
extraBugId:
'Non-skipped tests cannot include a CRBug tag at the beginning of the description: `it.skip(\'testname (crbug.com/BUGID)\', async() => {})',
comment: 'A skipped test must have an attached comment with an explanation written before the test',
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [],
create: function(context) {
const sourceCode = context.sourceCode;
return {
MemberExpression(node) {
if (node.object.type !== 'Identifier' || node.property.type !== 'Identifier') {
return;
}
if ((node.object.name === 'it' || node.object.name === 'describe') &&
(node.property.name === 'skip' || node.property.name === 'skipOnPlatforms') &&
node.parent?.type === 'CallExpression') {
const testNameNode = node.property.name === 'skip' ? node.parent.arguments[0] : node.parent.arguments[1];
if (!testNameNode) {
return;
}
const textValue = getTextValue(testNameNode);
if (!textValue || !TEST_NAME_REGEX.test(textValue)) {
context.report({
node,
messageId: 'missingBugId',
});
}
const attachedComments = sourceCode.getCommentsBefore(node.parent);
if (attachedComments.length === 0) {
context.report({node, messageId: 'comment'});
}
}
},
CallExpression(node: TSESTree.CallExpression) {
if (node.callee.type === 'Identifier' && node.callee.name === 'it' && node.arguments[0]) {
const textValue = getTextValue(node.arguments[0]);
if (textValue && TEST_NAME_REGEX.test(textValue)) {
context.report({
node,
messageId: 'extraBugId',
});
}
}
},
};
},
});