@zgeoff/oxlint-config 0.5.1 → 0.5.2

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.
@@ -2,9 +2,6 @@ import { existsSync, readFileSync, statSync } from 'node:fs';
2
2
  import { isBuiltin } from 'node:module';
3
3
  import { dirname, join, resolve } from 'node:path';
4
4
 
5
- // a `.js` specifier in TypeScript source names the `.ts` file beside it
6
- const sourceExtensions = { '.js': ['.ts', '.tsx'], '.mjs': ['.mts'], '.cjs': ['.cts'] };
7
-
8
5
  // `module` for a package or builtin, `file` with the absolute path, or
9
6
  // `missing`. The own name, which `self` marks, resolves through `exports`, `#`
10
7
  // names through `imports`, and a workspace name to a path in its folder.
@@ -18,70 +15,37 @@ export default function resolveImportTarget(filename, specifier, settings) {
18
15
  return resolveManifestTarget(manifest, specifier, settings);
19
16
  }
20
17
 
21
- function resolveManifestTarget(manifest, specifier, settings) {
22
- if (!specifier.startsWith('#')) {
23
- return resolveBareTarget(manifest, specifier, settings);
24
- }
25
-
26
- const mapped = findMappedTarget(manifest?.imports, specifier, settings.conditions);
27
-
28
- return mapped !== null && !isPathSpecifier(mapped)
29
- ? resolveBareTarget(manifest, mapped, settings)
30
- : toMappedTarget(manifest, mapped);
31
- }
32
-
33
- // a builtin keeps its name even when a package shares it
34
- function resolveBareTarget(manifest, specifier, settings) {
35
- const name = manifest?.name;
36
-
37
- if (isBuiltin(specifier)) {
38
- return { kind: 'module', specifier };
39
- }
40
-
41
- if (isPackageSpecifier(name, specifier)) {
42
- const subpath = `.${specifier.slice(name.length)}`;
43
- const mapped = findMappedTarget(toSubpathMap(manifest.exports), subpath, settings.conditions);
44
-
45
- return { ...toMappedTarget(manifest, mapped), self: true };
46
- }
47
-
48
- return resolveWorkspaceTarget(settings.workspaces, specifier);
18
+ function isPathSpecifier(specifier) {
19
+ return /^[./]/u.test(specifier);
49
20
  }
50
21
 
51
- function isPackageSpecifier(name, specifier) {
52
- return typeof name === 'string' && (specifier === name || specifier.startsWith(`${name}/`));
22
+ function toTarget(path) {
23
+ return path === null ? { kind: 'missing' } : { kind: 'file', path };
53
24
  }
54
25
 
55
- // the edges judge the package folder joined with the subpath, such as
56
- // `packages/store/testing`, so the exports map's keys matter and not its targets
57
- function resolveWorkspaceTarget(workspaces, specifier) {
58
- const workspace = workspaces.find((candidate) => isPackageSpecifier(candidate.name, specifier));
26
+ // a `.js` specifier in TypeScript source names the `.ts` file beside it
27
+ const sourceExtensions = { '.js': ['.ts', '.tsx'], '.mjs': ['.mts'], '.cjs': ['.cts'] };
59
28
 
60
- if (workspace === undefined) {
61
- return { kind: 'module', specifier };
62
- }
29
+ function resolveImportedFile(filename, specifier, extensions) {
30
+ const base = resolve(dirname(filename), specifier);
31
+ const swapped = Object.keys(sourceExtensions).flatMap((ext) =>
32
+ base.endsWith(ext)
33
+ ? sourceExtensions[ext].map((other) => `${base.slice(0, -ext.length)}${other}`)
34
+ : [],
35
+ );
63
36
 
64
- const subpath = `.${specifier.slice(workspace.name.length)}`;
37
+ const candidates = [
38
+ base,
39
+ ...swapped,
40
+ ...extensions.map((ext) => `${base}${ext}`),
41
+ ...extensions.map((ext) => join(base, `index${ext}`)),
42
+ ];
65
43
 
66
- return isExportedSubpath(workspace.exports, subpath)
67
- ? { kind: 'file', path: join(workspace.dir, subpath) }
68
- : { kind: 'missing' };
44
+ return candidates.find((candidate) => probeFile(candidate)) ?? null;
69
45
  }
70
46
 
71
- // with no exports map, every subpath is public, and an empty one exports none
72
- function isExportedSubpath(exports, subpath) {
73
- if (exports === undefined) {
74
- return true;
75
- }
76
-
77
- if (typeof exports === 'object' && exports !== null && Object.keys(exports).length === 0) {
78
- return false;
79
- }
80
-
81
- const map = toSubpathMap(exports);
82
- const key = Object.hasOwn(map, subpath) ? subpath : findPatternKey(map, subpath);
83
-
84
- return key !== undefined && map[key] !== null;
47
+ function probeFile(path) {
48
+ return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
85
49
  }
86
50
 
87
51
  // a bundler query such as Vite's `./styles.css?url` names the file before it
@@ -91,51 +55,60 @@ function toFileSpecifier(specifier) {
91
55
  return query === -1 ? specifier : specifier.slice(0, query);
92
56
  }
93
57
 
94
- function toTarget(path) {
95
- return path === null ? { kind: 'missing' } : { kind: 'file', path };
96
- }
97
-
98
- function toMappedTarget(manifest, mapped) {
99
- return mapped === null
100
- ? { kind: 'missing' }
101
- : { kind: 'file', path: resolve(manifest.dir, mapped) };
102
- }
103
-
104
- function isPathSpecifier(specifier) {
105
- return /^[./]/u.test(specifier);
106
- }
107
-
108
58
  const manifests = new Map();
109
59
 
110
- function readPackageManifest(dir, root) {
111
- if (manifests.has(dir)) {
112
- return manifests.get(dir);
60
+ function readPackageManifest(directory, root) {
61
+ if (manifests.has(directory)) {
62
+ return manifests.get(directory);
113
63
  }
114
64
 
115
- const path = join(dir, 'package.json');
116
- const parent = dirname(dir);
65
+ const path = join(directory, 'package.json');
66
+ const parent = dirname(directory);
117
67
  let manifest = null;
118
68
 
119
69
  if (existsSync(path)) {
120
- manifest = { dir, ...JSON.parse(readFileSync(path, 'utf8')) };
121
- } else if (dir !== root && parent !== dir) {
70
+ manifest = { directory, ...JSON.parse(readFileSync(path, 'utf8')) };
71
+ } else if (directory !== root && parent !== directory) {
122
72
  manifest = readPackageManifest(parent, root);
123
73
  }
124
74
 
125
- manifests.set(dir, manifest);
75
+ manifests.set(directory, manifest);
126
76
 
127
77
  return manifest;
128
78
  }
129
79
 
130
- // `exports` without `.` keys is the target of the package's own name alone
131
- function toSubpathMap(exports) {
132
- const isSubpathMap =
133
- typeof exports === 'object' &&
134
- exports !== null &&
135
- !Array.isArray(exports) &&
136
- Object.keys(exports).some((key) => key.startsWith('.'));
80
+ function resolveManifestTarget(manifest, specifier, settings) {
81
+ if (!specifier.startsWith('#')) {
82
+ return resolveBareTarget(manifest, specifier, settings);
83
+ }
137
84
 
138
- return isSubpathMap || exports === undefined ? exports : { '.': exports };
85
+ const mapped = findMappedTarget(manifest?.imports, specifier, settings.conditions);
86
+
87
+ return mapped !== null && !isPathSpecifier(mapped)
88
+ ? resolveBareTarget(manifest, mapped, settings)
89
+ : toMappedTarget(manifest, mapped);
90
+ }
91
+
92
+ // a builtin keeps its name even when a package shares it
93
+ function resolveBareTarget(manifest, specifier, settings) {
94
+ const name = manifest?.name;
95
+
96
+ if (isBuiltin(specifier)) {
97
+ return { kind: 'module', specifier };
98
+ }
99
+
100
+ if (isPackageSpecifier(name, specifier)) {
101
+ const subpath = `.${specifier.slice(name.length)}`;
102
+ const mapped = findMappedTarget(toSubpathMap(manifest.exports), subpath, settings.conditions);
103
+
104
+ return { ...toMappedTarget(manifest, mapped), self: true };
105
+ }
106
+
107
+ return resolveWorkspaceTarget(settings.workspaces, specifier);
108
+ }
109
+
110
+ function isPackageSpecifier(name, specifier) {
111
+ return typeof name === 'string' && (specifier === name || specifier.startsWith(`${name}/`));
139
112
  }
140
113
 
141
114
  // an exact key wins; otherwise the `*` pattern with the longest prefix, then
@@ -154,6 +127,30 @@ function findMappedTarget(map, key, conditions) {
154
127
  return pattern === undefined ? null : findPatternTarget(map, pattern, { key, conditions });
155
128
  }
156
129
 
130
+ // node's rule: the first key, in object order, that names an active
131
+ // condition; in an array, the first entry that yields a target
132
+ function findConditionTarget(value, conditions) {
133
+ if (typeof value === 'string') {
134
+ return value;
135
+ }
136
+
137
+ if (typeof value !== 'object' || value === null) {
138
+ return null;
139
+ }
140
+
141
+ const candidates = Array.isArray(value)
142
+ ? value
143
+ : Object.keys(value)
144
+ .filter((condition) => conditions.has(condition))
145
+ .map((condition) => value[condition]);
146
+
147
+ return (
148
+ candidates
149
+ .map((candidate) => findConditionTarget(candidate, conditions))
150
+ .find((target) => target !== null) ?? null
151
+ );
152
+ }
153
+
157
154
  function findPatternKey(map, key) {
158
155
  const [pattern] = Object.keys(map)
159
156
  .filter((candidate) => isPatternMatch(candidate, key))
@@ -166,14 +163,6 @@ function findPatternKey(map, key) {
166
163
  return pattern;
167
164
  }
168
165
 
169
- function findPatternTarget(map, pattern, lookup) {
170
- const star = pattern.indexOf('*');
171
- const match = lookup.key.slice(star, lookup.key.length - (pattern.length - star - 1));
172
- const target = findConditionTarget(map[pattern], lookup.conditions);
173
-
174
- return target === null ? null : target.replaceAll('*', match);
175
- }
176
-
177
166
  function isPatternMatch(pattern, key) {
178
167
  const star = pattern.indexOf('*');
179
168
 
@@ -187,48 +176,59 @@ function isPatternMatch(pattern, key) {
187
176
  return key.length >= pattern.length && key.startsWith(prefix) && key.endsWith(suffix);
188
177
  }
189
178
 
190
- // node's rule: the first key, in object order, that names an active
191
- // condition; in an array, the first entry that yields a target
192
- function findConditionTarget(value, conditions) {
193
- if (typeof value === 'string') {
194
- return value;
195
- }
179
+ function findPatternTarget(map, pattern, lookup) {
180
+ const star = pattern.indexOf('*');
181
+ const match = lookup.key.slice(star, lookup.key.length - (pattern.length - star - 1));
182
+ const target = findConditionTarget(map[pattern], lookup.conditions);
196
183
 
197
- if (typeof value !== 'object' || value === null) {
198
- return null;
199
- }
184
+ return target === null ? null : target.replaceAll('*', match);
185
+ }
200
186
 
201
- const candidates = Array.isArray(value)
202
- ? value
203
- : Object.keys(value)
204
- .filter((condition) => conditions.has(condition))
205
- .map((condition) => value[condition]);
187
+ // `exports` without `.` keys is the target of the package's own name alone
188
+ function toSubpathMap(exports) {
189
+ const isSubpathMap =
190
+ typeof exports === 'object' &&
191
+ exports !== null &&
192
+ !Array.isArray(exports) &&
193
+ Object.keys(exports).some((key) => key.startsWith('.'));
206
194
 
207
- return (
208
- candidates
209
- .map((candidate) => findConditionTarget(candidate, conditions))
210
- .find((target) => target !== null) ?? null
211
- );
195
+ return isSubpathMap || exports === undefined ? exports : { '.': exports };
212
196
  }
213
197
 
214
- function resolveImportedFile(filename, specifier, extensions) {
215
- const base = resolve(dirname(filename), specifier);
216
- const swapped = Object.keys(sourceExtensions).flatMap((ext) =>
217
- base.endsWith(ext)
218
- ? sourceExtensions[ext].map((other) => `${base.slice(0, -ext.length)}${other}`)
219
- : [],
220
- );
198
+ function toMappedTarget(manifest, mapped) {
199
+ return mapped === null
200
+ ? { kind: 'missing' }
201
+ : { kind: 'file', path: resolve(manifest.directory, mapped) };
202
+ }
221
203
 
222
- const candidates = [
223
- base,
224
- ...swapped,
225
- ...extensions.map((ext) => `${base}${ext}`),
226
- ...extensions.map((ext) => join(base, `index${ext}`)),
227
- ];
204
+ // the edges judge the package folder joined with the subpath, such as
205
+ // `packages/store/testing`, so the exports map's keys matter and not its targets
206
+ function resolveWorkspaceTarget(workspaces, specifier) {
207
+ const workspace = workspaces.find((candidate) => isPackageSpecifier(candidate.name, specifier));
228
208
 
229
- return candidates.find((candidate) => probeFile(candidate)) ?? null;
209
+ if (workspace === undefined) {
210
+ return { kind: 'module', specifier };
211
+ }
212
+
213
+ const subpath = `.${specifier.slice(workspace.name.length)}`;
214
+
215
+ return isExportedSubpath(workspace.exports, subpath)
216
+ ? { kind: 'file', path: join(workspace.directory, subpath) }
217
+ : { kind: 'missing' };
230
218
  }
231
219
 
232
- function probeFile(path) {
233
- return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
220
+ // with no exports map, every subpath is public, and an empty one exports none
221
+ function isExportedSubpath(exports, subpath) {
222
+ if (exports === undefined) {
223
+ return true;
224
+ }
225
+
226
+ if (typeof exports === 'object' && exports !== null && Object.keys(exports).length === 0) {
227
+ return false;
228
+ }
229
+
230
+ const map = toSubpathMap(exports);
231
+ const key = Object.hasOwn(map, subpath) ? subpath : findPatternKey(map, subpath);
232
+
233
+ return key !== undefined && map[key] !== null;
234
234
  }
@@ -4,17 +4,17 @@ import { dirname, join } from 'node:path';
4
4
  // the nearest ancestor holding `.git` (a directory, or a worktree's file), or
5
5
  // null outside a repository
6
6
  export default function resolveRepoRoot(filename) {
7
- let dir = dirname(filename);
7
+ let directory = dirname(filename);
8
8
 
9
- while (!existsSync(join(dir, '.git'))) {
10
- const parent = dirname(dir);
9
+ while (!existsSync(join(directory, '.git'))) {
10
+ const parent = dirname(directory);
11
11
 
12
- if (parent === dir) {
12
+ if (parent === directory) {
13
13
  return null;
14
14
  }
15
15
 
16
- dir = parent;
16
+ directory = parent;
17
17
  }
18
18
 
19
- return dir;
19
+ return directory;
20
20
  }
@@ -10,10 +10,6 @@ const allowedStatementTypes = new Set([
10
10
  'TSInterfaceDeclaration',
11
11
  ]);
12
12
 
13
- // the members a `test` call may chain: `test.each(rows)(…)`,
14
- // `test.skipIf(gate)(…)`, `test.if(gate)(…)`, and chains of them
15
- const testModifiers = new Set(['each', 'skipIf', 'if']);
16
-
17
13
  const setupTestOnly = {
18
14
  meta: {
19
15
  type: 'problem',
@@ -102,6 +98,12 @@ function collectTestNames(context, program) {
102
98
  return new Set(names);
103
99
  }
104
100
 
101
+ function getImportedName(specifier) {
102
+ return specifier.imported.type === 'Identifier'
103
+ ? specifier.imported.name
104
+ : specifier.imported.value;
105
+ }
106
+
105
107
  // every import form and every module-level declaration defines a binding in
106
108
  // the module scope, which ESM nests under the global one
107
109
  function hasModuleBinding(context, program, name) {
@@ -111,12 +113,6 @@ function hasModuleBinding(context, program, name) {
111
113
  return scopes.some((candidate) => (candidate.set.get(name)?.defs.length ?? 0) > 0);
112
114
  }
113
115
 
114
- function getImportedName(specifier) {
115
- return specifier.imported.type === 'Identifier'
116
- ? specifier.imported.name
117
- : specifier.imported.value;
118
- }
119
-
120
116
  function isTestCall(statement, testNames) {
121
117
  return (
122
118
  statement.type === 'ExpressionStatement' &&
@@ -125,6 +121,10 @@ function isTestCall(statement, testNames) {
125
121
  );
126
122
  }
127
123
 
124
+ // the members a `test` call may chain: `test.each(rows)(…)`,
125
+ // `test.skipIf(gate)(…)`, `test.if(gate)(…)`, and chains of them
126
+ const testModifiers = new Set(['each', 'skipIf', 'if']);
127
+
128
128
  // `test` itself or a chain of its modifiers, such as `test.skipIf(gate).each(rows)`
129
129
  function isTestRunner(node, testNames) {
130
130
  let current = node;
@@ -6,38 +6,6 @@ import resolveRepoRoot from './resolve-repo-root.js';
6
6
  // `<name>.test.ts`, and the suffix lets a runner put it in its own lane
7
7
  const testFilePattern = /^(?<base>.+?)(?:\.browser)?\.test\.tsx?$/u;
8
8
 
9
- // suites that run the whole program have no single module to sit beside
10
- const exemptRootDirs = new Set(['e2e', 'evals']);
11
-
12
- // the repo root, not oxlint's working directory, so a per-package lint
13
- // script neither exempts a package's own e2e/ nor flags the root one
14
- function isUnderExemptRoot(cwd, filename) {
15
- const [first] = relative(resolveRepoRoot(filename) ?? cwd, filename).split(sep);
16
-
17
- return exemptRootDirs.has(first);
18
- }
19
-
20
- // a module is `<name>.<ext>` with a single-segment extension, so a
21
- // declaration file never stands in for one, and neither does another test
22
- // (`foo.test.ts` beside `foo.test.test.ts`)
23
- function probeModuleSibling(dir, name) {
24
- return readdirSync(dir).some((entry) => {
25
- const dot = entry.lastIndexOf('.');
26
-
27
- return (
28
- dot > 0 &&
29
- entry.slice(0, dot) === name &&
30
- !testFilePattern.test(entry) &&
31
- probeFile(join(dir, entry))
32
- );
33
- });
34
- }
35
-
36
- // follows a symlink, so a linked module counts as the file it points at
37
- function probeFile(path) {
38
- return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
39
- }
40
-
41
9
  const testBesideModule = {
42
10
  meta: {
43
11
  type: 'problem',
@@ -78,3 +46,35 @@ const testBesideModule = {
78
46
  };
79
47
 
80
48
  export default testBesideModule;
49
+
50
+ // suites that run the whole program have no single module to sit beside
51
+ const exemptRootDirs = new Set(['e2e', 'evals']);
52
+
53
+ // the repo root, not oxlint's working directory, so a per-package lint
54
+ // script neither exempts a package's own e2e/ nor flags the root one
55
+ function isUnderExemptRoot(cwd, filename) {
56
+ const [first] = relative(resolveRepoRoot(filename) ?? cwd, filename).split(sep);
57
+
58
+ return exemptRootDirs.has(first);
59
+ }
60
+
61
+ // a module is `<name>.<ext>` with a single-segment extension, so a
62
+ // declaration file never stands in for one, and neither does another test
63
+ // (`foo.test.ts` beside `foo.test.test.ts`)
64
+ function probeModuleSibling(directory, name) {
65
+ return readdirSync(directory).some((entry) => {
66
+ const dot = entry.lastIndexOf('.');
67
+
68
+ return (
69
+ dot > 0 &&
70
+ entry.slice(0, dot) === name &&
71
+ !testFilePattern.test(entry) &&
72
+ probeFile(join(directory, entry))
73
+ );
74
+ });
75
+ }
76
+
77
+ // follows a symlink, so a linked module counts as the file it points at
78
+ function probeFile(path) {
79
+ return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
80
+ }