eslint-plugin-kerfjs 5.0.0-beta.125 → 5.0.0-beta.126

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 CHANGED
@@ -143,6 +143,7 @@ When a real bug ships that the existing defense stack misses AND a new lint rule
143
143
  ```bash
144
144
  npm install
145
145
  npm test
146
+ npm run check # matrix-parity check, then the rule suite (what CI runs)
146
147
  npm run test:eslint-matrix # real packed-plugin loading on every supported ESLint major
147
148
  ```
148
149
 
package/index.js CHANGED
@@ -16,7 +16,7 @@ import uiPublicBoundaries from './lib/rules/ui-public-boundaries.js';
16
16
  import uiWiring from './lib/rules/ui-wiring.js';
17
17
 
18
18
  const plugin = {
19
- meta: { name: 'eslint-plugin-kerfjs', version: '5.0.0-beta.125' },
19
+ meta: { name: 'eslint-plugin-kerfjs', version: '5.0.0-beta.126' },
20
20
  rules: {
21
21
  'no-inline-jsx-event-handlers': noInlineJsxEventHandlers,
22
22
  'no-generated-view-edits': noGeneratedViewEdits,
@@ -96,14 +96,12 @@ function usesUi(cwd) {
96
96
 
97
97
  // Module-level once-per-lint-run guard. ESLint instantiates the rule
98
98
  // per-file; we do the project-level filesystem check the first time the
99
- // `Program` visitor runs and cache the result for the remaining files in
100
- // the same lint pass.
99
+ // `Program` visitor runs and skip it for the remaining files in the same
100
+ // lint pass.
101
101
  let CHECKED = false;
102
- let CACHED_RESULT = null;
103
102
 
104
103
  export function _resetForTests() {
105
104
  CHECKED = false;
106
- CACHED_RESULT = null;
107
105
  }
108
106
 
109
107
  function resolveManifestPath(cwd) {
@@ -388,7 +386,6 @@ function create(context) {
388
386
  // ESLint v9: `context.cwd` is a string. ESLint v8: `context.getCwd()`.
389
387
  const cwd = contextCwd(context);
390
388
  const checked = runCheck(cwd);
391
- CACHED_RESULT = checked;
392
389
  if (!checked) return;
393
390
 
394
391
  for (const { file, result } of checked.results) {
@@ -1,4 +1,4 @@
1
- import { existsSync, readFileSync } from 'node:fs';
1
+ import { readFileSync, statSync } from 'node:fs';
2
2
  import { dirname, resolve } from 'node:path';
3
3
 
4
4
  import {
@@ -49,27 +49,39 @@ function ownershipFacts(contract) {
49
49
  return facts;
50
50
  }
51
51
 
52
+ // The `name` of each directory's package.json, re-read when the manifest's
53
+ // modification time changes so a long-running editor ESLint server sees a
54
+ // renamed package.
52
55
  const packageNames = new Map();
56
+ function manifestName(manifest) {
57
+ let mtime;
58
+ try {
59
+ mtime = statSync(manifest).mtimeMs;
60
+ } catch {
61
+ packageNames.delete(manifest);
62
+ return undefined;
63
+ }
64
+ const cached = packageNames.get(manifest);
65
+ if (cached?.mtime === mtime) return cached.name;
66
+ let name;
67
+ try {
68
+ name = JSON.parse(readFileSync(manifest, 'utf8')).name;
69
+ } catch {
70
+ name = undefined;
71
+ }
72
+ if (typeof name !== 'string') name = undefined;
73
+ packageNames.set(manifest, { mtime, name });
74
+ return name;
75
+ }
76
+
53
77
  function ownPackage(filename) {
54
78
  for (
55
79
  let directory = dirname(resolve(filename));
56
80
  ;
57
81
  directory = dirname(directory)
58
82
  ) {
59
- if (packageNames.has(directory)) return packageNames.get(directory);
60
- const manifest = resolve(directory, 'package.json');
61
- if (existsSync(manifest)) {
62
- let name;
63
- try {
64
- name = JSON.parse(readFileSync(manifest, 'utf8')).name;
65
- } catch {
66
- name = undefined;
67
- }
68
- if (typeof name === 'string') {
69
- packageNames.set(directory, name);
70
- return name;
71
- }
72
- }
83
+ const name = manifestName(resolve(directory, 'package.json'));
84
+ if (name !== undefined) return name;
73
85
  if (dirname(directory) === directory) return undefined;
74
86
  }
75
87
  }
@@ -4,7 +4,6 @@ import { createRequire } from 'node:module';
4
4
  import process from 'node:process';
5
5
 
6
6
  const PROFILE = '.kerf-ui-profile.json';
7
- const cache = new Map();
8
7
  const loadCommonJs = createRequire(import.meta.url);
9
8
 
10
9
  export const UI_CONTRACT_LOAD_CODE = 'KUI-L090';
@@ -25,6 +24,41 @@ export function isComponentExport(name) {
25
24
 
26
25
  const readJson = (path) => JSON.parse(readFileSync(path, 'utf8'));
27
26
 
27
+ // A file's modification time, or null when it does not exist (yet).
28
+ function mtimeOf(path) {
29
+ try {
30
+ return statSync(path).mtimeMs;
31
+ } catch {
32
+ return null;
33
+ }
34
+ }
35
+
36
+ // The loaded contract, keyed by what it is built from rather than by the file
37
+ // being linted: the workspace, the settings, and the profile layers that apply
38
+ // to the file's directory. Each entry also records the modification time of
39
+ // every JSON file it read, so a long-running editor ESLint server picks up a
40
+ // regenerated catalog or an edited profile on its next lint. A failed load is
41
+ // never cached: the next lint retries it.
42
+ const cache = new Map();
43
+ // Settings objects are usually shared across every linted file, so their
44
+ // serialized form is computed once per object rather than on every call.
45
+ const settingsKeys = new WeakMap();
46
+ function settingsKey(settings) {
47
+ let key = settingsKeys.get(settings);
48
+ if (key === undefined) {
49
+ key = JSON.stringify(settings);
50
+ settingsKeys.set(settings, key);
51
+ }
52
+ return key;
53
+ }
54
+ const isFresh = (stamps) =>
55
+ stamps.every(([path, mtime]) => mtimeOf(path) === mtime);
56
+
57
+ /** Test seam: forget every loaded contract. */
58
+ export function clearUiContractCache() {
59
+ cache.clear();
60
+ }
61
+
28
62
  // The directory whose package.json names `packageName`, searched upward from a
29
63
  // catalog file; a catalog's `source` paths are relative to that package root.
30
64
  function packageRootOf(start, packageName) {
@@ -47,14 +81,14 @@ const exportName = (item) => (typeof item === 'string' ? item : item?.name);
47
81
  // the @kerfjs/ui catalog the rules load directly. Their entries join the
48
82
  // contract, and their components resolve by package subpath (a bare import)
49
83
  // or by source file (an app's relative import).
50
- function profileCatalogs(loadedProfile, basePackage) {
84
+ function profileCatalogs(loadedProfile, basePackage, read) {
51
85
  const catalogs = [];
52
86
  for (const catalog of loadedProfile.profile?.catalogs ?? []) {
53
87
  if (catalog.package === basePackage) continue;
54
88
  const owner = loadedProfile.provenance?.[`$catalogs.${catalog.package}`];
55
89
  if (!owner || !catalog.composition?.path || owner.startsWith('<')) continue;
56
90
  const path = resolve(dirname(owner), catalog.composition.path);
57
- const artifact = readJson(path);
91
+ const artifact = read(path);
58
92
  catalogs.push({ artifact, root: packageRootOf(path, catalog.package) });
59
93
  }
60
94
  return catalogs;
@@ -86,27 +120,18 @@ function packageAsset(cwd, name) {
86
120
  }
87
121
  }
88
122
 
89
- function discoveredProfiles(cwd, filename, settings) {
90
- if (settings.profile)
91
- return [
92
- {
93
- source: '<eslint settings kerfjs.ui.profile>',
94
- profile: settings.profile,
95
- },
96
- ];
123
+ // The profile layers that apply to `filename`, outermost first, without
124
+ // reading them: the shipped package defaults, then every `.kerf-ui-profile.json`
125
+ // from the workspace root down to the file's directory. Files whose directories
126
+ // see the same layers share one loaded contract.
127
+ function profileLayers(cwd, filename, settings) {
128
+ if (settings.profile || settings.profilePath) return [];
97
129
  const layers = [];
98
- const add = (path, expectedScope) => {
99
- const profile = readJson(path);
100
- if (profile.scope !== expectedScope)
101
- throw new Error(
102
- `${path} must have profile scope ${expectedScope}, received ${profile.scope ?? 'missing'}`,
103
- );
104
- layers.push({ source: path, profile });
105
- };
106
130
  const defaultsPath =
107
131
  settings.profileDefaultsPath ??
108
132
  packageAsset(cwd, 'application-ui-profile.defaults.json');
109
- if (defaultsPath && existsSync(defaultsPath)) add(defaultsPath, 'package');
133
+ if (defaultsPath && existsSync(defaultsPath))
134
+ layers.push({ path: defaultsPath, scope: 'package' });
110
135
  const workspace = resolve(settings.workspaceRoot ?? cwd);
111
136
  const target = resolve(dirname(filename));
112
137
  if (relative(workspace, target).startsWith('..')) return layers;
@@ -120,11 +145,36 @@ function discoveredProfiles(cwd, filename, settings) {
120
145
  for (const candidate of directories.reverse()) {
121
146
  const path = resolve(candidate, PROFILE);
122
147
  if (existsSync(path))
123
- add(path, candidate === workspace ? 'workspace' : 'directory');
148
+ layers.push({
149
+ path,
150
+ scope: candidate === workspace ? 'workspace' : 'directory',
151
+ });
124
152
  }
125
153
  return layers;
126
154
  }
127
155
 
156
+ function readProfiles(settings, layers, read) {
157
+ if (settings.profilePath)
158
+ return [
159
+ { source: settings.profilePath, profile: read(settings.profilePath) },
160
+ ];
161
+ if (settings.profile)
162
+ return [
163
+ {
164
+ source: '<eslint settings kerfjs.ui.profile>',
165
+ profile: settings.profile,
166
+ },
167
+ ];
168
+ return layers.map(({ path, scope }) => {
169
+ const profile = read(path);
170
+ if (profile.scope !== scope)
171
+ throw new Error(
172
+ `${path} must have profile scope ${scope}, received ${profile.scope ?? 'missing'}`,
173
+ );
174
+ return { source: path, profile };
175
+ });
176
+ }
177
+
128
178
  // `@kerfjs/ui`'s class-to-component rule ships beside its profile contract.
129
179
  // An older `@kerfjs/ui` without it cannot report KUI-L103 consistently, so it
130
180
  // fails the contract load instead of guessing.
@@ -148,29 +198,37 @@ export function loadUiContract(context) {
148
198
  const packageCwd = context.cwd ?? process.cwd();
149
199
  const cwd = resolve(settings.workspaceRoot ?? packageCwd);
150
200
  const filename = context.filename ?? context.getFilename();
151
- const key = JSON.stringify([cwd, filename, settings]);
152
- if (cache.has(key)) return cache.get(key);
201
+ const layers = profileLayers(packageCwd, filename, settings);
202
+ const key = JSON.stringify([
203
+ cwd,
204
+ packageCwd,
205
+ settingsKey(settings),
206
+ layers.map(({ path }) => path),
207
+ ]);
208
+ const cached = cache.get(key);
209
+ if (cached && isFresh(cached.stamps)) return cached.value;
210
+ cache.delete(key);
211
+ // Every JSON file the contract is built from, with the modification time
212
+ // read just before the file itself.
213
+ const stamps = [];
214
+ const read = (path) => {
215
+ stamps.push([path, mtimeOf(path)]);
216
+ return readJson(path);
217
+ };
153
218
  try {
154
219
  const catalog =
155
220
  settings.catalog ??
156
- readJson(
221
+ read(
157
222
  settings.catalogPath ??
158
223
  packageAsset(packageCwd, 'component-composition.json'),
159
224
  );
160
225
  const selection =
161
226
  settings.selectionCatalog ??
162
- readJson(
227
+ read(
163
228
  settings.selectionCatalogPath ??
164
229
  packageAsset(packageCwd, 'component-catalog.json'),
165
230
  );
166
- const profiles = settings.profilePath
167
- ? [
168
- {
169
- source: settings.profilePath,
170
- profile: readJson(settings.profilePath),
171
- },
172
- ]
173
- : discoveredProfiles(packageCwd, filename, settings);
231
+ const profiles = readProfiles(settings, layers, read);
174
232
  const profileContractPath =
175
233
  settings.profileContractPath ??
176
234
  packageAsset(packageCwd, 'application-ui-profile-sync.cjs');
@@ -185,7 +243,7 @@ export function loadUiContract(context) {
185
243
  dirname(profileContractPath),
186
244
  'application-ui-diagnostic-ids-v1.json',
187
245
  );
188
- const diagnosticRegistry = readJson(diagnosticRegistryPath);
246
+ const diagnosticRegistry = read(diagnosticRegistryPath);
189
247
  if (
190
248
  diagnosticRegistry.schemaVersion !== 1 ||
191
249
  !Array.isArray(diagnosticRegistry.ids)
@@ -228,6 +286,7 @@ export function loadUiContract(context) {
228
286
  for (const { artifact, root } of profileCatalogs(
229
287
  loadedProfile,
230
288
  selection.package,
289
+ read,
231
290
  ))
232
291
  for (const entry of artifact.entries ?? []) {
233
292
  const key = entry.key ?? `${artifact.package}:${entry.id}`;
@@ -328,12 +387,10 @@ export function loadUiContract(context) {
328
387
  ),
329
388
  error: undefined,
330
389
  };
331
- cache.set(key, value);
390
+ cache.set(key, { stamps, value });
332
391
  return value;
333
392
  } catch (error) {
334
- const value = { error: error.message };
335
- cache.set(key, value);
336
- return value;
393
+ return { error: error.message };
337
394
  }
338
395
  }
339
396
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-kerfjs",
3
- "version": "5.0.0-beta.125",
3
+ "version": "5.0.0-beta.126",
4
4
  "description": "ESLint rules that enforce kerf's hard rules — catches AI-shaped bugs at edit time.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -36,13 +36,16 @@
36
36
  "LICENSE"
37
37
  ],
38
38
  "scripts": {
39
+ "lint": "eslint --max-warnings 0 .",
39
40
  "test": "node --test tests/rules/*.test.js",
40
- "test:eslint-matrix": "node scripts/eslint-matrix.mjs"
41
+ "test:eslint-matrix": "node scripts/eslint-matrix.mjs",
42
+ "check": "node scripts/eslint-matrix.mjs --parity-only && npm run lint && npm test"
41
43
  },
42
44
  "peerDependencies": {
43
45
  "eslint": "^9.0.0 || ^10.0.0"
44
46
  },
45
47
  "devDependencies": {
48
+ "@eslint/js": "^9.39.5",
46
49
  "@typescript-eslint/parser": "^8.0.0",
47
50
  "eslint": "^9.0.0"
48
51
  }