@gosukiwi/eslint-plugin-colocate 0.0.2 → 0.2.0

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
@@ -115,20 +115,20 @@ The plugin builds an import graph over `root` and checks that each file's locati
115
115
 
116
116
  A folder's own entry file is never flagged for sitting in its own folder. If the folder itself is in the wrong place, the folders above it report that.
117
117
 
118
- **The app shell.** Entry points (files nothing imports) and what they import directly do not own those imports. So `main.ts → App.ts → pages/Home/Home.ts` leaves `Home` where it is, with no configuration.
118
+ **The app shell.** Entry points (files nothing imports) and what they import directly do not own those imports. So `main.ts → App.ts → pages/Home/Home.ts` leaves `Home` where it is, with no configuration. Shells still vote on shared placement: if a shell importer belongs to a different owner than the file's other importers, the file counts as shared, never private.
119
119
 
120
120
  For a longer bootstrap chain (`main → router → App → pages/...`), or a shell that imports loose top-level modules, declare those directories as `layers`.
121
121
 
122
122
  ## What it reports
123
123
 
124
- *This covers `colocate/ownership`'s findings. `colocate/entry` reports `reachesPastEntry` and `namedDoorReexport` — see [The entry rule](#the-entry-rule).*
124
+ _This covers `colocate/ownership`'s findings. `colocate/entry` reports `reachesPastEntry` and `namedDoorReexport` — see [The entry rule](#the-entry-rule)._
125
125
 
126
- | message | meaning | usual fix |
127
- | --- | --- | --- |
128
- | `privateOutsideOwner` | One owner imports this file, and it sits outside that owner's folder. | Move it inside the folder, or convert the owner into a folder with a matching entry file. |
129
- | `sharedTooHigh` | Several owners import it, and it sits above their common ancestor. | Move it down to the common ancestor. |
130
- | `sharedInsideOwner` | Several owners import it, and it sits inside one of theirs. | Hoist it to the common ancestor. |
131
- | `singletonFolder` | A directory holds a single source file named after the directory (or `index`), and no stylesheet beside it. | Flatten the directory, or colocate something with the file. |
126
+ | message | meaning | usual fix |
127
+ | --------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
128
+ | `privateOutsideOwner` | One owner imports this file, and it sits outside that owner's folder. | Move it inside the folder, or convert the owner into a folder with a matching entry file. |
129
+ | `sharedTooHigh` | Several owners import it, and it sits above their common ancestor. | Move it down to the common ancestor. |
130
+ | `sharedInsideOwner` | Several owners import it, and it sits inside one of theirs. | Hoist it to the common ancestor. |
131
+ | `singletonFolder` | A directory holds a single source file named after the directory (or `index`), and no stylesheet beside it. | Flatten the directory, or colocate something with the file. |
132
132
 
133
133
  `singletonFolder` looks for a companion stylesheet only in the same directory (`.css`, `.scss`, `.sass`, `.less`, `.styl`).
134
134
 
@@ -136,7 +136,7 @@ An `index` that re-exports two or more siblings is a namespace barrel and is lef
136
136
 
137
137
  ## Options
138
138
 
