@gosukiwi/eslint-plugin-colocate 0.0.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.
@@ -0,0 +1,161 @@
1
+ import path from "node:path";
2
+ import ts from "typescript";
3
+ import { safeRealpath, safeStat } from "./fs-safe.js";
4
+ import { isSourceFile, SOURCE_EXTS } from "./scope.js";
5
+ const RESOLUTION_OVERRIDES = {
6
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
7
+ allowJs: true,
8
+ };
9
+ export function findTsconfig(rootDir) {
10
+ return ts.findConfigFile(rootDir, (fileName) => ts.sys.fileExists(fileName));
11
+ }
12
+ function loadCompilerOptions(rootDir) {
13
+ const configPath = findTsconfig(rootDir);
14
+ if (configPath === undefined) {
15
+ return { options: {}, configPaths: [] };
16
+ }
17
+ const parsed = ts.getParsedCommandLineOfConfigFile(configPath, {}, {
18
+ ...ts.sys,
19
+ onUnRecoverableConfigFileDiagnostic: () => { },
20
+ });
21
+ if (parsed === undefined) {
22
+ return { options: {}, configPaths: [configPath] };
23
+ }
24
+ const configFile = parsed.options.configFile;
25
+ return {
26
+ options: parsed.options,
27
+ configPaths: [configPath, ...(configFile?.extendedSourceFiles ?? [])],
28
+ };
29
+ }
30
+ export function createResolutionSettings(rootDir) {
31
+ const { options, configPaths } = loadCompilerOptions(rootDir);
32
+ const resolutionOptions = {
33
+ ...options,
34
+ ...RESOLUTION_OVERRIDES,
35
+ };
36
+ return {
37
+ options: resolutionOptions,
38
+ cache: ts.createModuleResolutionCache(rootDir, (fileName) => fileName, resolutionOptions),
39
+ configPaths,
40
+ };
41
+ }
42
+ const IMPORT_JS_EXTS = [".mjs", ".cjs", ".jsx", ".js"];
43
+ function probeFile(candidate) {
44
+ const stat = safeStat(candidate);
45
+ if (stat === undefined || !stat.isFile()) {
46
+ return undefined;
47
+ }
48
+ return safeRealpath(candidate);
49
+ }
50
+ function probeResolvedPath(base) {
51
+ for (const ext of IMPORT_JS_EXTS) {
52
+ if (base.endsWith(ext)) {
53
+ base = base.slice(0, -ext.length);
54
+ break;
55
+ }
56
+ }
57
+ if (isSourceFile(base)) {
58
+ const exact = probeFile(base);
59
+ if (exact !== undefined) {
60
+ return exact;
61
+ }
62
+ }
63
+ for (const ext of SOURCE_EXTS) {
64
+ const candidate = probeFile(base + ext);
65
+ if (candidate !== undefined) {
66
+ return candidate;
67
+ }
68
+ }
69
+ for (const ext of SOURCE_EXTS) {
70
+ const candidate = probeFile(path.join(base, "index" + ext));
71
+ if (candidate !== undefined) {
72
+ return candidate;
73
+ }
74
+ }
75
+ return undefined;
76
+ }
77
+ function bestPathPattern(specifier, paths) {
78
+ if (Object.prototype.hasOwnProperty.call(paths, specifier)) {
79
+ return specifier;
80
+ }
81
+ let best;
82
+ let bestPrefixLength = -1;
83
+ for (const pattern of Object.keys(paths)) {
84
+ const star = pattern.indexOf("*");
85
+ if (star === -1) {
86
+ continue;
87
+ }
88
+ const prefix = pattern.slice(0, star);
89
+ const suffix = pattern.slice(star + 1);
90
+ if (!specifier.startsWith(prefix) ||
91
+ !specifier.endsWith(suffix) ||
92
+ specifier.length < prefix.length + suffix.length) {
93
+ continue;
94
+ }
95
+ if (prefix.length > bestPrefixLength) {
96
+ bestPrefixLength = prefix.length;
97
+ best = pattern;
98
+ }
99
+ }
100
+ return best;
101
+ }
102
+ function aliasCandidates(specifier, options) {
103
+ const paths = options.paths;
104
+ if (paths === undefined) {
105
+ return [];
106
+ }
107
+ const base = options.baseUrl ??
108
+ options.pathsBasePath ??
109
+ undefined;
110
+ if (base === undefined) {
111
+ return [];
112
+ }
113
+ const pattern = bestPathPattern(specifier, paths);
114
+ if (pattern === undefined) {
115
+ return [];
116
+ }
117
+ const star = pattern.indexOf("*");
118
+ const matched = star === -1
119
+ ? ""
120
+ : specifier.slice(star, specifier.length - (pattern.length - star - 1));
121
+ const targets = paths[pattern];
122
+ if (!Array.isArray(targets)) {
123
+ return [];
124
+ }
125
+ return targets
126
+ .filter((target) => typeof target === "string")
127
+ .map((target) => {
128
+ const targetStar = target.indexOf("*");
129
+ const mapped = targetStar === -1
130
+ ? target
131
+ : target.slice(0, targetStar) + matched + target.slice(targetStar + 1);
132
+ return path.resolve(base, mapped);
133
+ });
134
+ }
135
+ export function resolveSpecifier(specifier, fromDir, settings) {
136
+ const options = settings?.options ?? RESOLUTION_OVERRIDES;
137
+ let resolvedModule;
138
+ try {
139
+ ({ resolvedModule } = ts.resolveModuleName(specifier, path.join(fromDir, "__colocate__.ts"), options, ts.sys, settings?.cache));
140
+ }
141
+ catch {
142
+ resolvedModule = undefined;
143
+ }
144
+ if (resolvedModule !== undefined &&
145
+ isSourceFile(resolvedModule.resolvedFileName)) {
146
+ const resolved = safeRealpath(resolvedModule.resolvedFileName);
147
+ if (resolved !== undefined) {
148
+ return resolved;
149
+ }
150
+ }
151
+ if (specifier.startsWith(".")) {
152
+ return probeResolvedPath(path.resolve(fromDir, specifier));
153
+ }
154
+ for (const candidate of aliasCandidates(specifier, options)) {
155
+ const resolved = probeResolvedPath(candidate);
156
+ if (resolved !== undefined) {
157
+ return resolved;
158
+ }
159
+ }
160
+ return undefined;
161
+ }
@@ -0,0 +1 @@
1
+ export declare function resolveRootDir(rootOption: string, cwd: string): string;
@@ -0,0 +1,27 @@
1
+ import path from "node:path";
2
+ import { safeStat } from "./fs-safe.js";
3
+ function isProjectBoundary(dir) {
4
+ return (safeStat(path.join(dir, "package.json")) !== undefined ||
5
+ safeStat(path.join(dir, ".git")) !== undefined);
6
+ }
7
+ export function resolveRootDir(rootOption, cwd) {
8
+ if (path.isAbsolute(rootOption)) {
9
+ return rootOption;
10
+ }
11
+ let dir = cwd;
12
+ while (true) {
13
+ const candidate = path.resolve(dir, rootOption);
14
+ if (safeStat(candidate)?.isDirectory() === true) {
15
+ return candidate;
16
+ }
17
+ if (isProjectBoundary(dir)) {
18
+ break;
19
+ }
20
+ const parent = path.dirname(dir);
21
+ if (parent === dir) {
22
+ break;
23
+ }
24
+ dir = parent;
25
+ }
26
+ return path.resolve(cwd, rootOption);
27
+ }
@@ -0,0 +1,8 @@
1
+ export declare const SOURCE_EXTS: readonly [".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"];
2
+ export declare const SKIP_DIRS: Set<string>;
3
+ export declare function isSourceFile(p: string): boolean;
4
+ export declare function isTestFile(relPath: string): boolean;
5
+ export declare function matchesIgnore(relPath: string, ignoreGlobs: string[]): boolean;
6
+ export declare function isOutsideRoot(relPath: string): boolean;
7
+ export declare function isExcludedPath(relPath: string, ignoreGlobs: string[]): boolean;
8
+ export declare function isInGraphScope(relPath: string, ignoreGlobs: string[]): boolean;
@@ -0,0 +1,58 @@
1
+ import path from "node:path";
2
+ import { minimatch } from "minimatch";
3
+ export const SOURCE_EXTS = [
4
+ ".ts",
5
+ ".tsx",
6
+ ".js",
7
+ ".jsx",
8
+ ".mts",
9
+ ".cts",
10
+ ".mjs",
11
+ ".cjs",
12
+ ];
13
+ const DECLARATION_EXTS = [".d.ts", ".d.mts", ".d.cts"];
14
+ export const SKIP_DIRS = new Set([
15
+ "node_modules",
16
+ "dist",
17
+ "coverage",
18
+ ".git",
19
+ ".hg",
20
+ ".svn",
21
+ ]);
22
+ export function isSourceFile(p) {
23
+ if (DECLARATION_EXTS.some((ext) => p.endsWith(ext))) {
24
+ return false;
25
+ }
26
+ return SOURCE_EXTS.some((ext) => p.endsWith(ext));
27
+ }
28
+ export function isTestFile(relPath) {
29
+ return (relPath.split(path.sep).includes("__tests__") ||
30
+ /\.(test|spec)\./.test(path.basename(relPath)));
31
+ }
32
+ export function matchesIgnore(relPath, ignoreGlobs) {
33
+ const normalized = relPath.split(path.sep).join("/");
34
+ return ignoreGlobs.some((glob) => minimatch(normalized, glob));
35
+ }
36
+ export function isOutsideRoot(relPath) {
37
+ return (relPath === "" ||
38
+ relPath === ".." ||
39
+ relPath.startsWith(".." + path.sep) ||
40
+ path.isAbsolute(relPath));
41
+ }
42
+ export function isExcludedPath(relPath, ignoreGlobs) {
43
+ const segments = relPath.split(path.sep);
44
+ for (let i = 1; i <= segments.length; i += 1) {
45
+ if (SKIP_DIRS.has(segments[i - 1])) {
46
+ return true;
47
+ }
48
+ if (matchesIgnore(segments.slice(0, i).join(path.sep), ignoreGlobs)) {
49
+ return true;
50
+ }
51
+ }
52
+ return false;
53
+ }
54
+ export function isInGraphScope(relPath, ignoreGlobs) {
55
+ return (!isOutsideRoot(relPath) &&
56
+ !isTestFile(relPath) &&
57
+ !isExcludedPath(relPath, ignoreGlobs));
58
+ }
@@ -0,0 +1,16 @@
1
+ import type { Rule } from "eslint";
2
+ import type { Graph } from "./graph.js";
3
+ export interface Subject {
4
+ readonly rootDir: string;
5
+ readonly realRootDir: string;
6
+ readonly file: string;
7
+ readonly ignore: string[];
8
+ graph(): Graph;
9
+ covers(filePath: string): boolean;
10
+ display(filePath: string): string;
11
+ }
12
+ export declare function resolvedLintRoot(cwd: string, rootOption: string): {
13
+ rootDir: string;
14
+ realRootDir: string;
15
+ } | undefined;
16
+ export declare function resolveSubject(context: Rule.RuleContext): Subject | undefined;
@@ -0,0 +1,61 @@
1
+ import path from "node:path";
2
+ import { safeRealpath } from "./fs-safe.js";
3
+ import { getGraph } from "./graph-cache.js";
4
+ import { resolveRootDir } from "./root.js";
5
+ import { isInGraphScope, isSourceFile } from "./scope.js";
6
+ const resolvedLintRoots = new Map();
7
+ const fileRealpathsByParse = new WeakMap();
8
+ export function resolvedLintRoot(cwd, rootOption) {
9
+ const key = cwd + "\0" + rootOption;
10
+ const cached = resolvedLintRoots.get(key);
11
+ if (cached !== undefined) {
12
+ return cached;
13
+ }
14
+ const rootDir = resolveRootDir(rootOption, cwd);
15
+ const realRootDir = safeRealpath(rootDir);
16
+ if (realRootDir === undefined) {
17
+ return undefined;
18
+ }
19
+ const resolved = { rootDir, realRootDir };
20
+ resolvedLintRoots.set(key, resolved);
21
+ return resolved;
22
+ }
23
+ export function resolveSubject(context) {
24
+ const options = (context.options[0] ?? {});
25
+ const root = options.root ?? ".";
26
+ const ignore = options.ignore ?? [];
27
+ if (!isSourceFile(context.filename)) {
28
+ return undefined;
29
+ }
30
+ const resolved = resolvedLintRoot(context.cwd, root);
31
+ if (resolved === undefined) {
32
+ return undefined;
33
+ }
34
+ const parse = context.sourceCode;
35
+ let file;
36
+ if (fileRealpathsByParse.has(parse)) {
37
+ file = fileRealpathsByParse.get(parse);
38
+ }
39
+ else {
40
+ file = safeRealpath(context.filename);
41
+ fileRealpathsByParse.set(parse, file);
42
+ }
43
+ if (file === undefined) {
44
+ return undefined;
45
+ }
46
+ const { rootDir, realRootDir } = resolved;
47
+ if (!isInGraphScope(path.relative(realRootDir, file), ignore)) {
48
+ return undefined;
49
+ }
50
+ let graph;
51
+ return {
52
+ rootDir,
53
+ realRootDir,
54
+ file,
55
+ ignore,
56
+ graph: () => (graph ??= getGraph(rootDir, ignore, file, context.sourceCode)),
57
+ covers: (filePath) => isSourceFile(filePath) &&
58
+ isInGraphScope(path.relative(realRootDir, filePath), ignore),
59
+ display: (filePath) => path.relative(realRootDir, filePath).split(path.sep).join("/"),
60
+ };
61
+ }
@@ -0,0 +1,10 @@
1
+ interface DirStamp {
2
+ mtimeMs: number;
3
+ ctimeMs: number;
4
+ size: number;
5
+ }
6
+ export declare function collectSourceFiles(resolvedRoot: string, ignoreGlobs: string[]): {
7
+ files: string[];
8
+ dirStamps: Map<string, DirStamp>;
9
+ };
10
+ export {};
@@ -0,0 +1,97 @@
1
+ import path from "node:path";
2
+ import { classifyDirEntry, safeReaddir, safeRealpath, safeStat, } from "./fs-safe.js";
3
+ import { isAtOrInsideDir } from "./paths.js";
4
+ import { isExcludedPath, isSourceFile, isTestFile, matchesIgnore, SKIP_DIRS, } from "./scope.js";
5
+ function stampDir(dir, dirStamps) {
6
+ const stat = safeStat(dir);
7
+ if (stat !== undefined) {
8
+ dirStamps.set(dir, {
9
+ mtimeMs: stat.mtimeMs,
10
+ ctimeMs: stat.ctimeMs,
11
+ size: stat.size,
12
+ });
13
+ }
14
+ }
15
+ function resolveWalkRealPath(fullPath, followLink, rootDir, ignoreGlobs) {
16
+ if (!followLink) {
17
+ return fullPath;
18
+ }
19
+ const realPath = safeRealpath(fullPath);
20
+ if (realPath === undefined || realPath === fullPath) {
21
+ return realPath;
22
+ }
23
+ if (!isAtOrInsideDir(realPath, rootDir)) {
24
+ return undefined;
25
+ }
26
+ if (isExcludedPath(path.relative(rootDir, realPath), ignoreGlobs)) {
27
+ return undefined;
28
+ }
29
+ return realPath;
30
+ }
31
+ function visitDirectory(fullPath, realPath, isLink, ctx, nested) {
32
+ if (isLink) {
33
+ if (ctx.linkedRealDirs.has(realPath)) {
34
+ return;
35
+ }
36
+ ctx.linkedRealDirs.add(realPath);
37
+ }
38
+ walkDir(fullPath, ctx.rootDir, ctx.ignoreGlobs, ctx.files, ctx.dirStamps, nested, ctx.linkedRealDirs, ctx.behindLink || isLink);
39
+ }
40
+ function collectFileEntry(fullPath, realPath, relPath, isLink, files) {
41
+ if (!isSourceFile(fullPath) || isTestFile(relPath)) {
42
+ return;
43
+ }
44
+ files.add(realPath);
45
+ if (isLink && fullPath !== realPath) {
46
+ files.add(fullPath);
47
+ }
48
+ }
49
+ function visitEntry(entry, dir, ctx, nested) {
50
+ const fullPath = path.join(dir, entry.name);
51
+ const relPath = path.relative(ctx.rootDir, fullPath);
52
+ const kind = classifyDirEntry(entry, fullPath);
53
+ if (kind === "other") {
54
+ return;
55
+ }
56
+ if (SKIP_DIRS.has(entry.name) || matchesIgnore(relPath, ctx.ignoreGlobs)) {
57
+ return;
58
+ }
59
+ const isLink = entry.isSymbolicLink();
60
+ const realPath = resolveWalkRealPath(fullPath, isLink || ctx.behindLink, ctx.rootDir, ctx.ignoreGlobs);
61
+ if (realPath === undefined) {
62
+ return;
63
+ }
64
+ if (kind === "directory") {
65
+ visitDirectory(fullPath, realPath, isLink, ctx, nested);
66
+ return;
67
+ }
68
+ collectFileEntry(fullPath, realPath, relPath, isLink, ctx.files);
69
+ }
70
+ function walkDir(dir, rootDir, ignoreGlobs, files, dirStamps, ancestorRealDirs, linkedRealDirs, behindLink) {
71
+ const realDir = safeRealpath(dir);
72
+ if (realDir === undefined || ancestorRealDirs.has(realDir)) {
73
+ return;
74
+ }
75
+ stampDir(dir, dirStamps);
76
+ const nested = new Set(ancestorRealDirs).add(realDir);
77
+ const ctx = {
78
+ rootDir,
79
+ ignoreGlobs,
80
+ files,
81
+ dirStamps,
82
+ linkedRealDirs,
83
+ behindLink,
84
+ };
85
+ for (const entry of safeReaddir(dir)) {
86
+ visitEntry(entry, dir, ctx, nested);
87
+ }
88
+ }
89
+ export function collectSourceFiles(resolvedRoot, ignoreGlobs) {
90
+ const collected = new Set();
91
+ const dirStamps = new Map();
92
+ walkDir(resolvedRoot, resolvedRoot, ignoreGlobs, collected, dirStamps, new Set(), new Set(), false);
93
+ return {
94
+ files: [...collected].sort(),
95
+ dirStamps,
96
+ };
97
+ }
@@ -0,0 +1,3 @@
1
+ import type { Rule } from "eslint";
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,141 @@
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";
5
+ import { requireIsShadowed } from "../lib/require-binding.js";
6
+ import { resolveSpecifier } from "../lib/resolve.js";
7
+ import { resolveSubject } from "../lib/subject.js";
8
+ function staticSpecifier(source) {
9
+ if (source.type === "Literal") {
10
+ return typeof source.value === "string" ? source.value : undefined;
11
+ }
12
+ if (source.type === "TemplateLiteral" &&
13
+ source.expressions.length === 0 &&
14
+ source.quasis.length === 1) {
15
+ return source.quasis[0]?.value.cooked ?? undefined;
16
+ }
17
+ return undefined;
18
+ }
19
+ function crossedGate(specifier, subject) {
20
+ const graph = subject.graph();
21
+ const resolved = resolveSpecifier(specifier, path.dirname(subject.file), getGraphResolutionSettings(graph));
22
+ if (resolved === undefined) {
23
+ return undefined;
24
+ }
25
+ const target = canonicalGraphPath(graph, resolved);
26
+ if (!subject.covers(target)) {
27
+ return undefined;
28
+ }
29
+ const crossed = findCrossedGate(target, canonicalGraphPath(graph, subject.file), graph, subject.realRootDir);
30
+ return crossed === undefined ? undefined : { ...crossed, target };
31
+ }
32
+ const rule = {
33
+ meta: {
34
+ type: "problem",
35
+ docs: {
36
+ description: "Require imports to enter a module through its entry file",
37
+ url: "https://github.com/gosukiwi/eslint-plugin-colocate#the-entry-rule",
38
+ },
39
+ schema: [
40
+ {
41
+ type: "object",
42
+ properties: {
43
+ root: { type: "string" },
44
+ ignore: { type: "array", items: { type: "string" } },
45
+ },
46
+ additionalProperties: false,
47
+ },
48
+ ],
49
+ messages: {
50
+ namedDoorReexport: "Named door '{{door}}' re-exports '{{target}}'; use an index.ts in the same folder for a multi-file public surface, or export only what this file declares.",
51
+ reachesPastEntry: "'{{target}}' is inside module '{{module}}'; import it through '{{entry}}', or move it out of '{{module}}' if it is not part of it.",
52
+ },
53
+ },
54
+ create(context) {
55
+ const subject = resolveSubject(context);
56
+ if (subject === undefined) {
57
+ return {};
58
+ }
59
+ 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
+ ]))
64
+ : undefined;
65
+ const reportNamedDoorReexport = (node) => {
66
+ if (reexportsByPos === undefined) {
67
+ return;
68
+ }
69
+ const start = node.range?.[0];
70
+ if (start === undefined) {
71
+ return;
72
+ }
73
+ const target = reexportsByPos.get(start);
74
+ if (target === undefined) {
75
+ return;
76
+ }
77
+ context.report({
78
+ node,
79
+ messageId: "namedDoorReexport",
80
+ data: {
81
+ door: subject.display(subject.file),
82
+ target: subject.display(target),
83
+ },
84
+ });
85
+ };
86
+ const check = (source) => {
87
+ if (source === null || source === undefined) {
88
+ return;
89
+ }
90
+ const specifier = staticSpecifier(source);
91
+ if (specifier === undefined) {
92
+ return;
93
+ }
94
+ const crossed = crossedGate(specifier, subject);
95
+ if (crossed === undefined) {
96
+ return;
97
+ }
98
+ context.report({
99
+ node: source,
100
+ messageId: "reachesPastEntry",
101
+ data: {
102
+ target: subject.display(crossed.target),
103
+ module: subject.display(crossed.dir),
104
+ entry: subject.display(crossed.entry),
105
+ },
106
+ });
107
+ };
108
+ return {
109
+ ImportDeclaration: (node) => check(node.source),
110
+ ExportNamedDeclaration: (node) => {
111
+ check(node.source);
112
+ reportNamedDoorReexport(node);
113
+ },
114
+ ExportAllDeclaration: (node) => {
115
+ check(node.source);
116
+ reportNamedDoorReexport(node);
117
+ },
118
+ ExportDefaultDeclaration: (node) => {
119
+ reportNamedDoorReexport(node);
120
+ },
121
+ ImportExpression: (node) => check(node.source),
122
+ TSImportType: (node) => check(node.source ?? node.argument?.literal),
123
+ TSImportEqualsDeclaration: (node) => {
124
+ if (node.moduleReference?.type === "TSExternalModuleReference") {
125
+ check(node.moduleReference.expression);
126
+ }
127
+ reportNamedDoorReexport(node);
128
+ },
129
+ CallExpression: (node) => {
130
+ if (node.callee.type !== "Identifier" ||
131
+ node.callee.name !== "require" ||
132
+ node.arguments.length !== 1 ||
133
+ requireIsShadowed(context.sourceCode, node)) {
134
+ return;
135
+ }
136
+ check(node.arguments[0]);
137
+ },
138
+ };
139
+ },
140
+ };
141
+ export default rule;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from "eslint";
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,45 @@
1
+ import { ownershipFindings } from "../lib/findings.js";
2
+ import { resolveSubject } from "../lib/subject.js";
3
+ const rule = {
4
+ meta: {
5
+ type: "problem",
6
+ docs: {
7
+ description: "Enforce dependency-ownership file layout",
8
+ url: "https://github.com/gosukiwi/eslint-plugin-colocate#what-it-reports",
9
+ },
10
+ schema: [
11
+ {
12
+ type: "object",
13
+ properties: {
14
+ root: { type: "string" },
15
+ ignore: { type: "array", items: { type: "string" } },
16
+ layers: { type: "array", items: { type: "string" } },
17
+ },
18
+ additionalProperties: false,
19
+ },
20
+ ],
21
+ messages: {
22
+ privateOutsideOwner: "File is imported by a single owner but sits outside that owner's folder.",
23
+ sharedTooHigh: "File is imported by multiple owners but sits above their common ancestor directory.",
24
+ sharedInsideOwner: "File is imported by multiple owners but sits inside a single owner's folder.",
25
+ singletonFolder: "Directory contains a single source file with no companion CSS; colocate or flatten the file.",
26
+ },
27
+ },
28
+ create(context) {
29
+ const options = (context.options[0] ?? {});
30
+ const layers = options.layers ?? [];
31
+ return {
32
+ Program(node) {
33
+ const subject = resolveSubject(context);
34
+ if (subject === undefined) {
35
+ return;
36
+ }
37
+ const reportNode = node.body[0] ?? node;
38
+ for (const messageId of ownershipFindings(subject, context.cwd, layers)) {
39
+ context.report({ node: reportNode, messageId });
40
+ }
41
+ },
42
+ };
43
+ },
44
+ };
45
+ export default rule;
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@gosukiwi/eslint-plugin-colocate",
3
+ "version": "0.0.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "license": "MIT",
8
+ "author": "Federico Ramirez",
9
+ "type": "module",
10
+ "engines": {
11
+ "node": ">=20"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "test": "vitest run",
25
+ "typecheck": "tsc --noEmit",
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"
28
+ },
29
+ "peerDependencies": {
30
+ "eslint": ">=9"
31
+ },
32
+ "dependencies": {
33
+ "minimatch": "^10",
34
+ "typescript": "^5 || ^6"
35
+ },
36
+ "devDependencies": {
37
+ "@gosukiwi/crap4ts": "^0.2.2",
38
+ "@types/node": "^22.15.29",
39
+ "@typescript-eslint/parser": "^8",
40
+ "@vitest/coverage-v8": "^3.2.7",
41
+ "eslint": "^10",
42
+ "typescript": "^5 || ^6",
43
+ "vitest": "^3"
44
+ }
45
+ }