@sqve/stomme-ts-imports 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/config.d.ts +19 -0
- package/dist/config.js +267 -0
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.js +8 -0
- package/dist/rules/moduleBoundaries.d.ts +2 -0
- package/dist/rules/moduleBoundaries.js +232 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Oskar Grunning
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# stomme-ts-imports
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
stomme-ts-imports reads the module table in `stomme.toml` and checks TypeScript imports against it.
|
|
4
|
+
It runs through [Vite Plus](https://viteplus.dev) on Node 24 and Bun 1.4.
|
|
5
|
+
|
|
6
|
+
The package is public. The release publishes it to npm, and it is not on npm yet.
|
|
7
|
+
|
|
8
|
+
## Module table
|
|
9
|
+
|
|
10
|
+
`stomme.toml` declares each module with an `entry`, the files it makes `public`, and the modules it
|
|
11
|
+
`imports`:
|
|
12
|
+
|
|
13
|
+
```toml
|
|
14
|
+
schema = 1
|
|
15
|
+
version = "0.2.1"
|
|
16
|
+
language = "typescript"
|
|
17
|
+
|
|
18
|
+
[modules.errors]
|
|
19
|
+
entry = "src/errors.ts"
|
|
20
|
+
|
|
21
|
+
[modules.config]
|
|
22
|
+
entry = "src/config/config.ts"
|
|
23
|
+
public = ["src/config/models.ts"]
|
|
24
|
+
imports = ["errors"]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Paths are relative to the folder that holds `stomme.toml`.
|
|
28
|
+
|
|
29
|
+
## Development
|
|
30
|
+
|
|
31
|
+
From the repository root:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
pnpm install
|
|
35
|
+
pnpm --filter @sqve/stomme-ts-imports run check
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`check` runs typechecking, the self-lint through `stomme-ts-style`, the format check, the build, and
|
|
39
|
+
all tests.
|
|
40
|
+
|
|
41
|
+
Decisions and their reasons are in [docs/adr](../../docs/adr/README.md).
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
[MIT](LICENSE).
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ModuleRow {
|
|
2
|
+
name: string;
|
|
3
|
+
entry: string;
|
|
4
|
+
public: readonly string[];
|
|
5
|
+
imports: readonly string[];
|
|
6
|
+
}
|
|
7
|
+
export interface ProjectConfig {
|
|
8
|
+
root: string;
|
|
9
|
+
modules: ReadonlyMap<string, ModuleRow>;
|
|
10
|
+
}
|
|
11
|
+
export type ConfigResult = {
|
|
12
|
+
ok: true;
|
|
13
|
+
config: ProjectConfig;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
reason: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const parseConfig: (text: string, root: string) => ConfigResult;
|
|
19
|
+
export declare const findConfig: (directory: string) => ConfigResult | undefined;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
3
|
+
import { parse as parseToml } from 'smol-toml';
|
|
4
|
+
const configFileName = 'stomme.toml';
|
|
5
|
+
const schema = 1n;
|
|
6
|
+
const language = 'typescript';
|
|
7
|
+
const rowFields = new Set(['entry', 'public', 'imports']);
|
|
8
|
+
const errorMessage = (error) => (error instanceof Error ? error.message : String(error));
|
|
9
|
+
const refuse = (reason) => ({ ok: false, reason });
|
|
10
|
+
const isObject = (value) => typeof value === 'object' && value !== null;
|
|
11
|
+
const isTable = (value) => isObject(value) && !Array.isArray(value) && Object.getPrototypeOf(value) === null;
|
|
12
|
+
const isStringList = (value) => Array.isArray(value) && value.every((item) => typeof item === 'string');
|
|
13
|
+
const isStringTable = (value) => isTable(value) && Object.values(value).every((item) => typeof item === 'string');
|
|
14
|
+
const formatValue = (value) => {
|
|
15
|
+
if (typeof value === 'bigint') {
|
|
16
|
+
return value.toString();
|
|
17
|
+
}
|
|
18
|
+
return JSON.stringify(value);
|
|
19
|
+
};
|
|
20
|
+
const readPackageVersion = () => {
|
|
21
|
+
const text = readFileSync(new URL('../package.json', import.meta.url), 'utf8');
|
|
22
|
+
const manifest = JSON.parse(text);
|
|
23
|
+
if (!isObject(manifest) || !('version' in manifest)) {
|
|
24
|
+
throw new TypeError('package.json has no version');
|
|
25
|
+
}
|
|
26
|
+
const { version } = manifest;
|
|
27
|
+
if (typeof version !== 'string') {
|
|
28
|
+
throw new TypeError('package.json has no string version');
|
|
29
|
+
}
|
|
30
|
+
return version;
|
|
31
|
+
};
|
|
32
|
+
let packageVersion;
|
|
33
|
+
const getPackageVersion = () => (packageVersion ??= readPackageVersion());
|
|
34
|
+
const pathProblem = (name, path) => {
|
|
35
|
+
if (isAbsolute(path)) {
|
|
36
|
+
return `has a module \`${name}\` path \`${path}\` that is absolute; use a path relative to the project root`;
|
|
37
|
+
}
|
|
38
|
+
if (path.split('/').some((part) => part === '..')) {
|
|
39
|
+
return `has a module \`${name}\` path \`${path}\` with a \`..\` part; use a path inside the project`;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
43
|
+
const rowFieldProblem = (name, value) => {
|
|
44
|
+
for (const key of Object.keys(value).toSorted()) {
|
|
45
|
+
if (!rowFields.has(key)) {
|
|
46
|
+
return `has a module \`${name}\` with \`${key}\`; read only \`entry\`, \`public\`, and \`imports\``;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
};
|
|
51
|
+
const rowPathProblem = (name, entry, publicFiles) => {
|
|
52
|
+
for (const path of [entry, ...publicFiles]) {
|
|
53
|
+
const problem = pathProblem(name, path);
|
|
54
|
+
if (problem !== undefined) {
|
|
55
|
+
return problem;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
};
|
|
60
|
+
const rowImportProblem = (name, imports, moduleNames) => {
|
|
61
|
+
for (const target of imports) {
|
|
62
|
+
if (!moduleNames.has(target)) {
|
|
63
|
+
return `has a module \`${name}\` that imports \`${target}\`, which has no \`[modules.${target}]\` row`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
};
|
|
68
|
+
const parseRow = (name, value, moduleNames) => {
|
|
69
|
+
if (!isTable(value)) {
|
|
70
|
+
return { ok: false, reason: `has a module \`${name}\` that is not a table` };
|
|
71
|
+
}
|
|
72
|
+
const fieldProblem = rowFieldProblem(name, value);
|
|
73
|
+
if (fieldProblem !== undefined) {
|
|
74
|
+
return { ok: false, reason: fieldProblem };
|
|
75
|
+
}
|
|
76
|
+
const entry = value.entry;
|
|
77
|
+
if (typeof entry !== 'string') {
|
|
78
|
+
return { ok: false, reason: `has a module \`${name}\` without a string \`entry\`` };
|
|
79
|
+
}
|
|
80
|
+
const publicFiles = value.public ?? [];
|
|
81
|
+
if (!isStringList(publicFiles)) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
reason: `has a module \`${name}\` with \`public\` that is not a list of strings`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const imports = value.imports ?? [];
|
|
88
|
+
if (!isStringList(imports)) {
|
|
89
|
+
return {
|
|
90
|
+
ok: false,
|
|
91
|
+
reason: `has a module \`${name}\` with \`imports\` that is not a list of strings`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const pathRefusal = rowPathProblem(name, entry, publicFiles);
|
|
95
|
+
if (pathRefusal !== undefined) {
|
|
96
|
+
return { ok: false, reason: pathRefusal };
|
|
97
|
+
}
|
|
98
|
+
const importProblem = rowImportProblem(name, imports, moduleNames);
|
|
99
|
+
if (importProblem !== undefined) {
|
|
100
|
+
return { ok: false, reason: importProblem };
|
|
101
|
+
}
|
|
102
|
+
return { ok: true, row: { name, entry, public: publicFiles, imports } };
|
|
103
|
+
};
|
|
104
|
+
const parseModules = (value) => {
|
|
105
|
+
if (value === undefined) {
|
|
106
|
+
return { ok: true, modules: new Map() };
|
|
107
|
+
}
|
|
108
|
+
if (!isTable(value)) {
|
|
109
|
+
return { ok: false, reason: 'has `modules` that is not a table' };
|
|
110
|
+
}
|
|
111
|
+
const names = Object.keys(value).toSorted();
|
|
112
|
+
const moduleNames = new Set(names);
|
|
113
|
+
const modules = new Map();
|
|
114
|
+
for (const name of names) {
|
|
115
|
+
const result = parseRow(name, value[name], moduleNames);
|
|
116
|
+
if (!result.ok) {
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
modules.set(name, result.row);
|
|
120
|
+
}
|
|
121
|
+
return { ok: true, modules };
|
|
122
|
+
};
|
|
123
|
+
const schemaProblem = (table) => {
|
|
124
|
+
const value = table.schema;
|
|
125
|
+
if (value === undefined) {
|
|
126
|
+
return `is missing \`schema\`; use \`schema = ${schema}\``;
|
|
127
|
+
}
|
|
128
|
+
if (typeof value !== 'bigint') {
|
|
129
|
+
return `has a \`schema\` that is not an integer; use \`schema = ${schema}\``;
|
|
130
|
+
}
|
|
131
|
+
if (value !== schema) {
|
|
132
|
+
return `has \`schema = ${formatValue(value)}\`; this package reads \`schema = ${schema}\``;
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
};
|
|
136
|
+
const versionProblem = (table) => {
|
|
137
|
+
const value = table.version;
|
|
138
|
+
if (value === undefined) {
|
|
139
|
+
return 'is missing `version`';
|
|
140
|
+
}
|
|
141
|
+
if (typeof value !== 'string') {
|
|
142
|
+
return 'has `version` that is not a string';
|
|
143
|
+
}
|
|
144
|
+
const expected = getPackageVersion();
|
|
145
|
+
if (value !== expected) {
|
|
146
|
+
return `has \`version = ${formatValue(value)}\`; this package is version ${expected}`;
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
};
|
|
150
|
+
const languageProblem = (table) => {
|
|
151
|
+
const value = table.language;
|
|
152
|
+
if (value === undefined) {
|
|
153
|
+
return 'is missing `language`';
|
|
154
|
+
}
|
|
155
|
+
if (typeof value !== 'string') {
|
|
156
|
+
return 'has `language` that is not a string';
|
|
157
|
+
}
|
|
158
|
+
if (value !== language) {
|
|
159
|
+
return `has \`language = ${formatValue(value)}\`; this package reads only \`language = "${language}"\``;
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
};
|
|
163
|
+
const layersProblem = (table) => {
|
|
164
|
+
const value = table.layers;
|
|
165
|
+
if (value !== undefined && !isStringList(value)) {
|
|
166
|
+
return 'has `layers` that is not a list of strings';
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
};
|
|
170
|
+
const rolesProblem = (table) => {
|
|
171
|
+
const value = table.roles;
|
|
172
|
+
if (value !== undefined && !isStringTable(value)) {
|
|
173
|
+
return 'has `roles` that is not a table of strings';
|
|
174
|
+
}
|
|
175
|
+
return undefined;
|
|
176
|
+
};
|
|
177
|
+
export const parseConfig = (text, root) => {
|
|
178
|
+
let table;
|
|
179
|
+
try {
|
|
180
|
+
table = parseToml(text, { integersAsBigInt: true });
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
return refuse(`is not valid TOML: ${errorMessage(error)}`);
|
|
184
|
+
}
|
|
185
|
+
const schemaRefusal = schemaProblem(table);
|
|
186
|
+
if (schemaRefusal !== undefined) {
|
|
187
|
+
return refuse(schemaRefusal);
|
|
188
|
+
}
|
|
189
|
+
const versionRefusal = versionProblem(table);
|
|
190
|
+
if (versionRefusal !== undefined) {
|
|
191
|
+
return refuse(versionRefusal);
|
|
192
|
+
}
|
|
193
|
+
const languageRefusal = languageProblem(table);
|
|
194
|
+
if (languageRefusal !== undefined) {
|
|
195
|
+
return refuse(languageRefusal);
|
|
196
|
+
}
|
|
197
|
+
const layersRefusal = layersProblem(table);
|
|
198
|
+
if (layersRefusal !== undefined) {
|
|
199
|
+
return refuse(layersRefusal);
|
|
200
|
+
}
|
|
201
|
+
const rolesRefusal = rolesProblem(table);
|
|
202
|
+
if (rolesRefusal !== undefined) {
|
|
203
|
+
return refuse(rolesRefusal);
|
|
204
|
+
}
|
|
205
|
+
const modules = parseModules(table.modules);
|
|
206
|
+
if (!modules.ok) {
|
|
207
|
+
return modules;
|
|
208
|
+
}
|
|
209
|
+
return { ok: true, config: { root, modules: modules.modules } };
|
|
210
|
+
};
|
|
211
|
+
const fileByDirectory = new Map();
|
|
212
|
+
const parseByFile = new Map();
|
|
213
|
+
const findConfigFile = (directory) => {
|
|
214
|
+
const visited = [];
|
|
215
|
+
let current = resolve(directory);
|
|
216
|
+
let found;
|
|
217
|
+
while (found === undefined) {
|
|
218
|
+
const cached = fileByDirectory.get(current);
|
|
219
|
+
if (cached !== undefined) {
|
|
220
|
+
found = cached;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
visited.push(current);
|
|
224
|
+
const candidate = join(current, configFileName);
|
|
225
|
+
if (existsSync(candidate)) {
|
|
226
|
+
found = candidate;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
const parent = dirname(current);
|
|
230
|
+
if (parent === current) {
|
|
231
|
+
found = null;
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
current = parent;
|
|
235
|
+
}
|
|
236
|
+
for (const folder of visited) {
|
|
237
|
+
fileByDirectory.set(folder, found ?? null);
|
|
238
|
+
}
|
|
239
|
+
return found ?? undefined;
|
|
240
|
+
};
|
|
241
|
+
export const findConfig = (directory) => {
|
|
242
|
+
const path = findConfigFile(directory);
|
|
243
|
+
if (path === undefined) {
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
let text;
|
|
247
|
+
try {
|
|
248
|
+
text = readFileSync(path, 'utf8');
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return refuse(`could not read the file: ${errorMessage(error)}`);
|
|
252
|
+
}
|
|
253
|
+
let mtimeMs;
|
|
254
|
+
try {
|
|
255
|
+
mtimeMs = statSync(path).mtimeMs;
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
return parseConfig(text, dirname(path));
|
|
259
|
+
}
|
|
260
|
+
const cached = parseByFile.get(path);
|
|
261
|
+
if (cached?.mtimeMs === mtimeMs) {
|
|
262
|
+
return cached.result;
|
|
263
|
+
}
|
|
264
|
+
const result = parseConfig(text, dirname(path));
|
|
265
|
+
parseByFile.set(path, { mtimeMs, result });
|
|
266
|
+
return result;
|
|
267
|
+
};
|
package/dist/plugin.d.ts
ADDED
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { findConfig } from '../config.js';
|
|
4
|
+
const stripExtension = (path) => path.replace(/\.(?:cjs|cts|js|jsx|mjs|mts|ts|tsx)$/, '');
|
|
5
|
+
const packageRoots = new Map();
|
|
6
|
+
const packageRootOf = (directory) => {
|
|
7
|
+
if (packageRoots.has(directory)) {
|
|
8
|
+
return packageRoots.get(directory);
|
|
9
|
+
}
|
|
10
|
+
const parent = posix.dirname(directory);
|
|
11
|
+
let found;
|
|
12
|
+
if (existsSync(posix.join(directory, 'package.json'))) {
|
|
13
|
+
found = directory;
|
|
14
|
+
}
|
|
15
|
+
else if (parent !== directory) {
|
|
16
|
+
found = packageRootOf(parent);
|
|
17
|
+
}
|
|
18
|
+
packageRoots.set(directory, found);
|
|
19
|
+
return found;
|
|
20
|
+
};
|
|
21
|
+
const sourceRootOf = (path) => {
|
|
22
|
+
const packageRoot = packageRootOf(posix.dirname(path));
|
|
23
|
+
if (packageRoot !== undefined) {
|
|
24
|
+
return path.startsWith(`${packageRoot}/src/`) ? `${packageRoot}/src` : undefined;
|
|
25
|
+
}
|
|
26
|
+
const segments = path.split('/');
|
|
27
|
+
const index = segments.lastIndexOf('src');
|
|
28
|
+
return index === -1 ? undefined : segments.slice(0, index + 1).join('/');
|
|
29
|
+
};
|
|
30
|
+
const isSupportFile = (sourceRoot, path) => {
|
|
31
|
+
if (/\.test\.tsx?$/.test(path)) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
const relative = path.slice(sourceRoot.length + 1);
|
|
35
|
+
return relative.split('/').slice(0, -1).includes('fixtures');
|
|
36
|
+
};
|
|
37
|
+
const moduleEntryOf = (config, row) => stripExtension(posix.normalize(posix.join(config.root, row.entry)));
|
|
38
|
+
const isFolderModule = (entry) => posix.basename(entry) === posix.basename(posix.dirname(entry));
|
|
39
|
+
const locateModules = (config) => [...config.modules.values()].map((row) => {
|
|
40
|
+
const entry = moduleEntryOf(config, row);
|
|
41
|
+
const isFolder = isFolderModule(entry);
|
|
42
|
+
return {
|
|
43
|
+
row,
|
|
44
|
+
entry,
|
|
45
|
+
location: isFolder ? posix.dirname(entry) : entry,
|
|
46
|
+
isFolder,
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
const containsPath = (module, path) => module.isFolder
|
|
50
|
+
? path === module.location || path.startsWith(`${module.location}/`)
|
|
51
|
+
: path === module.location;
|
|
52
|
+
const moduleOf = (modules, path) => modules
|
|
53
|
+
.filter((module) => containsPath(module, path))
|
|
54
|
+
.toSorted((first, second) => second.location.length - first.location.length)[0];
|
|
55
|
+
const publicLocationsOf = (config, module) => [
|
|
56
|
+
module.entry,
|
|
57
|
+
...module.row.public.map((file) => stripExtension(posix.normalize(posix.join(config.root, file)))),
|
|
58
|
+
];
|
|
59
|
+
const isPublicPath = (config, module, path) => publicLocationsOf(config, module).includes(path);
|
|
60
|
+
const allowedTargetsOf = (config, ownModule) => {
|
|
61
|
+
const importedNames = new Set();
|
|
62
|
+
for (const row of config.modules.values()) {
|
|
63
|
+
for (const target of row.imports) {
|
|
64
|
+
importedNames.add(target);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const allowedTargets = new Set();
|
|
68
|
+
if (ownModule !== undefined) {
|
|
69
|
+
allowedTargets.add(ownModule.row.name);
|
|
70
|
+
for (const target of ownModule.row.imports) {
|
|
71
|
+
allowedTargets.add(target);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const row of config.modules.values()) {
|
|
75
|
+
if (!importedNames.has(row.name)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
for (const target of row.imports) {
|
|
79
|
+
allowedTargets.add(target);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return allowedTargets;
|
|
83
|
+
};
|
|
84
|
+
const permittedTargetsOf = (config, ownModule, support) => {
|
|
85
|
+
if (support) {
|
|
86
|
+
return allowedTargetsOf(config, ownModule);
|
|
87
|
+
}
|
|
88
|
+
if (ownModule === undefined) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
return new Set(ownModule.row.imports);
|
|
92
|
+
};
|
|
93
|
+
const literalSourceOf = (source) => {
|
|
94
|
+
if (source.type === 'Literal' && typeof source.value === 'string') {
|
|
95
|
+
return source.value;
|
|
96
|
+
}
|
|
97
|
+
if (source.type !== 'TemplateLiteral' || source.expressions.length > 0) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
return source.quasis[0]?.value.cooked ?? undefined;
|
|
101
|
+
};
|
|
102
|
+
export const moduleBoundariesRule = {
|
|
103
|
+
meta: {
|
|
104
|
+
type: 'problem',
|
|
105
|
+
schema: [],
|
|
106
|
+
messages: {
|
|
107
|
+
notAllowed: 'Module "{{source}}" must not import module "{{target}}". Add "{{target}}" to imports of [modules.{{source}}] in stomme.toml if the edge is intended.',
|
|
108
|
+
private: 'Import module "{{folder}}" through its entry file, not its private file "{{file}}". Add the file to public of [modules.{{folder}}] in stomme.toml if other modules depend on it.',
|
|
109
|
+
unknownFile: 'File "{{file}}" is not in a module. Add a [modules.<name>] row with its entry in stomme.toml.',
|
|
110
|
+
computedImport: 'Import a literal path so the module table can follow it. A computed import() hides its target.',
|
|
111
|
+
noConfig: 'No stomme.toml above this file. Add one with a [modules.<name>] row for each module.',
|
|
112
|
+
brokenConfig: 'stomme.toml {{reason}}',
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
create(context) {
|
|
116
|
+
const filename = context.filename.replaceAll('\\', '/');
|
|
117
|
+
const sourceRoot = sourceRootOf(filename);
|
|
118
|
+
if (sourceRoot === undefined) {
|
|
119
|
+
return {};
|
|
120
|
+
}
|
|
121
|
+
const result = findConfig(posix.dirname(filename));
|
|
122
|
+
if (result === undefined) {
|
|
123
|
+
return {
|
|
124
|
+
Program(node) {
|
|
125
|
+
context.report({ node, messageId: 'noConfig' });
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (!result.ok) {
|
|
130
|
+
return {
|
|
131
|
+
Program(node) {
|
|
132
|
+
context.report({ node, messageId: 'brokenConfig', data: { reason: result.reason } });
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const { config } = result;
|
|
137
|
+
if (config.modules.size === 0) {
|
|
138
|
+
return {};
|
|
139
|
+
}
|
|
140
|
+
const modules = locateModules(config);
|
|
141
|
+
const ownPath = stripExtension(filename);
|
|
142
|
+
const ownModule = moduleOf(modules, ownPath);
|
|
143
|
+
const support = isSupportFile(sourceRoot, filename);
|
|
144
|
+
const sourceName = ownModule?.row.name ?? ownPath.slice(sourceRoot.length + 1);
|
|
145
|
+
const allowedTargets = permittedTargetsOf(config, ownModule, support);
|
|
146
|
+
const checkPrivateFile = (node, target, targetPath) => {
|
|
147
|
+
if (!target.isFolder || targetPath === target.location) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (isPublicPath(config, target, targetPath)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (!containsPath(target, ownPath)) {
|
|
154
|
+
context.report({
|
|
155
|
+
node,
|
|
156
|
+
messageId: 'private',
|
|
157
|
+
data: {
|
|
158
|
+
folder: target.row.name,
|
|
159
|
+
file: targetPath.slice(target.location.length + 1),
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const checkTarget = (node, specifier) => {
|
|
165
|
+
const targetPath = stripExtension(posix.normalize(posix.join(posix.dirname(filename), specifier)));
|
|
166
|
+
if (!targetPath.startsWith(`${sourceRoot}/`)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const target = moduleOf(modules, targetPath);
|
|
170
|
+
if (!support && target !== undefined) {
|
|
171
|
+
checkPrivateFile(node, target, targetPath);
|
|
172
|
+
}
|
|
173
|
+
if (allowedTargets === undefined || target === undefined || target === ownModule) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (!allowedTargets.has(target.row.name)) {
|
|
177
|
+
context.report({
|
|
178
|
+
node,
|
|
179
|
+
messageId: 'notAllowed',
|
|
180
|
+
data: { source: sourceName, target: target.row.name },
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const check = (node, source) => {
|
|
185
|
+
const specifier = literalSourceOf(source);
|
|
186
|
+
if (specifier === undefined || !specifier.startsWith('.')) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
checkTarget(node, specifier);
|
|
190
|
+
};
|
|
191
|
+
const checkSource = (node, source) => {
|
|
192
|
+
if (source !== null && source !== undefined) {
|
|
193
|
+
check(node, source);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
return {
|
|
197
|
+
Program(node) {
|
|
198
|
+
if (ownModule === undefined && !support) {
|
|
199
|
+
context.report({
|
|
200
|
+
node,
|
|
201
|
+
messageId: 'unknownFile',
|
|
202
|
+
data: { file: filename.slice(sourceRoot.length + 1) },
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
ImportDeclaration(node) {
|
|
207
|
+
check(node, node.source);
|
|
208
|
+
},
|
|
209
|
+
ExportAllDeclaration(node) {
|
|
210
|
+
check(node, node.source);
|
|
211
|
+
},
|
|
212
|
+
ExportNamedDeclaration(node) {
|
|
213
|
+
checkSource(node, node.source);
|
|
214
|
+
},
|
|
215
|
+
ImportExpression(node) {
|
|
216
|
+
const specifier = literalSourceOf(node.source);
|
|
217
|
+
if (specifier === undefined) {
|
|
218
|
+
if (!support) {
|
|
219
|
+
context.report({ node, messageId: 'computedImport' });
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (specifier.startsWith('.')) {
|
|
224
|
+
checkTarget(node, specifier);
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
TSImportType(node) {
|
|
228
|
+
check(node, node.source);
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
},
|
|
232
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqve/stomme-ts-imports",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Oxlint import rules for the module table in stomme.toml.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"imports",
|
|
7
|
+
"lint",
|
|
8
|
+
"modules",
|
|
9
|
+
"oxlint",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/sQVe/stomme/tree/main/packages/imports#readme",
|
|
13
|
+
"bugs": "https://github.com/sQVe/stomme/issues",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Oskar Grunning",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/sQVe/stomme.git",
|
|
19
|
+
"directory": "packages/imports"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/plugin.d.ts",
|
|
28
|
+
"default": "./dist/plugin.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@oxlint/plugins": "1.79.0",
|
|
37
|
+
"smol-toml": "1.9.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "24.13.3",
|
|
41
|
+
"oxlint": "1.79.0",
|
|
42
|
+
"oxlint-tsgolint": "7.0.2001",
|
|
43
|
+
"typescript": "7.0.2",
|
|
44
|
+
"vite-plus": "0.3.0",
|
|
45
|
+
"vitest": "4.1.11"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"bun": ">=1.4.2",
|
|
49
|
+
"node": ">=24.21.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
53
|
+
"check": "pnpm typecheck && pnpm style:check && pnpm format:check && pnpm build && pnpm test",
|
|
54
|
+
"format": "vp fmt",
|
|
55
|
+
"format:check": "vp fmt --check",
|
|
56
|
+
"lint": "vp lint --deny-warnings",
|
|
57
|
+
"style:check": "node ../style/src/cli.ts",
|
|
58
|
+
"style:fix": "node ../style/src/cli.ts --fix",
|
|
59
|
+
"test": "vp test",
|
|
60
|
+
"typecheck": "tsc --noEmit"
|
|
61
|
+
}
|
|
6
62
|
}
|