139
- *These apply to `colocate/ownership`. See [The entry rule](#the-entry-rule) for `colocate/entry`'s options.* Unknown option names are rejected.
139
+ _These apply to `colocate/ownership`. See [The entry rule](#the-entry-rule) for `colocate/entry`'s options._ Unknown option names are rejected.
140
140
 
141
141
  ### `root`
142
142
 
@@ -155,7 +155,7 @@ Only the layer's own files and each child folder's entry (`Button/Button.ts` or
155
155
  This is also how a deeper app shell is described:
156
156
 
157
157
  ```js
158
- layers: ["src", "src/pages"] // top-level modules and pages are public peers
158
+ layers: ["src", "src/pages"]; // top-level modules and pages are public peers
159
159
  ```
160
160
 
161
161
  ### `ignore`
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import ownership from "./rules/ownership.js";
3
3
  export default {
4
4
  meta: {
5
5
  name: "@gosukiwi/eslint-plugin-colocate",
6
- version: "0.0.2",
6
+ version: "0.2.0",
7
7
  },
8
8
  rules: {
9
9
  entry,
@@ -1,4 +1,4 @@
1
- import type { Graph } from "./graph.js";
1
+ import type { Graph } from "../graph.js";
2
2
  import type { Subject } from "./subject.js";
3
3
  export interface SingletonDirectoryStats {
4
4
  sourceCount: number;
@@ -1,8 +1,8 @@
1
1
  import path from "node:path";
2
- import { derivedFromGraph } from "./derived.js";
3
- import { classifyDirEntry, safeReaddir, safeStat } from "./fs-safe.js";
2
+ import { derivedFromGraph } from "../derived.js";
3
+ import { classifyDirEntry, safeReaddir, safeStat } from "../fs-safe.js";
4
4
  import { getSharedColocationIssue, isPrivateOutsideOwner, resolveLayerDirectories, } from "./owners.js";
5
- import { isSourceFile, isTestFile, matchesIgnore, SKIP_DIRS, } from "./scope.js";
5
+ import { isSourceFile, isTestFile, matchesIgnore, SKIP_DIRS, } from "../scope.js";
6
6
  const singletonStatsByGraph = derivedFromGraph(() => new Map());
7
7
  const STYLESHEET_EXTS = [".css", ".scss", ".sass", ".less", ".styl"];
8
8
  function isStylesheet(filePath) {
@@ -1,4 +1,4 @@
1
- import { type Graph } from "./graph.js";
1
+ import { type Graph } from "../graph.js";
2
2
  export type VisitToken = object;
3
3
  export declare const REVALIDATE_AFTER_MS = 100;
4
4
  export declare function stampIsAmbiguous(mtimeMs: number, builtAt: number, coarseTimestamps: boolean, stampedAt: number): boolean;
@@ -1,9 +1,9 @@
1
1
  import path from "node:path";
2
- import { safeRealpath, safeStat } from "./fs-safe.js";
3
- import { buildGraphFromFiles } from "./graph.js";
4
- import { findTsconfig } from "./resolve.js";
5
- import { isInGraphScope, isSourceFile } from "./scope.js";
6
- import { collectSourceFiles } from "./walk.js";
2
+ import { safeRealpath, safeStat } from "../fs-safe.js";
3
+ import { buildGraphFromFiles } from "../graph.js";
4
+ import { findTsconfig } from "../resolve.js";
5
+ import { isInGraphScope, isSourceFile } from "../scope.js";
6
+ import { collectSourceFiles } from "../walk.js";
7
7
  export const REVALIDATE_AFTER_MS = 100;
8
8
  let cache;
9
9
  function cacheKey(rootDir, ignoreGlobs) {
@@ -0,0 +1,5 @@
1
+ export { ownershipFindings, singletonDirectoryStats, type OwnershipFinding, type SingletonDirectoryStats, } from "./findings.js";
2
+ export { getGraph, REVALIDATE_AFTER_MS, stampIsAmbiguous, type VisitToken, } from "./graph-cache.js";
3
+ export { collectLayerDirectories, collectReExports, getColocationConsumers, getOwner, getSharedColocationIssue, getShells, isLayerPublicModule, isPrivateOutsideOwner, resolveLayerDirectories, shouldSkipColocation, type Owner, type OwnershipContext, } from "./owners.js";
4
+ export { resolveRootDir } from "./root.js";
5
+ export { resolveSubject, resolvedLintRoot, type Subject } from "./subject.js";
@@ -0,0 +1,5 @@
1
+ export { ownershipFindings, singletonDirectoryStats, } from "./findings.js";
2
+ export { getGraph, REVALIDATE_AFTER_MS, stampIsAmbiguous, } from "./graph-cache.js";
3
+ export { collectLayerDirectories, collectReExports, getColocationConsumers, getOwner, getSharedColocationIssue, getShells, isLayerPublicModule, isPrivateOutsideOwner, resolveLayerDirectories, shouldSkipColocation, } from "./owners.js";
4
+ export { resolveRootDir } from "./root.js";
5
+ export { resolveSubject, resolvedLintRoot } from "./subject.js";
@@ -1,4 +1,4 @@
1
- import { type Graph } from "./graph.js";
1
+ import { type Graph } from "../graph.js";
2
2
  export interface OwnershipContext {
3
3
  graph: Graph;
4
4
  rootDir: string;
@@ -1,13 +1,13 @@
1
1
  import path from "node:path";
2
2
  import { minimatch } from "minimatch";
3
3
  import ts from "typescript";
4
- import { derivedFromGraph } from "./derived.js";
5
- import { safeReadFile, safeReaddir, safeRealpath } from "./fs-safe.js";
6
- import { canonicalGraphPath, getGraphResolutionSettings, graphFilesInDir, } from "./graph.js";
7
- import { parseSourceFile } from "./parse.js";
8
- import { isInsideDir } from "./paths.js";
9
- import { resolveSpecifier } from "./resolve.js";
10
- import { SKIP_DIRS } from "./scope.js";
4
+ import { derivedFromGraph } from "../derived.js";
5
+ import { safeReadFile, safeReaddir, safeRealpath } from "../fs-safe.js";
6
+ import { canonicalGraphPath, getGraphResolutionSettings, graphFilesInDir, } from "../graph.js";
7
+ import { parseSourceFile } from "../parse.js";
8
+ import { isInsideDir } from "../paths.js";
9
+ import { resolveSpecifier } from "../resolve.js";
10
+ import { SKIP_DIRS } from "../scope.js";
11
11
  const reExportsByGraph = derivedFromGraph(() => new Map());
12
12
  function scanReExports(indexFile, dir, graph) {
13
13
  const content = safeReadFile(indexFile);
@@ -55,7 +55,7 @@ function isNamespaceBarrel(filePath, graph) {
55
55
  if (basename !== "index") {
56
56
  return false;
57
57
  }
58
- return (collectReExports(filePath, path.dirname(filePath), graph).length >= 2);
58
+ return collectReExports(filePath, path.dirname(filePath), graph).length >= 2;
59
59
  }
60
60
  function isOwnerEntryFile(file, dir, graph) {
61
61
  if (path.dirname(file) !== dir) {
@@ -211,9 +211,24 @@ export const getShells = derivedFromGraph((graph) => {
211
211
  }
212
212
  return shells;
213
213
  });
214
+ function partitionImporters(filePath, graph, shells) {
215
+ const consumers = [];
216
+ const shellVoices = [];
217
+ for (const importer of graph.importers.get(filePath) ?? []) {
218
+ if (isNamespaceBarrel(importer, graph)) {
219
+ continue;
220
+ }
221
+ if (shells.has(importer)) {
222
+ shellVoices.push(importer);
223
+ }
224
+ else {
225
+ consumers.push(importer);
226
+ }
227
+ }
228
+ return { consumers, shellVoices };
229
+ }
214
230
  export function getColocationConsumers(filePath, graph, shells) {
215
- const importers = graph.importers.get(filePath) ?? [];
216
- return importers.filter((importer) => !shells.has(importer) && !isNamespaceBarrel(importer, graph));
231
+ return partitionImporters(filePath, graph, shells).consumers;
217
232
  }
218
233
  function collectLayerDirs(dir, cwd, rootDir, layerGlobs, out) {
219
234
  for (const entry of safeReaddir(dir)) {
@@ -301,6 +316,9 @@ export function isPrivateOutsideOwner(filePath, ctx) {
301
316
  if (owners.size !== 1) {
302
317
  return false;
303
318
  }
319
+ if (collectExtraShellOwners(filePath, ctx, owners).size > 0) {
320
+ return false;
321
+ }
304
322
  const owner = owners.values().next().value;
305
323
  if (owner === undefined) {
306
324
  return false;
@@ -350,14 +368,21 @@ function folderOwnerAncestors(filePath, graph, rootDir) {
350
368
  }
351
369
  return dirs;
352
370
  }
353
- export function getSharedColocationIssue(filePath, ctx) {
354
- if (shouldSkipColocation(filePath, ctx)) {
355
- return undefined;
356
- }
357
- const owners = collectConsumerOwners(filePath, ctx);
358
- if (owners.size < 2) {
359
- return undefined;
371
+ function shellVoiceImporters(filePath, graph, shells) {
372
+ return partitionImporters(filePath, graph, shells).shellVoices;
373
+ }
374
+ function collectExtraShellOwners(filePath, ctx, countedOwners) {
375
+ const shells = getShells(ctx.graph);
376
+ const extra = new Map();
377
+ for (const importer of shellVoiceImporters(filePath, ctx.graph, shells)) {
378
+ const owner = getOwner(importer, ctx.graph, ctx.rootDir);
379
+ if (!countedOwners.has(owner.path) && !extra.has(owner.path)) {
380
+ extra.set(owner.path, owner);
381
+ }
360
382
  }
383
+ return extra;
384
+ }
385
+ function evaluateSharedPosition(filePath, ctx, owners) {
361
386
  const ownerDirs = [...new Set([...owners.values()].map(ownerDir))];
362
387
  const lca = longestCommonAncestor(ownerDirs);
363
388
  const consumerFolderDirs = [...owners.values()]
@@ -384,3 +409,20 @@ export function getSharedColocationIssue(filePath, ctx) {
384
409
  }
385
410
  return undefined;
386
411
  }
412
+ export function getSharedColocationIssue(filePath, ctx) {
413
+ if (shouldSkipColocation(filePath, ctx)) {
414
+ return undefined;
415
+ }
416
+ const owners = collectConsumerOwners(filePath, ctx);
417
+ if (owners.size === 0) {
418
+ return undefined;
419
+ }
420
+ const merged = new Map([
421
+ ...owners,
422
+ ...collectExtraShellOwners(filePath, ctx, owners),
423
+ ]);
424
+ if (merged.size < 2) {
425
+ return undefined;
426
+ }
427
+ return evaluateSharedPosition(filePath, ctx, merged);
428
+ }
@@ -1,5 +1,5 @@
1
1
  import path from "node:path";
2
- import { safeStat } from "./fs-safe.js";
2
+ import { safeStat } from "../fs-safe.js";
3
3
  function isProjectBoundary(dir) {
4
4
  return (safeStat(path.join(dir, "package.json")) !== undefined ||
5
5
  safeStat(path.join(dir, ".git")) !== undefined);
@@ -1,5 +1,5 @@
1
1
  import type { Rule } from "eslint";
2
- import type { Graph } from "./graph.js";
2
+ import type { Graph } from "../graph.js";
3
3
  export interface Subject {
4
4
  readonly rootDir: string;
5
5
  readonly realRootDir: string;
@@ -1,8 +1,8 @@
1
1
  import path from "node:path";
2
- import { safeRealpath } from "./fs-safe.js";
2
+ import { safeRealpath } from "../fs-safe.js";
3
3
  import { getGraph } from "./graph-cache.js";
4
4
  import { resolveRootDir } from "./root.js";
5
- import { isInGraphScope, isSourceFile } from "./scope.js";
5
+ import { isInGraphScope, isSourceFile } from "../scope.js";
6
6
  const resolvedLintRoots = new Map();
7
7
  const fileRealpathsByParse = new WeakMap();
8
8
  export function resolvedLintRoot(cwd, rootOption) {
package/dist/lib/graph.js CHANGED
@@ -3,7 +3,7 @@ import ts from "typescript";
3
3
  import { derivedFromGraph } from "./derived.js";
4
4
  import { safeReadFile, safeRealpath } from "./fs-safe.js";
5
5
  import { extractSpecifiers } from "./parse.js";
6
- import { createResolutionSettings, resolveSpecifier } from "./resolve.js";
6
+ import { createResolutionSettings, resolveSpecifier, } from "./resolve.js";
7
7
  import { collectSourceFiles } from "./walk.js";
8
8
  const graphFileSet = derivedFromGraph((graph) => new Set(graph.files));
9
9
  export function graphHasFile(graph, filePath) {
@@ -54,7 +54,7 @@ function noProjectResolutionSettings() {
54
54
  configPaths: [],
55
55
  };
56
56
  }
57
- export const getGraphResolutionSettings = derivedFromGraph((_graph) => noProjectResolutionSettings());
57
+ export const getGraphResolutionSettings = derivedFromGraph(() => noProjectResolutionSettings());
58
58
  export function buildGraph(rootDir, ignoreGlobs) {
59
59
  return buildGraphWithConfigs(rootDir, ignoreGlobs).graph;
60
60
  }
@@ -1,4 +1,4 @@
1
- import type { Graph } from "./graph.js";
1
+ import type { Graph } from "../graph.js";
2
2
  export declare function isEntryFile(filePath: string): boolean;
3
3
  export declare const getGates: ((graph: Graph) => ReadonlyMap<string, string>) & {
4
4
  prime: (graph: Graph, value: ReadonlyMap<string, string>) => void;
@@ -1,6 +1,6 @@
1
1
  import path from "node:path";
2
- import { derivedFromGraph } from "./derived.js";
3
- import { isInsideDir } from "./paths.js";
2
+ import { derivedFromGraph } from "../derived.js";
3
+ import { isInsideDir } from "../paths.js";
4
4
  export function isEntryFile(filePath) {
5
5
  const base = path.basename(filePath, path.extname(filePath));
6
6
  return base === "index" || base === path.basename(path.dirname(filePath));
@@ -0,0 +1,2 @@
1
+ export { findCrossedGate, getGates, isEntryFile, type CrossedGate, } from "./gates.js";
2
+ export { isNamedDoor, namedDoorReexports } from "./named-door.js";
@@ -0,0 +1,2 @@
1
+ export { findCrossedGate, getGates, isEntryFile, } from "./gates.js";
2
+ export { isNamedDoor, namedDoorReexports } from "./named-door.js";
@@ -1,4 +1,4 @@
1
- import { type Graph } from "./graph.js";
1
+ import { type Graph } from "../graph.js";
2
2
  export declare function isNamedDoor(filePath: string): boolean;
3
3
  export declare function namedDoorReexports(filePath: string, graph: Graph, content?: string): readonly {
4
4
  target: string;
@@ -1,11 +1,11 @@
1
1
  import path from "node:path";
2
2
  import ts from "typescript";
3
3
  import { isEntryFile } from "./gates.js";
4
- import { safeReadFile } from "./fs-safe.js";
5
- import { canonicalGraphPath, getGraphResolutionSettings, graphHasFile, } from "./graph.js";
6
- import { parseSourceFile, stringLiteralText } from "./parse.js";
7
- import { scopeBindsRequire } from "./require-binding.js";
8
- import { resolveSpecifier } from "./resolve.js";
4
+ import { safeReadFile } from "../fs-safe.js";
5
+ import { canonicalGraphPath, getGraphResolutionSettings, graphHasFile, } from "../graph.js";
6
+ import { parseSourceFile, stringLiteralText } from "../parse.js";
7
+ import { scopeBindsRequire } from "../require-binding.js";
8
+ import { resolveSpecifier } from "../resolve.js";
9
9
  export function isNamedDoor(filePath) {
10
10
  if (!isEntryFile(filePath)) {
11
11
  return false;
@@ -321,9 +321,7 @@ export function namedDoorReexports(filePath, graph, content) {
321
321
  if (!isNamedDoor(filePath)) {
322
322
  return [];
323
323
  }
324
- const source = typeof content === "string"
325
- ? content
326
- : safeReadFile(filePath);
324
+ const source = typeof content === "string" ? content : safeReadFile(filePath);
327
325
  if (source === undefined) {
328
326
  return [];
329
327
  }
@@ -128,7 +128,9 @@ function aliasCandidates(specifier, options) {
128
128
  const targetStar = target.indexOf("*");
129
129
  const mapped = targetStar === -1
130
130
  ? target
131
- : target.slice(0, targetStar) + matched + target.slice(targetStar + 1);
131
+ : target.slice(0, targetStar) +
132
+ matched +
133
+ target.slice(targetStar + 1);
132
134
  return path.resolve(base, mapped);
133
135
  });
134
136
  }
@@ -1,10 +1,9 @@
1
1
  import path from "node:path";
2
- import { findCrossedGate } from "../lib/gates.js";
3
- import { canonicalGraphPath, getGraphResolutionSettings } from "../lib/graph.js";
4
- import { isNamedDoor, namedDoorReexports } from "../lib/named-door.js";
2
+ import { findCrossedGate, isNamedDoor, namedDoorReexports, } from "../lib/named-door/index.js";
3
+ import { canonicalGraphPath, getGraphResolutionSettings, } from "../lib/graph.js";
5
4
  import { requireIsShadowed } from "../lib/require-binding.js";
6
5
  import { resolveSpecifier } from "../lib/resolve.js";
7
- import { resolveSubject } from "../lib/subject.js";
6
+ import { resolveSubject } from "../lib/findings/index.js";
8
7
  function staticSpecifier(source) {
9
8
  if (source.type === "Literal") {
10
9
  return typeof source.value === "string" ? source.value : undefined;
@@ -57,10 +56,7 @@ const rule = {
57
56
  return {};
58
57
  }
59
58
  const reexportsByPos = isNamedDoor(subject.file)
60
- ? new Map(namedDoorReexports(subject.file, subject.graph(), context.sourceCode.getText()).map((reexport) => [
61
- reexport.pos,
62
- reexport.target,
63
- ]))
59
+ ? new Map(namedDoorReexports(subject.file, subject.graph(), context.sourceCode.getText()).map((reexport) => [reexport.pos, reexport.target]))
64
60
  : undefined;
65
61
  const reportNamedDoorReexport = (node) => {
66
62
  if (reexportsByPos === undefined) {
@@ -1,5 +1,4 @@
1
- import { ownershipFindings } from "../lib/findings.js";
2
- import { resolveSubject } from "../lib/subject.js";
1
+ import { ownershipFindings, resolveSubject } from "../lib/findings/index.js";
3
2
  const rule = {
4
3
  meta: {
5
4
  type: "problem",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gosukiwi/eslint-plugin-colocate",
3
- "version": "0.0.2",
3
+ "version": "0.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -24,7 +24,10 @@
24
24
  "test": "vitest run",
25
25
  "typecheck": "tsc --noEmit",
26
26
  "check:placement": "vite-node scripts/check-placement.ts",
27
- "crap": "vitest run --coverage --coverage.provider=v8 --coverage.reporter=lcov && crap4ts src --coverage ./coverage/lcov.info --max-crap 15"
27
+ "crap": "vitest run --coverage --coverage.provider=v8 --coverage.reporter=lcov && crap4ts src --coverage ./coverage/lcov.info --max-crap 15",
28
+ "format": "prettier --write .",
29
+ "format:check": "prettier --check .",
30
+ "lint": "eslint . --max-warnings 0"
28
31
  },
29
32
  "peerDependencies": {
30
33
  "eslint": ">=9"
@@ -34,12 +37,16 @@
34
37
  "typescript": "^5 || ^6"
35
38
  },
36
39
  "devDependencies": {
40
+ "@eslint/js": "^10.0.1",
37
41
  "@gosukiwi/crap4ts": "^0.2.2",
42
+ "@gosukiwi/eslint-plugin-colocate": "^0.1.0",
38
43
  "@types/node": "^22.15.29",
39
44
  "@typescript-eslint/parser": "^8",
40
45
  "@vitest/coverage-v8": "^3.2.7",
41
46
  "eslint": "^10",
47
+ "prettier": "^3.9.6",
42
48
  "typescript": "^5 || ^6",
49
+ "typescript-eslint": "^8.70.0",
43
50
  "vitest": "^3"
44
51
  }
45
52
  }
File without changes