@zgeoff/oxlint-config 0.7.2 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/banned-test-calls.js +10 -0
- package/find-bun-test-name.js +25 -0
- package/find-chain-root.js +13 -0
- package/find-static-key.js +17 -0
- package/is-bun-test-file.js +10 -0
- package/no-file-snapshot.js +9 -17
- package/oxlintrc.json +1 -0
- package/package.json +7 -1
- package/plugin.js +2 -0
- package/setup-test-only.js +64 -2
- package/test-shape.js +111 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import getImportedName from './get-imported-name.js';
|
|
2
|
+
|
|
3
|
+
export default function findBunTestName(context, identifier) {
|
|
4
|
+
for (let scope = context.sourceCode.getScope(identifier); scope !== null; scope = scope.upper) {
|
|
5
|
+
const variable = scope.set.get(identifier.name);
|
|
6
|
+
|
|
7
|
+
if (variable !== undefined && variable.defs.length > 0) {
|
|
8
|
+
const [definition] = variable.defs;
|
|
9
|
+
|
|
10
|
+
return isBunTestSpecifier(definition.node) ? getImportedName(definition.node) : null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Bun's test runner declares its functions as globals, so a name nothing declares is Bun's
|
|
15
|
+
return identifier.name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isBunTestSpecifier(node) {
|
|
19
|
+
return (
|
|
20
|
+
node.type === 'ImportSpecifier' &&
|
|
21
|
+
node.importKind !== 'type' &&
|
|
22
|
+
node.parent.source.value === 'bun:test' &&
|
|
23
|
+
node.parent.importKind !== 'type'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default function findChainRoot(node, isThroughCalls) {
|
|
2
|
+
let current = node;
|
|
3
|
+
|
|
4
|
+
for (;;) {
|
|
5
|
+
if (current.type === 'MemberExpression') {
|
|
6
|
+
current = current.object;
|
|
7
|
+
} else if (isThroughCalls && current.type === 'CallExpression') {
|
|
8
|
+
current = current.callee;
|
|
9
|
+
} else {
|
|
10
|
+
return current.type === 'Identifier' ? current : null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default function findStaticKey(member) {
|
|
2
|
+
const property = member.property;
|
|
3
|
+
|
|
4
|
+
if (!member.computed) {
|
|
5
|
+
return property.type === 'Identifier' ? property.name : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (property.type === 'Literal' && typeof property.value === 'string') {
|
|
9
|
+
return property.value;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (property.type === 'TemplateLiteral' && property.expressions.length === 0) {
|
|
13
|
+
return property.quasis[0].value.cooked;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default function isBunTestFile(program) {
|
|
2
|
+
return program.body.some(
|
|
3
|
+
(statement) =>
|
|
4
|
+
statement.type === 'ImportDeclaration' &&
|
|
5
|
+
statement.source.value === 'bun:test' &&
|
|
6
|
+
statement.importKind !== 'type' &&
|
|
7
|
+
(statement.specifiers.length === 0 ||
|
|
8
|
+
statement.specifiers.some((specifier) => specifier.importKind !== 'type')),
|
|
9
|
+
);
|
|
10
|
+
}
|
package/no-file-snapshot.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { statSync } from 'node:fs';
|
|
2
2
|
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
import findStaticKey from './find-static-key.js';
|
|
3
4
|
|
|
4
|
-
const fileMatchers = ['toMatchSnapshot', 'toThrowErrorMatchingSnapshot'];
|
|
5
|
+
const fileMatchers = new Set(['toMatchSnapshot', 'toThrowErrorMatchingSnapshot']);
|
|
5
6
|
|
|
6
7
|
const noFileSnapshot = {
|
|
7
8
|
meta: {
|
|
@@ -14,19 +15,14 @@ const noFileSnapshot = {
|
|
|
14
15
|
schema: [],
|
|
15
16
|
},
|
|
16
17
|
create(context) {
|
|
17
|
-
const entries = fileMatchers
|
|
18
|
-
.flatMap((matcher) => [
|
|
19
|
-
`MemberExpression[computed=false] > Identifier.property[name='${matcher}']`,
|
|
20
|
-
`MemberExpression[computed=true] > Literal.property[value='${matcher}']`,
|
|
21
|
-
])
|
|
22
|
-
.map((selector) => [
|
|
23
|
-
selector,
|
|
24
|
-
(node) =>
|
|
25
|
-
context.report({ node, messageId: 'matcher', data: { matcher: getMatcherName(node) } }),
|
|
26
|
-
]);
|
|
27
|
-
|
|
28
18
|
return {
|
|
29
|
-
|
|
19
|
+
MemberExpression(node) {
|
|
20
|
+
const matcher = findStaticKey(node);
|
|
21
|
+
|
|
22
|
+
if (fileMatchers.has(matcher)) {
|
|
23
|
+
context.report({ node: node.property, messageId: 'matcher', data: { matcher } });
|
|
24
|
+
}
|
|
25
|
+
},
|
|
30
26
|
Program() {
|
|
31
27
|
const filename = context.physicalFilename;
|
|
32
28
|
const name = `${basename(filename)}.snap`;
|
|
@@ -45,10 +41,6 @@ const noFileSnapshot = {
|
|
|
45
41
|
},
|
|
46
42
|
};
|
|
47
43
|
|
|
48
|
-
function getMatcherName(node) {
|
|
49
|
-
return node.type === 'Identifier' ? node.name : node.value;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
44
|
function probeFile(path) {
|
|
53
45
|
return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
54
46
|
}
|
package/oxlintrc.json
CHANGED
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"zgeoff/test-timeout-exception-ticket": "error",
|
|
42
42
|
"zgeoff/no-test-sleep": "error",
|
|
43
43
|
"zgeoff/no-require": "error",
|
|
44
|
+
"zgeoff/test-shape": "error",
|
|
44
45
|
|
|
45
46
|
// name a value for what it holds, in full words: one letter only for a
|
|
46
47
|
// coordinate, zod's `z`, Bun's shell `$` or an unread `_` placeholder, and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/oxlint-config",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Shareable oxlint config: strict categories, house-style rules, and a custom JS plugin",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -38,6 +38,12 @@
|
|
|
38
38
|
"no-test-sleep.js",
|
|
39
39
|
"no-require.js",
|
|
40
40
|
"get-imported-name.js",
|
|
41
|
+
"test-shape.js",
|
|
42
|
+
"banned-test-calls.js",
|
|
43
|
+
"is-bun-test-file.js",
|
|
44
|
+
"find-bun-test-name.js",
|
|
45
|
+
"find-chain-root.js",
|
|
46
|
+
"find-static-key.js",
|
|
41
47
|
"import-zones.js",
|
|
42
48
|
"import-zones-schema.js",
|
|
43
49
|
"import-zones-entry.js",
|
package/plugin.js
CHANGED
|
@@ -15,6 +15,7 @@ import noTestSleep from './no-test-sleep.js';
|
|
|
15
15
|
import resolveRepoRoot from './resolve-repo-root.js';
|
|
16
16
|
import setupTestOnly from './setup-test-only.js';
|
|
17
17
|
import testBesideModule from './test-beside-module.js';
|
|
18
|
+
import testShape from './test-shape.js';
|
|
18
19
|
import testTimeoutExceptionTicket from './test-timeout-exception-ticket.js';
|
|
19
20
|
import verbContract from './verb-contract.js';
|
|
20
21
|
|
|
@@ -112,6 +113,7 @@ const plugin = {
|
|
|
112
113
|
'test-timeout-exception-ticket': testTimeoutExceptionTicket,
|
|
113
114
|
'no-test-sleep': noTestSleep,
|
|
114
115
|
'no-require': noRequire,
|
|
116
|
+
'test-shape': testShape,
|
|
115
117
|
'import-zones': importZones,
|
|
116
118
|
},
|
|
117
119
|
};
|
package/setup-test-only.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { basename } from 'node:path';
|
|
2
|
+
import bannedTestCalls from './banned-test-calls.js';
|
|
3
|
+
import findBunTestName from './find-bun-test-name.js';
|
|
4
|
+
import findChainRoot from './find-chain-root.js';
|
|
2
5
|
import getImportedName from './get-imported-name.js';
|
|
6
|
+
import isBunTestFile from './is-bun-test-file.js';
|
|
3
7
|
|
|
4
8
|
const testFilePattern = /\.test\.tsx?$/u;
|
|
5
9
|
|
|
@@ -17,6 +21,9 @@ const setupTestOnly = {
|
|
|
17
21
|
'Remove the module-level `{{name}}`: a test file declares one function, `setupTest()`, and nothing else. Inline it where it is used, swap it for a registered matcher, or move it to the shared test utils with tests of its own. A module-level `const` stays only as a skip gate that `skipIf(...)` arguments alone read.',
|
|
18
22
|
statement:
|
|
19
23
|
'Move this statement into a test or `setupTest()`: at module level a test file holds only imports, types, `setupTest()` and `test` calls.',
|
|
24
|
+
modifier:
|
|
25
|
+
'`test.{{modifier}}` is banned: use `test.skipIf(<condition>)` for a skip. Committed `skip`, `todo`, `only` and `failing` tests are not allowed.',
|
|
26
|
+
concurrent: '`test.concurrent` is banned: tests that use `onTestFinished` run in order.',
|
|
20
27
|
},
|
|
21
28
|
schema: [],
|
|
22
29
|
},
|
|
@@ -28,11 +35,16 @@ const setupTestOnly = {
|
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
const testNames = collectTestNames(context, program);
|
|
38
|
+
const isBunTest = isBunTestFile(program);
|
|
31
39
|
const constants = [];
|
|
32
40
|
let hasSetupTest = false;
|
|
33
41
|
|
|
34
42
|
for (const statement of program.body) {
|
|
35
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
allowedStatementTypes.has(statement.type) ||
|
|
45
|
+
isTestCall(statement, testNames) ||
|
|
46
|
+
(isBunTest && isShapeCall(context, statement))
|
|
47
|
+
) {
|
|
36
48
|
continue;
|
|
37
49
|
}
|
|
38
50
|
|
|
@@ -52,7 +64,15 @@ const setupTestOnly = {
|
|
|
52
64
|
continue;
|
|
53
65
|
}
|
|
54
66
|
|
|
55
|
-
|
|
67
|
+
const modifier = findBannedModifier(statement, testNames);
|
|
68
|
+
|
|
69
|
+
if (modifier === null) {
|
|
70
|
+
emitReport(context, statement);
|
|
71
|
+
} else {
|
|
72
|
+
const messageId = modifier === 'concurrent' ? 'concurrent' : 'modifier';
|
|
73
|
+
|
|
74
|
+
context.report({ node: statement, messageId, data: { modifier } });
|
|
75
|
+
}
|
|
56
76
|
}
|
|
57
77
|
|
|
58
78
|
const gates = collectSkipGates(context, constants, testNames);
|
|
@@ -134,6 +154,48 @@ function isTestRunner(node, testNames) {
|
|
|
134
154
|
return testNames.has(current.name);
|
|
135
155
|
}
|
|
136
156
|
|
|
157
|
+
// zgeoff/test-shape reports these calls in a bun:test file with advice of its own
|
|
158
|
+
function isShapeCall(context, statement) {
|
|
159
|
+
if (statement.type !== 'ExpressionStatement' || statement.expression.type !== 'CallExpression') {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const root = findChainRoot(statement.expression.callee, true);
|
|
164
|
+
|
|
165
|
+
return root !== null && bannedTestCalls.has(findBunTestName(context, root));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const bannedModifiers = new Set(['concurrent', 'skip', 'todo', 'only', 'failing']);
|
|
169
|
+
|
|
170
|
+
function findBannedModifier(statement, testNames) {
|
|
171
|
+
if (statement.type !== 'ExpressionStatement' || statement.expression.type !== 'CallExpression') {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const modifiers = [];
|
|
176
|
+
let current = statement.expression.callee;
|
|
177
|
+
|
|
178
|
+
while (current.type === 'CallExpression' || current.type === 'MemberExpression') {
|
|
179
|
+
if (current.type === 'CallExpression') {
|
|
180
|
+
current = current.callee;
|
|
181
|
+
} else {
|
|
182
|
+
if (!current.computed) {
|
|
183
|
+
modifiers.push(current.property.name);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
current = current.object;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (current.type !== 'Identifier' || !testNames.has(current.name)) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return modifiers.includes('concurrent')
|
|
195
|
+
? 'concurrent'
|
|
196
|
+
: (modifiers.find((modifier) => bannedModifiers.has(modifier)) ?? null);
|
|
197
|
+
}
|
|
198
|
+
|
|
137
199
|
function emitReport(context, statement, declarators) {
|
|
138
200
|
const name = findDeclaredName(context, statement, declarators);
|
|
139
201
|
|
package/test-shape.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import bannedTestCalls from './banned-test-calls.js';
|
|
3
|
+
import findBunTestName from './find-bun-test-name.js';
|
|
4
|
+
import findChainRoot from './find-chain-root.js';
|
|
5
|
+
import findStaticKey from './find-static-key.js';
|
|
6
|
+
import getImportedName from './get-imported-name.js';
|
|
7
|
+
import isBunTestFile from './is-bun-test-file.js';
|
|
8
|
+
|
|
9
|
+
const testFilePattern = /\.test\.tsx?$/u;
|
|
10
|
+
|
|
11
|
+
const testShape = {
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'problem',
|
|
14
|
+
messages: {
|
|
15
|
+
describe: 'Write each test as a top-level `test()` call; `describe` is banned.',
|
|
16
|
+
it: 'Write each test as a `test()` call; `it`, the alias Bun gives `test`, is banned.',
|
|
17
|
+
hook: 'Put per-test setup in `setupTest()` and cleanup in `onTestFinished()`; `{{name}}` is banned.',
|
|
18
|
+
rename:
|
|
19
|
+
'Import each `bun:test` binding by its own name, so the test-shape checks see each call.',
|
|
20
|
+
title: 'Start a test title with `it ` or with a `#<unit> ` prefix.',
|
|
21
|
+
toEqual: 'Use `toStrictEqual`, or `toMatchObject` for a partial match; `toEqual` is banned.',
|
|
22
|
+
},
|
|
23
|
+
schema: [],
|
|
24
|
+
},
|
|
25
|
+
create(context) {
|
|
26
|
+
if (
|
|
27
|
+
!testFilePattern.test(basename(context.physicalFilename)) ||
|
|
28
|
+
!isBunTestFile(context.sourceCode.ast)
|
|
29
|
+
) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
CallExpression(node) {
|
|
35
|
+
emitCallShape(context, node);
|
|
36
|
+
},
|
|
37
|
+
MemberExpression(node) {
|
|
38
|
+
if (findStaticKey(node) === 'toEqual' && isBunTestRoot(context, node.object, 'expect')) {
|
|
39
|
+
context.report({ node: node.property, messageId: 'toEqual' });
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"ImportDeclaration[source.value='bun:test'][importKind!='type']"(node) {
|
|
43
|
+
for (const specifier of node.specifiers) {
|
|
44
|
+
if (isRenamed(specifier)) {
|
|
45
|
+
context.report({ node: specifier, messageId: 'rename' });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export default testShape;
|
|
54
|
+
|
|
55
|
+
// a unit may be a dotted path, such as #sessions.enrol
|
|
56
|
+
const titlePattern = /^(?:it |#\w+(?:\.\w+)* )/u;
|
|
57
|
+
|
|
58
|
+
function emitCallShape(context, node) {
|
|
59
|
+
const callee = node.callee;
|
|
60
|
+
const root = callee.type === 'CallExpression' ? null : findChainRoot(callee, false);
|
|
61
|
+
const name = root === null ? null : findBunTestName(context, root);
|
|
62
|
+
|
|
63
|
+
if (bannedTestCalls.has(name)) {
|
|
64
|
+
const messageId = name === 'describe' || name === 'it' ? name : 'hook';
|
|
65
|
+
|
|
66
|
+
context.report({ node, messageId, data: { name } });
|
|
67
|
+
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!isBunTestRoot(context, callee, 'test')) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const [titleNode] = node.arguments;
|
|
76
|
+
const title = findTitleStart(titleNode);
|
|
77
|
+
|
|
78
|
+
if (title !== null && !titlePattern.test(title)) {
|
|
79
|
+
context.report({ node: titleNode, messageId: 'title' });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isBunTestRoot(context, node, name) {
|
|
84
|
+
const root = findChainRoot(node, true);
|
|
85
|
+
|
|
86
|
+
return root !== null && findBunTestName(context, root) === name;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function findTitleStart(node) {
|
|
90
|
+
if (node?.type === 'Literal' && typeof node.value === 'string') {
|
|
91
|
+
return node.value;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (node?.type === 'TemplateLiteral') {
|
|
95
|
+
return node.quasis[0].value.cooked ?? '';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function isRenamed(specifier) {
|
|
102
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
specifier.type === 'ImportSpecifier' &&
|
|
108
|
+
specifier.importKind !== 'type' &&
|
|
109
|
+
getImportedName(specifier) !== specifier.local.name
|
|
110
|
+
);
|
|
111
|
+
}
|