@zgeoff/oxlint-config 0.7.2 → 0.7.4

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.
@@ -0,0 +1,10 @@
1
+ const bannedTestCalls = new Set([
2
+ 'describe',
3
+ 'it',
4
+ 'beforeAll',
5
+ 'beforeEach',
6
+ 'afterAll',
7
+ 'afterEach',
8
+ ]);
9
+
10
+ export default bannedTestCalls;
@@ -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
+ }
@@ -7,13 +7,15 @@ const runnerNames = new Set(['test', 'it', 'describe']);
7
7
 
8
8
  const unitLimitMs = 5000;
9
9
 
10
- export default function buildMaxTestTimeout(resolveRoot) {
10
+ export default function buildMaxTestTimeout(resolveRoot, rigLanes) {
11
11
  return {
12
12
  meta: {
13
13
  type: 'problem',
14
14
  messages: {
15
15
  timeout:
16
16
  'This timeout of {{timeoutMs}} ms is above the {{limitMs}} ms limit of the {{lane}} lane. Find its cause in the "Slow tests" section of the testing skill and fix it; a raised timeout only hides the cost.',
17
+ rigLanes:
18
+ 'Declare lanes in rig.config.ts only: this repo has one, and the `lanes` option here would give the limits a second source.',
17
19
  },
18
20
  schema: [
19
21
  {
@@ -31,7 +33,7 @@ export default function buildMaxTestTimeout(resolveRoot) {
31
33
  ],
32
34
  },
33
35
  create(context) {
34
- const limits = buildLaneLimits(context.options[0]?.lanes);
36
+ const limits = rigLanes ?? buildLaneLimits(context.options[0]?.lanes);
35
37
 
36
38
  const filename = context.physicalFilename;
37
39
  const lane = resolveLane(resolveRoot(filename) ?? context.cwd, filename, limits);
@@ -41,6 +43,14 @@ export default function buildMaxTestTimeout(resolveRoot) {
41
43
  };
42
44
 
43
45
  return {
46
+ Program() {
47
+ if (rigLanes !== null && context.options[0]?.lanes !== undefined) {
48
+ context.report({
49
+ loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
50
+ messageId: 'rigLanes',
51
+ });
52
+ }
53
+ },
44
54
  CallExpression(node) {
45
55
  // the inner call of `test.each(rows)(…)` takes the rows, not a timeout
46
56
  if (node.parent.type === 'CallExpression' && node.parent.callee === node) {
@@ -68,7 +78,8 @@ const laneTestPattern = /\.(?<lane>[^.]+)\.test\.tsx?$/u;
68
78
  function resolveLane(root, filename, limits) {
69
79
  const lane = laneTestPattern.exec(basename(filename))?.groups?.lane;
70
80
 
71
- if (lane !== undefined && limits.has(lane)) {
81
+ // the e2e lane is a directory, so a `.e2e` suffix outside it leaves the file in the unit run
82
+ if (lane !== undefined && lane !== 'e2e' && limits.has(lane)) {
72
83
  return lane;
73
84
  }
74
85
 
@@ -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
- ...Object.fromEntries(entries),
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.2",
3
+ "version": "0.7.4",
4
4
  "description": "Shareable oxlint config: strict categories, house-style rules, and a custom JS plugin",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,10 +34,17 @@
34
34
  "max-test-timeout.js",
35
35
  "test-timeout-exception-ticket.js",
36
36
  "build-lane-limits.js",
37
+ "read-rig-lanes.js",
37
38
  "resolve-suite-directory.js",
38
39
  "no-test-sleep.js",
39
40
  "no-require.js",
40
41
  "get-imported-name.js",
42
+ "test-shape.js",
43
+ "banned-test-calls.js",
44
+ "is-bun-test-file.js",
45
+ "find-bun-test-name.js",
46
+ "find-chain-root.js",
47
+ "find-static-key.js",
41
48
  "import-zones.js",
42
49
  "import-zones-schema.js",
43
50
  "import-zones-entry.js",
package/plugin.js CHANGED
@@ -1,3 +1,7 @@
1
+ import { existsSync } from 'node:fs';
2
+ // oxlint-disable-next-line zgeoff/no-require -- only resolves rig's path from the repo root; an import loads it
3
+ import { createRequire } from 'node:module';
4
+ import { join } from 'node:path';
1
5
  import functionVerb from './function-verb.js';
2
6
  import importZones from './import-zones.js';
3
7
  import maxConsecutiveLineComments from './max-consecutive-line-comments.js';
@@ -12,12 +16,25 @@ import noJSDoc from './no-jsdoc.js';
12
16
  import noRequire from './no-require.js';
13
17
  import noRestrictedWords from './no-restricted-words.js';
14
18
  import noTestSleep from './no-test-sleep.js';
19
+ import readRigLanes from './read-rig-lanes.js';
15
20
  import resolveRepoRoot from './resolve-repo-root.js';
16
21
  import setupTestOnly from './setup-test-only.js';
17
- import testBesideModule from './test-beside-module.js';
22
+ import buildTestBesideModule from './test-beside-module.js';
23
+ import testShape from './test-shape.js';
18
24
  import testTimeoutExceptionTicket from './test-timeout-exception-ticket.js';
19
25
  import verbContract from './verb-contract.js';
20
26
 
27
+ // A repo with a rig.config.ts takes its lane limits from it, read once when oxlint loads the plugin.
28
+ const rigLanes = await readRigLanes(process.cwd(), {
29
+ resolveRepoRoot,
30
+ probeFile: existsSync,
31
+ importModule: (url) => import(url),
32
+
33
+ // The parser comes from the @zgeoff/rig that the repo's rig.config.ts imports, so both read one
34
+ // version of the resolved table. A bare import would resolve from this package's folder instead.
35
+ resolveRepoRig: (root) => createRequire(join(root, 'package.json')).resolve('@zgeoff/rig'),
36
+ });
37
+
21
38
  const plugin = {
22
39
  meta: { name: 'zgeoff' },
23
40
  rules: {
@@ -105,13 +122,14 @@ const plugin = {
105
122
  'module-order': moduleOrder,
106
123
  'no-restricted-words': noRestrictedWords,
107
124
  'no-abbreviated-names': noAbbreviatedNames,
108
- 'test-beside-module': testBesideModule,
125
+ 'test-beside-module': buildTestBesideModule(rigLanes),
109
126
  'no-file-snapshot': noFileSnapshot,
110
127
  'setup-test-only': setupTestOnly,
111
- 'max-test-timeout': buildMaxTestTimeout(resolveRepoRoot),
128
+ 'max-test-timeout': buildMaxTestTimeout(resolveRepoRoot, rigLanes),
112
129
  'test-timeout-exception-ticket': testTimeoutExceptionTicket,
113
130
  'no-test-sleep': noTestSleep,
114
131
  'no-require': noRequire,
132
+ 'test-shape': testShape,
115
133
  'import-zones': importZones,
116
134
  },
117
135
  };
@@ -0,0 +1,53 @@
1
+ import { join } from 'node:path';
2
+ import { pathToFileURL } from 'node:url';
3
+
4
+ // Returns the limit in ms of each lane that the repo's rig.config.ts resolves, keyed by the lane
5
+ // suffix without its dot, and `e2e` for the directory lane. Returns null for a repo with no
6
+ // rig.config.ts, which leaves the rules on their own lane table.
7
+ export default async function readRigLanes(cwd, dependencies) {
8
+ const root = dependencies.resolveRepoRoot(join(cwd, 'oxlint'));
9
+
10
+ if (root === null) {
11
+ return null;
12
+ }
13
+
14
+ const configPath = join(root, 'rig.config.ts');
15
+
16
+ if (!dependencies.probeFile(configPath)) {
17
+ return null;
18
+ }
19
+
20
+ const config = await importConfig(configPath, dependencies.importModule);
21
+ const rig = await dependencies.importModule(
22
+ pathToFileURL(dependencies.resolveRepoRig(root)).href,
23
+ );
24
+ const parsed = rig.parseRigConfig(config.default);
25
+
26
+ if (parsed.kind === 'refused') {
27
+ throw new TypeError(
28
+ `${configPath} must default-export the result of defineRig from @zgeoff/rig: ${parsed.reason}`,
29
+ );
30
+ }
31
+
32
+ return new Map(
33
+ parsed.config.lanes
34
+ .filter((lane) => lane.name !== 'unit')
35
+ .map((lane) => [lane.suffix === null ? lane.name : lane.suffix.slice(1), lane.limitMs]),
36
+ );
37
+ }
38
+
39
+ // oxlint runs a plugin under Node, which strips the types of a .ts file from Node 22.18, and Bun
40
+ // runs one as it is.
41
+ async function importConfig(configPath, importModule) {
42
+ try {
43
+ return await importModule(pathToFileURL(configPath).href);
44
+ } catch (error) {
45
+ // oxlint prints a plugin's load error without its cause, so the message carries the cause's.
46
+ const reason = error instanceof Error ? error.message : String(error);
47
+
48
+ throw new Error(
49
+ `cannot load ${configPath}: ${reason} (reading a TypeScript config needs Node >= 22.18 or Bun)`,
50
+ { cause: error },
51
+ );
52
+ }
53
+ }
@@ -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 (allowedStatementTypes.has(statement.type) || isTestCall(statement, testNames)) {
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
- emitReport(context, statement);
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
 
@@ -6,64 +6,70 @@ import resolveSuiteDirectory from './resolve-suite-directory.js';
6
6
 
7
7
  const testFilePattern = /^(?<base>.+)\.test\.tsx?$/u;
8
8
 
9
- const testBesideModule = {
10
- meta: {
11
- type: 'problem',
12
- messages: {
13
- missing:
14
- 'Move this test beside the module it tests: no `{{name}}.<ext>` sits in this directory. A flow across several packages belongs in `integration/`, and a test that drives the built product in `e2e/`, at the repo root or an app root.',
15
- },
16
- schema: [
17
- {
18
- type: 'object',
19
- properties: {
20
- lanes: {
21
- anyOf: [
22
- { type: 'array', items: { type: 'string' } },
23
- { type: 'object', additionalProperties: { type: 'integer', minimum: 0 } },
24
- ],
9
+ export default function buildTestBesideModule(rigLanes) {
10
+ return {
11
+ meta: {
12
+ type: 'problem',
13
+ messages: {
14
+ missing:
15
+ 'Move this test beside the module it tests: no `{{name}}.<ext>` sits in this directory. A flow across several packages belongs in `integration/`, and a test that drives the built product in `e2e/`, at the repo root or an app root.',
16
+ rigLanes:
17
+ 'Declare lanes in rig.config.ts only: this repo has one, and the `lanes` option here would give the lanes a second source.',
18
+ },
19
+ schema: [
20
+ {
21
+ type: 'object',
22
+ properties: {
23
+ lanes: {
24
+ anyOf: [
25
+ { type: 'array', items: { type: 'string' } },
26
+ { type: 'object', additionalProperties: { type: 'integer', minimum: 0 } },
27
+ ],
28
+ },
25
29
  },
30
+ additionalProperties: false,
26
31
  },
27
- additionalProperties: false,
28
- },
29
- ],
30
- },
31
- create(context) {
32
- const lanes = buildLaneLimits(context.options[0]?.lanes);
32
+ ],
33
+ },
34
+ create(context) {
35
+ const lanes = rigLanes ?? buildLaneLimits(context.options[0]?.lanes);
33
36
 
34
- return {
35
- Program() {
36
- const filename = context.physicalFilename;
37
- const match = testFilePattern.exec(basename(filename));
37
+ return {
38
+ Program() {
39
+ const start = context.sourceCode.ast.body[0]?.loc.start ?? { line: 1, column: 0 };
38
40
 
39
- if (
40
- match === null ||
41
- resolveSuiteDirectory(resolveRepoRoot(filename) ?? context.cwd, filename) !== null
42
- ) {
43
- return;
44
- }
41
+ if (rigLanes !== null && context.options[0]?.lanes !== undefined) {
42
+ context.report({ loc: { start, end: start }, messageId: 'rigLanes' });
43
+ }
45
44
 
46
- const name = toModuleName(match.groups.base, lanes);
45
+ const filename = context.physicalFilename;
46
+ const match = testFilePattern.exec(basename(filename));
47
47
 
48
- if (probeModuleSibling(dirname(filename), name)) {
49
- return;
50
- }
48
+ if (
49
+ match === null ||
50
+ resolveSuiteDirectory(resolveRepoRoot(filename) ?? context.cwd, filename) !== null
51
+ ) {
52
+ return;
53
+ }
51
54
 
52
- // anchor on the first statement, not offset 0, so a disable comment
53
- // at the top of the file sits before the report and covers it
54
- const start = context.sourceCode.ast.body[0]?.loc.start ?? { line: 1, column: 0 };
55
+ const name = toModuleName(match.groups.base, lanes);
55
56
 
56
- context.report({
57
- loc: { start, end: start },
58
- messageId: 'missing',
59
- data: { name },
60
- });
61
- },
62
- };
63
- },
64
- };
57
+ if (probeModuleSibling(dirname(filename), name)) {
58
+ return;
59
+ }
65
60
 
66
- export default testBesideModule;
61
+ // anchor on the first statement, not offset 0, so a disable comment
62
+ // at the top of the file sits before the report and covers it
63
+ context.report({
64
+ loc: { start, end: start },
65
+ messageId: 'missing',
66
+ data: { name },
67
+ });
68
+ },
69
+ };
70
+ },
71
+ };
72
+ }
67
73
 
68
74
  function toModuleName(base, lanes) {
69
75
  const dot = base.lastIndexOf('.');
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
+ }