@autotelic/joggle 0.0.0-stage → 0.1.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/JOGGLE.md +634 -0
- package/LICENSE +21 -0
- package/README.md +88 -2
- package/bin/joggle.js +6 -0
- package/dist/answer-store.js +305 -0
- package/dist/architecture.js +190 -0
- package/dist/ask.js +184 -0
- package/dist/astro.js +75 -0
- package/dist/atoms.js +76 -0
- package/dist/bundles.js +156 -0
- package/dist/calibration.js +60 -0
- package/dist/candidates.js +561 -0
- package/dist/canonical.js +24 -0
- package/dist/cascade.js +98 -0
- package/dist/check.js +854 -0
- package/dist/cluster.js +69 -0
- package/dist/config.js +195 -0
- package/dist/content-mappers.js +83 -0
- package/dist/decision.js +203 -0
- package/dist/fingerprint.js +61 -0
- package/dist/git.js +127 -0
- package/dist/gitignore.js +65 -0
- package/dist/glob.js +51 -0
- package/dist/imports.js +153 -0
- package/dist/jsonc.js +95 -0
- package/dist/main.js +630 -0
- package/dist/manifest.js +42 -0
- package/dist/meta.js +170 -0
- package/dist/moves.js +47 -0
- package/dist/operation.js +133 -0
- package/dist/parsecache.js +264 -0
- package/dist/plans.js +425 -0
- package/dist/plugin.js +71 -0
- package/dist/plugins.js +136 -0
- package/dist/policy.js +676 -0
- package/dist/presets/composition.js +32 -0
- package/dist/report.js +474 -0
- package/dist/reporting.js +132 -0
- package/dist/result.js +28 -0
- package/dist/roles.js +303 -0
- package/dist/rule.js +194 -0
- package/dist/rules/append-only-honoured.js +190 -0
- package/dist/rules/bundle-conformance.js +245 -0
- package/dist/rules/call-pattern.js +209 -0
- package/dist/rules/cluster-verdict.js +700 -0
- package/dist/rules/compose-types.js +250 -0
- package/dist/rules/data-error-as-outage.js +482 -0
- package/dist/rules/declared-staleness.js +242 -0
- package/dist/rules/dependency-fit.js +297 -0
- package/dist/rules/deviation-has-a-shape.js +225 -0
- package/dist/rules/doc-matches-code.js +191 -0
- package/dist/rules/duplicate-call-run.js +240 -0
- package/dist/rules/duplicate-implementation.js +89 -0
- package/dist/rules/duplicate-meaning.js +130 -0
- package/dist/rules/fact-or-derivation.js +202 -0
- package/dist/rules/field-type-drift.js +373 -0
- package/dist/rules/generic-carries-a-caller.js +198 -0
- package/dist/rules/hoist-to-domain.js +288 -0
- package/dist/rules/import-architecture.js +393 -0
- package/dist/rules/index.js +105 -0
- package/dist/rules/inferred-over-recorded.js +214 -0
- package/dist/rules/intent-drift.js +286 -0
- package/dist/rules/invariant-has-a-reconciler.js +190 -0
- package/dist/rules/language-drift.js +234 -0
- package/dist/rules/meaning-switched-by-flag.js +189 -0
- package/dist/rules/module-direction.js +231 -0
- package/dist/rules/name-as-address.js +216 -0
- package/dist/rules/name-the-primitive.js +203 -0
- package/dist/rules/naming-drift.js +218 -0
- package/dist/rules/nullability-drift.js +220 -0
- package/dist/rules/object-shape.js +280 -0
- package/dist/rules/one-concept-one-type.js +243 -0
- package/dist/rules/page-needs-composition.js +227 -0
- package/dist/rules/reimplemented-primitive.js +283 -0
- package/dist/rules/reversal.js +264 -0
- package/dist/rules/rule-judgment.js +252 -0
- package/dist/rules/schema-excludes-domain-value.js +190 -0
- package/dist/rules/shallow-module.js +204 -0
- package/dist/rules/single-path.js +237 -0
- package/dist/rules/structural-collapse.js +198 -0
- package/dist/rules/temporal-coupling.js +205 -0
- package/dist/rules/types-over-logic.js +198 -0
- package/dist/rules/unaccounted-drop.js +190 -0
- package/dist/rules/unbounded-default-read.js +227 -0
- package/dist/run-cache.js +132 -0
- package/dist/schema.js +285 -0
- package/dist/similarity.js +119 -0
- package/dist/source.js +30 -0
- package/dist/sql.js +34 -0
- package/dist/stages.js +39 -0
- package/dist/state.js +65 -0
- package/dist/testing.js +131 -0
- package/dist/tsconfig-scope.js +32 -0
- package/dist/tsgo.js +127 -0
- package/dist/typefacts.js +455 -0
- package/dist/typetrace.js +283 -0
- package/dist/verdict.js +65 -0
- package/dist/vocabulary.js +365 -0
- package/dist/workspace.js +1963 -0
- package/extensions/joggle.ts +454 -0
- package/package.json +85 -3
|
@@ -0,0 +1,1963 @@
|
|
|
1
|
+
import { Clock, Effect, FileSystem, Order, Path, Predicate, Schema } from "effect";
|
|
2
|
+
import { parseSync } from "oxc-parser";
|
|
3
|
+
import { policy } from "./policy.js";
|
|
4
|
+
import { buildImportGraph, importsIn, } from "./imports.js";
|
|
5
|
+
import { isIgnored, orderRules, rulesAt } from "./gitignore.js";
|
|
6
|
+
import { shortHash } from "./state.js";
|
|
7
|
+
import { blockingErrors, componentName, isAstro, parseInputOf } from "./astro.js";
|
|
8
|
+
import { tsconfigScope } from "./tsconfig-scope.js";
|
|
9
|
+
import { looksLikeSource } from "./source.js";
|
|
10
|
+
import { manifestAt } from "./manifest.js";
|
|
11
|
+
import { shinglesOf } from "./similarity.js";
|
|
12
|
+
import { WorkspaceError } from "./schema.js";
|
|
13
|
+
import { emptyTypeIndex } from "./typetrace.js";
|
|
14
|
+
/* -------------------------------------------------------------------------- */
|
|
15
|
+
/* Vocabulary */
|
|
16
|
+
/* -------------------------------------------------------------------------- */
|
|
17
|
+
/** What a declaration is. A Schema, so the cache decoder can be exact about it. */
|
|
18
|
+
export const UnitKind = Schema.Literals(["function", "interface", "type", "const", "enum", "class"]);
|
|
19
|
+
/** One object literal: what it holds, and where it is. */
|
|
20
|
+
export const ObjectSite = Schema.Struct({
|
|
21
|
+
keys: Schema.Array(Schema.String),
|
|
22
|
+
start: Schema.Number,
|
|
23
|
+
/**
|
|
24
|
+
* True when the literal IS a type declaration, such as the field object of a
|
|
25
|
+
* `Schema.Struct`. A declaration is not a shape that needs a name; it is the
|
|
26
|
+
* name.
|
|
27
|
+
*/
|
|
28
|
+
declared: Schema.Boolean,
|
|
29
|
+
/**
|
|
30
|
+
* The declaration's non-optional keys, and empty for a literal that is not a
|
|
31
|
+
* declaration. A use of the type may omit its optional keys, so this is what
|
|
32
|
+
* tells a use from a shape that merely shares some keys.
|
|
33
|
+
*/
|
|
34
|
+
required: Schema.Array(Schema.String),
|
|
35
|
+
/**
|
|
36
|
+
* The keys whose value admits `null`, such as `Schema.NullOr(...)`. Nullability
|
|
37
|
+
* is a value, not strictness: a schema says whether the column can hold one.
|
|
38
|
+
*/
|
|
39
|
+
nullable: Schema.Array(Schema.String),
|
|
40
|
+
/**
|
|
41
|
+
* A key's mapping to the column it reads, from
|
|
42
|
+
* `.annotate({ sourceColumn: 'table.column' })`.
|
|
43
|
+
*
|
|
44
|
+
* The mapping is explicit because it is not derivable: `PayrollCrewRowSchema`
|
|
45
|
+
* reads `payroll_crew.shakti_user_id` into a field called `person_id`, and no
|
|
46
|
+
* name convention turns one into the other. A repository opts in by annotating;
|
|
47
|
+
* the rule then compares the two nullabilities.
|
|
48
|
+
*/
|
|
49
|
+
sources: Schema.Record(Schema.String, Schema.String),
|
|
50
|
+
/**
|
|
51
|
+
* Each field's `Schema` constructor chain, outermost first: `["optionalKey",
|
|
52
|
+
* "Finite"]`. A FACT about the wire schema, not about the repository: `Finite`
|
|
53
|
+
* excludes `Infinity` and `NaN`, `NullOr` admits a null, `Int` excludes a
|
|
54
|
+
* fraction. It is what a value produced on the other side has to satisfy.
|
|
55
|
+
*/
|
|
56
|
+
schemas: Schema.Record(Schema.String, Schema.Array(Schema.String)),
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* One column a migration creates or alters: its table, its name, whether it
|
|
60
|
+
* admits null.
|
|
61
|
+
*
|
|
62
|
+
* A migration is a specification, not an opinion, so this is a fact the checker
|
|
63
|
+
* reads from the source rather than a judgement. Knex defaults a column to
|
|
64
|
+
* nullable; `.notNullable()` is what makes it required. `.onDelete('SET NULL')`
|
|
65
|
+
* is deliberately NOT read as nullability: the column can still be nullable or
|
|
66
|
+
* not, and the action is about the parent row.
|
|
67
|
+
*/
|
|
68
|
+
export const ColumnFact = Schema.Struct({
|
|
69
|
+
table: Schema.String,
|
|
70
|
+
column: Schema.String,
|
|
71
|
+
nullable: Schema.Boolean,
|
|
72
|
+
/** The offset of the `table.<type>('<column>')` call, for the line. */
|
|
73
|
+
start: Schema.Number,
|
|
74
|
+
});
|
|
75
|
+
/** One call, where it is, what it names, and the shape of its arguments. */
|
|
76
|
+
export const CallSite = Schema.Struct({
|
|
77
|
+
/** Callee name, dotted for member calls: `createContext`, `React.useState`. */
|
|
78
|
+
name: Schema.String,
|
|
79
|
+
start: Schema.Number,
|
|
80
|
+
end: Schema.Number,
|
|
81
|
+
/**
|
|
82
|
+
* How many arguments the call passes. A call whose default path passes none is
|
|
83
|
+
* the shape a data-access question needs.
|
|
84
|
+
*/
|
|
85
|
+
argumentCount: Schema.Number,
|
|
86
|
+
/**
|
|
87
|
+
* The keys of the call's one object-literal argument, when it has exactly one.
|
|
88
|
+
*
|
|
89
|
+
* A FACT about the call: which filters it sets, not which it ought to. Which of
|
|
90
|
+
* the missing ones would bound a read is the question.
|
|
91
|
+
*/
|
|
92
|
+
argumentKeys: Schema.Array(Schema.String),
|
|
93
|
+
});
|
|
94
|
+
/**
|
|
95
|
+
* A place a string is built from other values: a template literal, or a `+`
|
|
96
|
+
* with a string literal on one side.
|
|
97
|
+
*
|
|
98
|
+
* Structural, not semantic. It says a string is constructed here and which
|
|
99
|
+
* references it interpolates (`$${row.amount}`, `total + " km"`); it does NOT
|
|
100
|
+
* say the string is money, a date or a percent. A rule that needs to know what
|
|
101
|
+
* the string means asks, instead of matching the text for known shapes.
|
|
102
|
+
*/
|
|
103
|
+
export const StringSite = Schema.Struct({
|
|
104
|
+
start: Schema.Number,
|
|
105
|
+
end: Schema.Number,
|
|
106
|
+
/** The references interpolated into it, dotted: `row.amount`, `total`. */
|
|
107
|
+
refs: Schema.Array(Schema.String),
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* An `if` statement, and whether its branch leaves the function.
|
|
111
|
+
*
|
|
112
|
+
* The guard's SHAPE is syntax -- a test, the references it names, whether the
|
|
113
|
+
* branch returns or throws -- so it belongs here rather than in a rule matching
|
|
114
|
+
* `if (!x)` against the source.
|
|
115
|
+
*/
|
|
116
|
+
export const GuardSite = Schema.Struct({
|
|
117
|
+
start: Schema.Number,
|
|
118
|
+
end: Schema.Number,
|
|
119
|
+
/** The references the test names, dotted. */
|
|
120
|
+
refs: Schema.Array(Schema.String),
|
|
121
|
+
/** True when the consequent returns or throws: a guard clause. */
|
|
122
|
+
exits: Schema.Boolean,
|
|
123
|
+
});
|
|
124
|
+
/**
|
|
125
|
+
* A `continue` in a loop: a place a unit skips an item.
|
|
126
|
+
*
|
|
127
|
+
* Structural, not semantic. It says the loop moved on without this item; it does
|
|
128
|
+
* NOT say the skip is a data loss the caller must know about. That is a question.
|
|
129
|
+
*/
|
|
130
|
+
export const SkipSite = Schema.Struct({
|
|
131
|
+
start: Schema.Number,
|
|
132
|
+
end: Schema.Number,
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* A string literal, and where it is.
|
|
136
|
+
*
|
|
137
|
+
* Structural: it says a piece of text is written here. It does NOT say the text
|
|
138
|
+
* is a label, a route name or a message. A rule that needs to know asks.
|
|
139
|
+
*/
|
|
140
|
+
export const LiteralSite = Schema.Struct({
|
|
141
|
+
value: Schema.String,
|
|
142
|
+
start: Schema.Number,
|
|
143
|
+
end: Schema.Number,
|
|
144
|
+
});
|
|
145
|
+
/** A `return`, where it is. The expression it returns is what a type question asks about. */
|
|
146
|
+
export const ReturnSite = Schema.Struct({
|
|
147
|
+
start: Schema.Number,
|
|
148
|
+
end: Schema.Number,
|
|
149
|
+
});
|
|
150
|
+
/** A comparison, its operator and span. A language construct, not a repository idiom. */
|
|
151
|
+
export const ComparisonSite = Schema.Struct({
|
|
152
|
+
start: Schema.Number,
|
|
153
|
+
end: Schema.Number,
|
|
154
|
+
operator: Schema.String,
|
|
155
|
+
});
|
|
156
|
+
/*
|
|
157
|
+
* Structure a JSX codebase reveals through names rather than shapes.
|
|
158
|
+
*
|
|
159
|
+
* A composition pattern is a convention about how components are organised --
|
|
160
|
+
* a context, a provider, blocks, and a dot-notation export -- and every part of
|
|
161
|
+
* that convention is spelled somewhere in the source. These three lists are
|
|
162
|
+
* enough to check it without a renderer, a bundler or a runtime.
|
|
163
|
+
*
|
|
164
|
+
* Declared as a Schema because it is written to the parse cache. Deriving the
|
|
165
|
+
* type from the decoder rather than declaring it beside one keeps a single source
|
|
166
|
+
* of truth: the cache file is external -- it can be truncated or edited by hand --
|
|
167
|
+
* so it is worth decoding, and the type follows from what the decoder accepts.
|
|
168
|
+
*/
|
|
169
|
+
export const StructureFacts = Schema.Struct({
|
|
170
|
+
/**
|
|
171
|
+
* Every call site in the file, in order.
|
|
172
|
+
*
|
|
173
|
+
* Ordered and not deduplicated, because both properties are load-bearing: a
|
|
174
|
+
* count needs multiplicity and a PATTERN is a sequence. The span is what lets a
|
|
175
|
+
* call be attributed to the declaration that makes it.
|
|
176
|
+
*/
|
|
177
|
+
callSites: Schema.Array(CallSite),
|
|
178
|
+
/** Element names as written, dotted for member JSX: `Button`, `Counter.Provider`. */
|
|
179
|
+
jsx: Schema.Array(Schema.String),
|
|
180
|
+
/** Every object literal: its key names, and where it starts. */
|
|
181
|
+
objects: Schema.Array(ObjectSite),
|
|
182
|
+
/** Columns a migration creates or alters, with their nullability. */
|
|
183
|
+
columns: Schema.Array(ColumnFact),
|
|
184
|
+
/** Every place a string is built from other values. */
|
|
185
|
+
stringSites: Schema.Array(StringSite),
|
|
186
|
+
/** Every `if` statement, with the references its test names. */
|
|
187
|
+
guards: Schema.Array(GuardSite),
|
|
188
|
+
/** Every `continue`: a place a loop skips an item. */
|
|
189
|
+
skips: Schema.Array(SkipSite),
|
|
190
|
+
/** Every string literal, with its value and span. */
|
|
191
|
+
literals: Schema.Array(LiteralSite),
|
|
192
|
+
/**
|
|
193
|
+
* Functions whose every parameter is optional or defaulted, by name.
|
|
194
|
+
*
|
|
195
|
+
* A list rather than a boolean on each unit: a fourth flag on `Unit` was a
|
|
196
|
+
* smell (the linter's `no-boolean-field-signals`), and this is a fact about the
|
|
197
|
+
* file, not a property of the declaration a rule reads.
|
|
198
|
+
*/
|
|
199
|
+
allOptionalFunctions: Schema.Array(Schema.String),
|
|
200
|
+
/** Every `return`, with its span. */
|
|
201
|
+
returns: Schema.Array(ReturnSite),
|
|
202
|
+
/** Every comparison (`===`, `>`, `in`, ...), a language construct. */
|
|
203
|
+
comparisons: Schema.Array(ComparisonSite),
|
|
204
|
+
});
|
|
205
|
+
/* -------------------------------------------------------------------------- */
|
|
206
|
+
/* Source helpers */
|
|
207
|
+
/* -------------------------------------------------------------------------- */
|
|
208
|
+
/** True for a plain object: the narrowing every raw JSON read begins with. */
|
|
209
|
+
export const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
210
|
+
/**
|
|
211
|
+
* Node kinds that name a *type*. In a type position the identifier is the whole
|
|
212
|
+
* meaning: `z.infer<typeof CompanyTypeSchema>` and `z.infer<typeof schema>` are
|
|
213
|
+
* different types, and `FastifyRequest<{ Params: { id: string } }>` is a
|
|
214
|
+
* different endpoint in every route that declares one. Erasing these made the
|
|
215
|
+
* shape matcher claim those pairs were identical, which cost 308 of 525
|
|
216
|
+
* verification calls on one codebase answering "keep both".
|
|
217
|
+
*/
|
|
218
|
+
const TYPE_REFERENCE_ROOT = new Set([
|
|
219
|
+
"TSClassImplements",
|
|
220
|
+
"TSInterfaceHeritage",
|
|
221
|
+
"TSTypeQuery",
|
|
222
|
+
"TSTypeReference",
|
|
223
|
+
]);
|
|
224
|
+
/**
|
|
225
|
+
* Node kinds whose `key` or `property` names the shape rather than the reader:
|
|
226
|
+
* `user.name`, `{ name: string }`, `{ name }`, class fields, enum members.
|
|
227
|
+
*/
|
|
228
|
+
const PROPERTY_BEARING = new Set([
|
|
229
|
+
"AccessorProperty",
|
|
230
|
+
"ClassProperty",
|
|
231
|
+
"MemberExpression",
|
|
232
|
+
"MethodDefinition",
|
|
233
|
+
"ObjectProperty",
|
|
234
|
+
"Property",
|
|
235
|
+
"PropertyDefinition",
|
|
236
|
+
"TSEnumMember",
|
|
237
|
+
"TSMethodSignature",
|
|
238
|
+
"TSPropertySignature",
|
|
239
|
+
]);
|
|
240
|
+
/** Every `Identifier` in a subtree, as start offsets. */
|
|
241
|
+
const identifierOffsets = (root) => {
|
|
242
|
+
const offsets = [];
|
|
243
|
+
const stack = [root];
|
|
244
|
+
while (stack.length > 0) {
|
|
245
|
+
const node = stack.pop();
|
|
246
|
+
if (Array.isArray(node)) {
|
|
247
|
+
for (const child of node)
|
|
248
|
+
stack.push(child);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (!isRecord(node))
|
|
252
|
+
continue;
|
|
253
|
+
if (node["type"] === "Identifier" && typeof node["start"] === "number") {
|
|
254
|
+
offsets.push(node["start"]);
|
|
255
|
+
}
|
|
256
|
+
for (const value of Object.values(node)) {
|
|
257
|
+
if (value !== null && typeof value === "object")
|
|
258
|
+
stack.push(value);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return offsets;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Collect every `Identifier` node in the file, marking the ones that are shape.
|
|
265
|
+
*
|
|
266
|
+
* Three things survive normalisation: property names, type references, and
|
|
267
|
+
* generic type-parameter *declarations* (which are local bindings, so `f<T>`
|
|
268
|
+
* and `g<U>` still compare equal). Everything else is a local name.
|
|
269
|
+
*/
|
|
270
|
+
const collectIdentifiers = (root) => {
|
|
271
|
+
const found = [];
|
|
272
|
+
const properties = new Set();
|
|
273
|
+
const types = new Set();
|
|
274
|
+
const typeRoots = [];
|
|
275
|
+
const typeParameters = new Set();
|
|
276
|
+
const stack = [root];
|
|
277
|
+
while (stack.length > 0) {
|
|
278
|
+
const node = stack.pop();
|
|
279
|
+
if (Array.isArray(node)) {
|
|
280
|
+
for (const child of node)
|
|
281
|
+
stack.push(child);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (!isRecord(node))
|
|
285
|
+
continue;
|
|
286
|
+
const kind = node["type"];
|
|
287
|
+
if (typeof kind === "string") {
|
|
288
|
+
if (PROPERTY_BEARING.has(kind)) {
|
|
289
|
+
for (const field of ["key", "property", "id"]) {
|
|
290
|
+
const child = node[field];
|
|
291
|
+
if (isRecord(child) && child["type"] === "Identifier" && typeof child["start"] === "number") {
|
|
292
|
+
properties.add(child["start"]);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (TYPE_REFERENCE_ROOT.has(kind))
|
|
297
|
+
typeRoots.push(node);
|
|
298
|
+
if (kind === "TSTypeParameter") {
|
|
299
|
+
const name = node["name"];
|
|
300
|
+
if (isRecord(name) && typeof name["name"] === "string")
|
|
301
|
+
typeParameters.add(name["name"]);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (kind === "Identifier" &&
|
|
305
|
+
typeof node["name"] === "string" &&
|
|
306
|
+
typeof node["start"] === "number" &&
|
|
307
|
+
typeof node["end"] === "number") {
|
|
308
|
+
found.push({ start: node["start"], end: node["end"], name: node["name"] });
|
|
309
|
+
}
|
|
310
|
+
for (const value of Object.values(node)) {
|
|
311
|
+
if (value !== null && typeof value === "object")
|
|
312
|
+
stack.push(value);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
for (const typeRoot of typeRoots) {
|
|
316
|
+
for (const offset of identifierOffsets(typeRoot)) {
|
|
317
|
+
const site = found.find((candidate) => candidate.start === offset);
|
|
318
|
+
if (site === undefined || typeParameters.has(site.name))
|
|
319
|
+
continue;
|
|
320
|
+
types.add(offset);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return found.map((site) => ({
|
|
324
|
+
...site,
|
|
325
|
+
siteKind: properties.has(site.start) ? "property" : types.has(site.start) ? "type" : "binding",
|
|
326
|
+
}));
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* Replace identifier names (and comments) with `_` / whitespace so that two
|
|
330
|
+
* declarations with the same structure but different vocabulary compare equal.
|
|
331
|
+
*
|
|
332
|
+
* Only the leading identifier characters are replaced, so a parameter's type
|
|
333
|
+
* annotation is preserved: `a: number` becomes `_: number`.
|
|
334
|
+
*/
|
|
335
|
+
const normalize = (text, span, identifiers, comments) => {
|
|
336
|
+
const edits = [];
|
|
337
|
+
for (const comment of comments) {
|
|
338
|
+
if (comment.start >= span.start && comment.end <= span.end) {
|
|
339
|
+
edits.push({ start: comment.start, end: comment.end, text: " " });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
for (const identifier of identifiers) {
|
|
343
|
+
if (identifier.siteKind !== "binding")
|
|
344
|
+
continue;
|
|
345
|
+
if (identifier.start < span.start)
|
|
346
|
+
continue;
|
|
347
|
+
if (identifier.start + identifier.name.length > span.end)
|
|
348
|
+
continue;
|
|
349
|
+
if (text.slice(identifier.start, identifier.start + identifier.name.length) !== identifier.name)
|
|
350
|
+
continue;
|
|
351
|
+
edits.push({ start: identifier.start, end: identifier.start + identifier.name.length, text: "_" });
|
|
352
|
+
}
|
|
353
|
+
edits.sort((a, b) => b.start - a.start);
|
|
354
|
+
let out = text.slice(span.start, span.end);
|
|
355
|
+
for (const edit of edits) {
|
|
356
|
+
const from = edit.start - span.start;
|
|
357
|
+
const to = edit.end - span.start;
|
|
358
|
+
out = out.slice(0, from) + edit.text + out.slice(to);
|
|
359
|
+
}
|
|
360
|
+
return out.replace(/\s+/g, " ").trim();
|
|
361
|
+
};
|
|
362
|
+
const TOKEN_PATTERN = /[A-Za-z0-9_$]+|[^\sA-Za-z0-9_$]/g;
|
|
363
|
+
/** Split a shape string into its tokens. */
|
|
364
|
+
export const tokenize = (shape) => {
|
|
365
|
+
const tokens = [];
|
|
366
|
+
for (const match of shape.matchAll(TOKEN_PATTERN))
|
|
367
|
+
tokens.push(match[0]);
|
|
368
|
+
return tokens;
|
|
369
|
+
};
|
|
370
|
+
/**
|
|
371
|
+
* The offset at which each line of a file begins, in UTF-16 code units.
|
|
372
|
+
*
|
|
373
|
+
* The unit the parser reports, so a parser offset and a line table agree. A
|
|
374
|
+
* table in UTF-8 bytes put every declaration after an em dash on the wrong line.
|
|
375
|
+
*
|
|
376
|
+
* @param text - The file's text.
|
|
377
|
+
* @returns One offset per line, the first being 0.
|
|
378
|
+
*/
|
|
379
|
+
export const lineStarts = (text) => {
|
|
380
|
+
const found = [0];
|
|
381
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
382
|
+
if (text[index] === "\n")
|
|
383
|
+
found.push(index + 1);
|
|
384
|
+
}
|
|
385
|
+
return found;
|
|
386
|
+
};
|
|
387
|
+
const locate = (starts, offset) => {
|
|
388
|
+
let low = 0;
|
|
389
|
+
let high = starts.length - 1;
|
|
390
|
+
while (low < high) {
|
|
391
|
+
const mid = Math.ceil((low + high) / 2);
|
|
392
|
+
const value = starts[mid];
|
|
393
|
+
if (value !== undefined && value <= offset)
|
|
394
|
+
low = mid;
|
|
395
|
+
else
|
|
396
|
+
high = mid - 1;
|
|
397
|
+
}
|
|
398
|
+
const start = starts[low];
|
|
399
|
+
return { line: low + 1, column: start === undefined ? 1 : offset - start + 1 };
|
|
400
|
+
};
|
|
401
|
+
/**
|
|
402
|
+
* The language of a file, from its extension.
|
|
403
|
+
*
|
|
404
|
+
* `.js` is the interesting one. JSX in a `.js` file is legal under the configs
|
|
405
|
+
* most React applications use, and oxc parses `lang: "js"` strictly enough to
|
|
406
|
+
* reject it. So the extension gives a first guess and the parser gets the last
|
|
407
|
+
* word -- see `parseSourceFile`, which gives a `.js` file a second reading as
|
|
408
|
+
* JSX when the first one reports errors, rather than guessing from the contents.
|
|
409
|
+
*/
|
|
410
|
+
const langOf = (file) => {
|
|
411
|
+
const dot = file.lastIndexOf(".");
|
|
412
|
+
const extension = dot < 0 ? "" : file.slice(dot);
|
|
413
|
+
switch (extension) {
|
|
414
|
+
case ".tsx":
|
|
415
|
+
return "tsx";
|
|
416
|
+
case ".jsx":
|
|
417
|
+
return "jsx";
|
|
418
|
+
case ".ts":
|
|
419
|
+
case ".mts":
|
|
420
|
+
case ".cts":
|
|
421
|
+
case ".astro":
|
|
422
|
+
return "ts";
|
|
423
|
+
default:
|
|
424
|
+
return "js";
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
/** The name a type reference or heritage clause points at, if it has one. */
|
|
428
|
+
const referenceName = (node) => {
|
|
429
|
+
if (!isRecord(node))
|
|
430
|
+
return undefined;
|
|
431
|
+
if (node["type"] === "TSTypeReference") {
|
|
432
|
+
const typeName = node["typeName"];
|
|
433
|
+
if (isRecord(typeName) && typeof typeName["name"] === "string")
|
|
434
|
+
return typeName["name"];
|
|
435
|
+
return undefined;
|
|
436
|
+
}
|
|
437
|
+
if (node["type"] === "TSInterfaceHeritage" || node["type"] === "TSClassImplements") {
|
|
438
|
+
return referenceName(node["expression"]);
|
|
439
|
+
}
|
|
440
|
+
if (node["type"] === "Identifier" && typeof node["name"] === "string")
|
|
441
|
+
return node["name"];
|
|
442
|
+
return undefined;
|
|
443
|
+
};
|
|
444
|
+
const fieldsOf = (node, text) => {
|
|
445
|
+
const memberLists = [];
|
|
446
|
+
const bases = [];
|
|
447
|
+
// A class contributes its TYPED properties and its inheritance. Its methods are
|
|
448
|
+
// their own units; a method name in a field map is not a field.
|
|
449
|
+
let typedMembersOnly = false;
|
|
450
|
+
if (node["type"] === "ClassDeclaration" || node["type"] === "ClassExpression") {
|
|
451
|
+
const body = node["body"];
|
|
452
|
+
if (isRecord(body))
|
|
453
|
+
memberLists.push(body["body"]);
|
|
454
|
+
typedMembersOnly = true;
|
|
455
|
+
const superClass = referenceName(node["superClass"]);
|
|
456
|
+
if (superClass !== undefined)
|
|
457
|
+
bases.push(superClass);
|
|
458
|
+
const implemented = node["implements"];
|
|
459
|
+
if (Array.isArray(implemented)) {
|
|
460
|
+
for (const entry of implemented) {
|
|
461
|
+
const name = referenceName(entry);
|
|
462
|
+
if (name !== undefined)
|
|
463
|
+
bases.push(name);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
else if (node["type"] === "TSInterfaceDeclaration") {
|
|
468
|
+
const body = node["body"];
|
|
469
|
+
if (isRecord(body))
|
|
470
|
+
memberLists.push(body["body"]);
|
|
471
|
+
const heritage = node["extends"];
|
|
472
|
+
if (Array.isArray(heritage)) {
|
|
473
|
+
for (const entry of heritage) {
|
|
474
|
+
const base = referenceName(entry);
|
|
475
|
+
if (base !== undefined)
|
|
476
|
+
bases.push(base);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
else if (node["type"] === "TSTypeAliasDeclaration" && isRecord(node["typeAnnotation"])) {
|
|
481
|
+
const annotation = node["typeAnnotation"];
|
|
482
|
+
if (Array.isArray(annotation["members"]))
|
|
483
|
+
memberLists.push(annotation["members"]);
|
|
484
|
+
if (annotation["type"] === "TSIntersectionType" && Array.isArray(annotation["types"])) {
|
|
485
|
+
for (const part of annotation["types"]) {
|
|
486
|
+
if (isRecord(part) && Array.isArray(part["members"]))
|
|
487
|
+
memberLists.push(part["members"]);
|
|
488
|
+
const base = referenceName(part);
|
|
489
|
+
if (base !== undefined)
|
|
490
|
+
bases.push(base);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
const base = referenceName(annotation);
|
|
495
|
+
if (base !== undefined)
|
|
496
|
+
bases.push(base);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
const names = [];
|
|
500
|
+
const declarations = new Map();
|
|
501
|
+
for (const members of memberLists) {
|
|
502
|
+
if (!Array.isArray(members))
|
|
503
|
+
continue;
|
|
504
|
+
for (const member of members) {
|
|
505
|
+
if (!isRecord(member))
|
|
506
|
+
continue;
|
|
507
|
+
const key = member["key"];
|
|
508
|
+
if (!isRecord(key))
|
|
509
|
+
continue;
|
|
510
|
+
const name = typeof key["name"] === "string"
|
|
511
|
+
? key["name"]
|
|
512
|
+
: typeof key["value"] === "string"
|
|
513
|
+
? key["value"]
|
|
514
|
+
: undefined;
|
|
515
|
+
if (name === undefined || declarations.has(name))
|
|
516
|
+
continue;
|
|
517
|
+
// The TYPE annotation, not the whole member.
|
|
518
|
+
//
|
|
519
|
+
// This recorded the member's source, which made "signal?: AbortSignal" and
|
|
520
|
+
// "signal: AbortSignal" different declarations -- and they are not, for the
|
|
521
|
+
// question being asked. A and B intersected with the first in one and the
|
|
522
|
+
// second in the other is "signal: AbortSignal", which is what the whole type
|
|
523
|
+
// wanted, so optionality is exactly the difference composition RESOLVES.
|
|
524
|
+
// Running against a real SDK, that false difference hid a genuine finding.
|
|
525
|
+
//
|
|
526
|
+
// A different TYPE is a different matter: "x: string" against "x: number"
|
|
527
|
+
// intersects to never, and there the fields genuinely disagree.
|
|
528
|
+
const annotation = member["typeAnnotation"];
|
|
529
|
+
if (typedMembersOnly && !isRecord(annotation))
|
|
530
|
+
continue;
|
|
531
|
+
const start = isRecord(annotation) ? annotation["start"] : undefined;
|
|
532
|
+
const end = isRecord(annotation) ? annotation["end"] : undefined;
|
|
533
|
+
// A class property's `typeAnnotation` node starts at the colon; an
|
|
534
|
+
// interface member's does not. One normal form, so two declarations of the
|
|
535
|
+
// same field compare equal whatever they are declared inside.
|
|
536
|
+
const declaration = typeof start === "number" && typeof end === "number"
|
|
537
|
+
? text.slice(start, end).replace(/^\s*:\s*/, "").replace(/\s+/g, " ").trim()
|
|
538
|
+
: name;
|
|
539
|
+
names.push(name);
|
|
540
|
+
declarations.set(name, declaration);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return { names, declarations, bases };
|
|
544
|
+
};
|
|
545
|
+
/** The property names a type declaration lists, in order: the keys of `fieldTypes`. */
|
|
546
|
+
export const unitFields = (unit) => [...unit.fieldTypes.keys()];
|
|
547
|
+
/**
|
|
548
|
+
* A predicate for the declarations that are named types with at least
|
|
549
|
+
* `minFields` fields.
|
|
550
|
+
*
|
|
551
|
+
* Two rules ask this with different thresholds; one predicate keeps the test
|
|
552
|
+
* itself in one place, so "what counts as a type worth comparing" cannot drift
|
|
553
|
+
* between them.
|
|
554
|
+
*
|
|
555
|
+
* @param minFields - The least number of fields worth comparing.
|
|
556
|
+
* @returns A predicate over units.
|
|
557
|
+
*/
|
|
558
|
+
export const typeUnits = (minFields) => (unit) => (unit.kind === "interface" || unit.kind === "type") && unitFields(unit).length >= minFields;
|
|
559
|
+
/** True when every parameter is optional or has a default. */
|
|
560
|
+
const paramsAllOptional = (node) => {
|
|
561
|
+
if (!isRecord(node))
|
|
562
|
+
return false;
|
|
563
|
+
const params = node["params"];
|
|
564
|
+
if (!Array.isArray(params) || params.length === 0)
|
|
565
|
+
return false;
|
|
566
|
+
return params.every((param) => {
|
|
567
|
+
if (!isRecord(param))
|
|
568
|
+
return false;
|
|
569
|
+
if (param["optional"] === true)
|
|
570
|
+
return true;
|
|
571
|
+
return param["type"] === "AssignmentPattern";
|
|
572
|
+
});
|
|
573
|
+
};
|
|
574
|
+
/** The initializer with any `as const` / `satisfies` wrapper removed. */
|
|
575
|
+
const unwrappedExpression = (node) => {
|
|
576
|
+
let current = node;
|
|
577
|
+
// Bounded rather than recursive: a wrapper stack deeper than this is a parser
|
|
578
|
+
// oddity, not a declaration, and the fallback is "not an initializer we read".
|
|
579
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
580
|
+
if (!isRecord(current))
|
|
581
|
+
return undefined;
|
|
582
|
+
const type = current["type"];
|
|
583
|
+
if (type === "TSAsExpression" ||
|
|
584
|
+
type === "TSSatisfiesExpression" ||
|
|
585
|
+
type === "TSTypeAssertion" ||
|
|
586
|
+
type === "ParenthesizedExpression") {
|
|
587
|
+
current = current["expression"];
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
return current;
|
|
591
|
+
}
|
|
592
|
+
return undefined;
|
|
593
|
+
};
|
|
594
|
+
/** A literal's value as text, or undefined when the node is not a literal. */
|
|
595
|
+
const literalTextOf = (node) => {
|
|
596
|
+
if (!isRecord(node) || node["type"] !== "Literal")
|
|
597
|
+
return undefined;
|
|
598
|
+
const value = node["value"];
|
|
599
|
+
if (Predicate.isString(value) || Predicate.isNumber(value) || Predicate.isBoolean(value)) {
|
|
600
|
+
return String(value);
|
|
601
|
+
}
|
|
602
|
+
return undefined;
|
|
603
|
+
};
|
|
604
|
+
/**
|
|
605
|
+
* The literal values a declaration names, when EVERY one is a literal.
|
|
606
|
+
*
|
|
607
|
+
* A half-read vocabulary is worse than none: if one element is an expression, two
|
|
608
|
+
* different sets would look alike. So one non-literal empties the list.
|
|
609
|
+
*/
|
|
610
|
+
const initializerValues = (node) => {
|
|
611
|
+
const init = unwrappedExpression(node);
|
|
612
|
+
if (init === undefined)
|
|
613
|
+
return [];
|
|
614
|
+
if (init["type"] === "ArrayExpression") {
|
|
615
|
+
const elements = init["elements"];
|
|
616
|
+
if (!Array.isArray(elements))
|
|
617
|
+
return [];
|
|
618
|
+
const values = [];
|
|
619
|
+
for (const element of elements) {
|
|
620
|
+
const value = literalTextOf(unwrappedExpression(element));
|
|
621
|
+
if (value === undefined)
|
|
622
|
+
return [];
|
|
623
|
+
values.push(value);
|
|
624
|
+
}
|
|
625
|
+
return values;
|
|
626
|
+
}
|
|
627
|
+
if (init["type"] === "ObjectExpression") {
|
|
628
|
+
const properties = init["properties"];
|
|
629
|
+
if (!Array.isArray(properties))
|
|
630
|
+
return [];
|
|
631
|
+
const values = [];
|
|
632
|
+
for (const property of properties) {
|
|
633
|
+
if (!isRecord(property) || property["type"] !== "Property")
|
|
634
|
+
return [];
|
|
635
|
+
const value = literalTextOf(unwrappedExpression(property["value"]));
|
|
636
|
+
if (value === undefined)
|
|
637
|
+
return [];
|
|
638
|
+
values.push(value);
|
|
639
|
+
}
|
|
640
|
+
return values;
|
|
641
|
+
}
|
|
642
|
+
const single = literalTextOf(init);
|
|
643
|
+
return single === undefined ? [] : [single];
|
|
644
|
+
};
|
|
645
|
+
/** An enum's member values, initializer first and the member name as fallback. */
|
|
646
|
+
const enumValues = (node) => {
|
|
647
|
+
// Oxc nests the members under `body`, not on the declaration.
|
|
648
|
+
const body = node["body"];
|
|
649
|
+
const members = isRecord(body) ? body["members"] : undefined;
|
|
650
|
+
if (!Array.isArray(members))
|
|
651
|
+
return [];
|
|
652
|
+
const values = [];
|
|
653
|
+
for (const member of members) {
|
|
654
|
+
if (!isRecord(member))
|
|
655
|
+
continue;
|
|
656
|
+
const id = member["id"];
|
|
657
|
+
const name = isRecord(id) && Predicate.isString(id["name"]) ? id["name"] : undefined;
|
|
658
|
+
const value = literalTextOf(member["initializer"]) ?? name;
|
|
659
|
+
if (value !== undefined)
|
|
660
|
+
values.push(value);
|
|
661
|
+
}
|
|
662
|
+
return values;
|
|
663
|
+
};
|
|
664
|
+
/** The alternatives a union names, or the keys a mapped type ranges over. */
|
|
665
|
+
const variantTextsOf = (node, text) => {
|
|
666
|
+
if (node["type"] !== "TSTypeAliasDeclaration")
|
|
667
|
+
return [];
|
|
668
|
+
const annotation = node["typeAnnotation"];
|
|
669
|
+
if (!isRecord(annotation))
|
|
670
|
+
return [];
|
|
671
|
+
const spanText = (part) => {
|
|
672
|
+
if (!isRecord(part))
|
|
673
|
+
return undefined;
|
|
674
|
+
const start = part["start"];
|
|
675
|
+
const end = part["end"];
|
|
676
|
+
if (!Predicate.isNumber(start) || !Predicate.isNumber(end))
|
|
677
|
+
return undefined;
|
|
678
|
+
return text.slice(start, end).replace(/\s+/g, " ").trim();
|
|
679
|
+
};
|
|
680
|
+
const unionTexts = (part) => {
|
|
681
|
+
if (!isRecord(part) || part["type"] !== "TSUnionType")
|
|
682
|
+
return [];
|
|
683
|
+
const types = part["types"];
|
|
684
|
+
if (!Array.isArray(types))
|
|
685
|
+
return [];
|
|
686
|
+
return types.map(spanText).filter((value) => value !== undefined);
|
|
687
|
+
};
|
|
688
|
+
if (annotation["type"] === "TSUnionType")
|
|
689
|
+
return unionTexts(annotation);
|
|
690
|
+
if (annotation["type"] === "TSMappedType") {
|
|
691
|
+
// Oxc puts the constraint beside `key`; the estree shape nests it under
|
|
692
|
+
// `typeParameter`. Read both, so the fact does not depend on the parser.
|
|
693
|
+
let constraint = annotation["constraint"];
|
|
694
|
+
if (constraint === undefined) {
|
|
695
|
+
const typeParameter = annotation["typeParameter"];
|
|
696
|
+
constraint = isRecord(typeParameter) ? typeParameter["constraint"] : undefined;
|
|
697
|
+
}
|
|
698
|
+
const keys = unionTexts(constraint);
|
|
699
|
+
if (keys.length > 0)
|
|
700
|
+
return keys;
|
|
701
|
+
const single = spanText(constraint);
|
|
702
|
+
return single === undefined ? [] : [single];
|
|
703
|
+
}
|
|
704
|
+
return [];
|
|
705
|
+
};
|
|
706
|
+
/** Declaration kinds that are a type by construction. */
|
|
707
|
+
const TYPE_DECLARATIONS = new Set([
|
|
708
|
+
"TSInterfaceDeclaration",
|
|
709
|
+
"TSTypeAliasDeclaration",
|
|
710
|
+
"ClassDeclaration",
|
|
711
|
+
"TSEnumDeclaration",
|
|
712
|
+
]);
|
|
713
|
+
/** Field names on a node that hold a type rather than a value. */
|
|
714
|
+
const TYPE_SURFACE = [
|
|
715
|
+
"typeAnnotation",
|
|
716
|
+
"returnType",
|
|
717
|
+
"typeParameters",
|
|
718
|
+
"typeArguments",
|
|
719
|
+
"superTypeParameters",
|
|
720
|
+
];
|
|
721
|
+
const isTypeDeclaration = (kind) => Predicate.isString(kind) && TYPE_DECLARATIONS.has(kind);
|
|
722
|
+
/** A type written on the declaration itself. */
|
|
723
|
+
const writesSurfaceType = (node) => TYPE_SURFACE.some((field) => isRecord(node[field]));
|
|
724
|
+
/** A parameter's own annotation, including the left side of a defaulted one. */
|
|
725
|
+
const hasTypedParameter = (params) => Array.isArray(params) &&
|
|
726
|
+
params.some((param) => {
|
|
727
|
+
if (!isRecord(param))
|
|
728
|
+
return false;
|
|
729
|
+
if (isRecord(param["typeAnnotation"]))
|
|
730
|
+
return true;
|
|
731
|
+
return (param["type"] === "AssignmentPattern" &&
|
|
732
|
+
isRecord(param["left"]) &&
|
|
733
|
+
isRecord(param["left"]["typeAnnotation"]));
|
|
734
|
+
});
|
|
735
|
+
/** An initializer that writes a type: `as const`, `satisfies`, or a type argument. */
|
|
736
|
+
const hasTypedInitializer = (init) => isRecord(init) &&
|
|
737
|
+
(init["type"] === "TSAsExpression" ||
|
|
738
|
+
init["type"] === "TSSatisfiesExpression" ||
|
|
739
|
+
isRecord(init["typeArguments"]));
|
|
740
|
+
/**
|
|
741
|
+
* True when a declaration writes a type, including a keyword type.
|
|
742
|
+
*
|
|
743
|
+
* The shallow fields are enough: annotations live on the declaration's params and
|
|
744
|
+
* return, not inside the body. A full walk would double the extraction cost to
|
|
745
|
+
* answer a question the surface already answers.
|
|
746
|
+
*/
|
|
747
|
+
const hasTypeSyntax = (node) => isRecord(node) &&
|
|
748
|
+
(isTypeDeclaration(node["type"]) ||
|
|
749
|
+
writesSurfaceType(node) ||
|
|
750
|
+
hasTypedParameter(node["params"]) ||
|
|
751
|
+
hasTypedInitializer(node["init"]));
|
|
752
|
+
const sitesIn = (program, text) => {
|
|
753
|
+
const body = program["body"];
|
|
754
|
+
if (!Array.isArray(body))
|
|
755
|
+
return [];
|
|
756
|
+
const sites = [];
|
|
757
|
+
const push = (node, kind, name, exported, docStart, nameStart, more) => {
|
|
758
|
+
if (!isRecord(node))
|
|
759
|
+
return;
|
|
760
|
+
if (typeof name !== "string")
|
|
761
|
+
return;
|
|
762
|
+
const start = node["start"];
|
|
763
|
+
const end = node["end"];
|
|
764
|
+
if (typeof start !== "number" || typeof end !== "number")
|
|
765
|
+
return;
|
|
766
|
+
const fieldSet = fieldsOf(node, text);
|
|
767
|
+
const typed = hasTypeSyntax(node);
|
|
768
|
+
const variants = more === undefined ? variantTextsOf(node, text) : more.variants;
|
|
769
|
+
const values = more === undefined
|
|
770
|
+
? node["type"] === "TSEnumDeclaration"
|
|
771
|
+
? enumValues(node)
|
|
772
|
+
: []
|
|
773
|
+
: more.values;
|
|
774
|
+
sites.push({
|
|
775
|
+
kind,
|
|
776
|
+
name,
|
|
777
|
+
start,
|
|
778
|
+
end,
|
|
779
|
+
namePosition: nameStart ?? start,
|
|
780
|
+
exported,
|
|
781
|
+
typed,
|
|
782
|
+
allParamsOptional: paramsAllOptional(node),
|
|
783
|
+
fieldTypes: fieldSet.declarations,
|
|
784
|
+
bases: fieldSet.bases,
|
|
785
|
+
variants,
|
|
786
|
+
values,
|
|
787
|
+
docStart: docStart ?? start,
|
|
788
|
+
});
|
|
789
|
+
};
|
|
790
|
+
const fromDeclaration = (declaration, exported, docStart) => {
|
|
791
|
+
if (!isRecord(declaration))
|
|
792
|
+
return;
|
|
793
|
+
switch (declaration["type"]) {
|
|
794
|
+
case "FunctionDeclaration": {
|
|
795
|
+
const id = declaration["id"];
|
|
796
|
+
push(declaration, "function", isRecord(id) ? id["name"] : undefined, exported, docStart, isRecord(id) && typeof id["start"] === "number" ? id["start"] : undefined, undefined);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
case "TSInterfaceDeclaration": {
|
|
800
|
+
const id = declaration["id"];
|
|
801
|
+
push(declaration, "interface", isRecord(id) ? id["name"] : undefined, exported, docStart, isRecord(id) && typeof id["start"] === "number" ? id["start"] : undefined, undefined);
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
case "TSTypeAliasDeclaration": {
|
|
805
|
+
const id = declaration["id"];
|
|
806
|
+
push(declaration, "type", isRecord(id) ? id["name"] : undefined, exported, docStart, isRecord(id) && typeof id["start"] === "number" ? id["start"] : undefined, undefined);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
case "VariableDeclaration": {
|
|
810
|
+
const declarators = declaration["declarations"];
|
|
811
|
+
if (!Array.isArray(declarators))
|
|
812
|
+
return;
|
|
813
|
+
for (const declarator of declarators) {
|
|
814
|
+
if (!isRecord(declarator))
|
|
815
|
+
continue;
|
|
816
|
+
const id = declarator["id"];
|
|
817
|
+
const init = declarator["init"];
|
|
818
|
+
const name = isRecord(id) ? id["name"] : undefined;
|
|
819
|
+
const nameStart = isRecord(id) && typeof id["start"] === "number" ? id["start"] : undefined;
|
|
820
|
+
// A declarator with no initializer is still a declaration when it is
|
|
821
|
+
// typed (`declare const X: T`), and it has a name rules can ask about.
|
|
822
|
+
if (!isRecord(init)) {
|
|
823
|
+
push(declarator, "const", name, exported, docStart, nameStart, { values: [], variants: [] });
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
const initType = unwrappedExpression(init)?.["type"];
|
|
827
|
+
if (initType === "ObjectExpression") {
|
|
828
|
+
// The methods are their own units; the object is a vocabulary or a
|
|
829
|
+
// config, and its keys and values are what a rule compares.
|
|
830
|
+
fromDeclaration(init, exported, docStart);
|
|
831
|
+
push(declarator, "const", name, exported, docStart, nameStart, {
|
|
832
|
+
values: initializerValues(init),
|
|
833
|
+
variants: [],
|
|
834
|
+
});
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (initType === "ArrowFunctionExpression" || initType === "FunctionExpression") {
|
|
838
|
+
push(init, "function", name, exported, docStart, nameStart, undefined);
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
// Every other initializer -- a schema call, a table, a literal, a new
|
|
842
|
+
// expression -- is a declaration whose VALUES and resolved type matter.
|
|
843
|
+
push(declarator, "const", name, exported, docStart, nameStart, {
|
|
844
|
+
values: initializerValues(init),
|
|
845
|
+
variants: [],
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
case "TSEnumDeclaration": {
|
|
851
|
+
const id = declaration["id"];
|
|
852
|
+
push(declaration, "enum", isRecord(id) ? id["name"] : undefined, exported, docStart, isRecord(id) && typeof id["start"] === "number" ? id["start"] : undefined, undefined);
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
// Class methods and object-literal methods were invisible: only top-level
|
|
856
|
+
// declarations became units, so a helper copied between two classes or two
|
|
857
|
+
// API objects was never a candidate. They are named Owner.member so a
|
|
858
|
+
// reader can tell which one the report means.
|
|
859
|
+
case "ClassDeclaration":
|
|
860
|
+
case "ClassExpression": {
|
|
861
|
+
const classId = declaration["id"];
|
|
862
|
+
const className = isRecord(classId) && typeof classId["name"] === "string" ? classId["name"] : undefined;
|
|
863
|
+
// The class is a declaration of its own: its properties are the shape, its
|
|
864
|
+
// methods are separate units, and inheritance is a base.
|
|
865
|
+
push(declaration, "class", className, exported, docStart, isRecord(classId) && typeof classId["start"] === "number" ? classId["start"] : undefined, undefined);
|
|
866
|
+
const body = declaration["body"];
|
|
867
|
+
if (!isRecord(body))
|
|
868
|
+
return;
|
|
869
|
+
const members = body["body"];
|
|
870
|
+
if (!Array.isArray(members))
|
|
871
|
+
return;
|
|
872
|
+
for (const member of members) {
|
|
873
|
+
if (!isRecord(member))
|
|
874
|
+
continue;
|
|
875
|
+
const key = member["key"];
|
|
876
|
+
const keyName = isRecord(key) && typeof key["name"] === "string" ? key["name"] : undefined;
|
|
877
|
+
if (keyName === undefined)
|
|
878
|
+
continue;
|
|
879
|
+
const keyStart = isRecord(key) && typeof key["start"] === "number" ? key["start"] : undefined;
|
|
880
|
+
const name = className === undefined ? keyName : `${className}.${keyName}`;
|
|
881
|
+
const value = member["value"];
|
|
882
|
+
if (isRecord(value) && typeof value["start"] === "number" && typeof value["end"] === "number") {
|
|
883
|
+
sites.push({
|
|
884
|
+
kind: "function",
|
|
885
|
+
name,
|
|
886
|
+
start: value["start"],
|
|
887
|
+
end: value["end"],
|
|
888
|
+
namePosition: keyStart ?? value["start"],
|
|
889
|
+
exported,
|
|
890
|
+
typed: hasTypeSyntax(value),
|
|
891
|
+
allParamsOptional: paramsAllOptional(value),
|
|
892
|
+
fieldTypes: new Map(),
|
|
893
|
+
bases: [],
|
|
894
|
+
variants: [],
|
|
895
|
+
values: [],
|
|
896
|
+
docStart: typeof member["start"] === "number" ? member["start"] : value["start"],
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
push(member, "function", name, exported, undefined, keyStart, undefined);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
case "ObjectExpression": {
|
|
906
|
+
const properties = declaration["properties"];
|
|
907
|
+
if (!Array.isArray(properties))
|
|
908
|
+
return;
|
|
909
|
+
for (const property of properties) {
|
|
910
|
+
if (!isRecord(property) || property["type"] !== "Property")
|
|
911
|
+
continue;
|
|
912
|
+
const key = property["key"];
|
|
913
|
+
const keyName = isRecord(key) && typeof key["name"] === "string" ? key["name"] : undefined;
|
|
914
|
+
if (keyName === undefined)
|
|
915
|
+
continue;
|
|
916
|
+
const keyStart = isRecord(key) && typeof key["start"] === "number" ? key["start"] : undefined;
|
|
917
|
+
const value = property["value"];
|
|
918
|
+
if (!isRecord(value))
|
|
919
|
+
continue;
|
|
920
|
+
const valueType = value["type"];
|
|
921
|
+
if (valueType !== "ArrowFunctionExpression" && valueType !== "FunctionExpression")
|
|
922
|
+
continue;
|
|
923
|
+
push(value, "function", keyName, exported, undefined, keyStart, undefined);
|
|
924
|
+
}
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
default:
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
for (const statement of body) {
|
|
932
|
+
if (!isRecord(statement))
|
|
933
|
+
continue;
|
|
934
|
+
switch (statement["type"]) {
|
|
935
|
+
case "ExportNamedDeclaration":
|
|
936
|
+
case "ExportDefaultDeclaration":
|
|
937
|
+
// The doc sits above the `export`, so the lookup starts at the statement.
|
|
938
|
+
fromDeclaration(statement["declaration"], true, typeof statement["start"] === "number" ? statement["start"] : undefined);
|
|
939
|
+
continue;
|
|
940
|
+
case "FunctionDeclaration":
|
|
941
|
+
case "TSInterfaceDeclaration":
|
|
942
|
+
case "TSTypeAliasDeclaration":
|
|
943
|
+
case "VariableDeclaration":
|
|
944
|
+
case "ClassDeclaration":
|
|
945
|
+
case "TSEnumDeclaration":
|
|
946
|
+
fromDeclaration(statement, false, typeof statement["start"] === "number" ? statement["start"] : undefined);
|
|
947
|
+
continue;
|
|
948
|
+
default:
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return sites;
|
|
953
|
+
};
|
|
954
|
+
/**
|
|
955
|
+
* Collect the names a file spells.
|
|
956
|
+
*
|
|
957
|
+
* One generic walk, three lists. A composition pattern is checkable from them
|
|
958
|
+
* because every part of the convention is literally written down: a call to
|
|
959
|
+
* `createContext`, a render of `Counter.Provider`, an object literal with
|
|
960
|
+
* `state`, `actions` and `meta` among its keys.
|
|
961
|
+
*/
|
|
962
|
+
const structureIn = (root, allOptionalFunctions) => {
|
|
963
|
+
const callSites = [];
|
|
964
|
+
const jsx = new Set();
|
|
965
|
+
const objects = [];
|
|
966
|
+
const columns = [];
|
|
967
|
+
const stringSites = [];
|
|
968
|
+
const guards = [];
|
|
969
|
+
const skips = [];
|
|
970
|
+
const literals = [];
|
|
971
|
+
const returns = [];
|
|
972
|
+
const comparisons = [];
|
|
973
|
+
const declaredObjects = new Map();
|
|
974
|
+
const nameOf = (node) => {
|
|
975
|
+
if (!isRecord(node))
|
|
976
|
+
return undefined;
|
|
977
|
+
if (node["type"] === "Identifier" && typeof node["name"] === "string")
|
|
978
|
+
return node["name"];
|
|
979
|
+
if (node["type"] === "MemberExpression" || node["type"] === "StaticMemberExpression") {
|
|
980
|
+
const object = nameOf(node["object"]);
|
|
981
|
+
const property = node["property"];
|
|
982
|
+
const key = isRecord(property) && typeof property["name"] === "string" ? property["name"] : undefined;
|
|
983
|
+
return object !== undefined && key !== undefined ? object + "." + key : undefined;
|
|
984
|
+
}
|
|
985
|
+
if (node["type"] === "JSXIdentifier" && typeof node["name"] === "string")
|
|
986
|
+
return node["name"];
|
|
987
|
+
if (node["type"] === "JSXMemberExpression") {
|
|
988
|
+
const object = nameOf(node["object"]);
|
|
989
|
+
const property = nameOf(node["property"]);
|
|
990
|
+
return object !== undefined && property !== undefined ? object + "." + property : undefined;
|
|
991
|
+
}
|
|
992
|
+
return undefined;
|
|
993
|
+
};
|
|
994
|
+
/** Every reference a subtree names, dotted, deduplicated. */
|
|
995
|
+
const refsIn = (node) => {
|
|
996
|
+
const found = [];
|
|
997
|
+
const walk = (value) => {
|
|
998
|
+
if (Array.isArray(value)) {
|
|
999
|
+
for (const child of value)
|
|
1000
|
+
walk(child);
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
if (!isRecord(value))
|
|
1004
|
+
return;
|
|
1005
|
+
const type = value["type"];
|
|
1006
|
+
if (type === "Identifier" && typeof value["name"] === "string") {
|
|
1007
|
+
found.push(value["name"]);
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
if (type === "MemberExpression" || type === "StaticMemberExpression") {
|
|
1011
|
+
const name = nameOf(value);
|
|
1012
|
+
if (name !== undefined)
|
|
1013
|
+
found.push(name);
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
for (const key of Object.keys(value)) {
|
|
1017
|
+
if (key === "type" || key === "start" || key === "end")
|
|
1018
|
+
continue;
|
|
1019
|
+
walk(value[key]);
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
walk(node);
|
|
1023
|
+
return [...new Set(found)];
|
|
1024
|
+
};
|
|
1025
|
+
/** True when a subtree returns or throws. */
|
|
1026
|
+
const exitsIn = (node) => {
|
|
1027
|
+
let found = false;
|
|
1028
|
+
const walk = (value) => {
|
|
1029
|
+
if (found)
|
|
1030
|
+
return;
|
|
1031
|
+
if (Array.isArray(value)) {
|
|
1032
|
+
for (const child of value)
|
|
1033
|
+
walk(child);
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
if (!isRecord(value))
|
|
1037
|
+
return;
|
|
1038
|
+
const type = value["type"];
|
|
1039
|
+
if (type === "ReturnStatement" || type === "ThrowStatement") {
|
|
1040
|
+
found = true;
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
for (const key of Object.keys(value)) {
|
|
1044
|
+
if (key === "type" || key === "start" || key === "end")
|
|
1045
|
+
continue;
|
|
1046
|
+
walk(value[key]);
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
walk(node);
|
|
1050
|
+
return found;
|
|
1051
|
+
};
|
|
1052
|
+
/** The method names of a call chain, outermost first: `[annotate, NullOr]`. */
|
|
1053
|
+
const chainOf = (node) => {
|
|
1054
|
+
const methods = [];
|
|
1055
|
+
let current = node;
|
|
1056
|
+
for (let guard = 0; guard < 32; guard += 1) {
|
|
1057
|
+
if (!isRecord(current) || current["type"] !== "CallExpression")
|
|
1058
|
+
break;
|
|
1059
|
+
const callee = current["callee"];
|
|
1060
|
+
if (!isRecord(callee))
|
|
1061
|
+
break;
|
|
1062
|
+
const property = callee["property"];
|
|
1063
|
+
const method = isRecord(property) && typeof property["name"] === "string" ? property["name"] : undefined;
|
|
1064
|
+
if (method !== undefined)
|
|
1065
|
+
methods.push(method);
|
|
1066
|
+
current = callee["object"];
|
|
1067
|
+
}
|
|
1068
|
+
return methods;
|
|
1069
|
+
};
|
|
1070
|
+
/**
|
|
1071
|
+
* A field value's `Schema` chain, including a bare member.
|
|
1072
|
+
*
|
|
1073
|
+
* `chainOf` follows CALL chains (`Schema.NullOr(Schema.String)`), so it misses
|
|
1074
|
+
* `Schema.Number`, which is a member and not a call -- and a bare `Finite` is
|
|
1075
|
+
* exactly the field a value has to satisfy.
|
|
1076
|
+
*/
|
|
1077
|
+
const schemasOf = (value) => {
|
|
1078
|
+
const methods = chainOf(value);
|
|
1079
|
+
if (methods.length > 0)
|
|
1080
|
+
return methods;
|
|
1081
|
+
if (isRecord(value) &&
|
|
1082
|
+
(value["type"] === "MemberExpression" || value["type"] === "StaticMemberExpression")) {
|
|
1083
|
+
const property = value["property"];
|
|
1084
|
+
if (isRecord(property) && typeof property["name"] === "string")
|
|
1085
|
+
return [property["name"]];
|
|
1086
|
+
}
|
|
1087
|
+
return [];
|
|
1088
|
+
};
|
|
1089
|
+
/** The `sourceColumn` an `.annotate({...})` in the chain names, or "". */
|
|
1090
|
+
const sourceColumnOf = (value) => {
|
|
1091
|
+
let current = value;
|
|
1092
|
+
for (let guard = 0; guard < 32; guard += 1) {
|
|
1093
|
+
if (!isRecord(current) || current["type"] !== "CallExpression")
|
|
1094
|
+
break;
|
|
1095
|
+
const callee = current["callee"];
|
|
1096
|
+
if (!isRecord(callee))
|
|
1097
|
+
break;
|
|
1098
|
+
const property = callee["property"];
|
|
1099
|
+
const method = isRecord(property) && typeof property["name"] === "string" ? property["name"] : undefined;
|
|
1100
|
+
if (method === "annotate") {
|
|
1101
|
+
const args = current["arguments"];
|
|
1102
|
+
if (Array.isArray(args)) {
|
|
1103
|
+
for (const arg of args) {
|
|
1104
|
+
if (!isRecord(arg) || arg["type"] !== "ObjectExpression")
|
|
1105
|
+
continue;
|
|
1106
|
+
const properties = arg["properties"];
|
|
1107
|
+
if (!Array.isArray(properties))
|
|
1108
|
+
continue;
|
|
1109
|
+
for (const entry of properties) {
|
|
1110
|
+
if (!isRecord(entry))
|
|
1111
|
+
continue;
|
|
1112
|
+
const key = entry["key"];
|
|
1113
|
+
if (!isRecord(key) || key["name"] !== "sourceColumn")
|
|
1114
|
+
continue;
|
|
1115
|
+
const field = entry["value"];
|
|
1116
|
+
if (isRecord(field) && typeof field["value"] === "string")
|
|
1117
|
+
return field["value"];
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
current = callee["object"];
|
|
1123
|
+
}
|
|
1124
|
+
return "";
|
|
1125
|
+
};
|
|
1126
|
+
// The non-optional keys of a `Schema.Struct` field object, the keys that admit
|
|
1127
|
+
// null, and the column each field reads.
|
|
1128
|
+
const declaredFieldsOf = (object) => {
|
|
1129
|
+
const properties = object["properties"];
|
|
1130
|
+
const required = [];
|
|
1131
|
+
const nullable = [];
|
|
1132
|
+
const sources = {};
|
|
1133
|
+
const schemas = {};
|
|
1134
|
+
if (!Array.isArray(properties))
|
|
1135
|
+
return { required, nullable, sources, schemas };
|
|
1136
|
+
for (const property of properties) {
|
|
1137
|
+
if (!isRecord(property))
|
|
1138
|
+
continue;
|
|
1139
|
+
const key = property["key"];
|
|
1140
|
+
const name = isRecord(key) && typeof key["name"] === "string" ? key["name"] : undefined;
|
|
1141
|
+
if (name === undefined)
|
|
1142
|
+
continue;
|
|
1143
|
+
const value = property["value"];
|
|
1144
|
+
const methods = schemasOf(value);
|
|
1145
|
+
if (!methods.includes("optionalKey") && !methods.includes("optional"))
|
|
1146
|
+
required.push(name);
|
|
1147
|
+
if (methods.includes("NullOr"))
|
|
1148
|
+
nullable.push(name);
|
|
1149
|
+
const source = sourceColumnOf(value);
|
|
1150
|
+
if (source !== "")
|
|
1151
|
+
sources[name] = source;
|
|
1152
|
+
if (methods.length > 0)
|
|
1153
|
+
schemas[name] = methods;
|
|
1154
|
+
}
|
|
1155
|
+
return { required, nullable, sources, schemas };
|
|
1156
|
+
};
|
|
1157
|
+
// A same-file `const NAME = 'value'`, so `createTable(PAYROLL_CREW, ...)` names
|
|
1158
|
+
// its table even though the table name is not a literal.
|
|
1159
|
+
const constStrings = new Map();
|
|
1160
|
+
const collectConsts = (node) => {
|
|
1161
|
+
if (Array.isArray(node)) {
|
|
1162
|
+
for (const child of node)
|
|
1163
|
+
collectConsts(child);
|
|
1164
|
+
return;
|
|
1165
|
+
}
|
|
1166
|
+
if (!isRecord(node))
|
|
1167
|
+
return;
|
|
1168
|
+
if (node["type"] === "VariableDeclarator") {
|
|
1169
|
+
const id = node["id"];
|
|
1170
|
+
const init = node["init"];
|
|
1171
|
+
if (isRecord(id) &&
|
|
1172
|
+
id["type"] === "Identifier" &&
|
|
1173
|
+
typeof id["name"] === "string" &&
|
|
1174
|
+
isRecord(init) &&
|
|
1175
|
+
init["type"] === "Literal" &&
|
|
1176
|
+
typeof init["value"] === "string") {
|
|
1177
|
+
constStrings.set(id["name"], init["value"]);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
for (const value of Object.values(node)) {
|
|
1181
|
+
if (value !== null && typeof value === "object")
|
|
1182
|
+
collectConsts(value);
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
collectConsts(root);
|
|
1186
|
+
const tableNameOf = (arg) => {
|
|
1187
|
+
if (!isRecord(arg))
|
|
1188
|
+
return "";
|
|
1189
|
+
if (typeof arg["value"] === "string")
|
|
1190
|
+
return arg["value"];
|
|
1191
|
+
if (arg["type"] === "Identifier" && typeof arg["name"] === "string")
|
|
1192
|
+
return constStrings.get(arg["name"]) ?? "";
|
|
1193
|
+
return "";
|
|
1194
|
+
};
|
|
1195
|
+
const COLUMN_TYPES = new Set([
|
|
1196
|
+
"uuid", "string", "text", "integer", "bigInteger", "smallint", "tinyint", "float", "double",
|
|
1197
|
+
"decimal", "boolean", "timestamp", "datetime", "date", "time", "json", "jsonb", "binary",
|
|
1198
|
+
"increments", "bigIncrements", "enu", "specificType",
|
|
1199
|
+
]);
|
|
1200
|
+
/** Record the column a `table.<type>('<name>')` chain builds, if it is one. */
|
|
1201
|
+
const addColumn = (table, node) => {
|
|
1202
|
+
let current = node;
|
|
1203
|
+
const methods = [];
|
|
1204
|
+
for (let guard = 0; guard < 32; guard += 1) {
|
|
1205
|
+
if (!isRecord(current) || current["type"] !== "CallExpression")
|
|
1206
|
+
return;
|
|
1207
|
+
const callee = current["callee"];
|
|
1208
|
+
if (!isRecord(callee))
|
|
1209
|
+
return;
|
|
1210
|
+
const property = callee["property"];
|
|
1211
|
+
const method = isRecord(property) && typeof property["name"] === "string" ? property["name"] : undefined;
|
|
1212
|
+
if (method !== undefined)
|
|
1213
|
+
methods.push(method);
|
|
1214
|
+
const base = callee["object"];
|
|
1215
|
+
if (method !== undefined && COLUMN_TYPES.has(method) && isRecord(base) && base["type"] === "Identifier") {
|
|
1216
|
+
const args = current["arguments"];
|
|
1217
|
+
const first = Array.isArray(args) ? args[0] : undefined;
|
|
1218
|
+
const start = current["start"];
|
|
1219
|
+
if (isRecord(first) && typeof first["value"] === "string" && typeof start === "number") {
|
|
1220
|
+
// A primary key is NOT NULL in Postgres even without `.notNullable()`.
|
|
1221
|
+
const nullable = !methods.includes("notNullable") && !methods.includes("primary");
|
|
1222
|
+
columns.push({ table, column: first["value"], nullable, start });
|
|
1223
|
+
}
|
|
1224
|
+
return;
|
|
1225
|
+
}
|
|
1226
|
+
current = base;
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
const statementsOf = (fn) => {
|
|
1230
|
+
if (!isRecord(fn))
|
|
1231
|
+
return [];
|
|
1232
|
+
const body = fn["body"];
|
|
1233
|
+
if (isRecord(body) && body["type"] === "BlockStatement") {
|
|
1234
|
+
const statements = body["body"];
|
|
1235
|
+
return Array.isArray(statements) ? statements : [];
|
|
1236
|
+
}
|
|
1237
|
+
return body === undefined ? [] : [body];
|
|
1238
|
+
};
|
|
1239
|
+
const stack = [root];
|
|
1240
|
+
while (stack.length > 0) {
|
|
1241
|
+
const node = stack.pop();
|
|
1242
|
+
if (Array.isArray(node)) {
|
|
1243
|
+
for (const child of node)
|
|
1244
|
+
stack.push(child);
|
|
1245
|
+
continue;
|
|
1246
|
+
}
|
|
1247
|
+
if (!isRecord(node))
|
|
1248
|
+
continue;
|
|
1249
|
+
switch (node["type"]) {
|
|
1250
|
+
case "CallExpression": {
|
|
1251
|
+
const called = nameOf(node["callee"]);
|
|
1252
|
+
const start = node["start"];
|
|
1253
|
+
const end = node["end"];
|
|
1254
|
+
// Every call site, in order, with its span.
|
|
1255
|
+
//
|
|
1256
|
+
// This was a Set, so a file calling useState thirty-four times reported
|
|
1257
|
+
// one -- and a rule whose TRIGGER counted those calls could never fire.
|
|
1258
|
+
// Order is kept because a call PATTERN is a sequence, and the span is
|
|
1259
|
+
// kept so a call can be attributed to the declaration that makes it.
|
|
1260
|
+
if (called !== undefined && typeof start === "number" && typeof end === "number") {
|
|
1261
|
+
const args = Array.isArray(node["arguments"]) ? node["arguments"] : [];
|
|
1262
|
+
const argumentKeys = [];
|
|
1263
|
+
if (args.length === 1 && isRecord(args[0]) && args[0]["type"] === "ObjectExpression") {
|
|
1264
|
+
const properties = args[0]["properties"];
|
|
1265
|
+
if (Array.isArray(properties)) {
|
|
1266
|
+
for (const property of properties) {
|
|
1267
|
+
if (!isRecord(property))
|
|
1268
|
+
continue;
|
|
1269
|
+
const key = property["key"];
|
|
1270
|
+
const name = isRecord(key)
|
|
1271
|
+
? typeof key["name"] === "string"
|
|
1272
|
+
? key["name"]
|
|
1273
|
+
: typeof key["value"] === "string"
|
|
1274
|
+
? key["value"]
|
|
1275
|
+
: undefined
|
|
1276
|
+
: undefined;
|
|
1277
|
+
if (name !== undefined)
|
|
1278
|
+
argumentKeys.push(name);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
callSites.push({ name: called, start, end, argumentCount: args.length, argumentKeys });
|
|
1283
|
+
}
|
|
1284
|
+
// A `Schema.Struct({...})` or `Schema.TaggedError<X>()("tag", {...})`
|
|
1285
|
+
// argument IS a named type. Marking the field object lets the object-shape
|
|
1286
|
+
// rule tell a declaration from a literal that needs a name.
|
|
1287
|
+
const callee = node["callee"];
|
|
1288
|
+
const inner = isRecord(callee) && callee["type"] === "CallExpression" ? nameOf(callee["callee"]) : undefined;
|
|
1289
|
+
if (called === "Schema.Struct" || inner === "Schema.TaggedError") {
|
|
1290
|
+
const args = node["arguments"];
|
|
1291
|
+
if (Array.isArray(args)) {
|
|
1292
|
+
for (const arg of args) {
|
|
1293
|
+
if (isRecord(arg) && arg["type"] === "ObjectExpression" && typeof arg["start"] === "number") {
|
|
1294
|
+
declaredObjects.set(arg["start"], declaredFieldsOf(arg));
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
// A migration is a specification: `knex.schema.createTable(T, t => {
|
|
1300
|
+
// t.uuid('col').notNullable() })`. Read statically, because the DB is the
|
|
1301
|
+
// other half of the contract and it is not at the keyboard.
|
|
1302
|
+
//
|
|
1303
|
+
// The method comes from the callee's PROPERTY, not `nameOf`: a fluent
|
|
1304
|
+
// chain like `.createTable(A, ...).createTable(B, ...)` has a CallExpression
|
|
1305
|
+
// as its callee's object, which `nameOf` does not follow.
|
|
1306
|
+
const calleeProperty = isRecord(node["callee"]) && isRecord(node["callee"]["property"]) &&
|
|
1307
|
+
typeof node["callee"]["property"]["name"] === "string"
|
|
1308
|
+
? node["callee"]["property"]["name"]
|
|
1309
|
+
: undefined;
|
|
1310
|
+
const last = calleeProperty ?? called?.split(".").at(-1);
|
|
1311
|
+
if (last === "createTable" || last === "alterTable") {
|
|
1312
|
+
const args = node["arguments"];
|
|
1313
|
+
const table = tableNameOf(Array.isArray(args) ? args[0] : undefined);
|
|
1314
|
+
const callback = Array.isArray(args) ? args[1] : undefined;
|
|
1315
|
+
if (table !== "") {
|
|
1316
|
+
for (const statement of statementsOf(callback)) {
|
|
1317
|
+
const expression = isRecord(statement) && statement["type"] === "ExpressionStatement"
|
|
1318
|
+
? statement["expression"]
|
|
1319
|
+
: statement;
|
|
1320
|
+
addColumn(table, expression);
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
break;
|
|
1325
|
+
}
|
|
1326
|
+
// A literal the author put a type ON is a named shape, whatever form the
|
|
1327
|
+
// naming takes. `satisfies T` and `as T` are checked against a type just
|
|
1328
|
+
// as an annotation is, and the object-shape rule keys on the literal's
|
|
1329
|
+
// field set, so without this a `satisfies Record<Role, Band>` looked like
|
|
1330
|
+
// a shape nobody named.
|
|
1331
|
+
case "TSSatisfiesExpression":
|
|
1332
|
+
case "TSAsExpression": {
|
|
1333
|
+
const expression = node["expression"];
|
|
1334
|
+
if (isRecord(expression) &&
|
|
1335
|
+
expression["type"] === "ObjectExpression" &&
|
|
1336
|
+
typeof expression["start"] === "number") {
|
|
1337
|
+
declaredObjects.set(expression["start"], { required: [], nullable: [], sources: {}, schemas: {} });
|
|
1338
|
+
}
|
|
1339
|
+
break;
|
|
1340
|
+
}
|
|
1341
|
+
case "VariableDeclarator": {
|
|
1342
|
+
const id = node["id"];
|
|
1343
|
+
const init = node["init"];
|
|
1344
|
+
if (isRecord(id) &&
|
|
1345
|
+
isRecord(id["typeAnnotation"]) &&
|
|
1346
|
+
isRecord(init) &&
|
|
1347
|
+
init["type"] === "ObjectExpression" &&
|
|
1348
|
+
typeof init["start"] === "number") {
|
|
1349
|
+
declaredObjects.set(init["start"], { required: [], nullable: [], sources: {}, schemas: {} });
|
|
1350
|
+
}
|
|
1351
|
+
break;
|
|
1352
|
+
}
|
|
1353
|
+
case "JSXOpeningElement":
|
|
1354
|
+
case "JSXSelfClosingElement": {
|
|
1355
|
+
const element = nameOf(node["name"]);
|
|
1356
|
+
if (element !== undefined)
|
|
1357
|
+
jsx.add(element);
|
|
1358
|
+
break;
|
|
1359
|
+
}
|
|
1360
|
+
case "ObjectExpression": {
|
|
1361
|
+
const properties = node["properties"];
|
|
1362
|
+
if (Array.isArray(properties)) {
|
|
1363
|
+
const keys = [];
|
|
1364
|
+
for (const property of properties) {
|
|
1365
|
+
if (!isRecord(property))
|
|
1366
|
+
continue;
|
|
1367
|
+
const key = property["key"];
|
|
1368
|
+
if (isRecord(key) && typeof key["name"] === "string")
|
|
1369
|
+
keys.push(key["name"]);
|
|
1370
|
+
else if (isRecord(key) && typeof key["value"] === "string")
|
|
1371
|
+
keys.push(key["value"]);
|
|
1372
|
+
}
|
|
1373
|
+
const start = node["start"];
|
|
1374
|
+
if (typeof start === "number") {
|
|
1375
|
+
const declared = declaredObjects.get(start);
|
|
1376
|
+
objects.push({
|
|
1377
|
+
keys,
|
|
1378
|
+
start,
|
|
1379
|
+
declared: declared !== undefined,
|
|
1380
|
+
required: declared?.required ?? [],
|
|
1381
|
+
nullable: declared?.nullable ?? [],
|
|
1382
|
+
sources: declared?.sources ?? {},
|
|
1383
|
+
schemas: declared?.schemas ?? {},
|
|
1384
|
+
});
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
break;
|
|
1388
|
+
}
|
|
1389
|
+
case "TemplateLiteral": {
|
|
1390
|
+
const start = node["start"];
|
|
1391
|
+
const end = node["end"];
|
|
1392
|
+
// The walker, not `nameOf`: an interpolation is often a call
|
|
1393
|
+
// (`${amount.toFixed(2)}`), and the reference that matters is inside it.
|
|
1394
|
+
if (typeof start === "number" && typeof end === "number") {
|
|
1395
|
+
stringSites.push({ start, end, refs: refsIn(node) });
|
|
1396
|
+
}
|
|
1397
|
+
break;
|
|
1398
|
+
}
|
|
1399
|
+
case "BinaryExpression": {
|
|
1400
|
+
const start = node["start"];
|
|
1401
|
+
const end = node["end"];
|
|
1402
|
+
const operator = node["operator"];
|
|
1403
|
+
// A `+` with a string literal or a template on one side builds a string.
|
|
1404
|
+
const isString = (value) => isRecord(value) &&
|
|
1405
|
+
(value["type"] === "TemplateLiteral" ||
|
|
1406
|
+
(value["type"] === "Literal" && typeof value["value"] === "string"));
|
|
1407
|
+
if (operator === "+" && (isString(node["left"]) || isString(node["right"]))) {
|
|
1408
|
+
if (typeof start === "number" && typeof end === "number") {
|
|
1409
|
+
stringSites.push({ start, end, refs: refsIn(node) });
|
|
1410
|
+
}
|
|
1411
|
+
break;
|
|
1412
|
+
}
|
|
1413
|
+
// A comparison, by its operator: `===`, `>`, `in`. Syntax, not meaning.
|
|
1414
|
+
const comparing = operator === "===" || operator === "!==" || operator === "==" || operator === "!=" ||
|
|
1415
|
+
operator === ">" || operator === "<" || operator === ">=" || operator === "<=" ||
|
|
1416
|
+
operator === "in" || operator === "instanceof";
|
|
1417
|
+
if (comparing && typeof operator === "string" && typeof start === "number" && typeof end === "number") {
|
|
1418
|
+
comparisons.push({ start, end, operator });
|
|
1419
|
+
}
|
|
1420
|
+
break;
|
|
1421
|
+
}
|
|
1422
|
+
case "ReturnStatement": {
|
|
1423
|
+
// The ARGUMENT's span, not the statement's: a type question asks about
|
|
1424
|
+
// the expression, and `getTypeAtPosition` on `return` answers nothing.
|
|
1425
|
+
const argument = node["argument"];
|
|
1426
|
+
if (isRecord(argument) && typeof argument["start"] === "number" && typeof argument["end"] === "number") {
|
|
1427
|
+
returns.push({ start: argument["start"], end: argument["end"] });
|
|
1428
|
+
}
|
|
1429
|
+
break;
|
|
1430
|
+
}
|
|
1431
|
+
case "Literal": {
|
|
1432
|
+
const start = node["start"];
|
|
1433
|
+
const end = node["end"];
|
|
1434
|
+
if (typeof node["value"] === "string" && typeof start === "number" && typeof end === "number") {
|
|
1435
|
+
literals.push({ value: node["value"], start, end });
|
|
1436
|
+
}
|
|
1437
|
+
break;
|
|
1438
|
+
}
|
|
1439
|
+
case "JSXText": {
|
|
1440
|
+
// The text between tags is its own node, not a Literal -- which is why the
|
|
1441
|
+
// review's own case, a label inside a component, was invisible at first.
|
|
1442
|
+
const start = node["start"];
|
|
1443
|
+
const end = node["end"];
|
|
1444
|
+
const raw = node["value"];
|
|
1445
|
+
if (typeof raw === "string" && typeof start === "number" && typeof end === "number") {
|
|
1446
|
+
const value = raw.trim();
|
|
1447
|
+
if (value !== "")
|
|
1448
|
+
literals.push({ value, start, end });
|
|
1449
|
+
}
|
|
1450
|
+
break;
|
|
1451
|
+
}
|
|
1452
|
+
case "ContinueStatement": {
|
|
1453
|
+
const start = node["start"];
|
|
1454
|
+
const end = node["end"];
|
|
1455
|
+
if (typeof start === "number" && typeof end === "number")
|
|
1456
|
+
skips.push({ start, end });
|
|
1457
|
+
break;
|
|
1458
|
+
}
|
|
1459
|
+
case "IfStatement": {
|
|
1460
|
+
const start = node["start"];
|
|
1461
|
+
const end = node["end"];
|
|
1462
|
+
if (typeof start === "number" && typeof end === "number") {
|
|
1463
|
+
guards.push({
|
|
1464
|
+
start,
|
|
1465
|
+
end,
|
|
1466
|
+
refs: refsIn(node["test"]),
|
|
1467
|
+
exits: exitsIn(node["consequent"]),
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
break;
|
|
1471
|
+
}
|
|
1472
|
+
default:
|
|
1473
|
+
break;
|
|
1474
|
+
}
|
|
1475
|
+
for (const value of Object.values(node)) {
|
|
1476
|
+
if (value !== null && typeof value === "object")
|
|
1477
|
+
stack.push(value);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
return { callSites, jsx: [...jsx], objects, columns, stringSites, guards, skips, literals, allOptionalFunctions, returns, comparisons };
|
|
1481
|
+
};
|
|
1482
|
+
const describeParseErrors = (errors) => {
|
|
1483
|
+
const first = errors[0];
|
|
1484
|
+
const message = typeof first === "object" &&
|
|
1485
|
+
first !== null &&
|
|
1486
|
+
typeof first["message"] === "string"
|
|
1487
|
+
? first["message"]
|
|
1488
|
+
: "the parser rejected this file";
|
|
1489
|
+
return errors.length > 1 ? message + " (and " + (errors.length - 1) + " more)" : message;
|
|
1490
|
+
};
|
|
1491
|
+
const sourceFileFrom = ({ file, text, parsed }) => {
|
|
1492
|
+
const program = parsed.program;
|
|
1493
|
+
const identifiers = collectIdentifiers(program);
|
|
1494
|
+
const comments = parsed.comments.map((comment) => ({
|
|
1495
|
+
start: comment.start,
|
|
1496
|
+
end: comment.end,
|
|
1497
|
+
value: comment.value,
|
|
1498
|
+
}));
|
|
1499
|
+
// The comment immediately above a declaration: docs/reference/names.md argues it is the
|
|
1500
|
+
// single spot an agent is guaranteed to read, so which copy carries one is
|
|
1501
|
+
// evidence about which copy to keep.
|
|
1502
|
+
const docFor = (from) => {
|
|
1503
|
+
let best;
|
|
1504
|
+
for (const comment of comments) {
|
|
1505
|
+
if (comment.end > from)
|
|
1506
|
+
continue;
|
|
1507
|
+
if (text.slice(comment.end, from).trim() !== "")
|
|
1508
|
+
continue;
|
|
1509
|
+
if (best === undefined || comment.end > best.end)
|
|
1510
|
+
best = comment;
|
|
1511
|
+
}
|
|
1512
|
+
return best?.value.replace(/^\s*\*+\s?/gm, "").trim() || undefined;
|
|
1513
|
+
};
|
|
1514
|
+
const starts = lineStarts(text);
|
|
1515
|
+
const units = [];
|
|
1516
|
+
const sites = isRecord(program) ? sitesIn(program, text) : [];
|
|
1517
|
+
if (isRecord(program)) {
|
|
1518
|
+
for (const site of sites) {
|
|
1519
|
+
const shape = normalize(text, site, identifiers, comments);
|
|
1520
|
+
const tokens = tokenize(shape);
|
|
1521
|
+
const from = locate(starts, site.start);
|
|
1522
|
+
const to = locate(starts, site.end);
|
|
1523
|
+
// Whether this declaration said anything about its types. In a `.js` file,
|
|
1524
|
+
// and in unannotated `.ts`, the answer is never. Recorded rather than
|
|
1525
|
+
// inferred later: "no types written" and "types written and empty" look the
|
|
1526
|
+
// same from outside and are not the same fact.
|
|
1527
|
+
const typeRefs = [
|
|
1528
|
+
...new Set(identifiers
|
|
1529
|
+
.filter((identifier) => identifier.siteKind === "type" &&
|
|
1530
|
+
identifier.start >= site.start &&
|
|
1531
|
+
identifier.end <= site.end)
|
|
1532
|
+
.map((identifier) => identifier.name)),
|
|
1533
|
+
];
|
|
1534
|
+
units.push({
|
|
1535
|
+
kind: site.kind,
|
|
1536
|
+
name: site.name,
|
|
1537
|
+
file,
|
|
1538
|
+
start: site.start,
|
|
1539
|
+
end: site.end,
|
|
1540
|
+
variants: site.variants,
|
|
1541
|
+
values: site.values,
|
|
1542
|
+
location: { file, line: from.line, column: from.column, endLine: to.line, endColumn: to.column },
|
|
1543
|
+
namePosition: site.namePosition,
|
|
1544
|
+
exported: site.exported,
|
|
1545
|
+
text: text.slice(site.start, site.end),
|
|
1546
|
+
shape,
|
|
1547
|
+
tokens,
|
|
1548
|
+
shingles: shinglesOf(tokens),
|
|
1549
|
+
normalizedHash: shortHash(shape),
|
|
1550
|
+
typeRefs,
|
|
1551
|
+
typed: site.typed,
|
|
1552
|
+
fieldTypes: site.fieldTypes,
|
|
1553
|
+
composed: site.bases.map((name) => ({ name, resolvedTo: "" })),
|
|
1554
|
+
calls: [],
|
|
1555
|
+
callSignature: "",
|
|
1556
|
+
test: policy.testFiles.test(file),
|
|
1557
|
+
typeSignature: "",
|
|
1558
|
+
typeFacts: undefined,
|
|
1559
|
+
doc: docFor(site.docStart),
|
|
1560
|
+
});
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
const imports = isRecord(program) ? importsIn(program) : [];
|
|
1564
|
+
const allOptionalFunctions = sites
|
|
1565
|
+
.filter((site) => site.kind === "function" && site.allParamsOptional)
|
|
1566
|
+
.map((site) => site.name);
|
|
1567
|
+
return { path: file, text, units, importStatements: imports, facts: structureIn(program, allOptionalFunctions) };
|
|
1568
|
+
};
|
|
1569
|
+
/** An `.astro` file's `Props`, named after its component; any other file unchanged. */
|
|
1570
|
+
const withComponentProps = (source) => !isAstro(source.path)
|
|
1571
|
+
? source
|
|
1572
|
+
: {
|
|
1573
|
+
...source,
|
|
1574
|
+
units: source.units.map((unit) => unit.name === "Props" && (unit.kind === "interface" || unit.kind === "type")
|
|
1575
|
+
? { ...unit, name: componentName(source.path) + "Props" }
|
|
1576
|
+
: unit),
|
|
1577
|
+
};
|
|
1578
|
+
/**
|
|
1579
|
+
* Parse a file, giving `.js` a second reading as JSX.
|
|
1580
|
+
*
|
|
1581
|
+
* One retry, and then the file is REPORTED as unparsed rather than silently
|
|
1582
|
+
* contributing nothing. The `errors` array was never read here, so a file the
|
|
1583
|
+
* parser rejected looked exactly like a file with no declarations in it -- and
|
|
1584
|
+
* every rule's view of the repository had a hole nobody could see.
|
|
1585
|
+
*/
|
|
1586
|
+
const parseSourceFile = ({ file, text }) => {
|
|
1587
|
+
const lang = langOf(file);
|
|
1588
|
+
const first = parseSync(file, parseInputOf({ file, text }), { sourceType: "module", lang });
|
|
1589
|
+
const errors = blockingErrors(file, first.errors);
|
|
1590
|
+
if (errors.length === 0)
|
|
1591
|
+
return { ok: true, file: withComponentProps(sourceFileFrom({ file, text, parsed: first })) };
|
|
1592
|
+
if (lang === "js") {
|
|
1593
|
+
const retry = parseSync(file, text, { sourceType: "module", lang: "jsx" });
|
|
1594
|
+
if (retry.errors.length === 0)
|
|
1595
|
+
return { ok: true, file: sourceFileFrom({ file, text, parsed: retry }) };
|
|
1596
|
+
}
|
|
1597
|
+
return { ok: false, reason: describeParseErrors(errors) };
|
|
1598
|
+
};
|
|
1599
|
+
/* -------------------------------------------------------------------------- */
|
|
1600
|
+
/* Loading */
|
|
1601
|
+
/* -------------------------------------------------------------------------- */
|
|
1602
|
+
const ignored = new Set(policy.ignoredDirectories);
|
|
1603
|
+
/**
|
|
1604
|
+
* Whether a name is one a transpiler emits rather than one a person wrote.
|
|
1605
|
+
*
|
|
1606
|
+
* The lists are explicit rather than "starts with `__`", because a person is
|
|
1607
|
+
* allowed to write `__proto__`-adjacent names and a filter that guesses at them
|
|
1608
|
+
* is a filter that will one day drop real code. Every entry here is a helper
|
|
1609
|
+
* TypeScript or Babel puts in a file by itself.
|
|
1610
|
+
*/
|
|
1611
|
+
const COMPILER_HELPERS = new Set([
|
|
1612
|
+
"__awaiter", "__generator", "__rest", "__spread", "__spreadArray", "__spreadArrays",
|
|
1613
|
+
"__values", "__read", "__readInt", "__assign", "__extends", "__decorate", "__metadata",
|
|
1614
|
+
"__param", "__createBinding", "__setFunctionName", "__toPrimitive", "__toPropertyKey",
|
|
1615
|
+
"__importDefault", "__importStar", "__export", "__exportStar", "__classPrivateFieldGet",
|
|
1616
|
+
"__classPrivateFieldSet", "__classPrivateFieldIn", "__addDisposableResource",
|
|
1617
|
+
"__disposeResources", "__esDecorate", "__runInitializers", "__propKey",
|
|
1618
|
+
"_interopRequireDefault", "_interopRequireWildcard", "_classCallCheck", "_defineProperty",
|
|
1619
|
+
"_toConsumableArray", "_typeof", "_extends", "_objectSpread", "_objectSpread2",
|
|
1620
|
+
"_slicedToArray", "_asyncToGenerator", "_createClass", "_getPrototypeOf", "_inherits",
|
|
1621
|
+
]);
|
|
1622
|
+
const isCompilerHelper = (name) => COMPILER_HELPERS.has(name);
|
|
1623
|
+
const extensionOf = (file) => {
|
|
1624
|
+
const cut = file.lastIndexOf(".");
|
|
1625
|
+
return cut === -1 ? "(no extension)" : file.slice(cut);
|
|
1626
|
+
};
|
|
1627
|
+
const summarise = (counts) => [...counts.entries()]
|
|
1628
|
+
.map(([extension, count]) => ({ extension, count }))
|
|
1629
|
+
.sort((left, right) => right.count - left.count || left.extension.localeCompare(right.extension));
|
|
1630
|
+
const walkLimits = { directories: 5000, files: 20000 };
|
|
1631
|
+
const depthOf = ({ root, dir, path }) => path.relative(root, dir).split(path.sep).filter((segment) => segment !== "" && segment !== ".")
|
|
1632
|
+
.length;
|
|
1633
|
+
/**
|
|
1634
|
+
* Walk a directory, honouring .gitignore as it goes.
|
|
1635
|
+
*
|
|
1636
|
+
* Rules accumulate as directories are entered. A rule only speaks for its own
|
|
1637
|
+
* directory and below it -- `isIgnored` returns early when the path is not under
|
|
1638
|
+
* the rule's base -- so a sibling's rules are inert rather than wrong, which is
|
|
1639
|
+
* what lets one list serve the whole walk.
|
|
1640
|
+
*/
|
|
1641
|
+
const walk = ({ dir, root, inherited = [] }) => Effect.gen(function* () {
|
|
1642
|
+
const fs = yield* FileSystem.FileSystem;
|
|
1643
|
+
const path = yield* Path.Path;
|
|
1644
|
+
const found = [];
|
|
1645
|
+
const skipped = new Map();
|
|
1646
|
+
let rules = inherited;
|
|
1647
|
+
let ignoredCount = 0;
|
|
1648
|
+
let ignoredDirs = 0;
|
|
1649
|
+
const stack = [];
|
|
1650
|
+
const seen = new Set();
|
|
1651
|
+
// The path the caller named must be readable; anything discovered below it
|
|
1652
|
+
// is best effort. One odd entry must not fail the whole run.
|
|
1653
|
+
const rootEntries = yield* fs.readDirectory(dir).pipe(Effect.mapError((cause) => WorkspaceError.make({ path: dir, operation: "readDirectory", cause })));
|
|
1654
|
+
seen.add(dir);
|
|
1655
|
+
for (const entry of rootEntries) {
|
|
1656
|
+
if (!ignored.has(entry))
|
|
1657
|
+
stack.push(path.join(dir, entry));
|
|
1658
|
+
}
|
|
1659
|
+
while (stack.length > 0 && seen.size < walkLimits.directories && found.length < walkLimits.files) {
|
|
1660
|
+
const current = stack.pop();
|
|
1661
|
+
if (current === undefined)
|
|
1662
|
+
break;
|
|
1663
|
+
// stat, not the name: a zero-byte file called "tsx" is not a directory.
|
|
1664
|
+
const info = yield* Effect.orElseSucceed(fs.stat(current), () => undefined);
|
|
1665
|
+
if (info === undefined)
|
|
1666
|
+
continue;
|
|
1667
|
+
// Ignored first: a generated .js file is both non-source and ignored, and
|
|
1668
|
+
// "we were told not to look" is the more useful of the two reasons.
|
|
1669
|
+
if (rules.length > 0 && isIgnored(rules, current, path)) {
|
|
1670
|
+
if (info.type === "File")
|
|
1671
|
+
ignoredCount += 1;
|
|
1672
|
+
else
|
|
1673
|
+
ignoredDirs += 1;
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
if (info.type === "File") {
|
|
1677
|
+
if (looksLikeSource(current)) {
|
|
1678
|
+
found.push(current);
|
|
1679
|
+
}
|
|
1680
|
+
else {
|
|
1681
|
+
const extension = extensionOf(current);
|
|
1682
|
+
skipped.set(extension, (skipped.get(extension) ?? 0) + 1);
|
|
1683
|
+
}
|
|
1684
|
+
continue;
|
|
1685
|
+
}
|
|
1686
|
+
if (info.type !== "Directory")
|
|
1687
|
+
continue;
|
|
1688
|
+
if (seen.has(current))
|
|
1689
|
+
continue;
|
|
1690
|
+
seen.add(current);
|
|
1691
|
+
// A directory's own .gitignore applies to what is inside it, so it is
|
|
1692
|
+
// read before its children are pushed.
|
|
1693
|
+
const here = yield* rulesAt(current, depthOf({ root, dir: current, path }));
|
|
1694
|
+
if (here.length > 0)
|
|
1695
|
+
rules = orderRules([...rules, ...here]);
|
|
1696
|
+
const entries = yield* Effect.orElseSucceed(fs.readDirectory(current), () => []);
|
|
1697
|
+
for (const entry of entries) {
|
|
1698
|
+
if (!ignored.has(entry))
|
|
1699
|
+
stack.push(path.join(current, entry));
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
// Both limits are reported, not silently obeyed: reaching one means the run
|
|
1703
|
+
// analysed part of the tree and said nothing about the rest.
|
|
1704
|
+
return {
|
|
1705
|
+
files: found.sort(Order.String),
|
|
1706
|
+
skippedExtensions: summarise(skipped),
|
|
1707
|
+
truncated: found.length >= walkLimits.files || seen.size >= walkLimits.directories,
|
|
1708
|
+
ignored: ignoredCount,
|
|
1709
|
+
ignoredDirectories: ignoredDirs,
|
|
1710
|
+
};
|
|
1711
|
+
});
|
|
1712
|
+
const resolveInputs = (root, inputs) => Effect.gen(function* () {
|
|
1713
|
+
const path = yield* Path.Path;
|
|
1714
|
+
const files = [];
|
|
1715
|
+
const skipped = new Map();
|
|
1716
|
+
let truncated = false;
|
|
1717
|
+
let ignored = 0;
|
|
1718
|
+
let ignoredDirectories = 0;
|
|
1719
|
+
for (const input of inputs) {
|
|
1720
|
+
const absolute = path.isAbsolute(input) ? input : path.join(root, input);
|
|
1721
|
+
if (looksLikeSource(absolute)) {
|
|
1722
|
+
files.push(absolute);
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
// A .gitignore above the walked directory still applies to it, so the
|
|
1726
|
+
// chain from the root down to (but not including) the input is loaded
|
|
1727
|
+
// first.
|
|
1728
|
+
const chain = [...(yield* rulesAt(root, 0))];
|
|
1729
|
+
const segments = path
|
|
1730
|
+
.relative(root, absolute)
|
|
1731
|
+
.split(path.sep)
|
|
1732
|
+
.filter((segment) => segment !== "" && segment !== ".");
|
|
1733
|
+
let cursor = root;
|
|
1734
|
+
for (const segment of segments.slice(0, -1)) {
|
|
1735
|
+
cursor = path.join(cursor, segment);
|
|
1736
|
+
chain.push(...(yield* rulesAt(cursor, path.relative(root, cursor).split(path.sep).length)));
|
|
1737
|
+
}
|
|
1738
|
+
const nested = yield* walk({ dir: absolute, root, inherited: orderRules(chain) });
|
|
1739
|
+
for (const file of nested.files)
|
|
1740
|
+
files.push(file);
|
|
1741
|
+
truncated = truncated || nested.truncated;
|
|
1742
|
+
ignored += nested.ignored;
|
|
1743
|
+
ignoredDirectories += nested.ignoredDirectories;
|
|
1744
|
+
for (const entry of nested.skippedExtensions) {
|
|
1745
|
+
skipped.set(entry.extension, (skipped.get(entry.extension) ?? 0) + entry.count);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
return { files, skippedExtensions: summarise(skipped), truncated, ignored, ignoredDirectories };
|
|
1749
|
+
});
|
|
1750
|
+
/**
|
|
1751
|
+
* The files this run will look at, before any of them are read.
|
|
1752
|
+
*
|
|
1753
|
+
* Separated from `loadWorkspace` so a caller can hash the inputs and decide
|
|
1754
|
+
* whether the analysis is worth doing at all: an unchanged repository should
|
|
1755
|
+
* cost a read, not a parse.
|
|
1756
|
+
*/
|
|
1757
|
+
export const discoverFiles = (root, inputs, discovered) => discovered !== undefined
|
|
1758
|
+
? Effect.gen(function* () {
|
|
1759
|
+
const path = yield* Path.Path;
|
|
1760
|
+
const inScope = yield* tsconfigScope(root);
|
|
1761
|
+
const walked = yield* resolveInputs(root, ["."]);
|
|
1762
|
+
const astro = walked.files.filter((file) => isAstro(file) && inScope(path.relative(root, file).split(path.sep).join("/")));
|
|
1763
|
+
return {
|
|
1764
|
+
files: [...discovered, ...astro].sort(Order.String),
|
|
1765
|
+
skippedExtensions: [],
|
|
1766
|
+
truncated: walked.truncated,
|
|
1767
|
+
ignored: 0,
|
|
1768
|
+
ignoredDirectories: 0,
|
|
1769
|
+
};
|
|
1770
|
+
})
|
|
1771
|
+
: resolveInputs(root, inputs.length > 0 ? inputs : ["."]);
|
|
1772
|
+
/** A parse cache that always misses, for a caller with no cache to give it. */
|
|
1773
|
+
export const noParses = () => ({
|
|
1774
|
+
get: () => undefined,
|
|
1775
|
+
set: () => undefined,
|
|
1776
|
+
hits: () => 0,
|
|
1777
|
+
misses: () => 0,
|
|
1778
|
+
});
|
|
1779
|
+
/**
|
|
1780
|
+
* Attach the checker's answer to each declaration, by name offset.
|
|
1781
|
+
*
|
|
1782
|
+
* Separate from {@link loadWorkspace} because the request list is built from the
|
|
1783
|
+
* units, and the answer needs a program: load, ask, apply. Mutating the units is
|
|
1784
|
+
* the same shape the resolution pass above already uses, and it keeps every rule
|
|
1785
|
+
* reading one object instead of a parallel index.
|
|
1786
|
+
*
|
|
1787
|
+
* @param workspace - the loaded workspace.
|
|
1788
|
+
* @param facts - resolved types keyed by {@link declarationKey}.
|
|
1789
|
+
* @returns how many declarations got a type.
|
|
1790
|
+
*/
|
|
1791
|
+
export const applyDeclarationTypes = (workspace, facts) => {
|
|
1792
|
+
let applied = 0;
|
|
1793
|
+
for (const unit of workspace.units) {
|
|
1794
|
+
const found = facts.get(unit.file + "\u0000" + String(unit.namePosition));
|
|
1795
|
+
unit.typeFacts = found;
|
|
1796
|
+
if (found !== undefined)
|
|
1797
|
+
applied += 1;
|
|
1798
|
+
}
|
|
1799
|
+
return applied;
|
|
1800
|
+
};
|
|
1801
|
+
/**
|
|
1802
|
+
* Read and index what tsgo sees, or the given paths, into the workspace every
|
|
1803
|
+
* rule reads.
|
|
1804
|
+
*/
|
|
1805
|
+
export const loadWorkspace = (root, inputs, input = {}) => Effect.gen(function* () {
|
|
1806
|
+
const fs = yield* FileSystem.FileSystem;
|
|
1807
|
+
const path = yield* Path.Path;
|
|
1808
|
+
const parses = input.parses ?? noParses();
|
|
1809
|
+
const types = input.types ?? emptyTypeIndex;
|
|
1810
|
+
const files = input.discovered ?? (yield* resolveInputs(root, inputs.length > 0 ? inputs : ["."])).files;
|
|
1811
|
+
// Where the time went, in the same shape the report already uses for rules.
|
|
1812
|
+
// Adding this is how I learned that the parser was never the expensive part
|
|
1813
|
+
// and that two guesses about the bottleneck were both wrong.
|
|
1814
|
+
const phases = [];
|
|
1815
|
+
const mark = (phase, since, now) => {
|
|
1816
|
+
phases.push({ phase, ms: now - since });
|
|
1817
|
+
return now;
|
|
1818
|
+
};
|
|
1819
|
+
let clock = yield* Clock.currentTimeMillis;
|
|
1820
|
+
const parsed = [];
|
|
1821
|
+
const unparsed = [];
|
|
1822
|
+
for (const absolute of files) {
|
|
1823
|
+
const preloaded = input.contents?.get(absolute);
|
|
1824
|
+
const text = preloaded ??
|
|
1825
|
+
(yield* fs.readFileString(absolute).pipe(Effect.mapError((cause) => WorkspaceError.make({ path: absolute, operation: "readFileString", cause }))));
|
|
1826
|
+
// Diagnostics carry paths relative to the root, so output is stable and
|
|
1827
|
+
// hosts such as GitHub Actions can annotate the right file.
|
|
1828
|
+
const relative = path.relative(root, absolute);
|
|
1829
|
+
const cached = parses.get(relative, text);
|
|
1830
|
+
if (cached !== undefined) {
|
|
1831
|
+
parsed.push(cached);
|
|
1832
|
+
continue;
|
|
1833
|
+
}
|
|
1834
|
+
const outcome = parseSourceFile({ file: relative, text });
|
|
1835
|
+
if (outcome.ok) {
|
|
1836
|
+
parsed.push(outcome.file);
|
|
1837
|
+
parses.set(relative, text, outcome.file);
|
|
1838
|
+
}
|
|
1839
|
+
else {
|
|
1840
|
+
unparsed.push({ path: relative, reason: outcome.reason });
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
clock = mark("read+parse", clock, yield* Clock.currentTimeMillis);
|
|
1844
|
+
const graph = buildImportGraph(parsed, path);
|
|
1845
|
+
clock = mark("import-graph", clock, yield* Clock.currentTimeMillis);
|
|
1846
|
+
// Resolution happens here, not at parse time, because it needs the whole
|
|
1847
|
+
// file set: a type name only means something once we know where it came from.
|
|
1848
|
+
// Edges indexed by the file that WRITES them.
|
|
1849
|
+
//
|
|
1850
|
+
// Resolving a type name used to scan the whole edge list once per type
|
|
1851
|
+
// reference, and the first loop it tried was `importersOf.get(file)` filtered
|
|
1852
|
+
// by `edge.importer === file` -- which only ever matches a file importing itself,
|
|
1853
|
+
// so it missed on every call and the second loop did all the work.
|
|
1854
|
+
//
|
|
1855
|
+
// On one repository that was 10,674 type references against 8,518 edges:
|
|
1856
|
+
// ninety million comparisons to answer a question each file already knew the
|
|
1857
|
+
// answer to. It was 4.4 seconds of a 4.6 second run, and I had assumed the
|
|
1858
|
+
// PARSER was the expensive part. It never was.
|
|
1859
|
+
const outward = new Map();
|
|
1860
|
+
for (const edge of graph.edges) {
|
|
1861
|
+
if (edge.resolution === "unresolved")
|
|
1862
|
+
continue;
|
|
1863
|
+
const existing = outward.get(edge.importer);
|
|
1864
|
+
if (existing === undefined)
|
|
1865
|
+
outward.set(edge.importer, [edge]);
|
|
1866
|
+
else
|
|
1867
|
+
existing.push(edge);
|
|
1868
|
+
}
|
|
1869
|
+
const resolveRef = (file, name) => {
|
|
1870
|
+
for (const edge of outward.get(file) ?? []) {
|
|
1871
|
+
if (edge.names.includes(name))
|
|
1872
|
+
return `${edge.to}#${name}`;
|
|
1873
|
+
}
|
|
1874
|
+
return `${file}#${name}`;
|
|
1875
|
+
};
|
|
1876
|
+
parsed.forEach((file) => {
|
|
1877
|
+
file.units.forEach((unit) => {
|
|
1878
|
+
// Calls made inside this declaration's span, in the order they are made,
|
|
1879
|
+
// resolved to the declaration they reach. `resolveRef` is the same
|
|
1880
|
+
// function the type pass uses: a name is a name, whether it appears in
|
|
1881
|
+
// type position or before a pair of parentheses.
|
|
1882
|
+
unit.calls = file.facts.callSites
|
|
1883
|
+
.filter((site) => site.start >= unit.start && site.end <= unit.end)
|
|
1884
|
+
.map((site) => resolveRef(file.path, site.name));
|
|
1885
|
+
unit.callSignature = unit.calls.join("|");
|
|
1886
|
+
// The types this declaration composes, resolved the same way. A field
|
|
1887
|
+
// set is only complete once `extends`/`&` bases are followed.
|
|
1888
|
+
unit.composed = unit.composed.map((base) => ({
|
|
1889
|
+
name: base.name,
|
|
1890
|
+
resolvedTo: resolveRef(file.path, base.name),
|
|
1891
|
+
}));
|
|
1892
|
+
unit.typeSignature = unit.typeRefs
|
|
1893
|
+
.map((name) => resolveRef(file.path, name))
|
|
1894
|
+
.sort(Order.String)
|
|
1895
|
+
.join("|");
|
|
1896
|
+
// `typeFacts` is filled by the checker pass, after this one: it is a
|
|
1897
|
+
// lookup per declaration NAME, and the name offset is on the unit.
|
|
1898
|
+
});
|
|
1899
|
+
});
|
|
1900
|
+
// Declarations from test files and from transpiler helpers are excluded
|
|
1901
|
+
// before any rule sees them, and counted, because a rule that silently
|
|
1902
|
+
// receives fewer candidates is a rule nobody can debug.
|
|
1903
|
+
// The nearest package.json above each directory an analysed file sits in.
|
|
1904
|
+
// Bounded to a few levels and cached per directory, so this is a handful of
|
|
1905
|
+
// reads rather than one per file.
|
|
1906
|
+
const manifests = new Map();
|
|
1907
|
+
const directories = [
|
|
1908
|
+
...new Set(parsed.map((file) => {
|
|
1909
|
+
const cut = file.path.lastIndexOf("/");
|
|
1910
|
+
return cut === -1 ? "." : file.path.slice(0, cut);
|
|
1911
|
+
})),
|
|
1912
|
+
];
|
|
1913
|
+
for (const directory of directories) {
|
|
1914
|
+
let cursor = directory;
|
|
1915
|
+
// Bounded by the path itself rather than by a constant. Six levels is
|
|
1916
|
+
// nothing in a monorepo: a file eight directories deep sits above its own
|
|
1917
|
+
// manifest, and was grouped under a different name than its siblings.
|
|
1918
|
+
for (let depth = 0; depth <= directory.split("/").length + 1; depth += 1) {
|
|
1919
|
+
// The root has a manifest too, and it is the one that governs a file
|
|
1920
|
+
// sitting at the top of the repository. Breaking before reading it made
|
|
1921
|
+
// every dependency of `app.ts` and `index.ts` look undeclared.
|
|
1922
|
+
const manifest = yield* manifestAt(cursor === "." ? root : path.join(root, cursor));
|
|
1923
|
+
if (manifest !== undefined) {
|
|
1924
|
+
manifests.set(directory, manifest);
|
|
1925
|
+
break;
|
|
1926
|
+
}
|
|
1927
|
+
const parent = cursor.includes("/") ? cursor.slice(0, cursor.lastIndexOf("/")) : ".";
|
|
1928
|
+
if (parent === cursor || cursor === ".")
|
|
1929
|
+
break;
|
|
1930
|
+
cursor = parent;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
clock = mark("resolve-types", clock, yield* Clock.currentTimeMillis);
|
|
1934
|
+
const allUnits = parsed.flatMap((file) => file.units);
|
|
1935
|
+
// Helpers go; test declarations stay and are marked, because whether they
|
|
1936
|
+
// should be compared depends on what they are compared AGAINST.
|
|
1937
|
+
const units = allUnits.filter((unit) => !isCompilerHelper(unit.name));
|
|
1938
|
+
clock = mark("units+manifests", clock, yield* Clock.currentTimeMillis);
|
|
1939
|
+
const testDeclarations = units.filter((unit) => unit.test).length;
|
|
1940
|
+
const excludedHelpers = allUnits.filter((unit) => isCompilerHelper(unit.name)).length;
|
|
1941
|
+
const byName = new Map();
|
|
1942
|
+
for (const unit of units) {
|
|
1943
|
+
const existing = byName.get(unit.name);
|
|
1944
|
+
if (existing === undefined)
|
|
1945
|
+
byName.set(unit.name, [unit]);
|
|
1946
|
+
else
|
|
1947
|
+
existing.push(unit);
|
|
1948
|
+
}
|
|
1949
|
+
return {
|
|
1950
|
+
root,
|
|
1951
|
+
files: parsed,
|
|
1952
|
+
units,
|
|
1953
|
+
byName,
|
|
1954
|
+
imports: graph,
|
|
1955
|
+
phases,
|
|
1956
|
+
parses,
|
|
1957
|
+
unparsed,
|
|
1958
|
+
manifests,
|
|
1959
|
+
testDeclarations,
|
|
1960
|
+
excludedHelpers,
|
|
1961
|
+
types,
|
|
1962
|
+
};
|
|
1963
|
+
});
|