@nifrajs/cli 5.1.0 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3313 -0
- package/README.md +3 -0
- package/dist/agent-files.d.ts +19 -11
- package/dist/agent-files.d.ts.map +1 -1
- package/dist/agent-files.js +26 -18
- package/dist/agent-files.js.map +1 -1
- package/dist/assure-hydration.d.ts.map +1 -1
- package/dist/assure-hydration.js +7 -2
- package/dist/assure-hydration.js.map +1 -1
- package/dist/assure.d.ts.map +1 -1
- package/dist/assure.js +14 -1
- package/dist/assure.js.map +1 -1
- package/dist/capabilities-tool.d.ts.map +1 -1
- package/dist/capabilities-tool.js +131 -45
- package/dist/capabilities-tool.js.map +1 -1
- package/dist/check-diagnostics.d.ts +5 -0
- package/dist/check-diagnostics.d.ts.map +1 -1
- package/dist/check-diagnostics.js +5 -0
- package/dist/check-diagnostics.js.map +1 -1
- package/dist/check-scan.d.ts +9 -3
- package/dist/check-scan.d.ts.map +1 -1
- package/dist/check-scan.js +61 -21
- package/dist/check-scan.js.map +1 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +55 -15
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +37 -18
- package/dist/cli.js.map +1 -1
- package/dist/command-catalog.d.ts.map +1 -1
- package/dist/command-catalog.js +9 -5
- package/dist/command-catalog.js.map +1 -1
- package/dist/db-tool.d.ts.map +1 -1
- package/dist/db-tool.js +2 -1
- package/dist/db-tool.js.map +1 -1
- package/dist/dev-bun-config.d.ts +6 -3
- package/dist/dev-bun-config.d.ts.map +1 -1
- package/dist/dev-bun-config.js +13 -10
- package/dist/dev-bun-config.js.map +1 -1
- package/dist/dev-feed-tool.d.ts +3 -2
- package/dist/dev-feed-tool.d.ts.map +1 -1
- package/dist/dev-feed-tool.js +26 -5
- package/dist/dev-feed-tool.js.map +1 -1
- package/dist/doctor.d.ts +15 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +79 -61
- package/dist/doctor.js.map +1 -1
- package/dist/env-file.d.ts.map +1 -1
- package/dist/env-file.js +41 -8
- package/dist/env-file.js.map +1 -1
- package/dist/i18n-check.d.ts +11 -1
- package/dist/i18n-check.d.ts.map +1 -1
- package/dist/i18n-check.js +170 -17
- package/dist/i18n-check.js.map +1 -1
- package/dist/init-agents.d.ts +46 -6
- package/dist/init-agents.d.ts.map +1 -1
- package/dist/init-agents.js +193 -31
- package/dist/init-agents.js.map +1 -1
- package/dist/internal/project-specifiers.d.ts.map +1 -1
- package/dist/internal/project-specifiers.js +9 -1
- package/dist/internal/project-specifiers.js.map +1 -1
- package/dist/internal/source-facts.d.ts +22 -0
- package/dist/internal/source-facts.d.ts.map +1 -1
- package/dist/internal/source-facts.js +322 -0
- package/dist/internal/source-facts.js.map +1 -1
- package/dist/internal/workspace-packages.d.ts +22 -0
- package/dist/internal/workspace-packages.d.ts.map +1 -0
- package/dist/internal/workspace-packages.js +215 -0
- package/dist/internal/workspace-packages.js.map +1 -0
- package/dist/load.d.ts +7 -2
- package/dist/load.d.ts.map +1 -1
- package/dist/load.js +11 -2
- package/dist/load.js.map +1 -1
- package/dist/lock-links.d.ts +42 -0
- package/dist/lock-links.d.ts.map +1 -0
- package/dist/lock-links.js +144 -0
- package/dist/lock-links.js.map +1 -0
- package/dist/mcp-exec.d.ts +1 -0
- package/dist/mcp-exec.d.ts.map +1 -1
- package/dist/mcp-exec.js +16 -5
- package/dist/mcp-exec.js.map +1 -1
- package/dist/mcp-http.js +1 -1
- package/dist/mcp-io.d.ts +23 -3
- package/dist/mcp-io.d.ts.map +1 -1
- package/dist/mcp-io.js +59 -12
- package/dist/mcp-io.js.map +1 -1
- package/dist/mcp-isolate.d.ts.map +1 -1
- package/dist/mcp-isolate.js +2 -1
- package/dist/mcp-isolate.js.map +1 -1
- package/dist/mcp-root.d.ts +6 -4
- package/dist/mcp-root.d.ts.map +1 -1
- package/dist/mcp-root.js +12 -8
- package/dist/mcp-root.js.map +1 -1
- package/dist/migrate-layout.d.ts +2 -1
- package/dist/migrate-layout.d.ts.map +1 -1
- package/dist/migrate-layout.js +435 -29
- package/dist/migrate-layout.js.map +1 -1
- package/dist/project-site.d.ts +7 -0
- package/dist/project-site.d.ts.map +1 -0
- package/dist/project-site.js +19 -0
- package/dist/project-site.js.map +1 -0
- package/dist/recipes/4.0.0.d.ts.map +1 -1
- package/dist/recipes/4.0.0.js +2 -0
- package/dist/recipes/4.0.0.js.map +1 -1
- package/dist/recipes/5.1.0.d.ts +9 -0
- package/dist/recipes/5.1.0.d.ts.map +1 -0
- package/dist/recipes/5.1.0.js +22 -0
- package/dist/recipes/5.1.0.js.map +1 -0
- package/dist/recipes/5.2.0.d.ts +10 -0
- package/dist/recipes/5.2.0.d.ts.map +1 -0
- package/dist/recipes/5.2.0.js +32 -0
- package/dist/recipes/5.2.0.js.map +1 -0
- package/dist/recipes/5.3.0.d.ts +11 -0
- package/dist/recipes/5.3.0.d.ts.map +1 -0
- package/dist/recipes/5.3.0.js +35 -0
- package/dist/recipes/5.3.0.js.map +1 -0
- package/dist/recipes/index.d.ts +3 -0
- package/dist/recipes/index.d.ts.map +1 -1
- package/dist/recipes/index.js +9 -0
- package/dist/recipes/index.js.map +1 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +4 -0
- package/dist/review.js.map +1 -1
- package/dist/rule-explain.d.ts +26 -0
- package/dist/rule-explain.d.ts.map +1 -0
- package/dist/rule-explain.js +49 -0
- package/dist/rule-explain.js.map +1 -0
- package/dist/rules/codes.d.ts +4 -0
- package/dist/rules/codes.d.ts.map +1 -1
- package/dist/rules/codes.js +4 -0
- package/dist/rules/codes.js.map +1 -1
- package/dist/rules/contract-visibility.d.ts.map +1 -1
- package/dist/rules/contract-visibility.js +93 -21
- package/dist/rules/contract-visibility.js.map +1 -1
- package/dist/rules/explanations.d.ts +14 -0
- package/dist/rules/explanations.d.ts.map +1 -0
- package/dist/rules/explanations.js +326 -0
- package/dist/rules/explanations.js.map +1 -0
- package/dist/rules/legacy.d.ts.map +1 -1
- package/dist/rules/legacy.js +24 -1
- package/dist/rules/legacy.js.map +1 -1
- package/dist/rules/merge-auth.d.ts +12 -0
- package/dist/rules/merge-auth.d.ts.map +1 -0
- package/dist/rules/merge-auth.js +52 -0
- package/dist/rules/merge-auth.js.map +1 -0
- package/dist/rules/pages.d.ts +51 -0
- package/dist/rules/pages.d.ts.map +1 -1
- package/dist/rules/pages.js +293 -2
- package/dist/rules/pages.js.map +1 -1
- package/dist/rules/route-statement.d.ts +10 -0
- package/dist/rules/route-statement.d.ts.map +1 -0
- package/dist/rules/route-statement.js +150 -0
- package/dist/rules/route-statement.js.map +1 -0
- package/dist/rules/routes.d.ts +3 -2
- package/dist/rules/routes.d.ts.map +1 -1
- package/dist/rules/routes.js +4 -3
- package/dist/rules/routes.js.map +1 -1
- package/dist/rules/zones.d.ts.map +1 -1
- package/dist/rules/zones.js +3 -2
- package/dist/rules/zones.js.map +1 -1
- package/dist/typecheck-coverage.d.ts +11 -0
- package/dist/typecheck-coverage.d.ts.map +1 -1
- package/dist/typecheck-coverage.js +50 -7
- package/dist/typecheck-coverage.js.map +1 -1
- package/dist/upgrade.d.ts +80 -11
- package/dist/upgrade.d.ts.map +1 -1
- package/dist/upgrade.js +332 -65
- package/dist/upgrade.js.map +1 -1
- package/dist/verification-plan.d.ts.map +1 -1
- package/dist/verification-plan.js +2 -0
- package/dist/verification-plan.js.map +1 -1
- package/dist/version-drift.d.ts.map +1 -1
- package/dist/version-drift.js +12 -1
- package/dist/version-drift.js.map +1 -1
- package/dist/web-app-options.d.ts +1 -1
- package/dist/web-app-options.d.ts.map +1 -1
- package/dist/web-app-options.js.map +1 -1
- package/docs/examples.json +19 -1
- package/docs/llms-full.txt +716 -138
- package/docs/types.json +516 -74
- package/package.json +13 -12
- package/src/agent-files.ts +27 -17
- package/src/assure-hydration.ts +8 -1
- package/src/assure.ts +11 -1
- package/src/capabilities-tool.ts +159 -50
- package/src/check-diagnostics.ts +10 -0
- package/src/check-scan.ts +64 -19
- package/src/check.ts +75 -15
- package/src/cli.ts +38 -17
- package/src/command-catalog.ts +16 -8
- package/src/db-tool.ts +2 -0
- package/src/dev-bun-config.ts +13 -9
- package/src/dev-feed-tool.ts +40 -6
- package/src/doctor.ts +102 -66
- package/src/env-file.ts +42 -7
- package/src/i18n-check.ts +254 -18
- package/src/init-agents.ts +221 -41
- package/src/internal/project-specifiers.ts +9 -1
- package/src/internal/source-facts.ts +397 -0
- package/src/internal/workspace-packages.ts +218 -0
- package/src/load.ts +19 -3
- package/src/lock-links.ts +173 -0
- package/src/mcp-exec.ts +18 -4
- package/src/mcp-http.ts +1 -1
- package/src/mcp-io.ts +72 -12
- package/src/mcp-isolate.ts +2 -0
- package/src/mcp-root.ts +14 -8
- package/src/migrate-layout.ts +496 -31
- package/src/project-site.ts +18 -0
- package/src/recipes/4.0.0.ts +2 -0
- package/src/recipes/5.1.0.ts +23 -0
- package/src/recipes/5.2.0.ts +33 -0
- package/src/recipes/5.3.0.ts +36 -0
- package/src/recipes/index.ts +9 -0
- package/src/review.ts +4 -0
- package/src/rule-explain.ts +75 -0
- package/src/rules/codes.ts +4 -0
- package/src/rules/contract-visibility.ts +118 -20
- package/src/rules/explanations.ts +339 -0
- package/src/rules/legacy.ts +24 -1
- package/src/rules/merge-auth.ts +55 -0
- package/src/rules/pages.ts +381 -2
- package/src/rules/route-statement.ts +170 -0
- package/src/rules/routes.ts +4 -3
- package/src/rules/zones.ts +3 -2
- package/src/typecheck-coverage.ts +51 -6
- package/src/upgrade.ts +430 -70
- package/src/verification-plan.ts +6 -0
- package/src/version-drift.ts +15 -0
- package/src/web-app-options.ts +9 -1
|
@@ -7,6 +7,18 @@ import type { TypeScriptApi } from "./typescript-import.ts"
|
|
|
7
7
|
* Program: a rule asks for a file only after its lexical fast path found a candidate. That keeps
|
|
8
8
|
* cold checks and projects without a TypeScript install on the existing conservative path.
|
|
9
9
|
*/
|
|
10
|
+
/** What {@link SourceFacts.untypedResponseRoutes} finds in one file. */
|
|
11
|
+
export interface UntypedResponseRoutes {
|
|
12
|
+
/** Whether `position` lies in one of those handlers. */
|
|
13
|
+
isUntypedAt(position: number): boolean
|
|
14
|
+
/**
|
|
15
|
+
* Positions of their returned raw `Response` values that the lexical pattern cannot see: a local
|
|
16
|
+
* `const` holding one, the context's `json()`/`text()` from a registered handler, or a call to an
|
|
17
|
+
* imported function `importReturnsResponse` answers for.
|
|
18
|
+
*/
|
|
19
|
+
readonly indirectReturns: readonly number[]
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
export interface SourceFacts {
|
|
11
23
|
parse(file: string, content: string): TSApi.SourceFile | undefined
|
|
12
24
|
isValueImportAt(
|
|
@@ -36,6 +48,20 @@ export interface SourceFacts {
|
|
|
36
48
|
path: string,
|
|
37
49
|
): boolean | undefined
|
|
38
50
|
isResponseSyntaxAt(source: TSApi.SourceFile, position: number): boolean | undefined
|
|
51
|
+
/**
|
|
52
|
+
* The route handlers of `source` that return no plain value, so a raw `Response` they return leaves
|
|
53
|
+
* the typed client nothing to infer. A route handler is a function passed to `.get("/path", …)` and
|
|
54
|
+
* the other method registrations, a function value of an `implement(contract, {...})` map, a local
|
|
55
|
+
* function either one names, or a local function a handler returns a call to (one hop). A handler
|
|
56
|
+
* that also returns a plain value keeps the route typed (the client drops a bare `Response` from the
|
|
57
|
+
* union), and so does a registration that declares `{ response: … }`.
|
|
58
|
+
*/
|
|
59
|
+
untypedResponseRoutes(
|
|
60
|
+
source: TSApi.SourceFile,
|
|
61
|
+
importReturnsResponse?: (specifier: string, name: string) => boolean,
|
|
62
|
+
): UntypedResponseRoutes
|
|
63
|
+
/** Whether the function `source` exports as `name` returns a raw `Response` on some path. */
|
|
64
|
+
exportReturnsResponse(source: TSApi.SourceFile, name: string): boolean
|
|
39
65
|
/** Whether the `import` keyword at `position` starts a runtime `import(specifier)` call (true) or a
|
|
40
66
|
* type-position `import("…")` such as `typeof import("x")` (false); undefined when neither. */
|
|
41
67
|
isDynamicImportAt(
|
|
@@ -82,6 +108,288 @@ function rawResponseExpression(ts: TypeScriptApi, node: TSApi.Expression): boole
|
|
|
82
108
|
)
|
|
83
109
|
}
|
|
84
110
|
|
|
111
|
+
const REGISTRATION_METHODS: ReadonlySet<string> = new Set([
|
|
112
|
+
"get",
|
|
113
|
+
"post",
|
|
114
|
+
"put",
|
|
115
|
+
"patch",
|
|
116
|
+
"delete",
|
|
117
|
+
"head",
|
|
118
|
+
"options",
|
|
119
|
+
"all",
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
function unwrapExpression(ts: TypeScriptApi, node: TSApi.Expression): TSApi.Expression {
|
|
123
|
+
let current = node
|
|
124
|
+
while (
|
|
125
|
+
ts.isParenthesizedExpression(current) ||
|
|
126
|
+
ts.isAsExpression(current) ||
|
|
127
|
+
ts.isSatisfiesExpression(current) ||
|
|
128
|
+
ts.isAwaitExpression(current)
|
|
129
|
+
)
|
|
130
|
+
current = current.expression
|
|
131
|
+
return current
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type FunctionNode =
|
|
135
|
+
| TSApi.ArrowFunction
|
|
136
|
+
| TSApi.FunctionExpression
|
|
137
|
+
| TSApi.FunctionDeclaration
|
|
138
|
+
| TSApi.MethodDeclaration
|
|
139
|
+
|
|
140
|
+
function isFunctionValue(ts: TypeScriptApi, node: TSApi.Node): node is FunctionNode {
|
|
141
|
+
return ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isMethodDeclaration(node)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Local functions by name; a name declared twice keeps both, which only ever adds findings. */
|
|
145
|
+
function localFunctions(
|
|
146
|
+
ts: TypeScriptApi,
|
|
147
|
+
source: TSApi.SourceFile,
|
|
148
|
+
): ReadonlyMap<string, readonly FunctionNode[]> {
|
|
149
|
+
const locals = new Map<string, FunctionNode[]>()
|
|
150
|
+
const declare = (name: string, fn: FunctionNode): void => {
|
|
151
|
+
locals.set(name, [...(locals.get(name) ?? []), fn])
|
|
152
|
+
}
|
|
153
|
+
const collect = (node: TSApi.Node): void => {
|
|
154
|
+
if (ts.isFunctionDeclaration(node) && node.name !== undefined) declare(node.name.text, node)
|
|
155
|
+
else if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
156
|
+
const value = unwrapExpression(ts, node.initializer)
|
|
157
|
+
if (isFunctionValue(ts, value)) declare(node.name.text, value)
|
|
158
|
+
}
|
|
159
|
+
ts.forEachChild(node, collect)
|
|
160
|
+
}
|
|
161
|
+
collect(source)
|
|
162
|
+
return locals
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The values `fn` itself returns: a concise arrow body, or each `return` outside nested functions. */
|
|
166
|
+
function returnedValues(ts: TypeScriptApi, fn: FunctionNode): readonly TSApi.Expression[] {
|
|
167
|
+
const body = fn.body
|
|
168
|
+
if (body === undefined) return []
|
|
169
|
+
if (!ts.isBlock(body)) return [body]
|
|
170
|
+
const values: TSApi.Expression[] = []
|
|
171
|
+
// The facade over TypeScript 7 guarantees `isFunctionLike`; a class method is function-like too.
|
|
172
|
+
const visit = (node: TSApi.Node): void => {
|
|
173
|
+
if (ts.isFunctionLike(node)) return
|
|
174
|
+
if (ts.isReturnStatement(node) && node.expression !== undefined) values.push(node.expression)
|
|
175
|
+
ts.forEachChild(node, visit)
|
|
176
|
+
}
|
|
177
|
+
ts.forEachChild(body, visit)
|
|
178
|
+
return values
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Local variables of `fn` (outside nested functions) initialised with a raw `Response`. */
|
|
182
|
+
function responseBindings(ts: TypeScriptApi, fn: FunctionNode): ReadonlySet<string> {
|
|
183
|
+
const names = new Set<string>()
|
|
184
|
+
const visit = (node: TSApi.Node): void => {
|
|
185
|
+
if (ts.isFunctionLike(node)) return
|
|
186
|
+
if (
|
|
187
|
+
ts.isVariableDeclaration(node) &&
|
|
188
|
+
ts.isIdentifier(node.name) &&
|
|
189
|
+
node.initializer !== undefined &&
|
|
190
|
+
rawResponseExpression(ts, unwrapExpression(ts, node.initializer))
|
|
191
|
+
)
|
|
192
|
+
names.add(node.name.text)
|
|
193
|
+
ts.forEachChild(node, visit)
|
|
194
|
+
}
|
|
195
|
+
if (fn.body !== undefined) ts.forEachChild(fn.body, visit)
|
|
196
|
+
return names
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Whether a value `fn` returns is a raw `Response`, written there or held in a local variable. */
|
|
200
|
+
function returnsRawResponse(ts: TypeScriptApi, fn: FunctionNode): boolean {
|
|
201
|
+
const bindings = responseBindings(ts, fn)
|
|
202
|
+
return returnedValues(ts, fn).some((value) => {
|
|
203
|
+
const unwrapped = unwrapExpression(ts, value)
|
|
204
|
+
return (
|
|
205
|
+
rawResponseExpression(ts, unwrapped) ||
|
|
206
|
+
(ts.isIdentifier(unwrapped) && bindings.has(unwrapped.text))
|
|
207
|
+
)
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Value imports from relative modules, by local name: `import { json as reply } from "./http"`. */
|
|
212
|
+
function relativeImports(
|
|
213
|
+
ts: TypeScriptApi,
|
|
214
|
+
source: TSApi.SourceFile,
|
|
215
|
+
): ReadonlyMap<string, { readonly specifier: string; readonly name: string }> {
|
|
216
|
+
const imports = new Map<string, { readonly specifier: string; readonly name: string }>()
|
|
217
|
+
for (const statement of source.statements) {
|
|
218
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
219
|
+
continue
|
|
220
|
+
const specifier = statement.moduleSpecifier.text
|
|
221
|
+
const clause = statement.importClause
|
|
222
|
+
if (!specifier.startsWith(".") || clause === undefined || clause.isTypeOnly) continue
|
|
223
|
+
const named = clause.namedBindings
|
|
224
|
+
if (named === undefined || !ts.isNamedImports(named)) continue
|
|
225
|
+
for (const element of named.elements) {
|
|
226
|
+
if (element.isTypeOnly) continue
|
|
227
|
+
const name = (element.propertyName ?? element.name).text
|
|
228
|
+
imports.set(element.name.text, { specifier, name })
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return imports
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The local name `source` exports as `name`: an exported declaration, or `export { local as name }`. */
|
|
235
|
+
function exportedLocal(
|
|
236
|
+
ts: TypeScriptApi,
|
|
237
|
+
source: TSApi.SourceFile,
|
|
238
|
+
name: string,
|
|
239
|
+
): string | undefined {
|
|
240
|
+
const exported = (modifiers: readonly TSApi.ModifierLike[] | undefined): boolean =>
|
|
241
|
+
modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) === true
|
|
242
|
+
for (const statement of source.statements) {
|
|
243
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
244
|
+
if (statement.name?.text === name && exported(statement.modifiers)) return name
|
|
245
|
+
} else if (ts.isVariableStatement(statement)) {
|
|
246
|
+
const declarations = statement.declarationList.declarations
|
|
247
|
+
if (
|
|
248
|
+
exported(statement.modifiers) &&
|
|
249
|
+
declarations.some((d) => ts.isIdentifier(d.name) && d.name.text === name)
|
|
250
|
+
)
|
|
251
|
+
return name
|
|
252
|
+
} else if (
|
|
253
|
+
ts.isExportDeclaration(statement) &&
|
|
254
|
+
statement.moduleSpecifier === undefined &&
|
|
255
|
+
!statement.isTypeOnly &&
|
|
256
|
+
statement.exportClause !== undefined &&
|
|
257
|
+
ts.isNamedExports(statement.exportClause)
|
|
258
|
+
) {
|
|
259
|
+
for (const element of statement.exportClause.elements)
|
|
260
|
+
if (element.name.text === name && !element.isTypeOnly)
|
|
261
|
+
return (element.propertyName ?? element.name).text
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return undefined
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface RouteHandlerSets {
|
|
268
|
+
/** Functions a route registration or an `implement()` map passes: their first parameter is the context. */
|
|
269
|
+
readonly direct: ReadonlySet<FunctionNode>
|
|
270
|
+
/** The direct handlers registered at least once without a `{ response: … }` declaration. */
|
|
271
|
+
readonly undeclared: ReadonlySet<FunctionNode>
|
|
272
|
+
/** Local functions a direct handler returns a call to (one hop), with the handlers that do. */
|
|
273
|
+
readonly returned: ReadonlyMap<FunctionNode, ReadonlySet<FunctionNode>>
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** What a handler's returned value is, as far as one file shows: a raw `Response` reached some way,
|
|
277
|
+
* a plain value the client types, or neither for sure. */
|
|
278
|
+
type ReturnKind = "raw" | "held" | "context" | "imported" | "helper" | "plain" | "unknown"
|
|
279
|
+
const RESPONSE_KINDS: ReadonlySet<ReturnKind> = new Set([
|
|
280
|
+
"raw",
|
|
281
|
+
"held",
|
|
282
|
+
"context",
|
|
283
|
+
"imported",
|
|
284
|
+
"helper",
|
|
285
|
+
])
|
|
286
|
+
const INDIRECT_KINDS: ReadonlySet<ReturnKind> = new Set(["held", "context", "imported"])
|
|
287
|
+
|
|
288
|
+
/** An options object literal with a `response` property: the route's output is declared. */
|
|
289
|
+
function declaresResponse(ts: TypeScriptApi, argument: TSApi.Expression): boolean {
|
|
290
|
+
return (
|
|
291
|
+
ts.isObjectLiteralExpression(argument) &&
|
|
292
|
+
argument.properties.some(
|
|
293
|
+
(property) =>
|
|
294
|
+
(ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) &&
|
|
295
|
+
ts.isIdentifier(property.name) &&
|
|
296
|
+
property.name.text === "response",
|
|
297
|
+
)
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Every route handler of `source`, by the rules {@link SourceFacts.isRouteHandlerAt} states. */
|
|
302
|
+
function routeHandlers(ts: TypeScriptApi, source: TSApi.SourceFile): RouteHandlerSets {
|
|
303
|
+
const locals = localFunctions(ts, source)
|
|
304
|
+
const handlers = new Set<FunctionNode>()
|
|
305
|
+
const undeclared = new Set<FunctionNode>()
|
|
306
|
+
const named = (expression: TSApi.Node): readonly FunctionNode[] => {
|
|
307
|
+
if (ts.isIdentifier(expression)) return locals.get(expression.text) ?? []
|
|
308
|
+
return isFunctionValue(ts, expression) ? [expression] : []
|
|
309
|
+
}
|
|
310
|
+
const find = (node: TSApi.Node): void => {
|
|
311
|
+
if (ts.isCallExpression(node)) {
|
|
312
|
+
const callee = node.expression
|
|
313
|
+
const method = ts.isPropertyAccessExpression(callee) ? callee.name.text : undefined
|
|
314
|
+
const [first, ...rest] = node.arguments
|
|
315
|
+
if (
|
|
316
|
+
method !== undefined &&
|
|
317
|
+
REGISTRATION_METHODS.has(method) &&
|
|
318
|
+
first !== undefined &&
|
|
319
|
+
(ts.isStringLiteralLike(first) || ts.isTemplateExpression(first))
|
|
320
|
+
) {
|
|
321
|
+
const arguments_ = rest.map((argument) => unwrapExpression(ts, argument))
|
|
322
|
+
const declared = arguments_.some((argument) => declaresResponse(ts, argument))
|
|
323
|
+
for (const argument of arguments_)
|
|
324
|
+
for (const fn of named(argument)) {
|
|
325
|
+
handlers.add(fn)
|
|
326
|
+
if (!declared) undeclared.add(fn)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const implement =
|
|
330
|
+
(ts.isIdentifier(callee) && callee.text === "implement") || method === "implement"
|
|
331
|
+
const map =
|
|
332
|
+
node.arguments[1] === undefined ? undefined : unwrapExpression(ts, node.arguments[1])
|
|
333
|
+
if (implement && map !== undefined && ts.isObjectLiteralExpression(map)) {
|
|
334
|
+
const add = (fn: FunctionNode): void => {
|
|
335
|
+
handlers.add(fn)
|
|
336
|
+
undeclared.add(fn)
|
|
337
|
+
}
|
|
338
|
+
for (const property of map.properties) {
|
|
339
|
+
if (ts.isMethodDeclaration(property)) add(property)
|
|
340
|
+
else if (ts.isPropertyAssignment(property))
|
|
341
|
+
for (const fn of named(unwrapExpression(ts, property.initializer))) add(fn)
|
|
342
|
+
else if (ts.isShorthandPropertyAssignment(property))
|
|
343
|
+
for (const fn of locals.get(property.name.text) ?? []) add(fn)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
ts.forEachChild(node, find)
|
|
348
|
+
}
|
|
349
|
+
find(source)
|
|
350
|
+
|
|
351
|
+
// One hop: a local function a handler returns a call to answers for that handler.
|
|
352
|
+
const returned = new Map<FunctionNode, Set<FunctionNode>>()
|
|
353
|
+
for (const handler of handlers) {
|
|
354
|
+
const follow = (node: TSApi.Node): void => {
|
|
355
|
+
const value =
|
|
356
|
+
ts.isReturnStatement(node) && node.expression !== undefined
|
|
357
|
+
? node.expression
|
|
358
|
+
: ts.isArrowFunction(node) && !ts.isBlock(node.body)
|
|
359
|
+
? node.body
|
|
360
|
+
: undefined
|
|
361
|
+
const call = value === undefined ? undefined : unwrapExpression(ts, value)
|
|
362
|
+
if (call !== undefined && ts.isCallExpression(call))
|
|
363
|
+
for (const fn of named(call.expression)) {
|
|
364
|
+
const callers = returned.get(fn) ?? new Set<FunctionNode>()
|
|
365
|
+
callers.add(handler)
|
|
366
|
+
returned.set(fn, callers)
|
|
367
|
+
}
|
|
368
|
+
ts.forEachChild(node, follow)
|
|
369
|
+
}
|
|
370
|
+
follow(handler)
|
|
371
|
+
}
|
|
372
|
+
return { direct: handlers, undeclared, returned }
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** `c.json(…)`/`c.text(…)` on the handler's own context parameter: both build a plain `Response`. */
|
|
376
|
+
function contextResponseCall(
|
|
377
|
+
ts: TypeScriptApi,
|
|
378
|
+
handler: FunctionNode,
|
|
379
|
+
value: TSApi.Expression,
|
|
380
|
+
): boolean {
|
|
381
|
+
const context = handler.parameters[0]?.name
|
|
382
|
+
if (context === undefined || !ts.isIdentifier(context)) return false
|
|
383
|
+
if (!ts.isCallExpression(value) || !ts.isPropertyAccessExpression(value.expression)) return false
|
|
384
|
+
const target = value.expression.expression
|
|
385
|
+
const method = value.expression.name.text
|
|
386
|
+
return (
|
|
387
|
+
ts.isIdentifier(target) &&
|
|
388
|
+
target.text === context.text &&
|
|
389
|
+
(method === "json" || method === "text")
|
|
390
|
+
)
|
|
391
|
+
}
|
|
392
|
+
|
|
85
393
|
export interface SourceFactsOptions {
|
|
86
394
|
/** Whether the compiler settings for a parsed file keep an import whose named specifiers are all
|
|
87
395
|
* type-only (`verbatimModuleSyntax`). Without it such an import is erased. */
|
|
@@ -215,6 +523,93 @@ export function createSourceFacts(
|
|
|
215
523
|
return false
|
|
216
524
|
}
|
|
217
525
|
|
|
526
|
+
const handlersBySource = new WeakMap<TSApi.SourceFile, RouteHandlerSets>()
|
|
527
|
+
const handlersOf = (source: TSApi.SourceFile): RouteHandlerSets => {
|
|
528
|
+
let handlers = handlersBySource.get(source)
|
|
529
|
+
if (handlers === undefined) {
|
|
530
|
+
handlers = routeHandlers(ts, source)
|
|
531
|
+
handlersBySource.set(source, handlers)
|
|
532
|
+
}
|
|
533
|
+
return handlers
|
|
534
|
+
}
|
|
535
|
+
const untypedResponseRoutes = (
|
|
536
|
+
source: TSApi.SourceFile,
|
|
537
|
+
importReturnsResponse?: (specifier: string, name: string) => boolean,
|
|
538
|
+
): UntypedResponseRoutes => {
|
|
539
|
+
const { direct, undeclared, returned } = handlersOf(source)
|
|
540
|
+
const locals = localFunctions(ts, source)
|
|
541
|
+
const imports = importReturnsResponse === undefined ? undefined : relativeImports(ts, source)
|
|
542
|
+
// A call this file cannot see into, or a conditional, may or may not be a Response: it neither
|
|
543
|
+
// marks a handler untyped nor keeps it typed.
|
|
544
|
+
const kindOf = (fn: FunctionNode, isDirect: boolean, value: TSApi.Expression): ReturnKind => {
|
|
545
|
+
if (rawResponseExpression(ts, value)) return "raw"
|
|
546
|
+
if (ts.isConditionalExpression(value) || ts.isNewExpression(value)) return "unknown"
|
|
547
|
+
if (!ts.isCallExpression(value)) return "plain"
|
|
548
|
+
if (isDirect && contextResponseCall(ts, fn, value)) return "context"
|
|
549
|
+
if (!ts.isIdentifier(value.expression)) return "unknown"
|
|
550
|
+
const imported = imports?.get(value.expression.text)
|
|
551
|
+
if (imported !== undefined)
|
|
552
|
+
return importReturnsResponse?.(imported.specifier, imported.name) === true
|
|
553
|
+
? "imported"
|
|
554
|
+
: "unknown"
|
|
555
|
+
// From a direct handler, a call to a local helper answers as the helper does (one hop).
|
|
556
|
+
const helpers = isDirect
|
|
557
|
+
? (locals.get(value.expression.text) ?? []).filter((helper) => returned.has(helper))
|
|
558
|
+
: []
|
|
559
|
+
if (helpers.length === 0) return "unknown"
|
|
560
|
+
const kinds = helpers.flatMap((helper) =>
|
|
561
|
+
returnKinds(helper, false).map((entry) => entry.kind),
|
|
562
|
+
)
|
|
563
|
+
if (kinds.some((kind) => kind === "plain")) return "plain"
|
|
564
|
+
return kinds.every((kind) => RESPONSE_KINDS.has(kind)) ? "helper" : "unknown"
|
|
565
|
+
}
|
|
566
|
+
const returnKinds = (fn: FunctionNode, isDirect: boolean) => {
|
|
567
|
+
const bindings = responseBindings(ts, fn)
|
|
568
|
+
return returnedValues(ts, fn).map((returnedValue) => {
|
|
569
|
+
const value = unwrapExpression(ts, returnedValue)
|
|
570
|
+
const kind: ReturnKind =
|
|
571
|
+
ts.isIdentifier(value) && bindings.has(value.text) ? "held" : kindOf(fn, isDirect, value)
|
|
572
|
+
return { value, kind }
|
|
573
|
+
})
|
|
574
|
+
}
|
|
575
|
+
// No plain return: a Response written anywhere in it (a `.then(() => new Response(…))` too)
|
|
576
|
+
// is what the route answers with.
|
|
577
|
+
const isUntyped = (fn: FunctionNode, isDirect: boolean): boolean =>
|
|
578
|
+
!returnKinds(fn, isDirect).some((entry) => entry.kind === "plain")
|
|
579
|
+
|
|
580
|
+
const untyped = new Map<FunctionNode, boolean>()
|
|
581
|
+
for (const handler of direct)
|
|
582
|
+
untyped.set(handler, undeclared.has(handler) && isUntyped(handler, true))
|
|
583
|
+
for (const [helper, callers] of returned) {
|
|
584
|
+
if (direct.has(helper)) continue
|
|
585
|
+
const reached = [...callers].some((caller) => untyped.get(caller) === true)
|
|
586
|
+
untyped.set(helper, reached && isUntyped(helper, false))
|
|
587
|
+
}
|
|
588
|
+
const indirectReturns: number[] = []
|
|
589
|
+
for (const [fn, untypedFn] of untyped) {
|
|
590
|
+
if (!untypedFn) continue
|
|
591
|
+
// A raw `new Response(` is the lexical pattern's to report, and a helper's at its own return.
|
|
592
|
+
for (const { value, kind } of returnKinds(fn, direct.has(fn)))
|
|
593
|
+
if (INDIRECT_KINDS.has(kind)) indirectReturns.push(value.getStart(source))
|
|
594
|
+
}
|
|
595
|
+
const byNode: ReadonlyMap<TSApi.Node, boolean> = untyped
|
|
596
|
+
return {
|
|
597
|
+
isUntypedAt: (position) => {
|
|
598
|
+
const handler = ancestorAt(ts, source, position, (node): node is TSApi.Node =>
|
|
599
|
+
byNode.has(node),
|
|
600
|
+
)
|
|
601
|
+
return handler !== undefined && byNode.get(handler) === true
|
|
602
|
+
},
|
|
603
|
+
indirectReturns,
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const exportReturnsResponse = (source: TSApi.SourceFile, name: string): boolean => {
|
|
608
|
+
const local = exportedLocal(ts, source, name)
|
|
609
|
+
if (local === undefined) return false
|
|
610
|
+
return (localFunctions(ts, source).get(local) ?? []).some((fn) => returnsRawResponse(ts, fn))
|
|
611
|
+
}
|
|
612
|
+
|
|
218
613
|
const isDynamicImportAt = (
|
|
219
614
|
source: TSApi.SourceFile,
|
|
220
615
|
position: number,
|
|
@@ -245,6 +640,8 @@ export function createSourceFacts(
|
|
|
245
640
|
isRouteArgumentAt,
|
|
246
641
|
isFunctionRouteCallAt,
|
|
247
642
|
isResponseSyntaxAt,
|
|
643
|
+
untypedResponseRoutes,
|
|
644
|
+
exportReturnsResponse,
|
|
248
645
|
isDynamicImportAt,
|
|
249
646
|
}
|
|
250
647
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bare specifiers that name a package the project owns, resolved through that package's `exports`
|
|
3
|
+
* the way Bun resolves them. A package is the project's when it is a workspace member, or when its
|
|
4
|
+
* `node_modules` entry links to a directory outside every `node_modules`; one installed from a
|
|
5
|
+
* registry is never read, because its source is not the project's to check.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs"
|
|
8
|
+
import { dirname, join, relative, sep } from "node:path"
|
|
9
|
+
import { workspaceMemberDirs } from "../mcp-root.ts"
|
|
10
|
+
|
|
11
|
+
/** Bun's runtime conditions, in the order a conditions object is read; `types` is taken only when it
|
|
12
|
+
* names a TypeScript source, which is what Bun would run. */
|
|
13
|
+
const CONDITIONS: ReadonlySet<string> = new Set(["bun", "import", "default"])
|
|
14
|
+
const TS_SOURCE = /\.[cm]?tsx?$/
|
|
15
|
+
const DECLARATION = /\.d\.[cm]?tsx?$/
|
|
16
|
+
/** Tried in order when a package without `exports` names a file without its extension. */
|
|
17
|
+
const EXTENSIONS: readonly string[] = ["", ".ts", ".tsx", ".mts", ".js", ".mjs", ".jsx"]
|
|
18
|
+
const PACKAGE_NAME = /^(?:@[a-z0-9][\w.~-]*\/)?[a-z0-9][\w.~-]*$/i
|
|
19
|
+
|
|
20
|
+
/** The file a specifier names, or why a package the project owns does not export it. */
|
|
21
|
+
export type ExportedFile =
|
|
22
|
+
| { readonly path: string }
|
|
23
|
+
| { readonly package: string; readonly reason: string }
|
|
24
|
+
|
|
25
|
+
const record = (value: unknown): Readonly<Record<string, unknown>> | undefined =>
|
|
26
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
27
|
+
? Object.fromEntries(Object.entries(value))
|
|
28
|
+
: undefined
|
|
29
|
+
|
|
30
|
+
const isFile = (path: string): boolean => {
|
|
31
|
+
try {
|
|
32
|
+
return statSync(path).isFile()
|
|
33
|
+
} catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const manifestOf = (dir: string): Readonly<Record<string, unknown>> | undefined => {
|
|
39
|
+
try {
|
|
40
|
+
return record(JSON.parse(readFileSync(join(dir, "package.json"), "utf8")))
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `@scope/name/sub/path` -> `["@scope/name", "./sub/path"]`; `undefined` for a relative, absolute,
|
|
47
|
+
* `#` or `scheme:` specifier. */
|
|
48
|
+
export function splitBareSpecifier(specifier: string): [string, string] | undefined {
|
|
49
|
+
if (/^[./#]/.test(specifier) || specifier.includes(":")) return undefined
|
|
50
|
+
const parts = specifier.split("/")
|
|
51
|
+
const take = specifier.startsWith("@") ? 2 : 1
|
|
52
|
+
const name = parts.slice(0, take).join("/")
|
|
53
|
+
if (!PACKAGE_NAME.test(name)) return undefined
|
|
54
|
+
const rest = parts.slice(take)
|
|
55
|
+
return [name, rest.length === 0 ? "." : `./${rest.join("/")}`]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The target a conditions value selects, with `*` replaced; `null` when the package excludes it. */
|
|
59
|
+
function conditionTarget(value: unknown, match: string): string | null | undefined {
|
|
60
|
+
if (value === null) return null
|
|
61
|
+
if (typeof value === "string") {
|
|
62
|
+
// As Node and Bun refuse them: a target leaves the package only through `..` or `node_modules`.
|
|
63
|
+
const segments = value.split("/")
|
|
64
|
+
if (segments[0] !== "." || segments.some((part) => part === ".." || part === "node_modules"))
|
|
65
|
+
return undefined
|
|
66
|
+
return value.replaceAll("*", match)
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
for (const item of value) {
|
|
70
|
+
const target = conditionTarget(item, match)
|
|
71
|
+
if (target !== undefined) return target
|
|
72
|
+
}
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
const conditions = record(value)
|
|
76
|
+
if (conditions === undefined) return undefined
|
|
77
|
+
for (const [key, inner] of Object.entries(conditions)) {
|
|
78
|
+
if (CONDITIONS.has(key)) {
|
|
79
|
+
const target = conditionTarget(inner, match)
|
|
80
|
+
if (target !== undefined) return target
|
|
81
|
+
} else if (key === "types") {
|
|
82
|
+
const target = conditionTarget(inner, match)
|
|
83
|
+
if (typeof target === "string" && TS_SOURCE.test(target) && !DECLARATION.test(target))
|
|
84
|
+
return target
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** What `exports` maps `subpath` to: an exact key first, then the `*` pattern with the longest
|
|
91
|
+
* prefix, as Node and Bun match them. */
|
|
92
|
+
export function exportsTarget(exports: unknown, subpath: string): string | null | undefined {
|
|
93
|
+
const map = record(exports)
|
|
94
|
+
const subpaths =
|
|
95
|
+
map !== undefined && Object.keys(map).some((key) => key.startsWith("."))
|
|
96
|
+
? map
|
|
97
|
+
: { ".": exports }
|
|
98
|
+
if (Object.hasOwn(subpaths, subpath)) return conditionTarget(subpaths[subpath], "")
|
|
99
|
+
let best: { readonly key: string; readonly match: string } | undefined
|
|
100
|
+
for (const key of Object.keys(subpaths)) {
|
|
101
|
+
const star = key.indexOf("*")
|
|
102
|
+
if (star === -1 || key.indexOf("*", star + 1) !== -1) continue
|
|
103
|
+
const prefix = key.slice(0, star)
|
|
104
|
+
const suffix = key.slice(star + 1)
|
|
105
|
+
if (
|
|
106
|
+
subpath.length < key.length ||
|
|
107
|
+
!subpath.startsWith(prefix) ||
|
|
108
|
+
!subpath.endsWith(suffix) ||
|
|
109
|
+
(best !== undefined && prefix.length <= best.key.indexOf("*"))
|
|
110
|
+
)
|
|
111
|
+
continue
|
|
112
|
+
best = { key, match: subpath.slice(prefix.length, subpath.length - suffix.length) }
|
|
113
|
+
}
|
|
114
|
+
if (best === undefined) return undefined
|
|
115
|
+
if (best.match.split("/").includes("..")) return undefined
|
|
116
|
+
return conditionTarget(subpaths[best.key], best.match)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const inside = (dir: string, path: string): boolean => {
|
|
120
|
+
const rel = relative(dir, path)
|
|
121
|
+
return rel !== "" && !rel.startsWith("..") && !rel.split(sep).includes("node_modules")
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The file `subpath` names in the package at `dir`, or why it names none. */
|
|
125
|
+
function entryIn(dir: string, name: string, subpath: string): ExportedFile {
|
|
126
|
+
const manifest = manifestOf(dir)
|
|
127
|
+
const shown = subpath === "." ? name : `${name}/${subpath.slice(2)}`
|
|
128
|
+
const exported =
|
|
129
|
+
manifest?.exports !== undefined
|
|
130
|
+
? exportsTarget(manifest.exports, subpath)
|
|
131
|
+
: subpath === "."
|
|
132
|
+
? typeof manifest?.module === "string"
|
|
133
|
+
? manifest.module
|
|
134
|
+
: typeof manifest?.main === "string"
|
|
135
|
+
? manifest.main
|
|
136
|
+
: "./index"
|
|
137
|
+
: subpath
|
|
138
|
+
if (exported === null || exported === undefined)
|
|
139
|
+
return { package: name, reason: `${name} does not export "${subpath}"` }
|
|
140
|
+
// An `exports` target is a file; a package without `exports` is probed the way Bun probes it.
|
|
141
|
+
const base = join(dir, exported)
|
|
142
|
+
const probes =
|
|
143
|
+
manifest?.exports !== undefined
|
|
144
|
+
? [base]
|
|
145
|
+
: [
|
|
146
|
+
...EXTENSIONS.map((extension) => `${base}${extension}`),
|
|
147
|
+
...EXTENSIONS.slice(1).map((extension) => join(base, `index${extension}`)),
|
|
148
|
+
]
|
|
149
|
+
const found = probes.find(isFile)
|
|
150
|
+
if (found === undefined)
|
|
151
|
+
return { package: name, reason: `${shown} resolves to ${exported}, which does not exist` }
|
|
152
|
+
let real: string
|
|
153
|
+
try {
|
|
154
|
+
real = realpathSync(found)
|
|
155
|
+
} catch {
|
|
156
|
+
return { package: name, reason: `${shown} resolves to ${exported}, which cannot be read` }
|
|
157
|
+
}
|
|
158
|
+
return inside(realpathSync(dir), real)
|
|
159
|
+
? { path: real }
|
|
160
|
+
: { package: name, reason: `${shown} resolves outside the package` }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The nearest directory at or above `start`, up to the repository root, whose package.json declares
|
|
164
|
+
* `workspaces`. */
|
|
165
|
+
function workspaceRoot(start: string): string | undefined {
|
|
166
|
+
for (let dir = start; ; ) {
|
|
167
|
+
const workspaces = manifestOf(dir)?.workspaces
|
|
168
|
+
if (workspaces !== undefined) return dir
|
|
169
|
+
const parent = dirname(dir)
|
|
170
|
+
if (parent === dir || existsSync(join(dir, ".git"))) return undefined
|
|
171
|
+
dir = parent
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Resolves a bare specifier from an absolute importing file; `undefined` when it is not bare. */
|
|
176
|
+
export type ExportedFileResolver = (from: string, specifier: string) => ExportedFile | undefined
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* A resolver over the workspace `root` belongs to. Workspace members are read from their manifests
|
|
180
|
+
* once; any other package is looked up in the `node_modules` above the importing file and read only
|
|
181
|
+
* when that entry links outside every `node_modules`.
|
|
182
|
+
*/
|
|
183
|
+
export async function createExportedFileResolver(root: string): Promise<ExportedFileResolver> {
|
|
184
|
+
const members = new Map<string, string>()
|
|
185
|
+
const top = workspaceRoot(root)
|
|
186
|
+
for (const dir of top === undefined ? [] : ((await workspaceMemberDirs(top)) ?? [])) {
|
|
187
|
+
const name = manifestOf(dir)?.name
|
|
188
|
+
if (typeof name === "string" && !members.has(name)) members.set(name, dir)
|
|
189
|
+
}
|
|
190
|
+
return (from, specifier) => {
|
|
191
|
+
const split = splitBareSpecifier(specifier)
|
|
192
|
+
if (split === undefined) return undefined
|
|
193
|
+
const [name, subpath] = split
|
|
194
|
+
const member = members.get(name)
|
|
195
|
+
if (member !== undefined) return entryIn(member, name, subpath)
|
|
196
|
+
for (let dir = dirname(from); ; ) {
|
|
197
|
+
const linked = join(dir, "node_modules", name)
|
|
198
|
+
if (existsSync(linked)) {
|
|
199
|
+
let real: string
|
|
200
|
+
try {
|
|
201
|
+
real = realpathSync(linked)
|
|
202
|
+
} catch {
|
|
203
|
+
break
|
|
204
|
+
}
|
|
205
|
+
return real.split(sep).includes("node_modules")
|
|
206
|
+
? {
|
|
207
|
+
package: name,
|
|
208
|
+
reason: `${name} is installed from a registry, not a workspace package`,
|
|
209
|
+
}
|
|
210
|
+
: entryIn(real, name, subpath)
|
|
211
|
+
}
|
|
212
|
+
const parent = dirname(dir)
|
|
213
|
+
if (parent === dir) break
|
|
214
|
+
dir = parent
|
|
215
|
+
}
|
|
216
|
+
return { package: name, reason: `${name} is not a workspace package and is not installed` }
|
|
217
|
+
}
|
|
218
|
+
}
|