-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (76 loc) · 2.02 KB
/
index.js
File metadata and controls
82 lines (76 loc) · 2.02 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
import process from 'node:process';
import pkg from './package.json' with { type: 'json' };
/** @type {Record<string, import('eslint').Rule.RuleModule>} */
const rules = {
'test-root-describe': {
meta: {
fixable: 'code',
},
create(context) {
const absoluteFileName = context.filename;
if (!absoluteFileName.endsWith('.spec.ts')) {
return {};
}
const relativeFileName = absoluteFileName
.replace(process.cwd(), '')
.replace(/\\/g, '/')
.replace(/^(?:\/(?:lib|src|test))?\//, '');
const testName = relativeFileName.replace(/\.spec\.ts$/, '');
return {
CallExpression(node) {
const { callee } = node;
if (
callee.type === 'Identifier' &&
callee.name === 'describe' &&
node.parent.parent.type === 'Program'
) {
const [descr] = node.arguments;
if (!descr) {
context.report({
node,
message: `Test root describe must have arguments`,
});
return;
}
const isOkay =
descr.type === 'Literal' &&
typeof descr.value === 'string' &&
testName === descr.value;
if (!isOkay) {
context.report({
node: descr,
message: `Test must be described by this string: '${testName}'`,
fix(fixer) {
return fixer.replaceText(descr, `'${testName}'`);
},
});
}
}
},
};
},
},
};
/** @type {Record<string, import('eslint').Linter.Config> } */
const configs = {
all: {
plugins: {
get '@containerbase'() {
return plugin;
},
},
rules: {
'@containerbase/test-root-describe': 'error',
},
},
};
/** @type {import('eslint').ESLint.Plugin} */
const plugin = {
meta: {
name: pkg.name,
version: pkg.version,
},
rules,
configs,
};
export default plugin;