@pavanyn/docugen 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -1
- package/dist/changes/schema.d.ts +2 -2
- package/dist/commands/extract.js +12 -1
- package/dist/commands/extract.js.map +1 -1
- package/dist/extract/endpoints/express.d.ts +21 -8
- package/dist/extract/endpoints/express.d.ts.map +1 -1
- package/dist/extract/endpoints/express.js +269 -272
- package/dist/extract/endpoints/express.js.map +1 -1
- package/dist/extract/endpoints/index.d.ts.map +1 -1
- package/dist/extract/endpoints/index.js +2 -0
- package/dist/extract/endpoints/index.js.map +1 -1
- package/dist/extract/endpoints/openapi.d.ts +4 -23
- package/dist/extract/endpoints/openapi.d.ts.map +1 -1
- package/dist/extract/endpoints/openapi.js +169 -118
- package/dist/extract/endpoints/openapi.js.map +1 -1
- package/dist/extract/endpoints/static-string.d.ts +26 -0
- package/dist/extract/endpoints/static-string.d.ts.map +1 -0
- package/dist/extract/endpoints/static-string.js +180 -0
- package/dist/extract/endpoints/static-string.js.map +1 -0
- package/dist/features/schema.d.ts +4 -4
- package/dist/graph/from-extraction.d.ts.map +1 -1
- package/dist/graph/from-extraction.js +6 -0
- package/dist/graph/from-extraction.js.map +1 -1
- package/dist/plans/schema.d.ts +4 -4
- package/dist/privacy/redact.d.ts.map +1 -1
- package/dist/privacy/redact.js +21 -2
- package/dist/privacy/redact.js.map +1 -1
- package/dist/render/pages/api.d.ts.map +1 -1
- package/dist/render/pages/api.js +30 -35
- package/dist/render/pages/api.js.map +1 -1
- package/dist/types/entries.d.ts +13 -1
- package/dist/types/entries.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -5,78 +5,43 @@ import { toPosix } from '../../util/paths.js';
|
|
|
5
5
|
import { readModuleBindings, resolveSymbolToFile } from '../../util/modules.js';
|
|
6
6
|
import { loadPathAliases } from '../../util/tsconfig.js';
|
|
7
7
|
import { literalString, parseSourceFile, positionOf, ts, walk } from '../../util/ts-ast.js';
|
|
8
|
+
import { compareStrings } from '../../util/sort.js';
|
|
9
|
+
import { redactSecrets } from '../../privacy/redact.js';
|
|
8
10
|
import { joinPath, paramsOf } from './paths.js';
|
|
9
11
|
import { applicationScope, owningWorkspace } from '../../detect/ownership.js';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
* The hard part is not finding `router.get(...)` — it is knowing what URL it
|
|
14
|
-
* ends up at. A microservice typically declares its routes in one file and
|
|
15
|
-
* mounts the whole router elsewhere with `app.use('/projects', projectRoutes)`,
|
|
16
|
-
* often through a barrel re-export. Ignoring the mount documents every endpoint
|
|
17
|
-
* at the wrong path, so mounts are resolved across files and a route whose
|
|
18
|
-
* mount cannot be resolved is reported rather than published at a guessed URL.
|
|
19
|
-
*/
|
|
20
|
-
const HTTP_METHODS = new Set([
|
|
21
|
-
'get',
|
|
22
|
-
'post',
|
|
23
|
-
'put',
|
|
24
|
-
'patch',
|
|
25
|
-
'delete',
|
|
26
|
-
'head',
|
|
27
|
-
'options',
|
|
28
|
-
'all',
|
|
29
|
-
]);
|
|
30
|
-
/** Calls that produce a mountable Router. */
|
|
12
|
+
import { evaluateStaticString } from './static-string.js';
|
|
13
|
+
const HTTP_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'all']);
|
|
31
14
|
const ROUTER_FACTORIES = new Set(['Router']);
|
|
32
|
-
/** Calls that produce the application itself, which is never mounted. */
|
|
33
15
|
const APP_FACTORIES = new Set(['express']);
|
|
34
|
-
/**
|
|
35
|
-
* Type names that prove a value is an Express app or router.
|
|
36
|
-
*
|
|
37
|
-
* The app frequently arrives as a function parameter rather than a local
|
|
38
|
-
* assignment (`export default async (app: Application) => { app.use(...) }`),
|
|
39
|
-
* and its type annotation is the precise signal for that — far safer than
|
|
40
|
-
* guessing from the variable's name.
|
|
41
|
-
*/
|
|
42
16
|
const ROUTER_TYPE_NAMES = new Set(['Router', 'IRouter', 'express.Router', 'e.Router']);
|
|
43
17
|
const APP_TYPE_NAMES = new Set([
|
|
44
|
-
'Application',
|
|
45
|
-
'Express',
|
|
46
|
-
'express.Application',
|
|
47
|
-
'express.Express',
|
|
48
|
-
'e.Application',
|
|
49
|
-
'e.Express',
|
|
18
|
+
'Application', 'Express', 'Express.Application', 'Express.Express',
|
|
19
|
+
'express.Application', 'express.Express', 'e.Application', 'e.Express',
|
|
50
20
|
]);
|
|
51
21
|
export async function extractExpressEndpoints(args) {
|
|
52
|
-
const files = (await fg(['**/*.{ts,js,mjs}'], {
|
|
53
|
-
.
|
|
54
|
-
|
|
22
|
+
const files = (await fg(['**/*.{ts,js,mjs}'], {
|
|
23
|
+
cwd: args.root, ignore: [...args.exclude], onlyFiles: true,
|
|
24
|
+
})).map(toPosix).sort(compareStrings);
|
|
55
25
|
const fileSet = new Set(files);
|
|
56
26
|
const aliases = await loadPathAliases(args.root);
|
|
57
27
|
const analyses = new Map();
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* A re-export barrel declares no routes of its own, so it never reaches
|
|
63
|
-
* `analyses` — but mount resolution passes straight through it. Loading
|
|
64
|
-
* lazily keeps the common path cheap while still following the chain.
|
|
65
|
-
*/
|
|
66
|
-
const loadBindings = async (file) => {
|
|
67
|
-
if (bindingsCache.has(file))
|
|
68
|
-
return bindingsCache.get(file);
|
|
69
|
-
let bindings;
|
|
28
|
+
const moduleCache = new Map();
|
|
29
|
+
const loadModule = async (file) => {
|
|
30
|
+
if (moduleCache.has(file))
|
|
31
|
+
return moduleCache.get(file);
|
|
70
32
|
try {
|
|
71
33
|
const contents = await fs.readFile(path.join(args.root, file), 'utf8');
|
|
72
|
-
|
|
34
|
+
const source = parseSourceFile(file, contents);
|
|
35
|
+
const module = { file, source, bindings: readModuleBindings(source) };
|
|
36
|
+
moduleCache.set(file, module);
|
|
37
|
+
return module;
|
|
73
38
|
}
|
|
74
39
|
catch {
|
|
75
|
-
|
|
40
|
+
moduleCache.set(file, undefined);
|
|
41
|
+
return undefined;
|
|
76
42
|
}
|
|
77
|
-
bindingsCache.set(file, bindings);
|
|
78
|
-
return bindings;
|
|
79
43
|
};
|
|
44
|
+
const loadBindings = async (file) => (await loadModule(file))?.bindings;
|
|
80
45
|
for (const relative of files) {
|
|
81
46
|
let contents;
|
|
82
47
|
try {
|
|
@@ -85,29 +50,20 @@ export async function extractExpressEndpoints(args) {
|
|
|
85
50
|
catch {
|
|
86
51
|
continue;
|
|
87
52
|
}
|
|
88
|
-
|
|
89
|
-
if (!/\b(?:Router|express)\s*\(|\.(?:get|post|put|patch|delete|use)\s*\(/.test(contents))
|
|
53
|
+
if (!/\b(?:Router|express)\s*\(|\.(?:get|post|put|patch|delete|head|options|all|use)\s*\(/.test(contents))
|
|
90
54
|
continue;
|
|
91
55
|
const analysis = analyseFile(relative, contents);
|
|
92
|
-
|
|
93
|
-
if (analysis.registrations.length === 0 &&
|
|
94
|
-
analysis.mounts.length === 0 &&
|
|
95
|
-
analysis.unconfirmedRouters.length === 0) {
|
|
56
|
+
moduleCache.set(relative, analysis);
|
|
57
|
+
if (analysis.registrations.length === 0 && analysis.mounts.length === 0 && analysis.unconfirmedRouters.length === 0)
|
|
96
58
|
continue;
|
|
97
|
-
}
|
|
98
59
|
analyses.set(relative, analysis);
|
|
99
60
|
}
|
|
100
|
-
// A file reaches `analyses` only with a registration, a mount, or a candidate
|
|
101
|
-
// router in a file that imports express — so anything here is genuine
|
|
102
|
-
// evidence. A repo where every router is unconfirmed still counts: producing
|
|
103
|
-
// silence there would be the "empty output looks like a clean repo" failure.
|
|
104
61
|
if (analyses.size === 0)
|
|
105
|
-
return { entries: [], gaps: [], found: false };
|
|
106
|
-
// Resolve every mount to the file that declares the router it points at, so
|
|
107
|
-
// a registration can be given the prefix it actually runs under.
|
|
62
|
+
return { entries: [], gaps: [], found: false, sourceOwnership: [] };
|
|
108
63
|
const locationsByRouter = new Map();
|
|
109
64
|
const mountEdges = new Map();
|
|
110
65
|
const gaps = [];
|
|
66
|
+
const workspaces = args.workspaces ?? [{ dir: '', manifests: [] }];
|
|
111
67
|
const routerKey = (file, variable) => `${file}\u0000${variable}`;
|
|
112
68
|
const targetVariable = (resolved) => {
|
|
113
69
|
const target = analyses.get(resolved.file);
|
|
@@ -125,30 +81,25 @@ export async function extractExpressEndpoints(args) {
|
|
|
125
81
|
const binding = target.bindings.exports.get(resolved.exportedName);
|
|
126
82
|
if (binding?.kind === 'local')
|
|
127
83
|
return binding.localName;
|
|
128
|
-
if (resolved.exportedName === 'default' && target.mounts.some((mount) => mount.routerVariable === '$default'))
|
|
84
|
+
if (resolved.exportedName === 'default' && target.mounts.some((mount) => mount.routerVariable === '$default'))
|
|
129
85
|
return '$default';
|
|
130
|
-
}
|
|
131
86
|
return resolved.exportedName;
|
|
132
87
|
};
|
|
133
88
|
for (const analysis of analyses.values()) {
|
|
134
89
|
for (const mount of analysis.mounts) {
|
|
90
|
+
const evaluated = mount.prefixExpression === undefined
|
|
91
|
+
? { value: mount.prefix, complete: true, original: mount.prefix }
|
|
92
|
+
: await evaluateStaticString({
|
|
93
|
+
module: analysis, expression: mount.prefixExpression, files: fileSet, aliases, loadModule,
|
|
94
|
+
});
|
|
135
95
|
const resolved = await resolveSymbolToFile({
|
|
136
|
-
fromFile: analysis.file,
|
|
137
|
-
symbol: mount.symbol,
|
|
138
|
-
loadBindings,
|
|
139
|
-
files: fileSet,
|
|
140
|
-
aliases,
|
|
96
|
+
fromFile: analysis.file, symbol: mount.symbol, loadBindings, files: fileSet, aliases,
|
|
141
97
|
});
|
|
142
98
|
if (resolved === undefined || !analyses.has(resolved.file)) {
|
|
143
|
-
|
|
144
|
-
// third-party middleware rather than a local router. Only worth
|
|
145
|
-
// reporting when it carries a path prefix that would change URLs.
|
|
146
|
-
if (mount.prefix !== '' && mount.prefix !== '/') {
|
|
99
|
+
if (evaluated.value !== '' && evaluated.value !== '/') {
|
|
147
100
|
gaps.push({
|
|
148
|
-
extractor: 'endpoints',
|
|
149
|
-
|
|
150
|
-
message: `'${mount.symbol}' is mounted at '${mount.prefix}' but docgen could not resolve what it ` +
|
|
151
|
-
'refers to. Any routes it declares are documented without this prefix.',
|
|
101
|
+
extractor: 'endpoints', kind: 'mount-target-unresolved',
|
|
102
|
+
message: `'${mount.symbol}' is mounted at '${evaluated.value}' but docgen could not resolve what it refers to.`,
|
|
152
103
|
source: { file: analysis.file, line: mount.line },
|
|
153
104
|
});
|
|
154
105
|
}
|
|
@@ -156,120 +107,133 @@ export async function extractExpressEndpoints(args) {
|
|
|
156
107
|
}
|
|
157
108
|
const from = routerKey(analysis.file, mount.routerVariable);
|
|
158
109
|
const to = routerKey(resolved.file, targetVariable(resolved));
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
110
|
+
mountEdges.set(from, [...(mountEdges.get(from) ?? []), {
|
|
111
|
+
to, prefix: evaluated.value, resolved: evaluated.complete,
|
|
112
|
+
...(evaluated.complete ? {} : { original: redactSecrets(evaluated.original).text }),
|
|
113
|
+
sourceFile: analysis.file, line: mount.line, symbol: mount.symbol,
|
|
114
|
+
}]);
|
|
162
115
|
}
|
|
163
116
|
if (analysis.unconfirmedRouters.length > 0) {
|
|
164
117
|
gaps.push({
|
|
165
|
-
extractor: 'endpoints',
|
|
166
|
-
|
|
167
|
-
message: `${analysis.file} registers rooted paths on ${analysis.unconfirmedRouters.join(', ')}, which ` +
|
|
168
|
-
'docgen could not confirm is an Express app or router. Those routes are not documented — ' +
|
|
169
|
-
'annotate the variable with an Express type to have them picked up.',
|
|
118
|
+
extractor: 'endpoints', kind: 'unconfirmed-router-variable',
|
|
119
|
+
message: `${analysis.file} registers rooted paths on ${analysis.unconfirmedRouters.join(', ')}, which docgen could not confirm is an Express app or router. Those routes are not documented; annotate the variable with an Express type.`,
|
|
170
120
|
source: { file: analysis.file },
|
|
171
121
|
});
|
|
172
122
|
}
|
|
173
123
|
}
|
|
174
124
|
const queue = [];
|
|
175
125
|
for (const analysis of analyses.values()) {
|
|
176
|
-
|
|
177
|
-
|
|
126
|
+
const workspace = owningWorkspace(analysis.file, workspaces);
|
|
127
|
+
for (const root of analysis.applicationRoots) {
|
|
178
128
|
queue.push({
|
|
179
|
-
key: routerKey(analysis.file,
|
|
180
|
-
|
|
181
|
-
application: applicationScope(workspace, 'express', `${analysis.file}#${variable}`),
|
|
129
|
+
key: routerKey(analysis.file, root.owner), prefix: '', resolved: true,
|
|
130
|
+
application: applicationScope(workspace, 'express', root.discriminator),
|
|
182
131
|
});
|
|
183
132
|
}
|
|
184
133
|
}
|
|
185
134
|
const visited = new Set();
|
|
135
|
+
const unresolvedFindingKeys = new Set();
|
|
186
136
|
while (queue.length > 0) {
|
|
187
137
|
const current = queue.shift();
|
|
188
138
|
if (current === undefined)
|
|
189
139
|
continue;
|
|
190
|
-
const visitKey = `${current.key}\u0000${current.application}\u0000${current.prefix}`;
|
|
140
|
+
const visitKey = `${current.key}\u0000${current.application}\u0000${current.prefix}\u0000${current.resolved}`;
|
|
191
141
|
if (visited.has(visitKey))
|
|
192
142
|
continue;
|
|
193
143
|
visited.add(visitKey);
|
|
194
144
|
const byApplication = locationsByRouter.get(current.key) ?? new Map();
|
|
195
|
-
const bucket = byApplication.get(current.application) ?? new
|
|
196
|
-
bucket.
|
|
145
|
+
const bucket = byApplication.get(current.application) ?? new Map();
|
|
146
|
+
bucket.set(`${current.prefix}\u0000${current.resolved}`, { prefix: current.prefix, resolved: current.resolved });
|
|
197
147
|
byApplication.set(current.application, bucket);
|
|
198
148
|
locationsByRouter.set(current.key, byApplication);
|
|
199
149
|
for (const edge of mountEdges.get(current.key) ?? []) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
150
|
+
const prefix = joinPath(current.prefix, edge.prefix);
|
|
151
|
+
const resolved = current.resolved && edge.resolved;
|
|
152
|
+
if (!edge.resolved) {
|
|
153
|
+
const findingKey = `${edge.sourceFile}\u0000${edge.line}\u0000${edge.symbol}\u0000${current.application}\u0000${prefix}`;
|
|
154
|
+
if (!unresolvedFindingKeys.has(findingKey)) {
|
|
155
|
+
unresolvedFindingKeys.add(findingKey);
|
|
156
|
+
gaps.push({
|
|
157
|
+
extractor: 'endpoints', kind: 'mount-prefix-unresolved',
|
|
158
|
+
message: `Mount prefix expression '${edge.original ?? edge.prefix}' for router '${edge.symbol}' in application '${current.application}' was only partially resolvable; '${prefix}' is used as an explicit placeholder path.`,
|
|
159
|
+
source: { file: edge.sourceFile, line: edge.line },
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
queue.push({ key: edge.to, prefix, application: current.application, resolved });
|
|
205
164
|
}
|
|
206
165
|
}
|
|
207
166
|
const entries = [];
|
|
208
167
|
for (const analysis of analyses.values()) {
|
|
209
168
|
if (analysis.registrations.length === 0)
|
|
210
169
|
continue;
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
170
|
+
const mountedVariables = new Set(analysis.registrations
|
|
171
|
+
.map((registration) => registration.routerVariable)
|
|
172
|
+
.filter((variable) => (locationsByRouter.get(routerKey(analysis.file, variable))?.size ?? 0) > 0));
|
|
173
|
+
const unmountedVariables = new Set(analysis.registrations
|
|
174
|
+
.map((registration) => registration.routerVariable)
|
|
175
|
+
.filter((variable) => analysis.routerVariables.has(variable) && !mountedVariables.has(variable)));
|
|
176
|
+
if (unmountedVariables.size > 0) {
|
|
218
177
|
gaps.push({
|
|
219
|
-
extractor: 'endpoints',
|
|
220
|
-
|
|
221
|
-
message: `${analysis.file} declares routes on a router that docgen did not find mounted anywhere. ` +
|
|
222
|
-
'The paths shown are relative to wherever it is mounted, so they may be incomplete.',
|
|
178
|
+
extractor: 'endpoints', kind: 'router-not-mounted',
|
|
179
|
+
message: `${analysis.file} declares routes on a router that docgen did not find mounted anywhere. The paths shown are relative and may be incomplete.`,
|
|
223
180
|
source: { file: analysis.file },
|
|
224
181
|
});
|
|
225
182
|
}
|
|
226
183
|
for (const registration of analysis.registrations) {
|
|
227
|
-
const locations =
|
|
228
|
-
const
|
|
229
|
-
? [{ application: undefined, prefix: '' }]
|
|
230
|
-
: [...locations.entries()]
|
|
231
|
-
.
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
const workspace = owningWorkspace(analysis.file, args.workspaces ?? [{ dir: '', manifests: [] }]);
|
|
184
|
+
const locations = locationsByRouter.get(routerKey(analysis.file, registration.routerVariable)) ?? new Map();
|
|
185
|
+
const effective = locations.size === 0
|
|
186
|
+
? [{ application: undefined, prefix: '', resolved: false }]
|
|
187
|
+
: [...locations.entries()].flatMap(([application, values]) => [...values.values()].map((value) => ({ application, ...value })))
|
|
188
|
+
.sort((a, b) => compareStrings(a.application ?? '', b.application ?? '') || compareStrings(a.prefix, b.prefix));
|
|
189
|
+
for (const location of effective) {
|
|
190
|
+
const fullPath = joinPath(location.prefix, registration.path);
|
|
191
|
+
const workspace = owningWorkspace(analysis.file, workspaces);
|
|
236
192
|
entries.push({
|
|
237
193
|
id: `endpoint:${registration.method}:${fullPath}`,
|
|
238
194
|
source: { file: analysis.file, line: registration.line, column: registration.column },
|
|
239
|
-
extractionMethod: 'ast',
|
|
240
|
-
certainty: 'high',
|
|
241
|
-
method: registration.method,
|
|
242
|
-
path: fullPath,
|
|
195
|
+
extractionMethod: 'ast', certainty: 'high', method: registration.method, path: fullPath,
|
|
243
196
|
...(args.workspaces !== undefined && args.workspaces.length > 1 ? { workspace } : {}),
|
|
244
|
-
...(application === undefined
|
|
245
|
-
|
|
246
|
-
|
|
197
|
+
...(location.application === undefined
|
|
198
|
+
? { finalPathResolved: false }
|
|
199
|
+
: { application: location.application, finalPathResolved: location.resolved }),
|
|
200
|
+
params: paramsOf(fullPath), handler: { file: analysis.file, line: registration.line },
|
|
247
201
|
middleware: registration.middleware,
|
|
248
|
-
...(registration.requestShape === undefined
|
|
249
|
-
? {}
|
|
250
|
-
: { requestShape: registration.requestShape }),
|
|
202
|
+
...(registration.requestShape === undefined ? {} : { requestShape: registration.requestShape }),
|
|
251
203
|
});
|
|
252
204
|
}
|
|
253
205
|
}
|
|
254
206
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
207
|
+
const sourceOwnership = [];
|
|
208
|
+
for (const analysis of analyses.values()) {
|
|
209
|
+
const workspace = owningWorkspace(analysis.file, workspaces);
|
|
210
|
+
const applications = new Map();
|
|
211
|
+
const owners = new Set([
|
|
212
|
+
...analysis.routerVariables,
|
|
213
|
+
...analysis.applicationRoots.map((root) => root.owner),
|
|
214
|
+
...analysis.registrations.map((registration) => registration.routerVariable),
|
|
215
|
+
]);
|
|
216
|
+
for (const owner of owners) {
|
|
217
|
+
for (const [application, values] of locationsByRouter.get(routerKey(analysis.file, owner)) ?? []) {
|
|
218
|
+
for (const value of values.values()) {
|
|
219
|
+
const origin = analysis.routerVariables.has(owner) ? 'router' : 'application';
|
|
220
|
+
applications.set(`${application}\u0000${value.prefix}\u0000${value.resolved}\u0000${origin}`, {
|
|
221
|
+
application, prefix: value.prefix, finalPathResolved: value.resolved, origin,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
sourceOwnership.push({
|
|
227
|
+
file: analysis.file, workspace,
|
|
228
|
+
applications: [...applications.values()].sort((a, b) => compareStrings(a.application, b.application) || compareStrings(a.prefix, b.prefix)),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
return { entries, gaps: dedupeGaps(gaps), found: true, sourceOwnership };
|
|
264
232
|
}
|
|
265
|
-
/** The module symbol a mount refers to: `routes` or `routes.default`. */
|
|
266
233
|
function mountedSymbolName(argument) {
|
|
267
234
|
if (ts.isIdentifier(argument))
|
|
268
235
|
return argument.text;
|
|
269
|
-
if (ts.isPropertyAccessExpression(argument) &&
|
|
270
|
-
ts.isIdentifier(argument.expression) &&
|
|
271
|
-
ts.isIdentifier(argument.name) &&
|
|
272
|
-
argument.name.text === 'default') {
|
|
236
|
+
if (ts.isPropertyAccessExpression(argument) && ts.isIdentifier(argument.expression) && argument.name.text === 'default') {
|
|
273
237
|
return argument.expression.text;
|
|
274
238
|
}
|
|
275
239
|
return undefined;
|
|
@@ -279,180 +243,206 @@ export function analyseFile(file, contents) {
|
|
|
279
243
|
const bindings = readModuleBindings(source);
|
|
280
244
|
const importsExpress = [...bindings.imports.values()].some((binding) => binding.specifier === 'express');
|
|
281
245
|
const routerVariables = new Set();
|
|
282
|
-
const
|
|
246
|
+
const appRoots = new Map();
|
|
247
|
+
const aliases = new Map();
|
|
283
248
|
const registrations = [];
|
|
284
249
|
const mounts = [];
|
|
285
250
|
const unconfirmed = new Set();
|
|
286
|
-
|
|
251
|
+
const addAppRoot = (owner, discriminator = `${file}#${owner}`) => {
|
|
252
|
+
appRoots.set(owner, { owner, discriminator });
|
|
253
|
+
};
|
|
287
254
|
walk(source, (node) => {
|
|
288
|
-
if (
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
255
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
256
|
+
const initializer = node.initializer;
|
|
257
|
+
if (initializer !== undefined && ts.isCallExpression(initializer)) {
|
|
258
|
+
if (isRouterChain(initializer))
|
|
259
|
+
routerVariables.add(node.name.text);
|
|
260
|
+
else {
|
|
261
|
+
const factory = factoryName(initializer);
|
|
262
|
+
if (factory !== undefined && ROUTER_FACTORIES.has(factory))
|
|
263
|
+
routerVariables.add(node.name.text);
|
|
264
|
+
else if (factory !== undefined && APP_FACTORIES.has(factory))
|
|
265
|
+
addAppRoot(node.name.text);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (node.type !== undefined)
|
|
269
|
+
classifyTyped(node.name.text, node.type.getText(source), routerVariables, addAppRoot);
|
|
298
270
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
? callee.text
|
|
302
|
-
: ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.name)
|
|
303
|
-
? callee.name.text
|
|
304
|
-
: undefined;
|
|
305
|
-
if (name === undefined)
|
|
306
|
-
return;
|
|
307
|
-
if (ROUTER_FACTORIES.has(name))
|
|
308
|
-
routerVariables.add(node.name.text);
|
|
309
|
-
else if (APP_FACTORIES.has(name))
|
|
310
|
-
appVariables.add(node.name.text);
|
|
311
|
-
});
|
|
312
|
-
walk(source, (node) => {
|
|
313
|
-
if (ts.isExportAssignment(node) && node.isExportEquals !== true &&
|
|
314
|
-
isRouterChain(node.expression)) {
|
|
315
|
-
routerVariables.add('$default');
|
|
271
|
+
if (ts.isParameter(node) && ts.isIdentifier(node.name) && node.type !== undefined) {
|
|
272
|
+
classifyTyped(node.name.text, node.type.getText(source), routerVariables, addAppRoot);
|
|
316
273
|
}
|
|
274
|
+
if (ts.isPropertyDeclaration(node) && isSimplePropertyName(node.name)) {
|
|
275
|
+
const className = enclosingClassName(node);
|
|
276
|
+
if (className === undefined)
|
|
277
|
+
return;
|
|
278
|
+
const owner = classOwner(className, node.name.text);
|
|
279
|
+
const typeText = node.type?.getText(source);
|
|
280
|
+
if (typeText !== undefined && APP_TYPE_NAMES.has(typeText))
|
|
281
|
+
addAppRoot(owner, `${file}#${className}.${node.name.text}`);
|
|
282
|
+
if (node.initializer !== undefined && ts.isCallExpression(node.initializer) && APP_FACTORIES.has(factoryName(node.initializer) ?? '')) {
|
|
283
|
+
addAppRoot(owner, `${file}#${className}.${node.name.text}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
287
|
+
const owner = thisPropertyOwner(node.left);
|
|
288
|
+
if (owner !== undefined && ts.isCallExpression(node.right) && APP_FACTORIES.has(factoryName(node.right) ?? '')) {
|
|
289
|
+
const [className, property] = splitClassOwner(owner);
|
|
290
|
+
addAppRoot(owner, `${file}#${className}.${property}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (ts.isExportAssignment(node) && node.isExportEquals !== true && isRouterChain(node.expression))
|
|
294
|
+
routerVariables.add('$default');
|
|
317
295
|
});
|
|
318
|
-
//
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
appVariables.add(name);
|
|
334
|
-
});
|
|
296
|
+
// Resolve harmless aliases such as `const server = this.app` before calls are
|
|
297
|
+
// inspected. Fixed-point iteration also handles alias-of-alias chains.
|
|
298
|
+
let changed = true;
|
|
299
|
+
while (changed) {
|
|
300
|
+
changed = false;
|
|
301
|
+
walk(source, (node) => {
|
|
302
|
+
if (!ts.isVariableDeclaration(node) || !ts.isIdentifier(node.name) || node.initializer === undefined)
|
|
303
|
+
return;
|
|
304
|
+
const target = expressionOwner(node.initializer, aliases);
|
|
305
|
+
if (target !== undefined && (appRoots.has(target) || routerVariables.has(target)) && aliases.get(node.name.text) !== target) {
|
|
306
|
+
aliases.set(node.name.text, target);
|
|
307
|
+
changed = true;
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
335
311
|
walk(source, (node) => {
|
|
336
|
-
if (!ts.isCallExpression(node))
|
|
312
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression))
|
|
337
313
|
return;
|
|
338
314
|
const callee = node.expression;
|
|
339
|
-
|
|
340
|
-
return;
|
|
341
|
-
const routerVariable = ts.isIdentifier(callee.expression)
|
|
342
|
-
? callee.expression.text
|
|
343
|
-
: ownerOfChain(node);
|
|
315
|
+
const routerVariable = expressionOwner(callee.expression, aliases) ?? ownerOfChain(node);
|
|
344
316
|
if (routerVariable === undefined)
|
|
345
317
|
return;
|
|
346
318
|
const methodName = callee.name.text.toLowerCase();
|
|
347
319
|
const position = positionOf(source, node, file);
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
if (!routerVariables.has(routerVariable) && !appVariables.has(routerVariable)) {
|
|
352
|
-
// It may still be a router docgen could not confirm — an untyped JS
|
|
353
|
-
// parameter, say. Recorded so the omission is visible rather than
|
|
354
|
-
// silent, but never emitted as an endpoint.
|
|
355
|
-
const first = node.arguments[0];
|
|
356
|
-
const candidatePath = literalString(first);
|
|
357
|
-
// Requiring express in the file keeps HTTP clients out: `axios.get('/api/x')`
|
|
358
|
-
// in a React app is a request, not a route, and reporting it as an
|
|
359
|
-
// unconfirmed router would claim the repo runs a server it does not.
|
|
360
|
-
if (importsExpress &&
|
|
361
|
-
(HTTP_METHODS.has(methodName) || methodName === 'use') &&
|
|
362
|
-
candidatePath !== undefined &&
|
|
363
|
-
candidatePath.startsWith('/')) {
|
|
320
|
+
if (!routerVariables.has(routerVariable) && !appRoots.has(routerVariable)) {
|
|
321
|
+
const candidatePath = literalString(node.arguments[0]);
|
|
322
|
+
if (importsExpress && (HTTP_METHODS.has(methodName) || methodName === 'use') && candidatePath?.startsWith('/') === true)
|
|
364
323
|
unconfirmed.add(routerVariable);
|
|
365
|
-
}
|
|
366
324
|
return;
|
|
367
325
|
}
|
|
368
326
|
if (methodName === 'use') {
|
|
369
327
|
const first = node.arguments[0];
|
|
370
|
-
const
|
|
371
|
-
const hasPrefix = literalPrefix?.startsWith('/') === true;
|
|
372
|
-
const prefix = hasPrefix ? literalPrefix : '';
|
|
328
|
+
const hasPrefix = first !== undefined && node.arguments.length > 1 && isPrefixExpression(first, source, bindings);
|
|
373
329
|
const mounted = hasPrefix ? node.arguments.slice(1) : node.arguments;
|
|
374
330
|
for (const argument of mounted) {
|
|
375
|
-
// `app.use('/x', routes)` and the ESM/CJS interop form
|
|
376
|
-
// `app.use('/x', routes.default)` both mount the same module.
|
|
377
331
|
const symbol = mountedSymbolName(argument);
|
|
378
|
-
if (symbol !== undefined)
|
|
379
|
-
mounts.push({
|
|
380
|
-
|
|
332
|
+
if (symbol !== undefined)
|
|
333
|
+
mounts.push({
|
|
334
|
+
routerVariable, prefix: hasPrefix ? literalString(first) ?? '' : '',
|
|
335
|
+
...(hasPrefix && literalString(first) === undefined ? { prefixExpression: first } : {}),
|
|
336
|
+
symbol, line: position.line ?? 1,
|
|
337
|
+
});
|
|
381
338
|
}
|
|
382
339
|
return;
|
|
383
340
|
}
|
|
384
341
|
if (!HTTP_METHODS.has(methodName))
|
|
385
342
|
return;
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
if (routePath === undefined) {
|
|
389
|
-
// A computed path cannot be resolved without running the module. Emitting
|
|
390
|
-
// nothing here is deliberate: a wrong URL is worse than a missing one.
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
// Express route paths are rooted. A non-rooted string is something else —
|
|
394
|
-
// a header name, an absolute URL passed to an HTTP client, an event name.
|
|
395
|
-
if (!routePath.startsWith('/'))
|
|
343
|
+
const routePath = literalString(node.arguments[0]);
|
|
344
|
+
if (routePath === undefined || !routePath.startsWith('/'))
|
|
396
345
|
return;
|
|
397
346
|
const middleware = [];
|
|
398
347
|
let requestShape;
|
|
399
|
-
// In Express the final argument is the route handler; everything between
|
|
400
|
-
// the path and it is middleware. Listing the handler as middleware would
|
|
401
|
-
// misreport the auth chain, which is the field a reader most relies on.
|
|
402
348
|
const handlerArguments = node.arguments.slice(1);
|
|
403
349
|
const middlewareArguments = handlerArguments.length > 1 ? handlerArguments.slice(0, -1) : [];
|
|
404
350
|
for (const argument of middlewareArguments) {
|
|
405
|
-
if (ts.isIdentifier(argument))
|
|
351
|
+
if (ts.isIdentifier(argument))
|
|
406
352
|
middleware.push(argument.text);
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
// `validate(CreateOrderSchema)` names the request contract. The
|
|
413
|
-
// validator library is not assumed — only that this argument is what
|
|
414
|
-
// the route validates against.
|
|
415
|
-
const shapeArgument = argument.arguments[0];
|
|
416
|
-
if (requestShape === undefined && shapeArgument !== undefined && ts.isIdentifier(shapeArgument)) {
|
|
417
|
-
requestShape = { name: shapeArgument.text, kind: 'validator-argument' };
|
|
353
|
+
else if (ts.isCallExpression(argument) && ts.isIdentifier(argument.expression)) {
|
|
354
|
+
middleware.push(`${argument.expression.text}()`);
|
|
355
|
+
const shape = argument.arguments[0];
|
|
356
|
+
if (requestShape === undefined && shape !== undefined && ts.isIdentifier(shape)) {
|
|
357
|
+
requestShape = { name: shape.text, kind: 'validator-argument' };
|
|
418
358
|
}
|
|
419
359
|
}
|
|
420
360
|
}
|
|
421
361
|
registrations.push({
|
|
422
|
-
routerVariable,
|
|
423
|
-
|
|
424
|
-
path: routePath,
|
|
425
|
-
middleware,
|
|
426
|
-
...(requestShape === undefined ? {} : { requestShape }),
|
|
427
|
-
line: position.line ?? 1,
|
|
428
|
-
column: position.column ?? 1,
|
|
362
|
+
routerVariable, method: methodName.toUpperCase(), path: routePath, middleware,
|
|
363
|
+
...(requestShape === undefined ? {} : { requestShape }), line: position.line ?? 1, column: position.column ?? 1,
|
|
429
364
|
});
|
|
430
365
|
});
|
|
431
366
|
const defaultExport = bindings.exports.get('default');
|
|
432
367
|
if (defaultExport?.kind === 'local' && routerVariables.has(defaultExport.localName)) {
|
|
433
368
|
mounts.push({ routerVariable: '$default', prefix: '', symbol: defaultExport.localName, line: 1 });
|
|
434
369
|
}
|
|
435
|
-
for (const name of [...routerVariables, ...
|
|
370
|
+
for (const name of [...routerVariables, ...appRoots.keys(), ...aliases.keys()])
|
|
436
371
|
unconfirmed.delete(name);
|
|
437
372
|
return {
|
|
438
|
-
file,
|
|
439
|
-
|
|
440
|
-
routerVariables,
|
|
441
|
-
appVariables,
|
|
442
|
-
registrations,
|
|
443
|
-
mounts,
|
|
444
|
-
unconfirmedRouters: [...unconfirmed].sort(),
|
|
373
|
+
file, source, bindings, routerVariables, applicationRoots: [...appRoots.values()].sort((a, b) => compareStrings(a.owner, b.owner)),
|
|
374
|
+
registrations, mounts, unconfirmedRouters: [...unconfirmed].sort(compareStrings),
|
|
445
375
|
};
|
|
446
376
|
}
|
|
377
|
+
function classifyTyped(name, typeText, routers, addApp) {
|
|
378
|
+
if (ROUTER_TYPE_NAMES.has(typeText))
|
|
379
|
+
routers.add(name);
|
|
380
|
+
else if (APP_TYPE_NAMES.has(typeText))
|
|
381
|
+
addApp(name);
|
|
382
|
+
}
|
|
383
|
+
function factoryName(call) {
|
|
384
|
+
const callee = call.expression;
|
|
385
|
+
return ts.isIdentifier(callee) ? callee.text
|
|
386
|
+
: ts.isPropertyAccessExpression(callee) ? callee.name.text
|
|
387
|
+
: undefined;
|
|
388
|
+
}
|
|
389
|
+
function classOwner(className, property) {
|
|
390
|
+
return `this:${className}:${property}`;
|
|
391
|
+
}
|
|
392
|
+
function splitClassOwner(owner) {
|
|
393
|
+
const [, className = 'Anonymous', property = 'app'] = owner.split(':');
|
|
394
|
+
return [className, property];
|
|
395
|
+
}
|
|
396
|
+
function enclosingClassName(node) {
|
|
397
|
+
let current = node;
|
|
398
|
+
while (current !== undefined) {
|
|
399
|
+
if ((ts.isClassDeclaration(current) || ts.isClassExpression(current)) && current.name !== undefined)
|
|
400
|
+
return current.name.text;
|
|
401
|
+
current = current.parent;
|
|
402
|
+
}
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
function thisPropertyOwner(node) {
|
|
406
|
+
if (!ts.isPropertyAccessExpression(node) || node.expression.kind !== ts.SyntaxKind.ThisKeyword)
|
|
407
|
+
return undefined;
|
|
408
|
+
const className = enclosingClassName(node);
|
|
409
|
+
return className === undefined ? undefined : classOwner(className, node.name.text);
|
|
410
|
+
}
|
|
411
|
+
function expressionOwner(expression, aliases) {
|
|
412
|
+
if (ts.isIdentifier(expression))
|
|
413
|
+
return aliases.get(expression.text) ?? expression.text;
|
|
414
|
+
return thisPropertyOwner(expression);
|
|
415
|
+
}
|
|
416
|
+
function isSimplePropertyName(name) {
|
|
417
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name);
|
|
418
|
+
}
|
|
419
|
+
function isPrefixExpression(expression, source, bindings) {
|
|
420
|
+
const literal = literalString(expression);
|
|
421
|
+
if (literal !== undefined)
|
|
422
|
+
return literal.startsWith('/');
|
|
423
|
+
if (ts.isTemplateExpression(expression) || ts.isBinaryExpression(expression) ||
|
|
424
|
+
ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression) ||
|
|
425
|
+
ts.isCallExpression(expression) || ts.isParenthesizedExpression(expression))
|
|
426
|
+
return true;
|
|
427
|
+
if (!ts.isIdentifier(expression))
|
|
428
|
+
return false;
|
|
429
|
+
if (bindings.imports.has(expression.text))
|
|
430
|
+
return /(?:path|prefix|base|mount|url)$/i.test(expression.text);
|
|
431
|
+
let initializer;
|
|
432
|
+
walk(source, (node) => {
|
|
433
|
+
if (initializer !== undefined || !ts.isVariableDeclaration(node) || !ts.isIdentifier(node.name))
|
|
434
|
+
return;
|
|
435
|
+
if (node.name.text === expression.text)
|
|
436
|
+
initializer = node.initializer;
|
|
437
|
+
});
|
|
438
|
+
return initializer !== undefined;
|
|
439
|
+
}
|
|
447
440
|
function isRouterChain(expression) {
|
|
448
441
|
let current = expression;
|
|
449
|
-
while (ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression))
|
|
442
|
+
while (ts.isCallExpression(current) && ts.isPropertyAccessExpression(current.expression))
|
|
450
443
|
current = current.expression.expression;
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
((ts.isIdentifier(current.expression) && ROUTER_FACTORIES.has(current.expression.text)) ||
|
|
454
|
-
(ts.isPropertyAccessExpression(current.expression) &&
|
|
455
|
-
ts.isIdentifier(current.expression.name) && ROUTER_FACTORIES.has(current.expression.name.text)));
|
|
444
|
+
return ts.isCallExpression(current) && ((ts.isIdentifier(current.expression) && ROUTER_FACTORIES.has(current.expression.text)) ||
|
|
445
|
+
(ts.isPropertyAccessExpression(current.expression) && ROUTER_FACTORIES.has(current.expression.name.text)));
|
|
456
446
|
}
|
|
457
447
|
function ownerOfChain(node) {
|
|
458
448
|
let current = node;
|
|
@@ -468,4 +458,11 @@ function ownerOfChain(node) {
|
|
|
468
458
|
}
|
|
469
459
|
return undefined;
|
|
470
460
|
}
|
|
461
|
+
function dedupeGaps(gaps) {
|
|
462
|
+
const unique = new Map();
|
|
463
|
+
for (const gap of gaps)
|
|
464
|
+
unique.set(`${gap.kind}\u0000${gap.source?.file ?? ''}\u0000${gap.source?.line ?? 0}\u0000${gap.message}`, gap);
|
|
465
|
+
return [...unique.values()].sort((a, b) => compareStrings(a.kind, b.kind) || compareStrings(a.source?.file ?? '', b.source?.file ?? '') ||
|
|
466
|
+
(a.source?.line ?? 0) - (b.source?.line ?? 0) || compareStrings(a.message, b.message));
|
|
467
|
+
}
|
|
471
468
|
//# sourceMappingURL=express.js.map
|