@barqjs/cli 0.2.0-next.0 → 0.2.0-next.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/dist/index.js +98 -4
- package/package.json +2 -2
- package/src/check.ts +148 -12
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { existsSync, readFileSync, readdirSync, realpathSync, statSync, writeFil
|
|
|
3
3
|
import { stdout } from "node:process";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
|
-
import { join, relative, resolve, sep } from "node:path";
|
|
6
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
7
7
|
// src/check.ts
|
|
8
8
|
/**
|
|
9
9
|
* `barq check`: every file of a project through the compiler's diagnostics,
|
|
@@ -57,11 +57,48 @@ const SKIPPED = /* @__PURE__ */ new Set([
|
|
|
57
57
|
]);
|
|
58
58
|
/** A fix can uncover the next one, such as a call inside a call. */
|
|
59
59
|
const MAX_PASSES = 5;
|
|
60
|
+
const NAMED_IMPORT = /import\s*\{([^}]*)\}\s*from\s*["']([^"']+)["']/g;
|
|
61
|
+
/**
|
|
62
|
+
* Whether `code` calls a binding it imports the way a `layer` binding is
|
|
63
|
+
* called, which makes it a module the build compiles though it never names
|
|
64
|
+
* barq. The same guess as `@barqjs/vite`'s, so both compile the same files.
|
|
65
|
+
*/
|
|
66
|
+
function callsThroughImport(code) {
|
|
67
|
+
for (const [, names, from] of code.matchAll(NAMED_IMPORT)) {
|
|
68
|
+
if (from === "@barqjs/css" || names === void 0) continue;
|
|
69
|
+
for (const entry of names.split(",")) {
|
|
70
|
+
const local = entry.trim().replace(/^type\s+/, "").split(/\s+as\s+/).pop()?.trim();
|
|
71
|
+
if (local === void 0 || !/^[A-Za-z_$][\w$]*$/.test(local)) continue;
|
|
72
|
+
if (new RegExp(`(?<![\\w$.])${local.replace(/\$/g, "\\$")}(?:\\.dynamic\\(|(?:\\.(?:create|props))?\\([^()]*\\{)`).test(code)) return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
60
77
|
/** A script with no JSX is compiled only when it names barq, as the build does. */
|
|
61
78
|
function wanted(path, text) {
|
|
62
79
|
if (path.endsWith(".d.ts")) return false;
|
|
63
80
|
if (JSX.test(path)) return true;
|
|
64
|
-
return SCRIPT.test(path) && text().includes("@barqjs/");
|
|
81
|
+
return SCRIPT.test(path) && (text().includes("@barqjs/") || callsThroughImport(text()));
|
|
82
|
+
}
|
|
83
|
+
const EXTENSIONS = [
|
|
84
|
+
"",
|
|
85
|
+
".ts",
|
|
86
|
+
".tsx",
|
|
87
|
+
".js",
|
|
88
|
+
".jsx",
|
|
89
|
+
".mts",
|
|
90
|
+
"/index.ts",
|
|
91
|
+
"/index.tsx",
|
|
92
|
+
"/index.js"
|
|
93
|
+
];
|
|
94
|
+
/** A relative import as the file it names, or `undefined` for a package. */
|
|
95
|
+
function resolveImport(from, specifier) {
|
|
96
|
+
if (!specifier.startsWith(".")) return void 0;
|
|
97
|
+
const base = resolve(dirname(from), specifier);
|
|
98
|
+
for (const extension of EXTENSIONS) {
|
|
99
|
+
const candidate = base + extension;
|
|
100
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
101
|
+
}
|
|
65
102
|
}
|
|
66
103
|
function collect(path, out) {
|
|
67
104
|
if (statSync(path).isDirectory()) {
|
|
@@ -76,6 +113,62 @@ function collect(path, out) {
|
|
|
76
113
|
function severityOf(level) {
|
|
77
114
|
return level === "error" ? "error" : level === "note" ? "note" : "warning";
|
|
78
115
|
}
|
|
116
|
+
/** How many modules deep a folded value is followed, as `@barqjs/vite` does. */
|
|
117
|
+
const IMPORT_DEPTH = 6;
|
|
118
|
+
/**
|
|
119
|
+
* The transform the build runs for `file`: compiled again with the values it
|
|
120
|
+
* asked for from the files it imports, until a round learns nothing, so a call
|
|
121
|
+
* through an imported `layer` binding or token folds, and declines, as it does
|
|
122
|
+
* in the build.
|
|
123
|
+
*/
|
|
124
|
+
function transformWithImports(file, source, options, project, depth, seen) {
|
|
125
|
+
let out = compiler().transform(source, options);
|
|
126
|
+
if (depth <= 0) return out;
|
|
127
|
+
const cssImports = [];
|
|
128
|
+
const asked = /* @__PURE__ */ new Set();
|
|
129
|
+
for (let round = 0; round < 4; round++) {
|
|
130
|
+
const fresh = (out.cssWanted ?? []).filter(([specifier, name]) => !asked.has(`${specifier}\u0000${name}`));
|
|
131
|
+
if (fresh.length === 0) break;
|
|
132
|
+
let learned = false;
|
|
133
|
+
for (const [specifier = "", name = ""] of fresh) {
|
|
134
|
+
asked.add(`${specifier}\u0000${name}`);
|
|
135
|
+
const dependency = resolveImport(file, specifier);
|
|
136
|
+
if (dependency === void 0) continue;
|
|
137
|
+
for (const row of exportsOf(dependency, project, depth - 1, new Set(seen))) {
|
|
138
|
+
if (row[0] !== name) continue;
|
|
139
|
+
cssImports.push([specifier, ...row]);
|
|
140
|
+
learned = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!learned) break;
|
|
144
|
+
out = compiler().transform(source, {
|
|
145
|
+
...options,
|
|
146
|
+
cssImports
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
function exportsOf(file, project, depth, seen) {
|
|
152
|
+
if (depth <= 0 || seen.has(file)) return [];
|
|
153
|
+
seen.add(file);
|
|
154
|
+
const known = project.exports.get(file);
|
|
155
|
+
if (known !== void 0) return known;
|
|
156
|
+
let rows = [];
|
|
157
|
+
try {
|
|
158
|
+
rows = transformWithImports(file, readFileSync(file, "utf8"), {
|
|
159
|
+
filename: file,
|
|
160
|
+
root: dirname(file),
|
|
161
|
+
dev: true,
|
|
162
|
+
diagnostics: false,
|
|
163
|
+
sourceNames: false,
|
|
164
|
+
cssExports: true
|
|
165
|
+
}, project, depth, seen).cssExports ?? [];
|
|
166
|
+
} catch {
|
|
167
|
+
rows = [];
|
|
168
|
+
}
|
|
169
|
+
project.exports.set(file, rows);
|
|
170
|
+
return rows;
|
|
171
|
+
}
|
|
79
172
|
/** A finding about the project and no one position in a file. */
|
|
80
173
|
function about(file, severity, message) {
|
|
81
174
|
return {
|
|
@@ -105,7 +198,7 @@ function findingsOf(file, root, source, project) {
|
|
|
105
198
|
const name = relative(root, file).split(sep).join("/");
|
|
106
199
|
let diagnostics;
|
|
107
200
|
try {
|
|
108
|
-
diagnostics =
|
|
201
|
+
diagnostics = transformWithImports(file, source, {
|
|
109
202
|
filename: file,
|
|
110
203
|
root,
|
|
111
204
|
dev: true,
|
|
@@ -113,7 +206,7 @@ function findingsOf(file, root, source, project) {
|
|
|
113
206
|
sourceNames: false,
|
|
114
207
|
checks: options.levels?.map(([code, level]) => [code, level]),
|
|
115
208
|
routes: project.patterns
|
|
116
|
-
}).diagnostics ?? [];
|
|
209
|
+
}, project, IMPORT_DEPTH, /* @__PURE__ */ new Set([file])).diagnostics ?? [];
|
|
117
210
|
} catch (error) {
|
|
118
211
|
return [about(name, "error", error instanceof Error ? error.message : String(error))];
|
|
119
212
|
}
|
|
@@ -163,6 +256,7 @@ function check(options) {
|
|
|
163
256
|
const routes = routesOf(root, options.routes ?? "src/routes");
|
|
164
257
|
const project = {
|
|
165
258
|
options,
|
|
259
|
+
exports: /* @__PURE__ */ new Map(),
|
|
166
260
|
patterns: routes.patterns === void 0 ? void 0 : [...routes.patterns, ...options.extraRoutes ?? []]
|
|
167
261
|
};
|
|
168
262
|
const findings = [...routes.findings];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barqjs/cli",
|
|
3
|
-
"version": "0.2.0-next.
|
|
3
|
+
"version": "0.2.0-next.1",
|
|
4
4
|
"description": "The barq command: check a project against the compiler's diagnostics, as text or JSON, and apply the fixes it offers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"test": "bun test --timeout 60000"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@barqjs/compiler": "0.2.0-next.
|
|
55
|
+
"@barqjs/compiler": "0.2.0-next.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@types/bun": "^1.4.4",
|
package/src/check.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
12
12
|
import { createRequire } from "node:module";
|
|
13
|
-
import { join, relative, resolve, sep } from "node:path";
|
|
13
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
14
14
|
|
|
15
15
|
export type Severity = "error" | "warning" | "note";
|
|
16
16
|
|
|
@@ -124,8 +124,10 @@ interface Native {
|
|
|
124
124
|
sourceNames: boolean;
|
|
125
125
|
checks?: string[][];
|
|
126
126
|
routes?: string[];
|
|
127
|
+
cssImports?: string[][];
|
|
128
|
+
cssExports?: boolean;
|
|
127
129
|
},
|
|
128
|
-
): { diagnostics?: NativeDiagnostic[] };
|
|
130
|
+
): { diagnostics?: NativeDiagnostic[]; cssWanted?: string[][]; cssExports?: string[][] };
|
|
129
131
|
diagnosticCodes(): { code: string; level: string; summary: string; docs: string }[];
|
|
130
132
|
routeTree(
|
|
131
133
|
root: string,
|
|
@@ -151,11 +153,61 @@ const SKIPPED = new Set(["node_modules", "dist", "build", "coverage", ".git", ".
|
|
|
151
153
|
/** A fix can uncover the next one, such as a call inside a call. */
|
|
152
154
|
const MAX_PASSES = 5;
|
|
153
155
|
|
|
156
|
+
const NAMED_IMPORT = /import\s*\{([^}]*)\}\s*from\s*["']([^"']+)["']/g;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Whether `code` calls a binding it imports the way a `layer` binding is
|
|
160
|
+
* called, which makes it a module the build compiles though it never names
|
|
161
|
+
* barq. The same guess as `@barqjs/vite`'s, so both compile the same files.
|
|
162
|
+
*/
|
|
163
|
+
function callsThroughImport(code: string): boolean {
|
|
164
|
+
for (const [, names, from] of code.matchAll(NAMED_IMPORT)) {
|
|
165
|
+
if (from === "@barqjs/css" || names === undefined) continue;
|
|
166
|
+
for (const entry of names.split(",")) {
|
|
167
|
+
const local = entry
|
|
168
|
+
.trim()
|
|
169
|
+
.replace(/^type\s+/, "")
|
|
170
|
+
.split(/\s+as\s+/)
|
|
171
|
+
.pop()
|
|
172
|
+
?.trim();
|
|
173
|
+
if (local === undefined || !/^[A-Za-z_$][\w$]*$/.test(local)) continue;
|
|
174
|
+
const call = new RegExp(
|
|
175
|
+
`(?<![\\w$.])${local.replace(/\$/g, "\\$")}(?:\\.dynamic\\(|(?:\\.(?:create|props))?\\([^()]*\\{)`,
|
|
176
|
+
);
|
|
177
|
+
if (call.test(code)) return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
154
183
|
/** A script with no JSX is compiled only when it names barq, as the build does. */
|
|
155
184
|
function wanted(path: string, text: () => string): boolean {
|
|
156
185
|
if (path.endsWith(".d.ts")) return false;
|
|
157
186
|
if (JSX.test(path)) return true;
|
|
158
|
-
return SCRIPT.test(path) && text().includes("@barqjs/");
|
|
187
|
+
return SCRIPT.test(path) && (text().includes("@barqjs/") || callsThroughImport(text()));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const EXTENSIONS = [
|
|
191
|
+
"",
|
|
192
|
+
".ts",
|
|
193
|
+
".tsx",
|
|
194
|
+
".js",
|
|
195
|
+
".jsx",
|
|
196
|
+
".mts",
|
|
197
|
+
"/index.ts",
|
|
198
|
+
"/index.tsx",
|
|
199
|
+
"/index.js",
|
|
200
|
+
];
|
|
201
|
+
|
|
202
|
+
/** A relative import as the file it names, or `undefined` for a package. */
|
|
203
|
+
function resolveImport(from: string, specifier: string): string | undefined {
|
|
204
|
+
if (!specifier.startsWith(".")) return undefined;
|
|
205
|
+
const base = resolve(dirname(from), specifier);
|
|
206
|
+
for (const extension of EXTENSIONS) {
|
|
207
|
+
const candidate = base + extension;
|
|
208
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
209
|
+
}
|
|
210
|
+
return undefined;
|
|
159
211
|
}
|
|
160
212
|
|
|
161
213
|
function collect(path: string, out: string[]): void {
|
|
@@ -177,6 +229,82 @@ interface Project {
|
|
|
177
229
|
options: CheckOptions;
|
|
178
230
|
/** The route patterns, when the project has file routes. */
|
|
179
231
|
patterns: string[] | undefined;
|
|
232
|
+
/** What each file exports that a style call folds against, once per file. */
|
|
233
|
+
exports: Map<string, string[][]>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** How many modules deep a folded value is followed, as `@barqjs/vite` does. */
|
|
237
|
+
const IMPORT_DEPTH = 6;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The transform the build runs for `file`: compiled again with the values it
|
|
241
|
+
* asked for from the files it imports, until a round learns nothing, so a call
|
|
242
|
+
* through an imported `layer` binding or token folds, and declines, as it does
|
|
243
|
+
* in the build.
|
|
244
|
+
*/
|
|
245
|
+
function transformWithImports(
|
|
246
|
+
file: string,
|
|
247
|
+
source: string,
|
|
248
|
+
options: Parameters<Native["transform"]>[1],
|
|
249
|
+
project: Project,
|
|
250
|
+
depth: number,
|
|
251
|
+
seen: Set<string>,
|
|
252
|
+
): ReturnType<Native["transform"]> {
|
|
253
|
+
let out = compiler().transform(source, options);
|
|
254
|
+
if (depth <= 0) return out;
|
|
255
|
+
const cssImports: string[][] = [];
|
|
256
|
+
const asked = new Set<string>();
|
|
257
|
+
for (let round = 0; round < 4; round++) {
|
|
258
|
+
const fresh = (out.cssWanted ?? []).filter(
|
|
259
|
+
([specifier, name]) => !asked.has(`${specifier}\u0000${name}`),
|
|
260
|
+
);
|
|
261
|
+
if (fresh.length === 0) break;
|
|
262
|
+
let learned = false;
|
|
263
|
+
for (const [specifier = "", name = ""] of fresh) {
|
|
264
|
+
asked.add(`${specifier}\u0000${name}`);
|
|
265
|
+
const dependency = resolveImport(file, specifier);
|
|
266
|
+
if (dependency === undefined) continue;
|
|
267
|
+
for (const row of exportsOf(dependency, project, depth - 1, new Set(seen))) {
|
|
268
|
+
if (row[0] !== name) continue;
|
|
269
|
+
cssImports.push([specifier, ...row]);
|
|
270
|
+
learned = true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!learned) break;
|
|
274
|
+
out = compiler().transform(source, { ...options, cssImports });
|
|
275
|
+
}
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function exportsOf(file: string, project: Project, depth: number, seen: Set<string>): string[][] {
|
|
280
|
+
if (depth <= 0 || seen.has(file)) return [];
|
|
281
|
+
seen.add(file);
|
|
282
|
+
const known = project.exports.get(file);
|
|
283
|
+
if (known !== undefined) return known;
|
|
284
|
+
let rows: string[][] = [];
|
|
285
|
+
try {
|
|
286
|
+
const source = readFileSync(file, "utf8");
|
|
287
|
+
rows =
|
|
288
|
+
transformWithImports(
|
|
289
|
+
file,
|
|
290
|
+
source,
|
|
291
|
+
{
|
|
292
|
+
filename: file,
|
|
293
|
+
root: dirname(file),
|
|
294
|
+
dev: true,
|
|
295
|
+
diagnostics: false,
|
|
296
|
+
sourceNames: false,
|
|
297
|
+
cssExports: true,
|
|
298
|
+
},
|
|
299
|
+
project,
|
|
300
|
+
depth,
|
|
301
|
+
seen,
|
|
302
|
+
).cssExports ?? [];
|
|
303
|
+
} catch {
|
|
304
|
+
rows = [];
|
|
305
|
+
}
|
|
306
|
+
project.exports.set(file, rows);
|
|
307
|
+
return rows;
|
|
180
308
|
}
|
|
181
309
|
|
|
182
310
|
/** A finding about the project and no one position in a file. */
|
|
@@ -212,15 +340,22 @@ function findingsOf(file: string, root: string, source: string, project: Project
|
|
|
212
340
|
let diagnostics: NativeDiagnostic[];
|
|
213
341
|
try {
|
|
214
342
|
diagnostics =
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
343
|
+
transformWithImports(
|
|
344
|
+
file,
|
|
345
|
+
source,
|
|
346
|
+
{
|
|
347
|
+
filename: file,
|
|
348
|
+
root,
|
|
349
|
+
dev: true,
|
|
350
|
+
diagnostics: true,
|
|
351
|
+
sourceNames: false,
|
|
352
|
+
checks: options.levels?.map(([code, level]) => [code, level]),
|
|
353
|
+
routes: project.patterns,
|
|
354
|
+
},
|
|
355
|
+
project,
|
|
356
|
+
IMPORT_DEPTH,
|
|
357
|
+
new Set([file]),
|
|
358
|
+
).diagnostics ?? [];
|
|
224
359
|
} catch (error) {
|
|
225
360
|
// The compiler refuses a file it cannot parse by throwing, and the message
|
|
226
361
|
// already carries the position.
|
|
@@ -287,6 +422,7 @@ export function check(options: CheckOptions): CheckResult {
|
|
|
287
422
|
const routes = routesOf(root, options.routes ?? "src/routes");
|
|
288
423
|
const project: Project = {
|
|
289
424
|
options,
|
|
425
|
+
exports: new Map(),
|
|
290
426
|
patterns:
|
|
291
427
|
routes.patterns === undefined
|
|
292
428
|
? undefined
|