@zgeoff/oxlint-config 0.7.1 → 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/README.md +10 -2
- 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/get-imported-name.js +5 -0
- package/is-bun-test-file.js +10 -0
- package/max-test-timeout.js +1 -6
- package/no-file-snapshot.js +9 -17
- package/no-require.js +111 -0
- package/no-test-sleep.js +1 -6
- package/oxlintrc.json +5 -0
- package/package.json +11 -1
- package/plugin.js +4 -0
- package/react.json +13 -0
- package/setup-test-only.js +65 -8
- package/test-shape.js +111 -0
package/README.md
CHANGED
|
@@ -43,6 +43,10 @@ Or import the config object in `oxlint.config.ts`:
|
|
|
43
43
|
import config from '@zgeoff/oxlint-config';
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
A repo with React extends `./node_modules/@zgeoff/oxlint-config/react.json` after the base config.
|
|
47
|
+
[The React overlay](../../docs/architecture/oxlint-config.md#the-react-overlay) says what it
|
|
48
|
+
changes.
|
|
49
|
+
|
|
46
50
|
The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for configs that want the
|
|
47
51
|
`zgeoff/*` rules without the rest.
|
|
48
52
|
|
|
@@ -54,8 +58,9 @@ The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for confi
|
|
|
54
58
|
direct dependency of the consuming project. Hoisting package managers (bun's default linker, npm)
|
|
55
59
|
resolve it transitively anyway, but isolated layouts (pnpm, `bun install --linker isolated`) fail
|
|
56
60
|
with `Cannot find module '@stylistic/eslint-plugin'` unless it's installed directly.
|
|
57
|
-
- Type-aware rules activate under `oxlint --type-aware
|
|
58
|
-
|
|
61
|
+
- Type-aware rules activate under `oxlint --type-aware`, which needs a type-aware backend: install
|
|
62
|
+
`oxlint-tsgolint`, or install `@maschwenk/tsrslint` and set `OXLINT_TSGOLINT_PATH` to the path
|
|
63
|
+
that its `tsrslint-path` command prints. The config works without a backend.
|
|
59
64
|
|
|
60
65
|
## Rules
|
|
61
66
|
|
|
@@ -70,6 +75,9 @@ The plugin is addressable on its own at `@zgeoff/oxlint-config/plugin` for confi
|
|
|
70
75
|
- Test files: a test sits beside its module, holds only `setupTest()` and its tests, uses inline
|
|
71
76
|
snapshots, keeps each timeout within its lane's limit, and waits on a condition, never a sleep.
|
|
72
77
|
See [test-file rules](../../docs/architecture/oxlint-config-tests.md).
|
|
78
|
+
- Require functions: `zgeoff/no-require` bans the `require` global and `createRequire`, and
|
|
79
|
+
`import/no-commonjs` bans CommonJS exports. See
|
|
80
|
+
[require functions](../../docs/architecture/oxlint-config.md#require-functions).
|
|
73
81
|
- Import boundaries: `zgeoff/import-zones` fails an import that crosses a declared zone boundary.
|
|
74
82
|
See [import zones](../../docs/architecture/oxlint-config-import-zones.md).
|
|
75
83
|
|
|
@@ -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/max-test-timeout.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { basename } from 'node:path';
|
|
2
2
|
import buildLaneLimits from './build-lane-limits.js';
|
|
3
|
+
import getImportedName from './get-imported-name.js';
|
|
3
4
|
import resolveSuiteDirectory from './resolve-suite-directory.js';
|
|
4
5
|
|
|
5
6
|
const runnerNames = new Set(['test', 'it', 'describe']);
|
|
@@ -125,12 +126,6 @@ function findVariable(context, identifier) {
|
|
|
125
126
|
return null;
|
|
126
127
|
}
|
|
127
128
|
|
|
128
|
-
function getImportedName(specifier) {
|
|
129
|
-
return specifier.imported.type === 'Identifier'
|
|
130
|
-
? specifier.imported.name
|
|
131
|
-
: specifier.imported.value;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
129
|
function emitTimeouts(context, args, limit) {
|
|
135
130
|
for (const argument of args) {
|
|
136
131
|
const value = argument.type === 'ObjectExpression' ? findTimeoutValue(argument) : argument;
|
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/no-require.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import getImportedName from './get-imported-name.js';
|
|
2
|
+
|
|
3
|
+
const moduleSources = new Set(['node:module', 'module']);
|
|
4
|
+
|
|
5
|
+
const noRequire = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: 'problem',
|
|
8
|
+
messages: {
|
|
9
|
+
createRequire:
|
|
10
|
+
'Use `import`, never `createRequire`: a require function loads modules that the import rules cannot see.',
|
|
11
|
+
require:
|
|
12
|
+
'Use `import`, never `require`: a require function loads modules that the import rules cannot see.',
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
ImportDeclaration(node) {
|
|
19
|
+
if (node.importKind === 'type' || !moduleSources.has(node.source.value)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (const variable of context.sourceCode.getDeclaredVariables(node)) {
|
|
24
|
+
emitCreateRequireReads(context, variable);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
'Program:exit'(program) {
|
|
28
|
+
for (const reference of collectRequireReferences(context, program)) {
|
|
29
|
+
context.report({ node: reference.identifier, messageId: 'require' });
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export default noRequire;
|
|
37
|
+
|
|
38
|
+
function emitCreateRequireReads(context, variable) {
|
|
39
|
+
const [definition] = variable.defs;
|
|
40
|
+
|
|
41
|
+
if (definition.node.type === 'ImportSpecifier') {
|
|
42
|
+
if (
|
|
43
|
+
definition.node.importKind !== 'type' &&
|
|
44
|
+
getImportedName(definition.node) === 'createRequire'
|
|
45
|
+
) {
|
|
46
|
+
context.report({ node: definition.node, messageId: 'createRequire' });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// a default import of node:module is the same object as its namespace
|
|
53
|
+
for (const reference of variable.references) {
|
|
54
|
+
const node = findCreateRequireRead(reference.identifier);
|
|
55
|
+
|
|
56
|
+
if (node !== null) {
|
|
57
|
+
context.report({ node, messageId: 'createRequire' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function findCreateRequireRead(object) {
|
|
63
|
+
const parent = object.parent;
|
|
64
|
+
|
|
65
|
+
if (parent.type === 'MemberExpression' && parent.object === object) {
|
|
66
|
+
return isCreateRequireKey(parent.property, parent.computed) ? parent : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const pattern = findDestructuringPattern(parent, object);
|
|
70
|
+
|
|
71
|
+
if (pattern?.type !== 'ObjectPattern') {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
pattern.properties.find(
|
|
77
|
+
(property) =>
|
|
78
|
+
property.type === 'Property' && isCreateRequireKey(property.key, property.computed),
|
|
79
|
+
) ?? null
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isCreateRequireKey(key, isComputed) {
|
|
84
|
+
return (
|
|
85
|
+
(key.type === 'Literal' && key.value === 'createRequire') ||
|
|
86
|
+
(!isComputed && key.type === 'Identifier' && key.name === 'createRequire')
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function findDestructuringPattern(parent, object) {
|
|
91
|
+
if (parent.type === 'VariableDeclarator' && parent.init === object) {
|
|
92
|
+
return parent.id;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (parent.type === 'AssignmentExpression' && parent.right === object) {
|
|
96
|
+
return parent.left;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// `env.node` declares `require` as a global, so a read resolves to that variable; without it,
|
|
103
|
+
// the read stays unresolved and passes through the global scope
|
|
104
|
+
function collectRequireReferences(context, program) {
|
|
105
|
+
const scope = context.sourceCode.getScope(program);
|
|
106
|
+
const declared = scope.set.get('require');
|
|
107
|
+
const declaredReferences = declared?.defs.length === 0 ? declared.references : [];
|
|
108
|
+
const unresolved = scope.through.filter((reference) => reference.identifier.name === 'require');
|
|
109
|
+
|
|
110
|
+
return [...new Set([...declaredReferences, ...unresolved])];
|
|
111
|
+
}
|
package/no-test-sleep.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { basename } from 'node:path';
|
|
2
2
|
import findFunctionName from './find-function-name.js';
|
|
3
|
+
import getImportedName from './get-imported-name.js';
|
|
3
4
|
|
|
4
5
|
const testFilePattern = /\.test\.tsx?$/u;
|
|
5
6
|
|
|
@@ -100,12 +101,6 @@ function emitSleepReads(context, variable, names) {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
function getImportedName(specifier) {
|
|
104
|
-
return specifier.imported.type === 'Identifier'
|
|
105
|
-
? specifier.imported.name
|
|
106
|
-
: specifier.imported.value;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
104
|
function isWrappedTimeout(call) {
|
|
110
105
|
const callee = call.callee;
|
|
111
106
|
const isTimeout =
|
package/oxlintrc.json
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
"zgeoff/max-test-timeout": "error",
|
|
41
41
|
"zgeoff/test-timeout-exception-ticket": "error",
|
|
42
42
|
"zgeoff/no-test-sleep": "error",
|
|
43
|
+
"zgeoff/no-require": "error",
|
|
44
|
+
"zgeoff/test-shape": "error",
|
|
43
45
|
|
|
44
46
|
// name a value for what it holds, in full words: one letter only for a
|
|
45
47
|
// coordinate, zod's `z`, Bun's shell `$` or an unread `_` placeholder, and
|
|
@@ -107,6 +109,9 @@
|
|
|
107
109
|
"typescript/consistent-type-assertions": ["error", { "assertionStyle": "never" }],
|
|
108
110
|
"typescript/no-unsafe-type-assertion": "off",
|
|
109
111
|
"import/no-cycle": "error",
|
|
112
|
+
"import/no-commonjs": "error",
|
|
113
|
+
// zod's documented import form is `import * as z from 'zod'`
|
|
114
|
+
"import/no-namespace": ["error", { "ignore": ["zod", "zod/*"] }],
|
|
110
115
|
|
|
111
116
|
"no-shadow": "error",
|
|
112
117
|
|
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": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"oxlintrc.json",
|
|
13
|
+
"react.json",
|
|
13
14
|
"plugin.js",
|
|
14
15
|
"function-verb.js",
|
|
15
16
|
"verb-contract.js",
|
|
@@ -35,6 +36,14 @@
|
|
|
35
36
|
"build-lane-limits.js",
|
|
36
37
|
"resolve-suite-directory.js",
|
|
37
38
|
"no-test-sleep.js",
|
|
39
|
+
"no-require.js",
|
|
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",
|
|
38
47
|
"import-zones.js",
|
|
39
48
|
"import-zones-schema.js",
|
|
40
49
|
"import-zones-entry.js",
|
|
@@ -55,6 +64,7 @@
|
|
|
55
64
|
"exports": {
|
|
56
65
|
".": "./index.js",
|
|
57
66
|
"./oxlintrc.json": "./oxlintrc.json",
|
|
67
|
+
"./react.json": "./react.json",
|
|
58
68
|
"./plugin": "./plugin.js",
|
|
59
69
|
"./import-zones": {
|
|
60
70
|
"types": "./import-zones-entry.d.ts",
|
package/plugin.js
CHANGED
|
@@ -9,11 +9,13 @@ import noCommentRuleOverride from './no-comment-rule-override.js';
|
|
|
9
9
|
import noFileSnapshot from './no-file-snapshot.js';
|
|
10
10
|
import noHiddenCast from './no-hidden-cast.js';
|
|
11
11
|
import noJSDoc from './no-jsdoc.js';
|
|
12
|
+
import noRequire from './no-require.js';
|
|
12
13
|
import noRestrictedWords from './no-restricted-words.js';
|
|
13
14
|
import noTestSleep from './no-test-sleep.js';
|
|
14
15
|
import resolveRepoRoot from './resolve-repo-root.js';
|
|
15
16
|
import setupTestOnly from './setup-test-only.js';
|
|
16
17
|
import testBesideModule from './test-beside-module.js';
|
|
18
|
+
import testShape from './test-shape.js';
|
|
17
19
|
import testTimeoutExceptionTicket from './test-timeout-exception-ticket.js';
|
|
18
20
|
import verbContract from './verb-contract.js';
|
|
19
21
|
|
|
@@ -110,6 +112,8 @@ const plugin = {
|
|
|
110
112
|
'max-test-timeout': buildMaxTestTimeout(resolveRepoRoot),
|
|
111
113
|
'test-timeout-exception-ticket': testTimeoutExceptionTicket,
|
|
112
114
|
'no-test-sleep': noTestSleep,
|
|
115
|
+
'no-require': noRequire,
|
|
116
|
+
'test-shape': testShape,
|
|
113
117
|
'import-zones': importZones,
|
|
114
118
|
},
|
|
115
119
|
};
|
package/react.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://raw.githubusercontent.com/oxc-project/oxc/main/npm/oxlint/configuration_schema.json",
|
|
3
|
+
"plugins": ["react"],
|
|
4
|
+
"rules": {
|
|
5
|
+
// React 17's JSX runtime and later need no React in scope
|
|
6
|
+
"react/react-in-jsx-scope": "off",
|
|
7
|
+
// forwarding a whole props object is how a component wraps another, more so with
|
|
8
|
+
// zgeoff/no-destructured-params in force
|
|
9
|
+
"react/jsx-props-no-spreading": "off",
|
|
10
|
+
// form and layout composition nests deeper than any fixed limit
|
|
11
|
+
"react/jsx-max-depth": "off"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/setup-test-only.js
CHANGED
|
@@ -1,4 +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';
|
|
5
|
+
import getImportedName from './get-imported-name.js';
|
|
6
|
+
import isBunTestFile from './is-bun-test-file.js';
|
|
2
7
|
|
|
3
8
|
const testFilePattern = /\.test\.tsx?$/u;
|
|
4
9
|
|
|
@@ -16,6 +21,9 @@ const setupTestOnly = {
|
|
|
16
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.',
|
|
17
22
|
statement:
|
|
18
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.',
|
|
19
27
|
},
|
|
20
28
|
schema: [],
|
|
21
29
|
},
|
|
@@ -27,11 +35,16 @@ const setupTestOnly = {
|
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
const testNames = collectTestNames(context, program);
|
|
38
|
+
const isBunTest = isBunTestFile(program);
|
|
30
39
|
const constants = [];
|
|
31
40
|
let hasSetupTest = false;
|
|
32
41
|
|
|
33
42
|
for (const statement of program.body) {
|
|
34
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
allowedStatementTypes.has(statement.type) ||
|
|
45
|
+
isTestCall(statement, testNames) ||
|
|
46
|
+
(isBunTest && isShapeCall(context, statement))
|
|
47
|
+
) {
|
|
35
48
|
continue;
|
|
36
49
|
}
|
|
37
50
|
|
|
@@ -51,7 +64,15 @@ const setupTestOnly = {
|
|
|
51
64
|
continue;
|
|
52
65
|
}
|
|
53
66
|
|
|
54
|
-
|
|
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
|
+
}
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
const gates = collectSkipGates(context, constants, testNames);
|
|
@@ -95,12 +116,6 @@ function collectTestNames(context, program) {
|
|
|
95
116
|
return new Set(names);
|
|
96
117
|
}
|
|
97
118
|
|
|
98
|
-
function getImportedName(specifier) {
|
|
99
|
-
return specifier.imported.type === 'Identifier'
|
|
100
|
-
? specifier.imported.name
|
|
101
|
-
: specifier.imported.value;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
119
|
// ESM puts module-level bindings in a module scope nested under the global one
|
|
105
120
|
function hasModuleBinding(context, program, name) {
|
|
106
121
|
const scope = context.sourceCode.getScope(program);
|
|
@@ -139,6 +154,48 @@ function isTestRunner(node, testNames) {
|
|
|
139
154
|
return testNames.has(current.name);
|
|
140
155
|
}
|
|
141
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
|
+
|
|
142
199
|
function emitReport(context, statement, declarators) {
|
|
143
200
|
const name = findDeclaredName(context, statement, declarators);
|
|
144
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
|
+
}
|