@ref0/workspace 0.11.0 → 0.13.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 +68 -15
- package/dist/lint/generic/ast-fields.mjs +32 -0
- package/dist/lint/generic/build-cache-inputs.d.mts +9 -0
- package/dist/lint/generic/build-cache-inputs.mjs +147 -0
- package/dist/lint/generic/effect-gen-wrappers.d.mts +13 -0
- package/dist/lint/generic/effect-gen-wrappers.mjs +100 -0
- package/dist/lint/generic/plugin.d.mts +5 -2
- package/dist/lint/generic/plugin.mjs +15 -2
- package/dist/lint/generic/public-docs.d.mts +12 -0
- package/dist/lint/generic/public-docs.mjs +345 -0
- package/dist/lint/generic/pure-effect-fn.mjs +107 -0
- package/dist/lint/generic/pure-top-level.d.mts +44 -0
- package/dist/lint/generic/pure-top-level.mjs +393 -0
- package/dist/lint/generic/workspace-packages.d.mts +30 -1
- package/dist/lint/generic/workspace-packages.mjs +46 -5
- package/dist/lint/graph.d.mts +6 -2
- package/dist/lint/graph.mjs +2 -2
- package/dist/review-record/review-record.mjs +121 -26
- package/dist/review-record/settings.d.mts +14 -2
- package/dist/review-record/settings.mjs +10 -3
- package/dist/task-sources.d.mts +16 -0
- package/dist/task-sources.mjs +121 -0
- package/dist/types/cache.mjs +2 -2
- package/dist/types/inputs.mjs +23 -16
- package/package.json +10 -2
- package/tsconfig/base.json +20 -0
- package/tsconfig/bundler.json +8 -0
- package/tsconfig/node-next.json +8 -0
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { collectEffectFnBindings, isOutermostEffectFnCall, pureAnnotation } from "./pure-effect-fn.mjs";
|
|
2
|
+
import { fieldOf, isNode, nameOf, nodeAt, nodesAt } from "./ast-fields.mjs";
|
|
3
|
+
import { decodedOnce } from "./rule-options.mjs";
|
|
4
|
+
import { fromRoot } from "./workspace-packages.mjs";
|
|
5
|
+
import { dirname, resolve } from "node:path";
|
|
6
|
+
import { Predicate, Schema } from "effect";
|
|
7
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
8
|
+
// src/lint/generic/pure-top-level.ts
|
|
9
|
+
const decodeOptions = Schema.decodeUnknownSync(Schema.Tuple([Schema.Struct({
|
|
10
|
+
root: Schema.String,
|
|
11
|
+
callees: Schema.Array(Schema.Struct({
|
|
12
|
+
from: Schema.String,
|
|
13
|
+
name: Schema.String,
|
|
14
|
+
chain: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
15
|
+
curried: Schema.optionalKey(Schema.Literal(true)),
|
|
16
|
+
runsArguments: Schema.optionalKey(Schema.Literal(true)),
|
|
17
|
+
why: Schema.String
|
|
18
|
+
})),
|
|
19
|
+
requireMarks: Schema.optionalKey(Schema.Boolean)
|
|
20
|
+
})]));
|
|
21
|
+
const optionsOf = decodedOnce((options) => decodeOptions(options)[0]);
|
|
22
|
+
/** Wrappers that change a value's type and nothing it does at run time. */
|
|
23
|
+
const typeOnlyWrappers = /* @__PURE__ */ new Set([
|
|
24
|
+
"ParenthesizedExpression",
|
|
25
|
+
"TSAsExpression",
|
|
26
|
+
"TSSatisfiesExpression",
|
|
27
|
+
"TSNonNullExpression"
|
|
28
|
+
]);
|
|
29
|
+
/** Nodes a mark doesn't run: a function's body runs only when something calls it. */
|
|
30
|
+
const deferredNodes = /* @__PURE__ */ new Set(["ArrowFunctionExpression", "FunctionExpression"]);
|
|
31
|
+
/** Nodes with an effect of their own, whatever they call. A class's static parts run as it's built. */
|
|
32
|
+
const effectNodes = /* @__PURE__ */ new Set([
|
|
33
|
+
"AssignmentExpression",
|
|
34
|
+
"UpdateExpression",
|
|
35
|
+
"TaggedTemplateExpression",
|
|
36
|
+
"AwaitExpression",
|
|
37
|
+
"YieldExpression",
|
|
38
|
+
"ClassExpression"
|
|
39
|
+
]);
|
|
40
|
+
/** Nodes that can start where a call inside them does, so a mark before both is the outer one's. */
|
|
41
|
+
const chainNodes = /* @__PURE__ */ new Set([
|
|
42
|
+
"CallExpression",
|
|
43
|
+
"NewExpression",
|
|
44
|
+
"MemberExpression",
|
|
45
|
+
...typeOnlyWrappers
|
|
46
|
+
]);
|
|
47
|
+
/** The fields a walk skips: `parent` points back up, and the rest hold no code that runs. */
|
|
48
|
+
const skippedFields = /* @__PURE__ */ new Set([
|
|
49
|
+
"parent",
|
|
50
|
+
"type",
|
|
51
|
+
"start",
|
|
52
|
+
"end",
|
|
53
|
+
"range",
|
|
54
|
+
"loc",
|
|
55
|
+
"typeAnnotation",
|
|
56
|
+
"typeArguments",
|
|
57
|
+
"typeParameters",
|
|
58
|
+
"returnType"
|
|
59
|
+
]);
|
|
60
|
+
/** `node` without the type-only wrappers around it. */
|
|
61
|
+
const unwrapped = (node) => node !== void 0 && typeOnlyWrappers.has(node.type) ? unwrapped(nodeAt(node, "expression")) : node;
|
|
62
|
+
const isCall = (node) => node?.type === "CallExpression" || node?.type === "NewExpression";
|
|
63
|
+
/**
|
|
64
|
+
* Where a mark before `node` reaches: the outermost chain node starting where it does, as in
|
|
65
|
+
* `f(x).m()`, `g()(x)` or `f() as T`, or `undefined` when a call there is the mark's and walks it.
|
|
66
|
+
*/
|
|
67
|
+
const chainTop = (node) => {
|
|
68
|
+
const parent = fieldOf(node, "parent");
|
|
69
|
+
if (!isNode(parent) || parent.start !== node.start || !chainNodes.has(parent.type)) return node;
|
|
70
|
+
return isCall(parent) ? void 0 : chainTop(parent);
|
|
71
|
+
};
|
|
72
|
+
/** Values whose code a walk sees whole, so a callee that runs or iterates one runs only that code. */
|
|
73
|
+
const seenValues = /* @__PURE__ */ new Set([
|
|
74
|
+
"CallExpression",
|
|
75
|
+
"NewExpression",
|
|
76
|
+
"ArrayExpression",
|
|
77
|
+
"ObjectExpression",
|
|
78
|
+
"Literal",
|
|
79
|
+
"TemplateLiteral"
|
|
80
|
+
]);
|
|
81
|
+
/**
|
|
82
|
+
* Whether `node` is a spread that iterates its operand, in an array or a call's arguments. An
|
|
83
|
+
* object's spread reads its operand's fields, as a member read does.
|
|
84
|
+
*/
|
|
85
|
+
const isIterated = (node, parent) => node.type === "SpreadElement" && parent?.type !== "ObjectExpression";
|
|
86
|
+
/**
|
|
87
|
+
* Whether `node` is an object literal's field whose read runs code the walk skips: a getter or a
|
|
88
|
+
* setter, or a `__proto__` key, which sets the prototype a read or an iteration falls back to.
|
|
89
|
+
*/
|
|
90
|
+
const isHiddenField = (node, parent) => {
|
|
91
|
+
if (node.type !== "Property" || parent?.type !== "ObjectExpression") return false;
|
|
92
|
+
if (fieldOf(node, "kind") !== "init") return true;
|
|
93
|
+
const key = nodeAt(node, "key");
|
|
94
|
+
return fieldOf(node, "computed") !== true && key !== void 0 && (fieldOf(key, "name") === "__proto__" || fieldOf(key, "value") === "__proto__");
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Whether iterating or spreading `node` runs only code a walk sees. A name's iterator or getters
|
|
98
|
+
* aren't seen, and an object literal's computed key or accessor holds a function, such as a
|
|
99
|
+
* `*[Symbol.iterator]()`, whose body the walk skips.
|
|
100
|
+
*/
|
|
101
|
+
const isSeenWhole = (node) => {
|
|
102
|
+
const value = unwrapped(node);
|
|
103
|
+
if (value === void 0 || !seenValues.has(value.type)) return false;
|
|
104
|
+
if (value.type !== "ObjectExpression") return true;
|
|
105
|
+
return nodesAt(value, "properties").every((property) => property.type === "Property" && fieldOf(property, "computed") !== true && fieldOf(property, "kind") === "init");
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* The calls a top-level statement runs as its module loads, past type-only casts: each declared
|
|
109
|
+
* value's, and a class's heritage, exported or not.
|
|
110
|
+
*/
|
|
111
|
+
const topLevelCalls = (statement) => {
|
|
112
|
+
const declaration = (statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? nodeAt(statement, "declaration") : void 0) ?? statement;
|
|
113
|
+
return (declaration.type === "VariableDeclaration" ? nodesAt(declaration, "declarations").map((declarator) => nodeAt(declarator, "init")) : declaration.type === "ClassDeclaration" || declaration.type === "ClassExpression" ? [nodeAt(declaration, "superClass")] : []).map(unwrapped).filter(isCall);
|
|
114
|
+
};
|
|
115
|
+
/** The method `call` calls by name, as `check` in `s.check(x)`, if its callee is a plain member. */
|
|
116
|
+
const methodOf = (call) => {
|
|
117
|
+
const callee = unwrapped(nodeAt(call, "callee"));
|
|
118
|
+
return callee?.type === "MemberExpression" && fieldOf(callee, "computed") !== true ? nameOf(nodeAt(callee, "property")) : void 0;
|
|
119
|
+
};
|
|
120
|
+
/** Class members whose value runs per instance, as each is built, while a computed key runs once. */
|
|
121
|
+
const instanceFields = /* @__PURE__ */ new Set(["PropertyDefinition", "AccessorProperty"]);
|
|
122
|
+
/**
|
|
123
|
+
* Whether `node` runs only when something calls a function: it's inside one's parameters or body,
|
|
124
|
+
* or an instance field's value. A computed key and a static field run as their class is built.
|
|
125
|
+
*/
|
|
126
|
+
const isDeferred = (node) => {
|
|
127
|
+
const parent = fieldOf(node, "parent");
|
|
128
|
+
if (!isNode(parent)) return false;
|
|
129
|
+
return deferredNodes.has(parent.type) || parent.type === "FunctionDeclaration" || instanceFields.has(parent.type) && fieldOf(parent, "static") !== true && fieldOf(parent, "value") === node || isDeferred(parent);
|
|
130
|
+
};
|
|
131
|
+
/** The name an import specifier of these kinds binds, whatever it names. */
|
|
132
|
+
const importedNames = /* @__PURE__ */ new Map([["ImportNamespaceSpecifier", ""], ["ImportDefaultSpecifier", "default"]]);
|
|
133
|
+
/** The fields of `node` that hold code, each as its nodes. */
|
|
134
|
+
const childrenOf = (node) => Object.keys(node).flatMap((field) => {
|
|
135
|
+
if (skippedFields.has(field)) return [];
|
|
136
|
+
const value = fieldOf(node, field);
|
|
137
|
+
return isNode(value) ? [value] : nodesAt(node, field);
|
|
138
|
+
});
|
|
139
|
+
/** Every identifier's name in `node`, a pattern's included. */
|
|
140
|
+
const namesIn = (node) => {
|
|
141
|
+
const name = nameOf(node);
|
|
142
|
+
return name === void 0 ? childrenOf(node).flatMap(namesIn) : [name];
|
|
143
|
+
};
|
|
144
|
+
/** The field of each declaring node that holds the names it binds. */
|
|
145
|
+
const bindingFields = /* @__PURE__ */ new Map([
|
|
146
|
+
["VariableDeclarator", "id"],
|
|
147
|
+
["FunctionDeclaration", "id"],
|
|
148
|
+
["ClassDeclaration", "id"],
|
|
149
|
+
["ClassExpression", "id"],
|
|
150
|
+
["CatchClause", "param"],
|
|
151
|
+
["TSEnumDeclaration", "id"],
|
|
152
|
+
["TSModuleDeclaration", "id"],
|
|
153
|
+
["TSImportEqualsDeclaration", "id"]
|
|
154
|
+
]);
|
|
155
|
+
/**
|
|
156
|
+
* Every name `node` declares outside the functions in it, a block's and a pattern's included. One
|
|
157
|
+
* too many binds a name to this file, so the list matches it less, never more.
|
|
158
|
+
*/
|
|
159
|
+
const declaredIn = (node) => {
|
|
160
|
+
const field = bindingFields.get(node.type);
|
|
161
|
+
const binder = field === void 0 ? void 0 : nodeAt(node, field);
|
|
162
|
+
const own = binder === void 0 ? [] : namesIn(binder);
|
|
163
|
+
return node.type === "FunctionDeclaration" || deferredNodes.has(node.type) ? own : [...own, ...childrenOf(node).flatMap(declaredIn)];
|
|
164
|
+
};
|
|
165
|
+
/** The scope of `program`, at `file` from the root. Relative imports resolve to a path from the root. */
|
|
166
|
+
const scopeOf = (program, { file, root, callees }) => {
|
|
167
|
+
const imports = /* @__PURE__ */ new Map();
|
|
168
|
+
const declared = /* @__PURE__ */ new Set();
|
|
169
|
+
for (const statement of nodesAt(program, "body")) {
|
|
170
|
+
if (statement.type === "ImportDeclaration") {
|
|
171
|
+
const source = fieldOf(nodeAt(statement, "source") ?? statement, "value");
|
|
172
|
+
if (!Predicate.isString(source)) continue;
|
|
173
|
+
const from = source.startsWith(".") ? fromRoot(root, resolve(root, dirname(file), source)) : source;
|
|
174
|
+
for (const specifier of nodesAt(statement, "specifiers")) {
|
|
175
|
+
const local = nameOf(nodeAt(specifier, "local"));
|
|
176
|
+
const name = importedNames.get(specifier.type) ?? nameOf(nodeAt(specifier, "imported"));
|
|
177
|
+
if (local !== void 0 && name !== void 0) imports.set(local, {
|
|
178
|
+
from,
|
|
179
|
+
name
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
for (const name of declaredIn(statement)) declared.add(name);
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
imports,
|
|
188
|
+
declared,
|
|
189
|
+
file,
|
|
190
|
+
root,
|
|
191
|
+
callees,
|
|
192
|
+
parameters: /* @__PURE__ */ new Set()
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Where `callee`, an identifier or a chain of plain members on one or on an array literal, is
|
|
197
|
+
* bound, or `undefined` for any other callee.
|
|
198
|
+
*/
|
|
199
|
+
const bindingOf = (callee, scope) => {
|
|
200
|
+
const node = unwrapped(callee);
|
|
201
|
+
const name = nameOf(node);
|
|
202
|
+
if (name !== void 0) {
|
|
203
|
+
if (scope.parameters.has(name)) return {
|
|
204
|
+
from: `${scope.file}#parameters`,
|
|
205
|
+
name
|
|
206
|
+
};
|
|
207
|
+
return scope.imports.get(name) ?? (scope.declared.has(name) ? {
|
|
208
|
+
from: scope.file,
|
|
209
|
+
name
|
|
210
|
+
} : {
|
|
211
|
+
from: "globalThis",
|
|
212
|
+
name
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (node?.type !== "MemberExpression" || fieldOf(node, "computed") === true) return void 0;
|
|
216
|
+
const property = nameOf(nodeAt(node, "property"));
|
|
217
|
+
const object = unwrapped(nodeAt(node, "object"));
|
|
218
|
+
const base = property === void 0 ? void 0 : object?.type === "ArrayExpression" ? {
|
|
219
|
+
from: "globalThis",
|
|
220
|
+
name: "[]"
|
|
221
|
+
} : bindingOf(object, scope);
|
|
222
|
+
if (base === void 0) return void 0;
|
|
223
|
+
return {
|
|
224
|
+
from: base.from,
|
|
225
|
+
name: base.name === "" ? `${property}` : `${base.name}.${property}`
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
/** The list's entry for `binding`: `*` takes any name its module exports, and its `chain` members. */
|
|
229
|
+
const listed = (binding, { callees }) => callees.find((entry) => entry.from === binding.from && (entry.name === "*" ? binding.name !== "" && binding.name.split(".").slice(1).every((member) => entry.chain?.includes(member) === true) : entry.name === binding.name));
|
|
230
|
+
/**
|
|
231
|
+
* The list's entry that covers `call`: its own callee's; for a method on a call's result, that
|
|
232
|
+
* call's when the entry lets it chain; and for a call of a call's result, that call's when it's
|
|
233
|
+
* curried.
|
|
234
|
+
*/
|
|
235
|
+
const entryOf = (call, scope) => {
|
|
236
|
+
const callee = unwrapped(nodeAt(call, "callee"));
|
|
237
|
+
if (isCall(callee)) {
|
|
238
|
+
const inner = entryOf(callee, scope);
|
|
239
|
+
return inner?.curried === true ? inner : void 0;
|
|
240
|
+
}
|
|
241
|
+
const object = callee?.type === "MemberExpression" ? unwrapped(nodeAt(callee, "object")) : void 0;
|
|
242
|
+
if (callee !== void 0 && isCall(object)) {
|
|
243
|
+
const method = fieldOf(callee, "computed") === true ? void 0 : nameOf(nodeAt(callee, "property"));
|
|
244
|
+
const inner = entryOf(object, scope);
|
|
245
|
+
return method !== void 0 && inner?.chain?.includes(method) === true ? inner : void 0;
|
|
246
|
+
}
|
|
247
|
+
const binding = bindingOf(callee, scope);
|
|
248
|
+
return binding === void 0 ? void 0 : listed(binding, scope);
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* A `/* @__PURE__ *\/` mark on a top-level call, in the files a repo turns this on for, may cover
|
|
252
|
+
* only calls its `callees` list: the marked call, the methods it chains and every call in its
|
|
253
|
+
* arguments, outside the functions they pass, save those a `runsArguments` callee calls. A mark
|
|
254
|
+
* over anything else lets a bundler drop an effect that no test sees, since tests run source
|
|
255
|
+
* unbundled. Top-level `Effect.fn*` marks are `pure-effect-fn`'s.
|
|
256
|
+
*
|
|
257
|
+
* With `{ requireMarks: true }`, a top-level call with no mark that the same walk would accept, a
|
|
258
|
+
* declared value or a class's heritage, needs one, and `vp lint --fix` adds it. A call the walk
|
|
259
|
+
* refuses stays unmarked, with no comment: the list decides which calls are marked.
|
|
260
|
+
*/
|
|
261
|
+
const pureTopLevelRule = defineRule({
|
|
262
|
+
meta: {
|
|
263
|
+
fixable: "code",
|
|
264
|
+
schema: [{
|
|
265
|
+
type: "object",
|
|
266
|
+
properties: {
|
|
267
|
+
root: { type: "string" },
|
|
268
|
+
callees: {
|
|
269
|
+
type: "array",
|
|
270
|
+
items: { type: "object" }
|
|
271
|
+
},
|
|
272
|
+
requireMarks: { type: "boolean" }
|
|
273
|
+
},
|
|
274
|
+
required: ["root", "callees"],
|
|
275
|
+
additionalProperties: false
|
|
276
|
+
}],
|
|
277
|
+
messages: {
|
|
278
|
+
missingPure: "This top-level call only builds a value, as the rule's callees list them, so it needs a /* @__PURE__ */ mark for a bundler to drop it when unused. `vp lint --fix` adds it.",
|
|
279
|
+
unlistedPure: "This /* @__PURE__ */ mark covers `{{callee}}`, which the rule's callees don't list. Add it there with why its call only builds a value, or drop the mark."
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
create(context) {
|
|
283
|
+
const { root, callees, requireMarks = false } = optionsOf(context.options);
|
|
284
|
+
const file = fromRoot(root, resolve(context.filename));
|
|
285
|
+
let scope = {
|
|
286
|
+
imports: /* @__PURE__ */ new Map(),
|
|
287
|
+
declared: /* @__PURE__ */ new Set(),
|
|
288
|
+
file,
|
|
289
|
+
root,
|
|
290
|
+
callees,
|
|
291
|
+
parameters: /* @__PURE__ */ new Set()
|
|
292
|
+
};
|
|
293
|
+
let bindings = {
|
|
294
|
+
namespaces: /* @__PURE__ */ new Set(),
|
|
295
|
+
direct: /* @__PURE__ */ new Set()
|
|
296
|
+
};
|
|
297
|
+
const reported = /* @__PURE__ */ new Set();
|
|
298
|
+
const report = (node) => {
|
|
299
|
+
if (reported.has(node.start)) return;
|
|
300
|
+
reported.add(node.start);
|
|
301
|
+
const shown = (isCall(node) ? nodeAt(node, "callee") : void 0) ?? node;
|
|
302
|
+
const text = context.sourceCode.text.slice(shown.start, shown.end).split("\n")[0] ?? "";
|
|
303
|
+
context.report({
|
|
304
|
+
node: { range: [node.start, node.end] },
|
|
305
|
+
messageId: "unlistedPure",
|
|
306
|
+
data: { callee: text.length > 60 ? `${text.slice(0, 57)}...` : text }
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
/**
|
|
310
|
+
* Reports each call under `node` that the list doesn't cover, outside the functions it passes,
|
|
311
|
+
* save those a `runsArguments` callee calls, whose bodies it walks with their parameters bound.
|
|
312
|
+
*/
|
|
313
|
+
const walk = (node, parent, at, refuse) => {
|
|
314
|
+
if (deferredNodes.has(node.type)) return;
|
|
315
|
+
if (isOutermostEffectFnCall(node, parent, bindings)) return;
|
|
316
|
+
const entry = isCall(node) ? entryOf(node, at) : void 0;
|
|
317
|
+
if (effectNodes.has(node.type)) refuse(node);
|
|
318
|
+
else if (node.type === "UnaryExpression" && fieldOf(node, "operator") === "delete") refuse(node);
|
|
319
|
+
else if (isCall(node) && entry === void 0) refuse(node);
|
|
320
|
+
else if (isIterated(node, parent) && !isSeenWhole(nodeAt(node, "argument"))) refuse(node);
|
|
321
|
+
else if (isHiddenField(node, parent)) refuse(node);
|
|
322
|
+
const method = methodOf(node);
|
|
323
|
+
const runsAll = entry?.runsArguments === true || method !== void 0 && entry?.chain?.includes(method) === true;
|
|
324
|
+
const runs = new Set(runsAll ? nodesAt(node, "arguments") : []);
|
|
325
|
+
for (const child of childrenOf(node)) if (runs.has(child)) walkRun(child, node, at, refuse);
|
|
326
|
+
else walk(child, node, at, refuse);
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* Walks `argument`, which a listed callee runs or iterates: an arrow's parameters and
|
|
330
|
+
* expression, or a value whose code is all here. Any other, such as a name, is refused.
|
|
331
|
+
*/
|
|
332
|
+
const walkRun = (argument, call, at, refuse) => {
|
|
333
|
+
if (!deferredNodes.has(argument.type)) {
|
|
334
|
+
if (isSeenWhole(argument)) walk(argument, call, at, refuse);
|
|
335
|
+
else refuse(argument);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const named = nodesAt(argument, "params").map(nameOf);
|
|
339
|
+
const body = nodeAt(argument, "body");
|
|
340
|
+
if (named.includes(void 0) || body === void 0 || body.type === "BlockStatement") {
|
|
341
|
+
refuse(argument);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const parameters = /* @__PURE__ */ new Set([...at.parameters, ...named.filter(Predicate.isString)]);
|
|
345
|
+
walk(body, argument, {
|
|
346
|
+
...at,
|
|
347
|
+
parameters
|
|
348
|
+
}, refuse);
|
|
349
|
+
};
|
|
350
|
+
const isMarked = (node) => node !== null && context.sourceCode.getCommentsBefore(node).some((comment) => comment.type === "Block" && pureAnnotation.test(comment.value));
|
|
351
|
+
/** Whether the walk from `top` refuses nothing, so a mark there would pass. */
|
|
352
|
+
const passes = (top) => {
|
|
353
|
+
let refused = false;
|
|
354
|
+
const parent = fieldOf(top, "parent");
|
|
355
|
+
walk(top, isNode(parent) ? parent : void 0, scope, () => {
|
|
356
|
+
refused = true;
|
|
357
|
+
});
|
|
358
|
+
return !refused;
|
|
359
|
+
};
|
|
360
|
+
/** Asks for a mark on `call`, a top-level call, when it has none and the walk would pass one. */
|
|
361
|
+
const requireMark = (call) => {
|
|
362
|
+
if (isMarked(context.sourceCode.getNodeByRangeIndex(call.start))) return;
|
|
363
|
+
const parent = fieldOf(call, "parent");
|
|
364
|
+
if (isOutermostEffectFnCall(call, isNode(parent) ? parent : void 0, bindings)) return;
|
|
365
|
+
if (!passes(chainTop(call) ?? call)) return;
|
|
366
|
+
context.report({
|
|
367
|
+
node: { range: [call.start, call.end] },
|
|
368
|
+
messageId: "missingPure",
|
|
369
|
+
fix: (fixer) => fixer.insertTextBeforeRange([call.start, call.start], "/* @__PURE__ */ ")
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
const visitCall = (node) => {
|
|
373
|
+
if (isDeferred(node)) return;
|
|
374
|
+
if (!isMarked(node)) return;
|
|
375
|
+
const top = chainTop(node);
|
|
376
|
+
if (top === void 0) return;
|
|
377
|
+
const parent = fieldOf(top, "parent");
|
|
378
|
+
walk(top, isNode(parent) ? parent : void 0, scope, report);
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
Program(program) {
|
|
382
|
+
const tree = program;
|
|
383
|
+
scope = scopeOf(tree, scope);
|
|
384
|
+
bindings = collectEffectFnBindings(nodesAt(tree, "body"));
|
|
385
|
+
if (!requireMarks) return;
|
|
386
|
+
for (const statement of nodesAt(tree, "body")) topLevelCalls(statement).forEach(requireMark);
|
|
387
|
+
},
|
|
388
|
+
CallExpression: visitCall,
|
|
389
|
+
NewExpression: visitCall
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
export { pureTopLevelRule };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** A package's `exports`: one target, or targets by subpath or by condition. */
|
|
3
|
+
declare const Exports: Schema.Union<readonly [Schema.String, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
|
|
4
|
+
/** A package's `exports`, as discovery reads it. */
|
|
5
|
+
type Exports = typeof Exports.Type;
|
|
1
6
|
/** Where a repo's packages are: its root, and the folders that hold each kind, as globs from it. */
|
|
2
7
|
export interface Workspace {
|
|
3
8
|
/** The repo's root, as an absolute path. */
|
|
@@ -21,9 +26,33 @@ export interface WorkspacePackage {
|
|
|
21
26
|
readonly entries: ReadonlyArray<string>;
|
|
22
27
|
/** Every package it depends on, by any kind of dependency. */
|
|
23
28
|
readonly dependencies: ReadonlySet<string>;
|
|
29
|
+
/** Whether its manifest says it's private, so it publishes nothing. */
|
|
30
|
+
readonly private: boolean;
|
|
31
|
+
/** Its `exports` as its manifest writes them, for the file behind each entry point. */
|
|
32
|
+
readonly exports: Exports | undefined;
|
|
33
|
+
/** The entry points it publishes: its `publishConfig.exports`' keys, or its own. */
|
|
34
|
+
readonly published: ReadonlyArray<string>;
|
|
24
35
|
}
|
|
25
36
|
/**
|
|
26
37
|
* The workspace's packages and apps, by name: each folder a glob matches that holds a
|
|
27
38
|
* `package.json`. Two folders with one name throw, since an import can't tell them apart.
|
|
28
39
|
*/
|
|
29
|
-
export declare const workspacePackages: ({ root, packages, apps }: Workspace) => ReadonlyMap<string, WorkspacePackage>;
|
|
40
|
+
export declare const workspacePackages: ({ root, packages, apps }: Workspace) => ReadonlyMap<string, WorkspacePackage>;
|
|
41
|
+
/**
|
|
42
|
+
* The file from the package's folder that `entry`, such as `.` or `./cast`, maps to: its target
|
|
43
|
+
* when that's a path, or its target under `condition`, such as `@ref0/source`, when it maps
|
|
44
|
+
* conditions. `strict` takes only the condition's.
|
|
45
|
+
*/
|
|
46
|
+
export declare const entryFile: ({ exports }: WorkspacePackage, entry: string, condition: string, strict?: boolean) => string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* The source file of each entry point a public package publishes, from the root, such as
|
|
49
|
+
* `packages/sdk/src/index.ts`: each entry it publishes, through that entry's file under
|
|
50
|
+
* `condition`. An entry with no file under it is left out.
|
|
51
|
+
*/
|
|
52
|
+
export declare const publishedEntries: (packages: ReadonlyMap<string, WorkspacePackage>, condition: string) => ReadonlyArray<string>;
|
|
53
|
+
/**
|
|
54
|
+
* Every workspace package `name` depends on, directly or through another, by any kind of
|
|
55
|
+
* dependency, by name.
|
|
56
|
+
*/
|
|
57
|
+
export declare const dependedOn: (name: string, packages: ReadonlyMap<string, WorkspacePackage>) => ReadonlyMap<string, WorkspacePackage>;
|
|
58
|
+
export { Exports };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { globSync, readFileSync } from "node:fs";
|
|
2
2
|
import { join, posix, relative, sep } from "node:path";
|
|
3
|
-
import { Schema } from "effect";
|
|
3
|
+
import { Predicate, Schema } from "effect";
|
|
4
4
|
// src/lint/generic/workspace-packages.ts
|
|
5
5
|
const dependencyKinds = [
|
|
6
6
|
"dependencies",
|
|
@@ -12,12 +12,14 @@ const Dependencies = Schema.optionalKey(Schema.Record(Schema.String, Schema.Stri
|
|
|
12
12
|
/** A package's `exports`: one target, or targets by subpath or by condition. */
|
|
13
13
|
const Exports = Schema.Union([Schema.String, Schema.Record(Schema.String, Schema.Json)]);
|
|
14
14
|
/**
|
|
15
|
-
* What discovery reads of a workspace package's `package.json`: its name,
|
|
16
|
-
* are its entry points, and what it depends on.
|
|
15
|
+
* What discovery reads of a workspace package's `package.json`: its name, whether it's private,
|
|
16
|
+
* its `exports`, whose keys are its entry points, those it publishes, and what it depends on.
|
|
17
17
|
*/
|
|
18
18
|
const decodeManifest = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Struct({
|
|
19
19
|
name: Schema.String,
|
|
20
|
+
private: Schema.optionalKey(Schema.Boolean),
|
|
20
21
|
exports: Schema.optionalKey(Exports),
|
|
22
|
+
publishConfig: Schema.optionalKey(Schema.Struct({ exports: Schema.optionalKey(Exports) })),
|
|
21
23
|
dependencies: Dependencies,
|
|
22
24
|
devDependencies: Dependencies,
|
|
23
25
|
peerDependencies: Dependencies,
|
|
@@ -67,7 +69,10 @@ const workspacePackages = ({ root, packages, apps = [] }) => {
|
|
|
67
69
|
directory,
|
|
68
70
|
app: twin?.app === true || app,
|
|
69
71
|
entries: entriesOf(manifest.exports),
|
|
70
|
-
dependencies: new Set(dependencyKinds.flatMap((kind) => Object.keys(manifest[kind] ?? {})))
|
|
72
|
+
dependencies: new Set(dependencyKinds.flatMap((kind) => Object.keys(manifest[kind] ?? {}))),
|
|
73
|
+
private: manifest.private === true,
|
|
74
|
+
exports: manifest.exports,
|
|
75
|
+
published: entriesOf(manifest.publishConfig?.exports ?? manifest.exports)
|
|
71
76
|
});
|
|
72
77
|
}
|
|
73
78
|
read.set(key, found);
|
|
@@ -75,4 +80,40 @@ const workspacePackages = ({ root, packages, apps = [] }) => {
|
|
|
75
80
|
};
|
|
76
81
|
/** The package that holds a path from the root: the deepest whose folder it's in. */
|
|
77
82
|
const ownerOf = (path, packages) => [...packages.values()].filter(({ directory }) => path.startsWith(`${directory}/`)).toSorted((a, b) => b.directory.length - a.directory.length)[0];
|
|
78
|
-
|
|
83
|
+
/**
|
|
84
|
+
* The file from the package's folder that `entry`, such as `.` or `./cast`, maps to: its target
|
|
85
|
+
* when that's a path, or its target under `condition`, such as `@ref0/source`, when it maps
|
|
86
|
+
* conditions. `strict` takes only the condition's.
|
|
87
|
+
*/
|
|
88
|
+
const entryFile = ({ exports }, entry, condition, strict = false) => {
|
|
89
|
+
if (exports === void 0 || isString(exports)) return entry === "." && !strict ? exports : void 0;
|
|
90
|
+
const target = Object.keys(exports).every((key) => key.startsWith(".")) ? exports[entry] : entry === "." ? exports : void 0;
|
|
91
|
+
if (isString(target)) return strict ? void 0 : target;
|
|
92
|
+
const file = Predicate.isObject(target) && !Array.isArray(target) ? target[condition] : void 0;
|
|
93
|
+
return isString(file) ? file : void 0;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The source file of each entry point a public package publishes, from the root, such as
|
|
97
|
+
* `packages/sdk/src/index.ts`: each entry it publishes, through that entry's file under
|
|
98
|
+
* `condition`. An entry with no file under it is left out.
|
|
99
|
+
*/
|
|
100
|
+
const publishedEntries = (packages, condition) => [...packages.values()].flatMap((workspacePackage) => workspacePackage.private ? [] : workspacePackage.published.flatMap((entry) => {
|
|
101
|
+
const file = entryFile(workspacePackage, entry, condition, true);
|
|
102
|
+
return file === void 0 ? [] : [posix.join(workspacePackage.directory, file.replace(/^\.\//, ""))];
|
|
103
|
+
}));
|
|
104
|
+
/**
|
|
105
|
+
* Every workspace package `name` depends on, directly or through another, by any kind of
|
|
106
|
+
* dependency, by name.
|
|
107
|
+
*/
|
|
108
|
+
const dependedOn = (name, packages) => {
|
|
109
|
+
const found = /* @__PURE__ */ new Map();
|
|
110
|
+
const waiting = [name];
|
|
111
|
+
for (let next = waiting.pop(); next !== void 0; next = waiting.pop()) for (const dependency of packages.get(next)?.dependencies ?? []) {
|
|
112
|
+
const workspacePackage = packages.get(dependency);
|
|
113
|
+
if (workspacePackage === void 0 || dependency === name || found.has(dependency)) continue;
|
|
114
|
+
found.set(dependency, workspacePackage);
|
|
115
|
+
waiting.push(dependency);
|
|
116
|
+
}
|
|
117
|
+
return found;
|
|
118
|
+
};
|
|
119
|
+
export { dependedOn, entryFile, fromRoot, ownerOf, publishedEntries, workspacePackages };
|
package/dist/lint/graph.d.mts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { BuildCacheInputsOptions } from "./generic/build-cache-inputs.mjs";
|
|
2
|
+
import { EffectGenWrappersOptions } from "./generic/effect-gen-wrappers.mjs";
|
|
1
3
|
import { FolderImportsOptions, FolderLayers, SourceLayers } from "./generic/folder-layers.mjs";
|
|
2
4
|
import { Edge, How, PackageGraph, PackageGraphOptions, graphProblems } from "./generic/package-graph.mjs";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
+
import { PublicDocsOptions } from "./generic/public-docs.mjs";
|
|
6
|
+
import { PureCallee, PureTopLevelOptions } from "./generic/pure-top-level.mjs";
|
|
7
|
+
import { Exports, Workspace, WorkspacePackage, dependedOn, entryFile, publishedEntries, workspacePackages } from "./generic/workspace-packages.mjs";
|
|
8
|
+
export { type BuildCacheInputsOptions, type Edge, type EffectGenWrappersOptions, type Exports, type FolderImportsOptions, type FolderLayers, type How, type PackageGraph, type PackageGraphOptions, type PublicDocsOptions, type PureCallee, type PureTopLevelOptions, type SourceLayers, type Workspace, type WorkspacePackage, dependedOn, entryFile, graphProblems, publishedEntries, workspacePackages };
|
package/dist/lint/graph.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { graphProblems } from "./generic/package-graph.mjs";
|
|
2
|
-
import { workspacePackages } from "./generic/workspace-packages.mjs";
|
|
3
|
-
export { graphProblems, workspacePackages };
|
|
2
|
+
import { dependedOn, entryFile, publishedEntries, workspacePackages } from "./generic/workspace-packages.mjs";
|
|
3
|
+
export { dependedOn, entryFile, graphProblems, publishedEntries, workspacePackages };
|