@codeyam/codeyam-cli 0.1.22 → 0.1.24
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/analyzer-template/.build-info.json +7 -7
- package/analyzer-template/log.txt +3 -3
- package/analyzer-template/packages/ai/src/lib/astScopes/methodSemantics.ts +135 -0
- package/analyzer-template/packages/ai/src/lib/astScopes/nodeToSource.ts +19 -0
- package/analyzer-template/packages/ai/src/lib/astScopes/paths.ts +11 -4
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateDataStructure.ts +5 -1
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.ts +1632 -1554
- package/analyzer-template/packages/database/src/lib/loadAnalysis.ts +7 -1
- package/analyzer-template/packages/github/dist/database/src/lib/loadAnalysis.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/database/src/lib/loadAnalysis.js +7 -1
- package/analyzer-template/packages/github/dist/database/src/lib/loadAnalysis.js.map +1 -1
- package/analyzer-template/project/runMultiScenarioServer.ts +26 -3
- package/background/src/lib/virtualized/project/runMultiScenarioServer.js +23 -3
- package/background/src/lib/virtualized/project/runMultiScenarioServer.js.map +1 -1
- package/codeyam-cli/src/commands/__tests__/editor.analyzeImportsArgs.test.js +47 -0
- package/codeyam-cli/src/commands/__tests__/editor.analyzeImportsArgs.test.js.map +1 -0
- package/codeyam-cli/src/commands/__tests__/editor.auditNoAutoAnalysis.test.js +17 -9
- package/codeyam-cli/src/commands/__tests__/editor.auditNoAutoAnalysis.test.js.map +1 -1
- package/codeyam-cli/src/commands/__tests__/editor.designSystem.test.js +30 -0
- package/codeyam-cli/src/commands/__tests__/editor.designSystem.test.js.map +1 -0
- package/codeyam-cli/src/commands/editor.js +207 -41
- package/codeyam-cli/src/commands/editor.js.map +1 -1
- package/codeyam-cli/src/commands/editorAnalyzeImportsArgs.js +23 -0
- package/codeyam-cli/src/commands/editorAnalyzeImportsArgs.js.map +1 -0
- package/codeyam-cli/src/data/designSystems.js +27 -0
- package/codeyam-cli/src/data/designSystems.js.map +1 -0
- package/codeyam-cli/src/utils/__tests__/editorApi.test.js +44 -0
- package/codeyam-cli/src/utils/__tests__/editorApi.test.js.map +1 -1
- package/codeyam-cli/src/utils/__tests__/editorAudit.test.js +103 -1
- package/codeyam-cli/src/utils/__tests__/editorAudit.test.js.map +1 -1
- package/codeyam-cli/src/utils/__tests__/editorScenarios.test.js +140 -1
- package/codeyam-cli/src/utils/__tests__/editorScenarios.test.js.map +1 -1
- package/codeyam-cli/src/utils/__tests__/editorSeedAdapter.test.js +50 -1
- package/codeyam-cli/src/utils/__tests__/editorSeedAdapter.test.js.map +1 -1
- package/codeyam-cli/src/utils/editorApi.js +16 -0
- package/codeyam-cli/src/utils/editorApi.js.map +1 -1
- package/codeyam-cli/src/utils/editorAudit.js +38 -2
- package/codeyam-cli/src/utils/editorAudit.js.map +1 -1
- package/codeyam-cli/src/utils/editorScenarios.js +60 -0
- package/codeyam-cli/src/utils/editorScenarios.js.map +1 -1
- package/codeyam-cli/src/utils/editorSeedAdapter.js +42 -2
- package/codeyam-cli/src/utils/editorSeedAdapter.js.map +1 -1
- package/codeyam-cli/src/utils/queue/__tests__/job.interactiveStart.test.js +159 -0
- package/codeyam-cli/src/utils/queue/__tests__/job.interactiveStart.test.js.map +1 -0
- package/codeyam-cli/src/utils/queue/job.js +9 -1
- package/codeyam-cli/src/utils/queue/job.js.map +1 -1
- package/codeyam-cli/src/webserver/__tests__/api.interactive-switch-scenario.test.js +98 -0
- package/codeyam-cli/src/webserver/__tests__/api.interactive-switch-scenario.test.js.map +1 -0
- package/codeyam-cli/src/webserver/__tests__/editorProxy.test.js +30 -11
- package/codeyam-cli/src/webserver/__tests__/editorProxy.test.js.map +1 -1
- package/codeyam-cli/src/webserver/app/routes/api.interactive-switch-scenario.js +34 -0
- package/codeyam-cli/src/webserver/app/routes/api.interactive-switch-scenario.js.map +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{InteractivePreview-CKeQT5Ty.js → InteractivePreview-DtYTSPL2.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{ScenarioViewer-DUMfcNVK.js → ScenarioViewer-CefgqbCr.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/Spinner-Bc8BG-Lw.js +34 -0
- package/codeyam-cli/src/webserver/build/client/assets/{_index-BAWd-Xjf.js → _index-C1YkzTAV.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{activity.(_tab)-BOARiB-g.js → activity.(_tab)-yH46LLUz.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/api.editor-verify-routes-l0sNRNKZ.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/api.interactive-switch-scenario-l0sNRNKZ.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{dev.empty-C8y4mmyv.js → dev.empty-CRepiabR.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{editor.entity.(_sha)-aIHKLB-m.js → editor.entity.(_sha)-oBrbke_R.js} +19 -19
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha._-ByHz6rAQ.js → entity._sha._-DYJRGiDI.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha.scenarios._scenarioId.dev-CmLO432x.js → entity._sha.scenarios._scenarioId.dev-wdiwx5-Z.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha.scenarios._scenarioId.fullscreen-Bz9sCUF_.js → entity._sha.scenarios._scenarioId.fullscreen-BrkN-40Y.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha_.create-scenario-DQM8E7L4.js → entity._sha_.create-scenario-DxfhekTZ.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha_.edit._scenarioId-CAoXLsQr.js → entity._sha_.edit._scenarioId-CRXJWmpB.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/manifest-694b698a.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{root-D2_tktnk.js → root-DXjFYOxD.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/useLastLogLine-D9QZKaLJ.js +2 -0
- package/codeyam-cli/src/webserver/build/server/assets/{analysisRunner-DjF-soOH.js → analysisRunner-zEYtiv0T.js} +1 -1
- package/codeyam-cli/src/webserver/build/server/assets/{index-nAvHGWbz.js → index-CcHPEbhi.js} +1 -1
- package/codeyam-cli/src/webserver/build/server/assets/{init-XhpIt-OT.js → init-D68IyWbU.js} +1 -1
- package/codeyam-cli/src/webserver/build/server/assets/{server-build-DVwiibFu.js → server-build-Cxzo0Zp2.js} +192 -148
- package/codeyam-cli/src/webserver/build/server/index.js +1 -1
- package/codeyam-cli/src/webserver/build-info.json +5 -5
- package/codeyam-cli/src/webserver/editorProxy.js +55 -3
- package/codeyam-cli/src/webserver/editorProxy.js.map +1 -1
- package/codeyam-cli/src/webserver/terminalServer.js +1 -2
- package/codeyam-cli/src/webserver/terminalServer.js.map +1 -1
- package/codeyam-cli/templates/codeyam-editor-reference.md +8 -6
- package/codeyam-cli/templates/design-systems/clean-dashboard-design-system.md +255 -0
- package/codeyam-cli/templates/design-systems/editorial-design-system.md +267 -0
- package/codeyam-cli/templates/design-systems/mono-brutalist-design-system.md +256 -0
- package/codeyam-cli/templates/design-systems/neo-brutalist-design-system.md +294 -0
- package/codeyam-cli/templates/nextjs-prisma-sqlite/seed-adapter.ts +47 -34
- package/codeyam-cli/templates/seed-adapters/supabase.ts +14 -5
- package/package.json +1 -1
- package/packages/ai/src/lib/astScopes/methodSemantics.js +99 -0
- package/packages/ai/src/lib/astScopes/methodSemantics.js.map +1 -1
- package/packages/ai/src/lib/astScopes/nodeToSource.js +16 -0
- package/packages/ai/src/lib/astScopes/nodeToSource.js.map +1 -1
- package/packages/ai/src/lib/astScopes/paths.js +12 -3
- package/packages/ai/src/lib/astScopes/paths.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js +5 -1
- package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js +1330 -1270
- package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js.map +1 -1
- package/packages/database/src/lib/loadAnalysis.js +7 -1
- package/packages/database/src/lib/loadAnalysis.js.map +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/Spinner-D0LgAaSa.js +0 -34
- package/codeyam-cli/src/webserver/build/client/assets/manifest-bcbb3d49.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/useLastLogLine-BNd5hYuW.js +0 -2
|
@@ -37,6 +37,22 @@ const COLLECTION_METHOD_PATTERN = /\.(?:has|delete|add|clear|get|set)\(/;
|
|
|
37
37
|
function isCollectionMethodPath(path) {
|
|
38
38
|
return COLLECTION_METHOD_PATTERN.test(path);
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a path contains an inline object literal inside a function call argument.
|
|
42
|
+
* e.g., setUndoEntry({ label: '...', undo: () => {} }) has '{' inside '(' ')'.
|
|
43
|
+
* These paths are call-site snapshots where the source code text was captured
|
|
44
|
+
* as the path. They don't represent schema structure and are very expensive to
|
|
45
|
+
* parse (avg 319 chars). They account for ~55% of equivalencies in complex entities.
|
|
46
|
+
*/
|
|
47
|
+
const INLINE_OBJECT_ARG_PATTERN = /\([^)]*\{[^}]*:/;
|
|
48
|
+
function hasInlineObjectArg(path) {
|
|
49
|
+
// Match function calls containing object literals with key-value pairs.
|
|
50
|
+
// Pattern: open paren, then { with a : inside before closing }.
|
|
51
|
+
// e.g., setUndoEntry({ label: '...' }) matches
|
|
52
|
+
// e.g., find(item) does NOT match
|
|
53
|
+
// e.g., fn({a:1, b:2}) matches
|
|
54
|
+
return INLINE_OBJECT_ARG_PATTERN.test(path);
|
|
55
|
+
}
|
|
40
56
|
// Primitive types that should not have child paths
|
|
41
57
|
const PRIMITIVE_TYPES = new Set([
|
|
42
58
|
'number',
|
|
@@ -122,6 +138,10 @@ function bestValueFromOptions(options) {
|
|
|
122
138
|
/** Timeout (ms) for the merge operation. Throws DataStructureTimeoutError if exceeded.
|
|
123
139
|
* All successful merges complete in <300ms. Anything exceeding 2s is pathological. */
|
|
124
140
|
const MERGE_TIMEOUT_MS = 2000;
|
|
141
|
+
/** Cap for schema size during postfix application and dep copy.
|
|
142
|
+
* Successful merges produce <3K ret keys. Beyond 5K, further entries
|
|
143
|
+
* are cross-products of unrelated equivalencies — noise, not signal. */
|
|
144
|
+
const SCHEMA_KEY_CAP = 5000;
|
|
125
145
|
export class DataStructureTimeoutError extends Error {
|
|
126
146
|
constructor(entityName, elapsedMs) {
|
|
127
147
|
super(`Data structure merge timed out for ${entityName} after ${Math.round(elapsedMs / 1000)}s (limit: ${MERGE_TIMEOUT_MS / 1000}s)`);
|
|
@@ -148,579 +168,591 @@ export default function mergeInDependentDataStructure({ importedExports, depende
|
|
|
148
168
|
dependencySchemas: {},
|
|
149
169
|
environmentVariables: [...(dataStructure.environmentVariables || [])],
|
|
150
170
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
171
|
+
try {
|
|
172
|
+
// Build a set of functions that have multiple DIFFERENT type parameters.
|
|
173
|
+
// For these functions, we must NOT normalize paths to avoid merging different schemas.
|
|
174
|
+
// e.g., if we have both useFetcher<{ data: UserData }>() and useFetcher<{ data: ConfigData }>(),
|
|
175
|
+
// they must stay separate and not both become 'returnValue'.
|
|
176
|
+
const functionsWithMultipleTypeParams = new Set();
|
|
177
|
+
const typeParamsByFunction = {};
|
|
178
|
+
// Helper to scan a schema for type parameters
|
|
179
|
+
const scanSchemaForTypeParams = (schema) => {
|
|
180
|
+
for (const schemaPath of Object.keys(schema ?? {})) {
|
|
181
|
+
const parts = splitOutsideParenthesesAndArrays(schemaPath);
|
|
182
|
+
if (parts.length > 0) {
|
|
183
|
+
const firstPart = parts[0];
|
|
184
|
+
const typeParam = getTypeParameter(firstPart);
|
|
185
|
+
if (typeParam) {
|
|
186
|
+
const baseName = cleanFunctionName(firstPart);
|
|
187
|
+
typeParamsByFunction[baseName] || (typeParamsByFunction[baseName] = new Set());
|
|
188
|
+
typeParamsByFunction[baseName].add(typeParam);
|
|
189
|
+
if (typeParamsByFunction[baseName].size > 1) {
|
|
190
|
+
functionsWithMultipleTypeParams.add(baseName);
|
|
191
|
+
}
|
|
170
192
|
}
|
|
171
193
|
}
|
|
172
194
|
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
scanSchemaForTypeParams(dependencySchemas[filePath][name]?.returnValueSchema);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
let equivalentSchemaPaths = [];
|
|
184
|
-
// O(1) index for findOrCreateEquivalentSchemaPathsEntry.
|
|
185
|
-
// Maps "(rootPath)::(normalizedFuncName)" → the entry containing that root.
|
|
186
|
-
// This replaces the O(E) linear search that was causing O(E²) gather performance.
|
|
187
|
-
const espIndex = new Map();
|
|
188
|
-
const espIndexKey = (path, functionName) => {
|
|
189
|
-
const normalized = cleanFunctionName(functionName);
|
|
190
|
-
const funcKey = normalized === rootScopeName ? '__self__' : normalized || '__self__';
|
|
191
|
-
return `${path}::${funcKey}`;
|
|
192
|
-
};
|
|
193
|
-
const updateEspIndex = (entry) => {
|
|
194
|
-
for (const root of entry.equivalentRoots) {
|
|
195
|
-
const funcName = root.function?.name ?? rootScopeName;
|
|
196
|
-
espIndex.set(espIndexKey(root.schemaRootPath, funcName), entry);
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
// Pre-build a lookup map from cleaned function name to dependency for O(1) lookups.
|
|
200
|
-
// This avoids O(n) linear search in findRelevantDependency which was causing O(n²) performance.
|
|
201
|
-
const dependencyByCleanedName = new Map();
|
|
202
|
-
for (const dep of importedExports) {
|
|
203
|
-
const cleanedName = cleanFunctionName(dep.name);
|
|
204
|
-
if (!dependencyByCleanedName.has(cleanedName)) {
|
|
205
|
-
dependencyByCleanedName.set(cleanedName, dep);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
const findRelevantDependency = (functionName) => {
|
|
209
|
-
return dependencyByCleanedName.get(cleanFunctionName(functionName));
|
|
210
|
-
};
|
|
211
|
-
const findRelevantDependentDataStructure = (functionName) => {
|
|
212
|
-
const dependency = findRelevantDependency(functionName);
|
|
213
|
-
if (!dependency)
|
|
214
|
-
return;
|
|
215
|
-
return dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
216
|
-
};
|
|
217
|
-
const findRelevantDependentAnalysisDataStructure = (functionName) => {
|
|
218
|
-
const dependency = findRelevantDependency(functionName);
|
|
219
|
-
if (!dependency)
|
|
220
|
-
return;
|
|
221
|
-
const dependentAnalysis = dependentAnalyses[dependency.filePath]?.[dependency.name];
|
|
222
|
-
if (!dependentAnalysis?.metadata?.mergedDataStructure) {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
return dependentAnalysis.metadata.mergedDataStructure;
|
|
226
|
-
};
|
|
227
|
-
const findOrCreateDependentSchemas = (dependency) => {
|
|
228
|
-
var _a, _b;
|
|
229
|
-
const { filePath, name } = dependency;
|
|
230
|
-
(_a = mergedDataStructure.dependencySchemas)[filePath] || (_a[filePath] = {});
|
|
231
|
-
(_b = mergedDataStructure.dependencySchemas[filePath])[name] || (_b[name] = {
|
|
232
|
-
signatureSchema: {},
|
|
233
|
-
returnValueSchema: {},
|
|
234
|
-
});
|
|
235
|
-
return mergedDataStructure.dependencySchemas[filePath][name];
|
|
236
|
-
};
|
|
237
|
-
const cleanSchema = (schema, context) => {
|
|
238
|
-
transformationTracer.traceSchemaTransform(rootScopeName, 'cleanKnownObjectFunctionsFromMapping', schema, cleanKnownObjectFunctionsFromMapping, context);
|
|
239
|
-
};
|
|
240
|
-
// Cache translatePath results — the same path is often translated multiple times
|
|
241
|
-
// (once per equivalency entry that references it). Avoids redundant
|
|
242
|
-
// splitOutsideParenthesesAndArrays calls on long paths.
|
|
243
|
-
const translatePathCache = new Map();
|
|
244
|
-
const translatePath = (path, dependencyName) => {
|
|
245
|
-
const cacheKey = `${dependencyName}\0${path}`;
|
|
246
|
-
const cached = translatePathCache.get(cacheKey);
|
|
247
|
-
if (cached !== undefined)
|
|
248
|
-
return cached;
|
|
249
|
-
let result = path;
|
|
250
|
-
if (path.startsWith(dependencyName)) {
|
|
251
|
-
const pathParts = splitOutsideParenthesesAndArrays(path);
|
|
252
|
-
if (pathParts.length > 1) {
|
|
253
|
-
if (pathParts[1].startsWith('functionCallReturnValue')) {
|
|
254
|
-
// Check if this function has multiple DIFFERENT type parameters.
|
|
255
|
-
// If so, DON'T normalize to returnValue - keep the full path to avoid
|
|
256
|
-
// merging different type-parameterized variants together.
|
|
257
|
-
const baseName = cleanFunctionName(pathParts[0]);
|
|
258
|
-
if (!functionsWithMultipleTypeParams.has(baseName)) {
|
|
259
|
-
// functionCallReturnValue immediately follows - normalize to returnValue
|
|
260
|
-
result = joinParenthesesAndArrays([
|
|
261
|
-
'returnValue',
|
|
262
|
-
...pathParts.slice(2),
|
|
263
|
-
]);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
else if (pathParts[0].endsWith(')') &&
|
|
267
|
-
pathParts[1].startsWith('signature[')) {
|
|
268
|
-
// Hook-style with signature access
|
|
269
|
-
result = joinParenthesesAndArrays(pathParts.slice(1));
|
|
270
|
-
}
|
|
195
|
+
};
|
|
196
|
+
// Scan the root entity's schema
|
|
197
|
+
scanSchemaForTypeParams(dataStructure.returnValueSchema);
|
|
198
|
+
// Also scan all dependency schemas for type parameters
|
|
199
|
+
for (const filePath of Object.keys(dependencySchemas ?? {})) {
|
|
200
|
+
for (const name of Object.keys(dependencySchemas[filePath] ?? {})) {
|
|
201
|
+
scanSchemaForTypeParams(dependencySchemas[filePath][name]?.returnValueSchema);
|
|
271
202
|
}
|
|
272
203
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const byFirstPart = new Map();
|
|
288
|
-
for (const path in schema) {
|
|
289
|
-
checkDeadline();
|
|
290
|
-
let parts = splitOutsideParenthesesAndArrays(path);
|
|
291
|
-
if (parts[0].startsWith(functionName)) {
|
|
292
|
-
const baseName = cleanFunctionName(parts[0]);
|
|
293
|
-
if (!functionsWithMultipleTypeParams.has(baseName)) {
|
|
294
|
-
parts =
|
|
295
|
-
parts[1] === 'functionCallReturnValue'
|
|
296
|
-
? ['returnValue', ...parts.slice(2)]
|
|
297
|
-
: parts.slice(1);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
const entry = { path, parts };
|
|
301
|
-
// Index by the base of the first part (before any function call args)
|
|
302
|
-
const firstPart = parts[0] ?? '';
|
|
303
|
-
const parenIdx = firstPart.indexOf('(');
|
|
304
|
-
const firstPartBase = parenIdx >= 0 ? firstPart.slice(0, parenIdx) : firstPart;
|
|
305
|
-
let bucket = byFirstPart.get(firstPartBase);
|
|
306
|
-
if (!bucket) {
|
|
307
|
-
bucket = [];
|
|
308
|
-
byFirstPart.set(firstPartBase, bucket);
|
|
309
|
-
}
|
|
310
|
-
bucket.push(entry);
|
|
204
|
+
let equivalentSchemaPaths = [];
|
|
205
|
+
// O(1) index for findOrCreateEquivalentSchemaPathsEntry.
|
|
206
|
+
// Maps "(rootPath)::(normalizedFuncName)" → the entry containing that root.
|
|
207
|
+
// This replaces the O(E) linear search that was causing O(E²) gather performance.
|
|
208
|
+
const espIndex = new Map();
|
|
209
|
+
const espIndexKey = (path, functionName) => {
|
|
210
|
+
const normalized = cleanFunctionName(functionName);
|
|
211
|
+
const funcKey = normalized === rootScopeName ? '__self__' : normalized || '__self__';
|
|
212
|
+
return `${path}::${funcKey}`;
|
|
213
|
+
};
|
|
214
|
+
const updateEspIndex = (entry) => {
|
|
215
|
+
for (const root of entry.equivalentRoots) {
|
|
216
|
+
const funcName = root.function?.name ?? rootScopeName;
|
|
217
|
+
espIndex.set(espIndexKey(root.schemaRootPath, funcName), entry);
|
|
311
218
|
}
|
|
312
|
-
const result = { byFirstPart };
|
|
313
|
-
normalizedSchemaCache.set(schema, result);
|
|
314
|
-
return result;
|
|
315
219
|
};
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
? undefined
|
|
324
|
-
: findRelevantDependency(p.functionName),
|
|
325
|
-
}));
|
|
326
|
-
let equivalentSchemaPathsEntry;
|
|
327
|
-
// Collect the signature indices from the new roots we want to add
|
|
328
|
-
const newRootSignatureIndices = new Set();
|
|
329
|
-
for (const root of equivalentRoots) {
|
|
330
|
-
const idx = extractSignatureIndex(root.schemaRootPath);
|
|
331
|
-
if (idx !== undefined) {
|
|
332
|
-
newRootSignatureIndices.add(idx);
|
|
333
|
-
}
|
|
220
|
+
// Pre-build a lookup map from cleaned function name to dependency for O(1) lookups.
|
|
221
|
+
// This avoids O(n) linear search in findRelevantDependency which was causing O(n²) performance.
|
|
222
|
+
const dependencyByCleanedName = new Map();
|
|
223
|
+
for (const dep of importedExports) {
|
|
224
|
+
const cleanedName = cleanFunctionName(dep.name);
|
|
225
|
+
if (!dependencyByCleanedName.has(cleanedName)) {
|
|
226
|
+
dependencyByCleanedName.set(cleanedName, dep);
|
|
334
227
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
228
|
+
}
|
|
229
|
+
const findRelevantDependency = (functionName) => {
|
|
230
|
+
return dependencyByCleanedName.get(cleanFunctionName(functionName));
|
|
231
|
+
};
|
|
232
|
+
const findRelevantDependentDataStructure = (functionName) => {
|
|
233
|
+
const dependency = findRelevantDependency(functionName);
|
|
234
|
+
if (!dependency)
|
|
235
|
+
return;
|
|
236
|
+
return dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
237
|
+
};
|
|
238
|
+
const findRelevantDependentAnalysisDataStructure = (functionName) => {
|
|
239
|
+
const dependency = findRelevantDependency(functionName);
|
|
240
|
+
if (!dependency)
|
|
241
|
+
return;
|
|
242
|
+
const dependentAnalysis = dependentAnalyses[dependency.filePath]?.[dependency.name];
|
|
243
|
+
if (!dependentAnalysis?.metadata?.mergedDataStructure) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
return dependentAnalysis.metadata.mergedDataStructure;
|
|
247
|
+
};
|
|
248
|
+
const findOrCreateDependentSchemas = (dependency) => {
|
|
249
|
+
var _a, _b;
|
|
250
|
+
const { filePath, name } = dependency;
|
|
251
|
+
(_a = mergedDataStructure.dependencySchemas)[filePath] || (_a[filePath] = {});
|
|
252
|
+
(_b = mergedDataStructure.dependencySchemas[filePath])[name] || (_b[name] = {
|
|
253
|
+
signatureSchema: {},
|
|
254
|
+
returnValueSchema: {},
|
|
255
|
+
});
|
|
256
|
+
return mergedDataStructure.dependencySchemas[filePath][name];
|
|
257
|
+
};
|
|
258
|
+
const cleanSchema = (schema, context) => {
|
|
259
|
+
transformationTracer.traceSchemaTransform(rootScopeName, 'cleanKnownObjectFunctionsFromMapping', schema, cleanKnownObjectFunctionsFromMapping, context);
|
|
260
|
+
};
|
|
261
|
+
// Cache translatePath results — the same path is often translated multiple times
|
|
262
|
+
// (once per equivalency entry that references it). Avoids redundant
|
|
263
|
+
// splitOutsideParenthesesAndArrays calls on long paths.
|
|
264
|
+
const translatePathCache = new Map();
|
|
265
|
+
const translatePath = (path, dependencyName) => {
|
|
266
|
+
const cacheKey = `${dependencyName}\0${path}`;
|
|
267
|
+
const cached = translatePathCache.get(cacheKey);
|
|
268
|
+
if (cached !== undefined)
|
|
269
|
+
return cached;
|
|
270
|
+
let result = path;
|
|
271
|
+
if (path.startsWith(dependencyName)) {
|
|
272
|
+
const pathParts = splitOutsideParenthesesAndArrays(path);
|
|
273
|
+
if (pathParts.length > 1) {
|
|
274
|
+
if (pathParts[1].startsWith('functionCallReturnValue')) {
|
|
275
|
+
// Check if this function has multiple DIFFERENT type parameters.
|
|
276
|
+
// If so, DON'T normalize to returnValue - keep the full path to avoid
|
|
277
|
+
// merging different type-parameterized variants together.
|
|
278
|
+
const baseName = cleanFunctionName(pathParts[0]);
|
|
279
|
+
if (!functionsWithMultipleTypeParams.has(baseName)) {
|
|
280
|
+
// functionCallReturnValue immediately follows - normalize to returnValue
|
|
281
|
+
result = joinParenthesesAndArrays([
|
|
282
|
+
'returnValue',
|
|
283
|
+
...pathParts.slice(2),
|
|
284
|
+
]);
|
|
357
285
|
}
|
|
358
286
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
: '__self__';
|
|
364
|
-
const newIdx = extractSignatureIndex(newRoot.schemaRootPath);
|
|
365
|
-
if (newIdx !== undefined) {
|
|
366
|
-
const existingIndices = existingIndicesByFunction.get(funcKey);
|
|
367
|
-
if (existingIndices && existingIndices.size > 0) {
|
|
368
|
-
if (!existingIndices.has(newIdx)) {
|
|
369
|
-
hasConflict = true;
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
287
|
+
else if (pathParts[0].endsWith(')') &&
|
|
288
|
+
pathParts[1].startsWith('signature[')) {
|
|
289
|
+
// Hook-style with signature access
|
|
290
|
+
result = joinParenthesesAndArrays(pathParts.slice(1));
|
|
374
291
|
}
|
|
375
|
-
if (hasConflict)
|
|
376
|
-
continue;
|
|
377
292
|
}
|
|
378
|
-
equivalentSchemaPathsEntry = candidate;
|
|
379
293
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
294
|
+
translatePathCache.set(cacheKey, result);
|
|
295
|
+
return result;
|
|
296
|
+
};
|
|
297
|
+
const gatherAllEquivalentSchemaPaths = (functionName, sourceAndUsageEquivalencies, dataStructure) => {
|
|
298
|
+
checkDeadline();
|
|
299
|
+
if (!sourceAndUsageEquivalencies)
|
|
300
|
+
return;
|
|
301
|
+
const normalizedSchemaCache = new Map();
|
|
302
|
+
const getSchemaIndex = (schema) => {
|
|
303
|
+
if (!schema)
|
|
304
|
+
return { byFirstPart: new Map() };
|
|
305
|
+
const cached = normalizedSchemaCache.get(schema);
|
|
306
|
+
if (cached)
|
|
307
|
+
return cached;
|
|
308
|
+
const byFirstPart = new Map();
|
|
309
|
+
for (const path in schema) {
|
|
310
|
+
checkDeadline();
|
|
311
|
+
let parts = splitOutsideParenthesesAndArrays(path);
|
|
312
|
+
if (parts[0].startsWith(functionName)) {
|
|
313
|
+
const baseName = cleanFunctionName(parts[0]);
|
|
314
|
+
if (!functionsWithMultipleTypeParams.has(baseName)) {
|
|
315
|
+
parts =
|
|
316
|
+
parts[1] === 'functionCallReturnValue'
|
|
317
|
+
? ['returnValue', ...parts.slice(2)]
|
|
318
|
+
: parts.slice(1);
|
|
401
319
|
}
|
|
402
320
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
321
|
+
const entry = { path, parts };
|
|
322
|
+
// Index by the base of the first part (before any function call args)
|
|
323
|
+
const firstPart = parts[0] ?? '';
|
|
324
|
+
const parenIdx = firstPart.indexOf('(');
|
|
325
|
+
const firstPartBase = parenIdx >= 0 ? firstPart.slice(0, parenIdx) : firstPart;
|
|
326
|
+
let bucket = byFirstPart.get(firstPartBase);
|
|
327
|
+
if (!bucket) {
|
|
328
|
+
bucket = [];
|
|
329
|
+
byFirstPart.set(firstPartBase, bucket);
|
|
406
330
|
}
|
|
407
|
-
|
|
408
|
-
filteredRoots = equivalentRoots.filter((root) => {
|
|
409
|
-
const idx = extractSignatureIndex(root.schemaRootPath);
|
|
410
|
-
return idx === undefined || idx === primaryIndex;
|
|
411
|
-
});
|
|
331
|
+
bucket.push(entry);
|
|
412
332
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
};
|
|
436
|
-
// Helper to extract function name from a path that starts with a function call.
|
|
437
|
-
// e.g., 'ScenarioViewer().signature[0].scenario' -> 'ScenarioViewer'
|
|
438
|
-
// Returns undefined if the path doesn't start with a function call or the function isn't a dependency.
|
|
439
|
-
const extractFunctionNameFromPath = (path) => {
|
|
440
|
-
const parts = splitOutsideParenthesesAndArrays(path);
|
|
441
|
-
if (parts.length > 0 && parts[0].endsWith(')')) {
|
|
442
|
-
// Extract the function name without the () suffix and type params
|
|
443
|
-
const funcCallPart = parts[0];
|
|
444
|
-
const funcName = cleanFunctionName(funcCallPart.replace(/\(\)$/, ''));
|
|
445
|
-
// Check if this function is a dependency
|
|
446
|
-
if (findRelevantDependency(funcName)) {
|
|
447
|
-
return funcName;
|
|
333
|
+
const result = { byFirstPart };
|
|
334
|
+
normalizedSchemaCache.set(schema, result);
|
|
335
|
+
return result;
|
|
336
|
+
};
|
|
337
|
+
const findOrCreateEquivalentSchemaPathsEntry = (allPaths) => {
|
|
338
|
+
const equivalentRoots = allPaths
|
|
339
|
+
.filter((p) => p.functionName === rootScopeName ||
|
|
340
|
+
!!findRelevantDependency(p.functionName))
|
|
341
|
+
.map((p) => ({
|
|
342
|
+
schemaRootPath: p.path,
|
|
343
|
+
function: p.functionName === rootScopeName
|
|
344
|
+
? undefined
|
|
345
|
+
: findRelevantDependency(p.functionName),
|
|
346
|
+
}));
|
|
347
|
+
let equivalentSchemaPathsEntry;
|
|
348
|
+
// Collect the signature indices from the new roots we want to add
|
|
349
|
+
const newRootSignatureIndices = new Set();
|
|
350
|
+
for (const root of equivalentRoots) {
|
|
351
|
+
const idx = extractSignatureIndex(root.schemaRootPath);
|
|
352
|
+
if (idx !== undefined) {
|
|
353
|
+
newRootSignatureIndices.add(idx);
|
|
354
|
+
}
|
|
448
355
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
const allEquivalencies = [
|
|
453
|
-
sourceAndUsageEquivalencies.usageEquivalencies,
|
|
454
|
-
sourceAndUsageEquivalencies.sourceEquivalencies,
|
|
455
|
-
].filter(Boolean);
|
|
456
|
-
for (const equivalencies of allEquivalencies) {
|
|
457
|
-
const schemaPathEntries = Object.entries(equivalencies);
|
|
458
|
-
for (const [schemaPath, usages] of schemaPathEntries) {
|
|
459
|
-
checkDeadline();
|
|
460
|
-
// Skip equivalency entries whose source path is a Set/Map membership operation.
|
|
461
|
-
// Patterns like `.has(articleId)`, `.delete(articleId)`, `.add(articleId)` on
|
|
462
|
-
// Sets/Maps represent membership checks, not meaningful data flow for schema generation.
|
|
463
|
-
// In the Margo LibraryPage case, these account for 74% of all equivalency targets
|
|
464
|
-
// (19,444 of 26,340) and cause a combinatorial explosion in the merge.
|
|
465
|
-
if (isCollectionMethodPath(schemaPath))
|
|
466
|
-
continue;
|
|
467
|
-
// First, check if the raw schemaPath starts with a function call to a dependency.
|
|
468
|
-
// If so, use that dependency name for translation (so translatePath can strip the prefix).
|
|
469
|
-
const extractedFuncName = extractFunctionNameFromPath(schemaPath);
|
|
470
|
-
const effectiveFunctionName = extractedFuncName || functionName;
|
|
471
|
-
const translatedPath = translatePath(schemaPath, effectiveFunctionName);
|
|
472
|
-
const allPathsRaw = [
|
|
473
|
-
{ path: translatedPath, functionName: effectiveFunctionName },
|
|
474
|
-
...usages
|
|
475
|
-
.filter((u) => !isCollectionMethodPath(u.schemaPath))
|
|
476
|
-
.map((u) => ({
|
|
477
|
-
path: translatePath(u.schemaPath, u.scopeNodeName),
|
|
478
|
-
functionName: u.scopeNodeName,
|
|
479
|
-
})),
|
|
480
|
-
].filter((pathInfo) => !pathInfo.path.includes('.map('));
|
|
481
|
-
// Deduplicate by translated path + function name.
|
|
482
|
-
// Multiple call variants (e.g., loadView(viewKey(null,null)) vs loadView(viewKey(newTag,newCol)))
|
|
483
|
-
// translate to the same path after stripping arguments. Processing duplicates
|
|
484
|
-
// creates O(n²) work in the schema matching loops below.
|
|
485
|
-
const seenPathKeys = new Set();
|
|
486
|
-
const allPaths = allPathsRaw.filter((p) => {
|
|
487
|
-
const key = `${p.functionName ?? ''}::${p.path}`;
|
|
488
|
-
if (seenPathKeys.has(key))
|
|
489
|
-
return false;
|
|
490
|
-
seenPathKeys.add(key);
|
|
491
|
-
return true;
|
|
492
|
-
});
|
|
493
|
-
// Fix 38: Derive base paths from property access paths.
|
|
494
|
-
// When we have equivalent paths like:
|
|
495
|
-
// Parent: signature[0].scenarios[].name
|
|
496
|
-
// Child: signature[0].selectedScenario.name
|
|
497
|
-
// We want to derive the base paths by finding the common suffix:
|
|
498
|
-
// Common suffix: .name
|
|
499
|
-
// Parent base: signature[0].scenarios[]
|
|
500
|
-
// Child base: signature[0].selectedScenario
|
|
501
|
-
// This allows the merge to find nested child schema fields under the base prop.
|
|
502
|
-
// Find child signature paths (paths from child components)
|
|
503
|
-
const childPaths = allPaths.filter((p) => p.functionName &&
|
|
504
|
-
p.functionName !== rootScopeName &&
|
|
505
|
-
p.functionName !== effectiveFunctionName);
|
|
506
|
-
// Find parent paths (paths from this component)
|
|
507
|
-
const parentPaths = allPaths.filter((p) => !p.functionName ||
|
|
508
|
-
p.functionName === rootScopeName ||
|
|
509
|
-
p.functionName === effectiveFunctionName);
|
|
510
|
-
const derivedBasePaths = [];
|
|
511
|
-
const allPathSet = new Set(allPaths.map((p) => p.path));
|
|
512
|
-
const derivedBasePathSet = new Set();
|
|
513
|
-
// For each child path, find its equivalent parent path and derive bases
|
|
514
|
-
for (const childPathInfo of childPaths) {
|
|
356
|
+
// Use espIndex Map for O(1) lookup instead of O(E) linear search.
|
|
357
|
+
// Falls back to linear search only when Map hit has a signature index conflict.
|
|
358
|
+
for (const pathInfo of allPaths) {
|
|
515
359
|
checkDeadline();
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
360
|
+
if (equivalentSchemaPathsEntry)
|
|
361
|
+
break;
|
|
362
|
+
const candidate = espIndex.get(espIndexKey(pathInfo.path, pathInfo.functionName));
|
|
363
|
+
if (!candidate)
|
|
364
|
+
continue;
|
|
365
|
+
// Verify no signature index conflict with the candidate entry
|
|
366
|
+
if (newRootSignatureIndices.size > 0) {
|
|
367
|
+
const existingIndicesByFunction = new Map();
|
|
368
|
+
for (const er of candidate.equivalentRoots) {
|
|
369
|
+
const funcKey = er.function
|
|
370
|
+
? `${er.function.name}::${er.function.filePath}`
|
|
371
|
+
: '__self__';
|
|
372
|
+
const idx = extractSignatureIndex(er.schemaRootPath);
|
|
373
|
+
if (idx !== undefined) {
|
|
374
|
+
if (!existingIndicesByFunction.has(funcKey)) {
|
|
375
|
+
existingIndicesByFunction.set(funcKey, new Set());
|
|
376
|
+
}
|
|
377
|
+
existingIndicesByFunction.get(funcKey).add(idx);
|
|
530
378
|
}
|
|
531
379
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
const isParentSignaturePath = parentBaseParts[0]?.startsWith('signature[');
|
|
545
|
-
if (isChildSignaturePath && isParentSignaturePath) {
|
|
546
|
-
const childBase = joinParenthesesAndArrays(childBaseParts);
|
|
547
|
-
const parentBase = joinParenthesesAndArrays(parentBaseParts);
|
|
548
|
-
// Only derive if:
|
|
549
|
-
// 1. Parent has array iteration (e.g., scenarios[]) and child does NOT
|
|
550
|
-
// 2. Bases are different
|
|
551
|
-
// 3. Child base is NOT just "signature[N]" (too generic - every component has this)
|
|
552
|
-
// We only want specific prop paths like "signature[0].selectedScenario"
|
|
553
|
-
// This targets array-to-object mappings like scenarios[] -> selectedScenario
|
|
554
|
-
const parentHasArrayIterator = parentBase.includes('[]');
|
|
555
|
-
const childHasArrayIterator = childBase.includes('[]');
|
|
556
|
-
// Skip if child base is just the generic signature marker (e.g., "signature[0]")
|
|
557
|
-
const childBaseIsGenericSignature = /^signature\[\d+\]$/.test(childBase);
|
|
558
|
-
if (childBase !== parentBase &&
|
|
559
|
-
parentHasArrayIterator &&
|
|
560
|
-
!childHasArrayIterator &&
|
|
561
|
-
!childBaseIsGenericSignature) {
|
|
562
|
-
// Add child base if not already present (O(1) Set lookup)
|
|
563
|
-
if (!allPathSet.has(childBase) &&
|
|
564
|
-
!derivedBasePathSet.has(childBase)) {
|
|
565
|
-
derivedBasePaths.push({
|
|
566
|
-
path: childBase,
|
|
567
|
-
functionName: childPathInfo.functionName,
|
|
568
|
-
});
|
|
569
|
-
derivedBasePathSet.add(childBase);
|
|
570
|
-
}
|
|
571
|
-
// Add parent base if not already present (O(1) Set lookup)
|
|
572
|
-
if (!allPathSet.has(parentBase) &&
|
|
573
|
-
!derivedBasePathSet.has(parentBase)) {
|
|
574
|
-
derivedBasePaths.push({
|
|
575
|
-
path: parentBase,
|
|
576
|
-
functionName: parentPathInfo.functionName,
|
|
577
|
-
});
|
|
578
|
-
derivedBasePathSet.add(parentBase);
|
|
380
|
+
let hasConflict = false;
|
|
381
|
+
for (const newRoot of equivalentRoots) {
|
|
382
|
+
const funcKey = newRoot.function
|
|
383
|
+
? `${newRoot.function.name}::${newRoot.function.filePath}`
|
|
384
|
+
: '__self__';
|
|
385
|
+
const newIdx = extractSignatureIndex(newRoot.schemaRootPath);
|
|
386
|
+
if (newIdx !== undefined) {
|
|
387
|
+
const existingIndices = existingIndicesByFunction.get(funcKey);
|
|
388
|
+
if (existingIndices && existingIndices.size > 0) {
|
|
389
|
+
if (!existingIndices.has(newIdx)) {
|
|
390
|
+
hasConflict = true;
|
|
391
|
+
break;
|
|
579
392
|
}
|
|
580
393
|
}
|
|
581
394
|
}
|
|
582
395
|
}
|
|
396
|
+
if (hasConflict)
|
|
397
|
+
continue;
|
|
583
398
|
}
|
|
399
|
+
equivalentSchemaPathsEntry = candidate;
|
|
584
400
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
401
|
+
if (!equivalentSchemaPathsEntry) {
|
|
402
|
+
// Before creating a new entry, filter out roots that have conflicting
|
|
403
|
+
// signature indices from the same function. An entry should never contain
|
|
404
|
+
// roots with different signature indices from the same function.
|
|
405
|
+
// This prevents the bug where signature[1], signature[2], signature[4]
|
|
406
|
+
// all get merged together due to incorrect sourceEquivalencies.
|
|
407
|
+
let filteredRoots = equivalentRoots;
|
|
408
|
+
if (newRootSignatureIndices.size > 1) {
|
|
409
|
+
// There are multiple signature indices - we need to filter to keep only
|
|
410
|
+
// one consistent set. We'll keep the roots that match the PRIMARY index
|
|
411
|
+
// (the first signature index we encounter from self, or the lowest index).
|
|
412
|
+
// First, determine the primary index - prefer the self root's index
|
|
413
|
+
let primaryIndex;
|
|
414
|
+
for (const root of equivalentRoots) {
|
|
415
|
+
if (!root.function) {
|
|
416
|
+
// This is a self root
|
|
417
|
+
const idx = extractSignatureIndex(root.schemaRootPath);
|
|
418
|
+
if (idx !== undefined) {
|
|
419
|
+
primaryIndex = idx;
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// If no self root has a signature index, use the lowest index
|
|
425
|
+
if (primaryIndex === undefined) {
|
|
426
|
+
primaryIndex = Math.min(...newRootSignatureIndices);
|
|
427
|
+
}
|
|
428
|
+
// Filter roots: keep if no signature index OR signature index matches primary
|
|
429
|
+
filteredRoots = equivalentRoots.filter((root) => {
|
|
430
|
+
const idx = extractSignatureIndex(root.schemaRootPath);
|
|
431
|
+
return idx === undefined || idx === primaryIndex;
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
equivalentSchemaPathsEntry = {
|
|
435
|
+
equivalentRoots: filteredRoots,
|
|
436
|
+
equivalentPostfixes: {},
|
|
437
|
+
};
|
|
438
|
+
equivalentSchemaPaths.push(equivalentSchemaPathsEntry);
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
equivalentSchemaPathsEntry.equivalentRoots.push(...equivalentRoots);
|
|
442
|
+
}
|
|
443
|
+
// Deduplicate roots using a Set for O(n) instead of O(n²)
|
|
444
|
+
const seenRoots = new Set();
|
|
445
|
+
equivalentSchemaPathsEntry.equivalentRoots =
|
|
446
|
+
equivalentSchemaPathsEntry.equivalentRoots.filter((er) => {
|
|
447
|
+
const key = er.schemaRootPath + '::' + (er.function?.name ?? '');
|
|
448
|
+
if (seenRoots.has(key))
|
|
449
|
+
return false;
|
|
450
|
+
seenRoots.add(key);
|
|
451
|
+
return true;
|
|
604
452
|
});
|
|
453
|
+
// Keep the espIndex in sync after adding/deduplicating roots
|
|
454
|
+
updateEspIndex(equivalentSchemaPathsEntry);
|
|
455
|
+
return equivalentSchemaPathsEntry;
|
|
456
|
+
};
|
|
457
|
+
// Helper to extract function name from a path that starts with a function call.
|
|
458
|
+
// e.g., 'ScenarioViewer().signature[0].scenario' -> 'ScenarioViewer'
|
|
459
|
+
// Returns undefined if the path doesn't start with a function call or the function isn't a dependency.
|
|
460
|
+
const extractFunctionNameFromPath = (path) => {
|
|
461
|
+
const parts = splitOutsideParenthesesAndArrays(path);
|
|
462
|
+
if (parts.length > 0 && parts[0].endsWith(')')) {
|
|
463
|
+
// Extract the function name without the () suffix and type params
|
|
464
|
+
const funcCallPart = parts[0];
|
|
465
|
+
const funcName = cleanFunctionName(funcCallPart.replace(/\(\)$/, ''));
|
|
466
|
+
// Check if this function is a dependency
|
|
467
|
+
if (findRelevantDependency(funcName)) {
|
|
468
|
+
return funcName;
|
|
469
|
+
}
|
|
605
470
|
}
|
|
606
|
-
|
|
471
|
+
return undefined;
|
|
472
|
+
};
|
|
473
|
+
const allEquivalencies = [
|
|
474
|
+
sourceAndUsageEquivalencies.usageEquivalencies,
|
|
475
|
+
sourceAndUsageEquivalencies.sourceEquivalencies,
|
|
476
|
+
].filter(Boolean);
|
|
477
|
+
// Global dedup across ALL equivalency entries. The same (scope, targetPath)
|
|
478
|
+
// pair often appears in 30-50 different source entries (e.g., every variable
|
|
479
|
+
// that flows through loadView references the same 50 target paths).
|
|
480
|
+
// Processing these redundantly accounts for 96% of work in the gather phase.
|
|
481
|
+
const globalSeenTargets = new Set();
|
|
482
|
+
for (const equivalencies of allEquivalencies) {
|
|
483
|
+
const schemaPathEntries = Object.entries(equivalencies);
|
|
484
|
+
for (const [schemaPath, usages] of schemaPathEntries) {
|
|
607
485
|
checkDeadline();
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
//
|
|
616
|
-
//
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
const
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
486
|
+
// Skip equivalency entries whose source path is a Set/Map membership operation.
|
|
487
|
+
// Patterns like `.has(articleId)`, `.delete(articleId)`, `.add(articleId)` on
|
|
488
|
+
// Sets/Maps represent membership checks, not meaningful data flow for schema generation.
|
|
489
|
+
// In the Margo LibraryPage case, these account for 74% of all equivalency targets
|
|
490
|
+
// (19,444 of 26,340) and cause a combinatorial explosion in the merge.
|
|
491
|
+
if (isCollectionMethodPath(schemaPath))
|
|
492
|
+
continue;
|
|
493
|
+
// Skip paths with inline object literals in function call arguments.
|
|
494
|
+
// These are call-site snapshots (e.g., setUndoEntry({ label: '...', undo: ... }))
|
|
495
|
+
// that embed source code text as path strings. They're expensive to parse
|
|
496
|
+
// and don't contribute useful schema information.
|
|
497
|
+
if (hasInlineObjectArg(schemaPath))
|
|
498
|
+
continue;
|
|
499
|
+
// First, check if the raw schemaPath starts with a function call to a dependency.
|
|
500
|
+
// If so, use that dependency name for translation (so translatePath can strip the prefix).
|
|
501
|
+
const extractedFuncName = extractFunctionNameFromPath(schemaPath);
|
|
502
|
+
const effectiveFunctionName = extractedFuncName || functionName;
|
|
503
|
+
const translatedPath = translatePath(schemaPath, effectiveFunctionName);
|
|
504
|
+
const allPathsRaw = [
|
|
505
|
+
{ path: translatedPath, functionName: effectiveFunctionName },
|
|
506
|
+
...usages
|
|
507
|
+
.filter((u) => !isCollectionMethodPath(u.schemaPath))
|
|
508
|
+
.map((u) => ({
|
|
509
|
+
path: translatePath(u.schemaPath, u.scopeNodeName),
|
|
510
|
+
functionName: u.scopeNodeName,
|
|
511
|
+
})),
|
|
512
|
+
].filter((pathInfo) => !pathInfo.path.includes('.map('));
|
|
513
|
+
// Deduplicate by translated path + function name, both within this entry
|
|
514
|
+
// AND across all entries. The same target path appears in 30-50 different
|
|
515
|
+
// source entries (every variable flowing through loadView references the same
|
|
516
|
+
// 50 target paths). Without global dedup, we process 5,533 targets instead of 217.
|
|
517
|
+
const allPaths = allPathsRaw.filter((p) => {
|
|
518
|
+
const key = `${p.functionName ?? ''}::${p.path}`;
|
|
519
|
+
if (globalSeenTargets.has(key))
|
|
520
|
+
return false;
|
|
521
|
+
globalSeenTargets.add(key);
|
|
522
|
+
return true;
|
|
523
|
+
});
|
|
524
|
+
// Fix 38: Derive base paths from property access paths.
|
|
525
|
+
// When we have equivalent paths like:
|
|
526
|
+
// Parent: signature[0].scenarios[].name
|
|
527
|
+
// Child: signature[0].selectedScenario.name
|
|
528
|
+
// We want to derive the base paths by finding the common suffix:
|
|
529
|
+
// Common suffix: .name
|
|
530
|
+
// Parent base: signature[0].scenarios[]
|
|
531
|
+
// Child base: signature[0].selectedScenario
|
|
532
|
+
// This allows the merge to find nested child schema fields under the base prop.
|
|
533
|
+
// Find child signature paths (paths from child components)
|
|
534
|
+
const childPaths = allPaths.filter((p) => p.functionName &&
|
|
535
|
+
p.functionName !== rootScopeName &&
|
|
536
|
+
p.functionName !== effectiveFunctionName);
|
|
537
|
+
// Find parent paths (paths from this component)
|
|
538
|
+
const parentPaths = allPaths.filter((p) => !p.functionName ||
|
|
539
|
+
p.functionName === rootScopeName ||
|
|
540
|
+
p.functionName === effectiveFunctionName);
|
|
541
|
+
const derivedBasePaths = [];
|
|
542
|
+
const allPathSet = new Set(allPaths.map((p) => p.path));
|
|
543
|
+
const derivedBasePathSet = new Set();
|
|
544
|
+
// For each child path, find its equivalent parent path and derive bases
|
|
545
|
+
for (const childPathInfo of childPaths) {
|
|
546
|
+
checkDeadline();
|
|
547
|
+
const childParts = splitOutsideParenthesesAndArrays(childPathInfo.path);
|
|
548
|
+
// Look for a parent path that shares a common suffix with this child path
|
|
549
|
+
for (const parentPathInfo of parentPaths) {
|
|
550
|
+
const parentParts = splitOutsideParenthesesAndArrays(parentPathInfo.path);
|
|
551
|
+
// Find the common suffix (from the end)
|
|
552
|
+
let commonSuffixLength = 0;
|
|
553
|
+
const minLen = Math.min(childParts.length, parentParts.length);
|
|
554
|
+
for (let i = 1; i <= minLen; i++) {
|
|
555
|
+
if (childParts[childParts.length - i] ===
|
|
556
|
+
parentParts[parentParts.length - i]) {
|
|
557
|
+
commonSuffixLength = i;
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
654
560
|
break;
|
|
655
561
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
562
|
+
}
|
|
563
|
+
// If there's a common suffix and both paths have more parts than the suffix
|
|
564
|
+
if (commonSuffixLength > 0 &&
|
|
565
|
+
childParts.length > commonSuffixLength &&
|
|
566
|
+
parentParts.length > commonSuffixLength) {
|
|
567
|
+
const childBaseParts = childParts.slice(0, childParts.length - commonSuffixLength);
|
|
568
|
+
const parentBaseParts = parentParts.slice(0, parentParts.length - commonSuffixLength);
|
|
569
|
+
// Only derive if BOTH paths look like signature paths.
|
|
570
|
+
// This ensures we're handling JSX child-to-parent prop mappings,
|
|
571
|
+
// not other complex equivalencies like function call returns.
|
|
572
|
+
const isChildSignaturePath = childBaseParts[0]?.startsWith('signature[') ||
|
|
573
|
+
(childBaseParts[0]?.endsWith(')') &&
|
|
574
|
+
childBaseParts[1]?.startsWith('signature['));
|
|
575
|
+
const isParentSignaturePath = parentBaseParts[0]?.startsWith('signature[');
|
|
576
|
+
if (isChildSignaturePath && isParentSignaturePath) {
|
|
577
|
+
const childBase = joinParenthesesAndArrays(childBaseParts);
|
|
578
|
+
const parentBase = joinParenthesesAndArrays(parentBaseParts);
|
|
579
|
+
// Only derive if:
|
|
580
|
+
// 1. Parent has array iteration (e.g., scenarios[]) and child does NOT
|
|
581
|
+
// 2. Bases are different
|
|
582
|
+
// 3. Child base is NOT just "signature[N]" (too generic - every component has this)
|
|
583
|
+
// We only want specific prop paths like "signature[0].selectedScenario"
|
|
584
|
+
// This targets array-to-object mappings like scenarios[] -> selectedScenario
|
|
585
|
+
const parentHasArrayIterator = parentBase.includes('[]');
|
|
586
|
+
const childHasArrayIterator = childBase.includes('[]');
|
|
587
|
+
// Skip if child base is just the generic signature marker (e.g., "signature[0]")
|
|
588
|
+
const childBaseIsGenericSignature = /^signature\[\d+\]$/.test(childBase);
|
|
589
|
+
if (childBase !== parentBase &&
|
|
590
|
+
parentHasArrayIterator &&
|
|
591
|
+
!childHasArrayIterator &&
|
|
592
|
+
!childBaseIsGenericSignature) {
|
|
593
|
+
// Add child base if not already present (O(1) Set lookup)
|
|
594
|
+
if (!allPathSet.has(childBase) &&
|
|
595
|
+
!derivedBasePathSet.has(childBase)) {
|
|
596
|
+
derivedBasePaths.push({
|
|
597
|
+
path: childBase,
|
|
598
|
+
functionName: childPathInfo.functionName,
|
|
599
|
+
});
|
|
600
|
+
derivedBasePathSet.add(childBase);
|
|
601
|
+
}
|
|
602
|
+
// Add parent base if not already present (O(1) Set lookup)
|
|
603
|
+
if (!allPathSet.has(parentBase) &&
|
|
604
|
+
!derivedBasePathSet.has(parentBase)) {
|
|
605
|
+
derivedBasePaths.push({
|
|
606
|
+
path: parentBase,
|
|
607
|
+
functionName: parentPathInfo.functionName,
|
|
608
|
+
});
|
|
609
|
+
derivedBasePathSet.add(parentBase);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
664
612
|
}
|
|
665
613
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
allPaths.push(...derivedBasePaths);
|
|
617
|
+
const entry = findOrCreateEquivalentSchemaPathsEntry(allPaths);
|
|
618
|
+
// Trace equivalency gathering - helps debug why paths may not be connected
|
|
619
|
+
if (allPaths.length > 1) {
|
|
620
|
+
transformationTracer.operation(rootScopeName, {
|
|
621
|
+
operation: 'gatherEquivalency',
|
|
622
|
+
stage: 'gathering',
|
|
623
|
+
path: translatedPath,
|
|
624
|
+
context: {
|
|
625
|
+
sourceFunction: functionName,
|
|
626
|
+
equivalentPaths: allPaths.map((p) => ({
|
|
627
|
+
path: p.path,
|
|
628
|
+
function: p.functionName,
|
|
629
|
+
})),
|
|
630
|
+
equivalentRoots: entry.equivalentRoots.map((r) => ({
|
|
631
|
+
path: r.schemaRootPath,
|
|
632
|
+
function: r.function?.name,
|
|
633
|
+
})),
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
for (const equivalentRoot of entry.equivalentRoots) {
|
|
638
|
+
checkDeadline();
|
|
639
|
+
const dataStructures = equivalentRoot.function &&
|
|
640
|
+
equivalentRoot.function.name !== rootScopeName
|
|
641
|
+
? [
|
|
642
|
+
findRelevantDependentDataStructure(equivalentRoot.function.name),
|
|
643
|
+
findRelevantDependentAnalysisDataStructure(equivalentRoot.function.name),
|
|
644
|
+
]
|
|
645
|
+
: [dataStructure];
|
|
646
|
+
// Determine if this is a signature schema path.
|
|
647
|
+
// The path might be 'signature[0]...' directly, or 'FuncName().signature[0]...' if it has a function prefix.
|
|
648
|
+
const schemaRootParts = splitOutsideParenthesesAndArrays(equivalentRoot.schemaRootPath);
|
|
649
|
+
const isSignaturePath = equivalentRoot.schemaRootPath.startsWith('signature[') ||
|
|
650
|
+
(schemaRootParts[0]?.endsWith(')') &&
|
|
651
|
+
schemaRootParts[1]?.startsWith('signature['));
|
|
652
|
+
const schemas = dataStructures.map((dataStructure) => isSignaturePath
|
|
653
|
+
? dataStructure?.signatureSchema
|
|
654
|
+
: dataStructure?.returnValueSchema);
|
|
655
|
+
let pathParts = splitOutsideParenthesesAndArrays(equivalentRoot.schemaRootPath);
|
|
656
|
+
// Fix: When processing a child component's schema, the schemaRootPath has the function
|
|
657
|
+
// prefix (e.g., 'ScenarioViewer().signature[0].scenario'), but the child's schema paths
|
|
658
|
+
// don't have that prefix (e.g., 'signature[0].scenario.metadata.screenshotPaths').
|
|
659
|
+
// Strip the function prefix from pathParts so they can match.
|
|
660
|
+
if (equivalentRoot.function &&
|
|
661
|
+
pathParts[0].endsWith(')') &&
|
|
662
|
+
pathParts[1]?.startsWith('signature[')) {
|
|
663
|
+
pathParts = pathParts.slice(1);
|
|
664
|
+
}
|
|
665
|
+
for (const schema of schemas) {
|
|
666
|
+
// Use pre-computed index to only iterate schema entries whose
|
|
667
|
+
// normalized first part matches pathParts[0], instead of all entries.
|
|
668
|
+
const schemaIndex = getSchemaIndex(schema);
|
|
669
|
+
const lookupPart = pathParts[0] ?? '';
|
|
670
|
+
const lookupParenIdx = lookupPart.indexOf('(');
|
|
671
|
+
const lookupBase = lookupParenIdx >= 0
|
|
672
|
+
? lookupPart.slice(0, lookupParenIdx)
|
|
673
|
+
: lookupPart;
|
|
674
|
+
const candidates = schemaIndex.byFirstPart.get(lookupBase) || [];
|
|
675
|
+
for (const { path: schemaPath, parts: schemaPathParts, } of candidates) {
|
|
676
|
+
checkDeadline();
|
|
677
|
+
if (schemaPathParts.length < pathParts.length)
|
|
678
|
+
continue;
|
|
679
|
+
// Check if all path parts match (allowing function call variants)
|
|
680
|
+
let allMatch = true;
|
|
681
|
+
let matchedUpToIndex = pathParts.length;
|
|
682
|
+
for (let i = 0; i < pathParts.length; i++) {
|
|
683
|
+
if (!pathPartMatches(pathParts[i], schemaPathParts[i])) {
|
|
684
|
+
allMatch = false;
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
// If the last pathPart matched a function call variant,
|
|
688
|
+
// we need to include it in the postfix calculation
|
|
689
|
+
if (i === pathParts.length - 1 &&
|
|
690
|
+
schemaPathParts[i] !== pathParts[i] &&
|
|
691
|
+
schemaPathParts[i].startsWith(pathParts[i] + '(')) {
|
|
692
|
+
// The schemaPathPart is a function call variant (e.g., 'isEntityBeingAnalyzed(entity.sha)')
|
|
693
|
+
// We want to include this as part of the postfix
|
|
694
|
+
matchedUpToIndex = i;
|
|
699
695
|
}
|
|
700
696
|
}
|
|
701
|
-
|
|
702
|
-
//
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
697
|
+
if (allMatch) {
|
|
698
|
+
// When we matched a function call variant at the end (e.g., 'foo' matched 'foo(args)'),
|
|
699
|
+
// the base itself should be marked as a function, and the function call details
|
|
700
|
+
// should be included as sub-paths
|
|
701
|
+
if (matchedUpToIndex < pathParts.length) {
|
|
702
|
+
// This is a function call variant match at the last position
|
|
703
|
+
// Mark the base as a function (empty postfix = the base path itself)
|
|
704
|
+
entry.equivalentPostfixes[''] = bestValueFromOptions([
|
|
705
|
+
entry.equivalentPostfixes[''],
|
|
706
|
+
'function',
|
|
707
|
+
]);
|
|
708
|
+
// Also capture the function call and any remaining parts
|
|
709
|
+
// e.g., 'isEntityBeingAnalyzed(entity.sha)' or 'isEntityBeingAnalyzed(entity.sha).functionCallReturnValue'
|
|
710
|
+
const funcCallPart = schemaPathParts[matchedUpToIndex];
|
|
711
|
+
const baseName = pathParts[matchedUpToIndex]; // e.g., 'isEntityBeingAnalyzed'
|
|
712
|
+
const argsMatch = funcCallPart.match(/\(.*\)$/);
|
|
713
|
+
if (argsMatch) {
|
|
714
|
+
// Create postfix using just the args portion: (entity.sha) instead of isEntityBeingAnalyzed(entity.sha)
|
|
715
|
+
// This avoids duplicating the base name in the final path
|
|
716
|
+
const argsPortion = argsMatch[0]; // e.g., '(entity.sha)'
|
|
717
|
+
const remainingParts = schemaPathParts.slice(matchedUpToIndex + 1);
|
|
718
|
+
// Build the postfix as: (args).remaining.parts
|
|
719
|
+
const funcPostfix = joinParenthesesAndArrays([
|
|
720
|
+
argsPortion,
|
|
721
|
+
...remainingParts,
|
|
722
|
+
]);
|
|
723
|
+
entry.equivalentPostfixes[funcPostfix] = entry
|
|
724
|
+
.equivalentPostfixes[funcPostfix]
|
|
725
|
+
? bestValueFromOptions([
|
|
726
|
+
entry.equivalentPostfixes[funcPostfix],
|
|
727
|
+
schema[schemaPath],
|
|
728
|
+
])
|
|
729
|
+
: schema[schemaPath];
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
else {
|
|
733
|
+
// Regular exact match - use the standard postfix logic
|
|
734
|
+
const postfix = joinParenthesesAndArrays(schemaPathParts.slice(matchedUpToIndex));
|
|
735
|
+
const previousValue = entry.equivalentPostfixes[postfix];
|
|
736
|
+
const newValue = schema[schemaPath];
|
|
737
|
+
entry.equivalentPostfixes[postfix] = previousValue
|
|
738
|
+
? bestValueFromOptions([previousValue, newValue])
|
|
739
|
+
: newValue;
|
|
740
|
+
// Trace postfix gathering - shows where type info comes from
|
|
741
|
+
if (entry.equivalentPostfixes[postfix] !== previousValue) {
|
|
742
|
+
transformationTracer.operation(rootScopeName, {
|
|
743
|
+
operation: 'gatherPostfix',
|
|
744
|
+
stage: 'gathering',
|
|
745
|
+
path: postfix || '(root)',
|
|
746
|
+
before: previousValue,
|
|
747
|
+
after: entry.equivalentPostfixes[postfix],
|
|
748
|
+
context: {
|
|
749
|
+
sourceSchemaPath: schemaPath,
|
|
750
|
+
sourceFunction: equivalentRoot.function?.name || rootScopeName,
|
|
751
|
+
equivalentRootPath: equivalentRoot.schemaRootPath,
|
|
752
|
+
rawValue: newValue,
|
|
753
|
+
},
|
|
754
|
+
});
|
|
755
|
+
}
|
|
724
756
|
}
|
|
725
757
|
}
|
|
726
758
|
}
|
|
@@ -728,836 +760,864 @@ export default function mergeInDependentDataStructure({ importedExports, depende
|
|
|
728
760
|
}
|
|
729
761
|
}
|
|
730
762
|
}
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
763
|
+
if (Object.keys(dataStructure?.returnValueSchema ?? {}).length > 0) {
|
|
764
|
+
// Find all paths that contain functionCallReturnValue and extract unique base paths
|
|
765
|
+
// For each path containing functionCallReturnValue, find the FIRST occurrence and use
|
|
766
|
+
// that as a base path. This handles nested cases like:
|
|
767
|
+
// X().functionCallReturnValue.A.B.Y().functionCallReturnValue
|
|
768
|
+
// where we want both X().functionCallReturnValue and Y().functionCallReturnValue as bases
|
|
769
|
+
const allBasePaths = new Set();
|
|
770
|
+
for (const path of Object.keys(dataStructure.returnValueSchema)) {
|
|
771
|
+
checkDeadline();
|
|
772
|
+
const parts = splitOutsideParenthesesAndArrays(path);
|
|
773
|
+
// Find all positions of functionCallReturnValue and create base paths for each
|
|
774
|
+
for (let i = 0; i < parts.length; i++) {
|
|
775
|
+
if (parts[i] === 'functionCallReturnValue') {
|
|
776
|
+
const basePath = joinParenthesesAndArrays(parts.slice(0, i + 1));
|
|
777
|
+
allBasePaths.add(basePath);
|
|
778
|
+
}
|
|
747
779
|
}
|
|
748
780
|
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
781
|
+
// Sort by length so shorter paths are processed first
|
|
782
|
+
const sortedBasePaths = [...allBasePaths].sort((a, b) => a.length - b.length);
|
|
783
|
+
for (const basePath of sortedBasePaths) {
|
|
784
|
+
const translatedBasePath = translatePath(basePath, functionName);
|
|
785
|
+
const entry = findOrCreateEquivalentSchemaPathsEntry([
|
|
786
|
+
{ path: translatedBasePath, functionName: functionName },
|
|
787
|
+
]);
|
|
788
|
+
const newRoot = {
|
|
789
|
+
schemaRootPath: translatedBasePath,
|
|
790
|
+
function: findRelevantDependency(functionName),
|
|
791
|
+
};
|
|
792
|
+
entry.equivalentRoots.push(newRoot);
|
|
793
|
+
// Update index for the newly added root
|
|
794
|
+
const newRootFuncName = newRoot.function?.name ?? rootScopeName;
|
|
795
|
+
espIndex.set(espIndexKey(newRoot.schemaRootPath, newRootFuncName), entry);
|
|
796
|
+
const basePathParts = splitOutsideParenthesesAndArrays(basePath);
|
|
797
|
+
for (const schemaPath in dataStructure.returnValueSchema) {
|
|
798
|
+
checkDeadline();
|
|
799
|
+
const schemaPathParts = splitOutsideParenthesesAndArrays(schemaPath);
|
|
800
|
+
if (schemaPathParts.length < basePathParts.length)
|
|
801
|
+
continue;
|
|
802
|
+
// Check if this schemaPath actually starts with this basePath
|
|
803
|
+
// (not just has the same length prefix)
|
|
804
|
+
const prefixParts = schemaPathParts.slice(0, basePathParts.length);
|
|
805
|
+
if (joinParenthesesAndArrays(prefixParts) !==
|
|
806
|
+
joinParenthesesAndArrays(basePathParts)) {
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
const postfix = joinParenthesesAndArrays(schemaPathParts.slice(basePathParts.length));
|
|
810
|
+
const newValue = entry.equivalentPostfixes[postfix]
|
|
811
|
+
? bestValueFromOptions([
|
|
812
|
+
entry.equivalentPostfixes[postfix],
|
|
813
|
+
dataStructure.returnValueSchema[schemaPath],
|
|
814
|
+
])
|
|
815
|
+
: dataStructure.returnValueSchema[schemaPath];
|
|
816
|
+
entry.equivalentPostfixes[postfix] = newValue;
|
|
777
817
|
}
|
|
778
|
-
const postfix = joinParenthesesAndArrays(schemaPathParts.slice(basePathParts.length));
|
|
779
|
-
const newValue = entry.equivalentPostfixes[postfix]
|
|
780
|
-
? bestValueFromOptions([
|
|
781
|
-
entry.equivalentPostfixes[postfix],
|
|
782
|
-
dataStructure.returnValueSchema[schemaPath],
|
|
783
|
-
])
|
|
784
|
-
: dataStructure.returnValueSchema[schemaPath];
|
|
785
|
-
entry.equivalentPostfixes[postfix] = newValue;
|
|
786
818
|
}
|
|
787
819
|
}
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
820
|
+
};
|
|
821
|
+
const mergeAllEquivalentSchemaPaths = () => {
|
|
822
|
+
const mergedEquivalentSchemaPaths = [];
|
|
823
|
+
// Pre-pass: Connect entries with array/array-element relationships.
|
|
824
|
+
// This handles cases like:
|
|
825
|
+
// - Entry A has root 'surveys' (array)
|
|
826
|
+
// - Entry B has root 'surveys[]' (array element)
|
|
827
|
+
// These need to be connected so Entry B's field postfixes flow to Entry A.
|
|
828
|
+
// We do this before the main merge to ensure the connection happens regardless
|
|
829
|
+
// of processing order.
|
|
830
|
+
for (const esp of equivalentSchemaPaths) {
|
|
831
|
+
checkDeadline();
|
|
832
|
+
for (const root of esp.equivalentRoots) {
|
|
833
|
+
if (root.schemaRootPath.endsWith('[]')) {
|
|
834
|
+
// Find a matching parent entry with the base array path
|
|
835
|
+
const baseArrayPath = root.schemaRootPath.slice(0, -2);
|
|
836
|
+
const parentEntry = equivalentSchemaPaths.find((other) => other !== esp &&
|
|
837
|
+
other.equivalentRoots.some((otherRoot) => otherRoot.schemaRootPath === baseArrayPath &&
|
|
838
|
+
otherRoot.function?.name === root.function?.name &&
|
|
839
|
+
otherRoot.function?.filePath === root.function?.filePath));
|
|
840
|
+
if (parentEntry) {
|
|
841
|
+
// Add transformed postfixes from child (array element) to parent (array)
|
|
842
|
+
// so they can be applied with [] prefix to parent paths
|
|
843
|
+
for (const [postfixPath, postfixValue] of Object.entries(esp.equivalentPostfixes)) {
|
|
844
|
+
checkDeadline();
|
|
845
|
+
const transformedPostfix = joinParenthesesAndArrays(['[]', postfixPath].filter(Boolean));
|
|
846
|
+
if (!(transformedPostfix in parentEntry.equivalentPostfixes)) {
|
|
847
|
+
parentEntry.equivalentPostfixes[transformedPostfix] =
|
|
848
|
+
postfixValue;
|
|
849
|
+
}
|
|
818
850
|
}
|
|
819
851
|
}
|
|
820
852
|
}
|
|
821
853
|
}
|
|
822
854
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
855
|
+
const findEquivalentSchemaPathEntry = (schemaSubPath, equivalentRootFunction) => {
|
|
856
|
+
let postfix;
|
|
857
|
+
// Get the signature index we're looking for (if any)
|
|
858
|
+
const lookingForSignatureIndex = extractSignatureIndex(schemaSubPath);
|
|
859
|
+
const equivalentEntry = mergedEquivalentSchemaPaths.find((esp) => esp.equivalentRoots.some((er) => {
|
|
860
|
+
if ((schemaSubPath.startsWith('returnValue') ||
|
|
861
|
+
schemaSubPath.startsWith('signature[')) &&
|
|
862
|
+
(er.function?.name !== equivalentRootFunction?.name ||
|
|
863
|
+
er.function?.filePath !== equivalentRootFunction?.filePath)) {
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
if (schemaSubPath === er.schemaRootPath) {
|
|
867
|
+
// Additional check: if we're looking for a signature path, make sure
|
|
868
|
+
// the entry doesn't already have DIFFERENT signature indices.
|
|
869
|
+
// This prevents entries with signature[1], signature[2], signature[4]
|
|
870
|
+
// from all being merged together.
|
|
871
|
+
if (lookingForSignatureIndex !== undefined) {
|
|
872
|
+
const hasConflictingSignatureIndex = esp.equivalentRoots.some((otherRoot) => {
|
|
873
|
+
// Only check roots from the same function
|
|
874
|
+
if (otherRoot.function?.name !==
|
|
875
|
+
equivalentRootFunction?.name ||
|
|
876
|
+
otherRoot.function?.filePath !==
|
|
877
|
+
equivalentRootFunction?.filePath) {
|
|
878
|
+
return false;
|
|
879
|
+
}
|
|
880
|
+
const otherIndex = extractSignatureIndex(otherRoot.schemaRootPath);
|
|
881
|
+
return (otherIndex !== undefined &&
|
|
882
|
+
otherIndex !== lookingForSignatureIndex);
|
|
883
|
+
});
|
|
884
|
+
if (hasConflictingSignatureIndex) {
|
|
846
885
|
return false;
|
|
847
886
|
}
|
|
848
|
-
const otherIndex = extractSignatureIndex(otherRoot.schemaRootPath);
|
|
849
|
-
return (otherIndex !== undefined &&
|
|
850
|
-
otherIndex !== lookingForSignatureIndex);
|
|
851
|
-
});
|
|
852
|
-
if (hasConflictingSignatureIndex) {
|
|
853
|
-
return false;
|
|
854
887
|
}
|
|
888
|
+
postfix = er.postfix;
|
|
889
|
+
return true;
|
|
855
890
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
891
|
+
return false;
|
|
892
|
+
}));
|
|
893
|
+
return { equivalentEntry, postfix };
|
|
894
|
+
};
|
|
895
|
+
const sortedEquivalentSchemaPaths = equivalentSchemaPaths.sort((a, b) => Math.max(...a.equivalentRoots.map((er) => splitOutsideParenthesesAndArrays(er.schemaRootPath).length)) -
|
|
896
|
+
Math.max(...b.equivalentRoots.map((er) => splitOutsideParenthesesAndArrays(er.schemaRootPath).length)));
|
|
897
|
+
for (const esp of sortedEquivalentSchemaPaths) {
|
|
898
|
+
checkDeadline();
|
|
899
|
+
if (esp.equivalentRoots.length === 0)
|
|
900
|
+
continue;
|
|
901
|
+
let bestCandidateLength;
|
|
902
|
+
let bestCandidate;
|
|
903
|
+
let postfix;
|
|
904
|
+
for (const equivalentRoot of esp.equivalentRoots) {
|
|
905
|
+
const rootSchemaPath = equivalentRoot.schemaRootPath;
|
|
906
|
+
const schemaPathParts = splitOutsideParenthesesAndArrays(rootSchemaPath);
|
|
907
|
+
for (let i = 0; i < schemaPathParts.length; i++) {
|
|
908
|
+
const subPath = joinParenthesesAndArrays(schemaPathParts.slice(0, i + 1));
|
|
909
|
+
const { equivalentEntry, postfix: equivalentEntryPostfix } = findEquivalentSchemaPathEntry(subPath, equivalentRoot.function);
|
|
910
|
+
if (equivalentEntry &&
|
|
911
|
+
(!bestCandidateLength || bestCandidateLength > i + 1)) {
|
|
912
|
+
bestCandidate = equivalentEntry;
|
|
913
|
+
bestCandidateLength = i + 1;
|
|
914
|
+
postfix = joinParenthesesAndArrays([
|
|
915
|
+
equivalentEntryPostfix,
|
|
916
|
+
...schemaPathParts.slice(i + 1),
|
|
917
|
+
].filter(Boolean));
|
|
918
|
+
}
|
|
883
919
|
}
|
|
884
920
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
921
|
+
if (bestCandidate) {
|
|
922
|
+
for (const root of esp.equivalentRoots) {
|
|
923
|
+
if (postfix.length > 0) {
|
|
924
|
+
root.postfix = postfix;
|
|
925
|
+
}
|
|
926
|
+
bestCandidate.equivalentRoots.push(root);
|
|
890
927
|
}
|
|
891
|
-
|
|
928
|
+
const postfixesToMerge = postfix.length > 0
|
|
929
|
+
? Object.keys(esp.equivalentPostfixes).reduce((acc, postfixPath) => {
|
|
930
|
+
const fullPath = joinParenthesesAndArrays([
|
|
931
|
+
postfix,
|
|
932
|
+
postfixPath,
|
|
933
|
+
]);
|
|
934
|
+
acc[fullPath] = esp.equivalentPostfixes[postfixPath];
|
|
935
|
+
return acc;
|
|
936
|
+
}, {})
|
|
937
|
+
: esp.equivalentPostfixes;
|
|
938
|
+
bestCandidate.equivalentPostfixes = {
|
|
939
|
+
...bestCandidate.equivalentPostfixes,
|
|
940
|
+
...postfixesToMerge,
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
mergedEquivalentSchemaPaths.push(esp);
|
|
892
945
|
}
|
|
893
|
-
const postfixesToMerge = postfix.length > 0
|
|
894
|
-
? Object.keys(esp.equivalentPostfixes).reduce((acc, postfixPath) => {
|
|
895
|
-
const fullPath = joinParenthesesAndArrays([
|
|
896
|
-
postfix,
|
|
897
|
-
postfixPath,
|
|
898
|
-
]);
|
|
899
|
-
acc[fullPath] = esp.equivalentPostfixes[postfixPath];
|
|
900
|
-
return acc;
|
|
901
|
-
}, {})
|
|
902
|
-
: esp.equivalentPostfixes;
|
|
903
|
-
bestCandidate.equivalentPostfixes = {
|
|
904
|
-
...bestCandidate.equivalentPostfixes,
|
|
905
|
-
...postfixesToMerge,
|
|
906
|
-
};
|
|
907
|
-
}
|
|
908
|
-
else {
|
|
909
|
-
mergedEquivalentSchemaPaths.push(esp);
|
|
910
946
|
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
mockedDependencies.add(`${dep.filePath}::${dep.name}`);
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
gatherAllEquivalentSchemaPaths(rootScopeName, dataStructure);
|
|
922
|
-
// Process dependencySchemas for all dependencies (including mocked ones)
|
|
923
|
-
// dependencySchemas contains usage information (how dependencies are called),
|
|
924
|
-
// not internal implementation, so we want this for mocked dependencies too
|
|
925
|
-
for (const dependency of importedExports) {
|
|
926
|
-
checkDeadline();
|
|
927
|
-
const dependentDataStructure = dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
928
|
-
if (!dependentDataStructure)
|
|
929
|
-
continue;
|
|
930
|
-
gatherAllEquivalentSchemaPaths(dependency.name, dependentDataStructure, dependentDataStructure);
|
|
931
|
-
}
|
|
932
|
-
for (const filePath in dependentAnalyses) {
|
|
933
|
-
for (const name in dependentAnalyses[filePath]) {
|
|
934
|
-
// Skip mocked dependencies - we don't want to merge in their internal implementation
|
|
935
|
-
if (mockedDependencies.has(`${filePath}::${name}`)) {
|
|
936
|
-
continue;
|
|
947
|
+
return mergedEquivalentSchemaPaths;
|
|
948
|
+
};
|
|
949
|
+
// Build a lookup of mocked dependencies to skip their internal implementation
|
|
950
|
+
const mockedDependencies = new Set();
|
|
951
|
+
for (const dep of importedExports) {
|
|
952
|
+
if (dep.isMocked) {
|
|
953
|
+
mockedDependencies.add(`${dep.filePath}::${dep.name}`);
|
|
937
954
|
}
|
|
938
|
-
const childMergedDataStructure = dependentAnalyses[filePath][name].metadata?.mergedDataStructure || {};
|
|
939
|
-
gatherAllEquivalentSchemaPaths(name, childMergedDataStructure);
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
const gatherElapsed = Date.now() - mergeStartTime;
|
|
943
|
-
equivalentSchemaPaths = mergeAllEquivalentSchemaPaths();
|
|
944
|
-
const mergeEspElapsed = Date.now() - mergeStartTime;
|
|
945
|
-
// Collect schemas that need cleaning — batch the calls for the end instead of
|
|
946
|
-
// calling cleanSchema inside the inner root loop (which was O(roots * schemaSize)).
|
|
947
|
-
const schemasToClean = new Set();
|
|
948
|
-
for (const esp of equivalentSchemaPaths) {
|
|
949
|
-
checkDeadline();
|
|
950
|
-
// Pre-compute which postfixes have children to avoid O(n²) lookups in the inner loop.
|
|
951
|
-
// A postfix "has children" if there are other postfixes that extend it.
|
|
952
|
-
const postfixesWithChildren = new Set();
|
|
953
|
-
const postfixKeys = Object.keys(esp.equivalentPostfixes);
|
|
954
|
-
// Pre-parse ALL postfix paths once. These parsed parts are reused in:
|
|
955
|
-
// 1. The children detection loop below
|
|
956
|
-
// 2. The inner postfix application loop (lines that split postfixPath and equivalentRoot.postfix)
|
|
957
|
-
// This eliminates thousands of redundant splitOutsideParenthesesAndArrays calls.
|
|
958
|
-
const postfixPartsCache = new Map();
|
|
959
|
-
for (const postfixPath of postfixKeys) {
|
|
960
|
-
if (!postfixPath)
|
|
961
|
-
continue;
|
|
962
|
-
postfixPartsCache.set(postfixPath, splitOutsideParenthesesAndArrays(postfixPath));
|
|
963
955
|
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
for (const postfixPath of postfixKeys) {
|
|
973
|
-
if (!postfixPath)
|
|
956
|
+
gatherAllEquivalentSchemaPaths(rootScopeName, dataStructure);
|
|
957
|
+
// Process dependencySchemas for all dependencies (including mocked ones)
|
|
958
|
+
// dependencySchemas contains usage information (how dependencies are called),
|
|
959
|
+
// not internal implementation, so we want this for mocked dependencies too
|
|
960
|
+
for (const dependency of importedExports) {
|
|
961
|
+
checkDeadline();
|
|
962
|
+
const dependentDataStructure = dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
963
|
+
if (!dependentDataStructure)
|
|
974
964
|
continue;
|
|
975
|
-
|
|
976
|
-
for (let i = 1; i < parts.length; i++) {
|
|
977
|
-
postfixPrefixSet.add(joinParenthesesAndArrays(parts.slice(0, i)));
|
|
978
|
-
}
|
|
965
|
+
gatherAllEquivalentSchemaPaths(dependency.name, dependentDataStructure, dependentDataStructure);
|
|
979
966
|
}
|
|
980
|
-
for (const
|
|
981
|
-
|
|
982
|
-
|
|
967
|
+
for (const filePath in dependentAnalyses) {
|
|
968
|
+
for (const name in dependentAnalyses[filePath]) {
|
|
969
|
+
// Skip mocked dependencies - we don't want to merge in their internal implementation
|
|
970
|
+
if (mockedDependencies.has(`${filePath}::${name}`)) {
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
const childMergedDataStructure = dependentAnalyses[filePath][name].metadata?.mergedDataStructure || {};
|
|
974
|
+
gatherAllEquivalentSchemaPaths(name, childMergedDataStructure);
|
|
983
975
|
}
|
|
984
976
|
}
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
const
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
seenRootKeys.add(key);
|
|
993
|
-
return true;
|
|
994
|
-
});
|
|
995
|
-
// Cap schema size to prevent combinatorial explosion.
|
|
996
|
-
// Successful merges produce <3K ret keys. Beyond 5K, further postfixes
|
|
997
|
-
// add noise but no useful data — they're cross-products of unrelated equivalencies.
|
|
998
|
-
const SCHEMA_KEY_CAP = 5000;
|
|
999
|
-
for (const equivalentRoot of uniqueRoots) {
|
|
977
|
+
const gatherElapsed = Date.now() - mergeStartTime;
|
|
978
|
+
equivalentSchemaPaths = mergeAllEquivalentSchemaPaths();
|
|
979
|
+
const mergeEspElapsed = Date.now() - mergeStartTime;
|
|
980
|
+
// Collect schemas that need cleaning — batch the calls for the end instead of
|
|
981
|
+
// calling cleanSchema inside the inner root loop (which was O(roots * schemaSize)).
|
|
982
|
+
const schemasToClean = new Set();
|
|
983
|
+
for (const esp of equivalentSchemaPaths) {
|
|
1000
984
|
checkDeadline();
|
|
1001
|
-
|
|
1002
|
-
if
|
|
1003
|
-
|
|
985
|
+
// Pre-compute which postfixes have children to avoid O(n²) lookups in the inner loop.
|
|
986
|
+
// A postfix "has children" if there are other postfixes that extend it.
|
|
987
|
+
const postfixesWithChildren = new Set();
|
|
988
|
+
const postfixKeys = Object.keys(esp.equivalentPostfixes);
|
|
989
|
+
// Pre-parse ALL postfix paths once. These parsed parts are reused in:
|
|
990
|
+
// 1. The children detection loop below
|
|
991
|
+
// 2. The inner postfix application loop (lines that split postfixPath and equivalentRoot.postfix)
|
|
992
|
+
// This eliminates thousands of redundant splitOutsideParenthesesAndArrays calls.
|
|
993
|
+
const postfixPartsCache = new Map();
|
|
994
|
+
for (const postfixPath of postfixKeys) {
|
|
995
|
+
if (!postfixPath)
|
|
996
|
+
continue;
|
|
997
|
+
postfixPartsCache.set(postfixPath, splitOutsideParenthesesAndArrays(postfixPath));
|
|
1004
998
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
999
|
+
// Check for empty postfix having children (any other postfixes exist)
|
|
1000
|
+
if (postfixKeys.length > 1 && '' in esp.equivalentPostfixes) {
|
|
1001
|
+
postfixesWithChildren.add('');
|
|
1007
1002
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1003
|
+
// Check for array element postfixes having children using a prefix set.
|
|
1004
|
+
// This avoids O(n²) scans across large postfix lists.
|
|
1005
|
+
// e.g., 'currentEntities[]' has children if a path like 'currentEntities[].sha' exists.
|
|
1006
|
+
const postfixPrefixSet = new Set();
|
|
1007
|
+
for (const postfixPath of postfixKeys) {
|
|
1008
|
+
if (!postfixPath)
|
|
1009
|
+
continue;
|
|
1010
|
+
const parts = postfixPartsCache.get(postfixPath);
|
|
1011
|
+
for (let i = 1; i < parts.length; i++) {
|
|
1012
|
+
postfixPrefixSet.add(joinParenthesesAndArrays(parts.slice(0, i)));
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
for (const postfixPath of postfixKeys) {
|
|
1016
|
+
if (postfixPath.endsWith('[]') && postfixPrefixSet.has(postfixPath)) {
|
|
1017
|
+
postfixesWithChildren.add(postfixPath);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
// Deduplicate equivalentRoots that would write to the same schema paths.
|
|
1021
|
+
// Roots with the same (function, schemaRootPath, postfix) are redundant.
|
|
1022
|
+
const seenRootKeys = new Set();
|
|
1023
|
+
const uniqueRoots = esp.equivalentRoots.filter((root) => {
|
|
1024
|
+
const key = `${root.function?.filePath ?? ''}::${root.function?.name ?? ''}::${root.schemaRootPath}::${root.postfix ?? ''}`;
|
|
1025
|
+
if (seenRootKeys.has(key))
|
|
1026
|
+
return false;
|
|
1027
|
+
seenRootKeys.add(key);
|
|
1028
|
+
return true;
|
|
1029
|
+
});
|
|
1030
|
+
for (const equivalentRoot of uniqueRoots) {
|
|
1017
1031
|
checkDeadline();
|
|
1018
|
-
let
|
|
1019
|
-
if (equivalentRoot.
|
|
1020
|
-
|
|
1021
|
-
// Must ensure exact path part match - "entityCode" should NOT match "entity" prefix.
|
|
1022
|
-
// Valid: "entity.foo" starts with "entity" (boundary at '.')
|
|
1023
|
-
// Valid: "entity[0]" starts with "entity" (boundary at '[')
|
|
1024
|
-
// Invalid: "entityCode" starts with "entity" (no boundary, different property)
|
|
1025
|
-
if (!postfixPath.startsWith(equivalentRoot.postfix)) {
|
|
1026
|
-
continue;
|
|
1027
|
-
}
|
|
1028
|
-
// Additional check: ensure the match is at a path boundary
|
|
1029
|
-
const nextChar = postfixPath[equivalentRoot.postfix.length];
|
|
1030
|
-
if (nextChar !== undefined && nextChar !== '.' && nextChar !== '[') {
|
|
1031
|
-
// The postfixPath continues with more characters that aren't a path separator.
|
|
1032
|
-
// This means "entity" matched "entityCode" which is wrong - they're different properties.
|
|
1033
|
-
continue;
|
|
1034
|
-
}
|
|
1035
|
-
const postFixPathParts = postfixPartsCache.get(postfixPath) ??
|
|
1036
|
-
splitOutsideParenthesesAndArrays(postfixPath);
|
|
1037
|
-
// Cache equivalentRoot.postfix parts — same root reused across all postfixes
|
|
1038
|
-
if (!postfixPartsCache.has(equivalentRoot.postfix)) {
|
|
1039
|
-
postfixPartsCache.set(equivalentRoot.postfix, splitOutsideParenthesesAndArrays(equivalentRoot.postfix));
|
|
1040
|
-
}
|
|
1041
|
-
const equivalentRootPostFixParts = postfixPartsCache.get(equivalentRoot.postfix);
|
|
1042
|
-
relevantPostfix = joinParenthesesAndArrays(postFixPathParts.slice(equivalentRootPostFixParts.length));
|
|
1032
|
+
let merged;
|
|
1033
|
+
if (equivalentRoot.function) {
|
|
1034
|
+
merged = findOrCreateDependentSchemas(equivalentRoot.function);
|
|
1043
1035
|
}
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
relevantPostfix,
|
|
1047
|
-
]);
|
|
1048
|
-
// Skip paths that would go through a primitive type
|
|
1049
|
-
// e.g., if schema has 'entities[].scenarioCount': 'number', skip 'entities[].scenarioCount.sha'
|
|
1050
|
-
if (wouldGoThroughPrimitive(newSchemaPath, schema)) {
|
|
1051
|
-
transformationTracer.operation(rootScopeName, {
|
|
1052
|
-
operation: 'skipPrimitivePath',
|
|
1053
|
-
stage: 'merged',
|
|
1054
|
-
path: newSchemaPath,
|
|
1055
|
-
context: {
|
|
1056
|
-
reason: 'would go through primitive type',
|
|
1057
|
-
postfixValue,
|
|
1058
|
-
},
|
|
1059
|
-
});
|
|
1060
|
-
continue;
|
|
1036
|
+
else {
|
|
1037
|
+
merged = mergedDataStructure;
|
|
1061
1038
|
}
|
|
1062
|
-
|
|
1063
|
-
// This prevents downgrading an object/array element to a primitive type.
|
|
1064
|
-
// Uses pre-computed postfixesWithChildren Set for O(1) lookup instead of O(n) iteration.
|
|
1065
|
-
const hasChildPostfixes = (relevantPostfix === '' || relevantPostfix.endsWith('[]')) &&
|
|
1066
|
-
postfixesWithChildren.has(postfixPath);
|
|
1067
|
-
if (PRIMITIVE_TYPES.has(postfixValue) && hasChildPostfixes) {
|
|
1039
|
+
if (!merged)
|
|
1068
1040
|
continue;
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
if (
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1041
|
+
const schema = equivalentRoot.schemaRootPath.startsWith('signature[')
|
|
1042
|
+
? merged.signatureSchema
|
|
1043
|
+
: merged.returnValueSchema;
|
|
1044
|
+
// Skip if this schema has already grown past the cap
|
|
1045
|
+
if (Object.keys(schema).length > SCHEMA_KEY_CAP)
|
|
1046
|
+
continue;
|
|
1047
|
+
for (const [postfixPath, postfixValue] of Object.entries(esp.equivalentPostfixes)) {
|
|
1048
|
+
checkDeadline();
|
|
1049
|
+
let relevantPostfix = postfixPath;
|
|
1050
|
+
if (equivalentRoot.postfix) {
|
|
1051
|
+
// Check if postfixPath starts with equivalentRoot.postfix at a path boundary.
|
|
1052
|
+
// Must ensure exact path part match - "entityCode" should NOT match "entity" prefix.
|
|
1053
|
+
// Valid: "entity.foo" starts with "entity" (boundary at '.')
|
|
1054
|
+
// Valid: "entity[0]" starts with "entity" (boundary at '[')
|
|
1055
|
+
// Invalid: "entityCode" starts with "entity" (no boundary, different property)
|
|
1056
|
+
if (!postfixPath.startsWith(equivalentRoot.postfix)) {
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
// Additional check: ensure the match is at a path boundary
|
|
1060
|
+
const nextChar = postfixPath[equivalentRoot.postfix.length];
|
|
1061
|
+
if (nextChar !== undefined &&
|
|
1062
|
+
nextChar !== '.' &&
|
|
1063
|
+
nextChar !== '[') {
|
|
1064
|
+
// The postfixPath continues with more characters that aren't a path separator.
|
|
1065
|
+
// This means "entity" matched "entityCode" which is wrong - they're different properties.
|
|
1066
|
+
continue;
|
|
1067
|
+
}
|
|
1068
|
+
const postFixPathParts = postfixPartsCache.get(postfixPath) ??
|
|
1069
|
+
splitOutsideParenthesesAndArrays(postfixPath);
|
|
1070
|
+
// Cache equivalentRoot.postfix parts — same root reused across all postfixes
|
|
1071
|
+
if (!postfixPartsCache.has(equivalentRoot.postfix)) {
|
|
1072
|
+
postfixPartsCache.set(equivalentRoot.postfix, splitOutsideParenthesesAndArrays(equivalentRoot.postfix));
|
|
1073
|
+
}
|
|
1074
|
+
const equivalentRootPostFixParts = postfixPartsCache.get(equivalentRoot.postfix);
|
|
1075
|
+
relevantPostfix = joinParenthesesAndArrays(postFixPathParts.slice(equivalentRootPostFixParts.length));
|
|
1076
|
+
}
|
|
1077
|
+
const newSchemaPath = joinParenthesesAndArrays([
|
|
1078
|
+
equivalentRoot.schemaRootPath,
|
|
1079
|
+
relevantPostfix,
|
|
1080
|
+
]);
|
|
1081
|
+
// Skip paths that would go through a primitive type
|
|
1082
|
+
// e.g., if schema has 'entities[].scenarioCount': 'number', skip 'entities[].scenarioCount.sha'
|
|
1083
|
+
if (wouldGoThroughPrimitive(newSchemaPath, schema)) {
|
|
1078
1084
|
transformationTracer.operation(rootScopeName, {
|
|
1079
|
-
operation: '
|
|
1085
|
+
operation: 'skipPrimitivePath',
|
|
1080
1086
|
stage: 'merged',
|
|
1081
1087
|
path: newSchemaPath,
|
|
1082
1088
|
context: {
|
|
1083
|
-
reason: 'would
|
|
1084
|
-
|
|
1085
|
-
newType: postfixValue,
|
|
1089
|
+
reason: 'would go through primitive type',
|
|
1090
|
+
postfixValue,
|
|
1086
1091
|
},
|
|
1087
1092
|
});
|
|
1088
1093
|
continue;
|
|
1089
1094
|
}
|
|
1090
|
-
//
|
|
1091
|
-
//
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
stage: 'merged',
|
|
1097
|
-
path: newSchemaPath,
|
|
1098
|
-
context: {
|
|
1099
|
-
reason: 'would overwrite complex type with primitive',
|
|
1100
|
-
existingType,
|
|
1101
|
-
newType: postfixValue,
|
|
1102
|
-
},
|
|
1103
|
-
});
|
|
1095
|
+
// Skip setting primitive type when there are child postfixes that indicate structure.
|
|
1096
|
+
// This prevents downgrading an object/array element to a primitive type.
|
|
1097
|
+
// Uses pre-computed postfixesWithChildren Set for O(1) lookup instead of O(n) iteration.
|
|
1098
|
+
const hasChildPostfixes = (relevantPostfix === '' || relevantPostfix.endsWith('[]')) &&
|
|
1099
|
+
postfixesWithChildren.has(postfixPath);
|
|
1100
|
+
if (PRIMITIVE_TYPES.has(postfixValue) && hasChildPostfixes) {
|
|
1104
1101
|
continue;
|
|
1105
1102
|
}
|
|
1103
|
+
// Don't overwrite a more specific type with a less specific one
|
|
1104
|
+
// This can happen when nested roots share entries with their parent roots
|
|
1105
|
+
const existingType = schema[newSchemaPath];
|
|
1106
|
+
if (existingType) {
|
|
1107
|
+
// Don't overwrite a primitive type with 'object' or 'array'
|
|
1108
|
+
// e.g., if schema has 'entities[].scenarioCount': 'number', don't overwrite with 'object'
|
|
1109
|
+
if (PRIMITIVE_TYPES.has(existingType) &&
|
|
1110
|
+
(postfixValue === 'object' || postfixValue === 'array')) {
|
|
1111
|
+
transformationTracer.operation(rootScopeName, {
|
|
1112
|
+
operation: 'skipTypeDowngrade',
|
|
1113
|
+
stage: 'merged',
|
|
1114
|
+
path: newSchemaPath,
|
|
1115
|
+
context: {
|
|
1116
|
+
reason: 'would overwrite primitive with object/array',
|
|
1117
|
+
existingType,
|
|
1118
|
+
newType: postfixValue,
|
|
1119
|
+
},
|
|
1120
|
+
});
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
// Don't overwrite a complex/union type with a primitive
|
|
1124
|
+
// e.g., if schema has 'scenarios[]': 'Scenario | null', don't overwrite with 'string'
|
|
1125
|
+
if (!PRIMITIVE_TYPES.has(existingType) &&
|
|
1126
|
+
PRIMITIVE_TYPES.has(postfixValue)) {
|
|
1127
|
+
transformationTracer.operation(rootScopeName, {
|
|
1128
|
+
operation: 'skipTypeDowngrade',
|
|
1129
|
+
stage: 'merged',
|
|
1130
|
+
path: newSchemaPath,
|
|
1131
|
+
context: {
|
|
1132
|
+
reason: 'would overwrite complex type with primitive',
|
|
1133
|
+
existingType,
|
|
1134
|
+
newType: postfixValue,
|
|
1135
|
+
},
|
|
1136
|
+
});
|
|
1137
|
+
continue;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
// Log the successful postfix merge
|
|
1141
|
+
transformationTracer.operation(rootScopeName, {
|
|
1142
|
+
operation: 'mergePostfix',
|
|
1143
|
+
stage: 'merged',
|
|
1144
|
+
path: newSchemaPath,
|
|
1145
|
+
before: existingType,
|
|
1146
|
+
after: postfixValue,
|
|
1147
|
+
context: {
|
|
1148
|
+
schemaRootPath: equivalentRoot.schemaRootPath,
|
|
1149
|
+
postfix: relevantPostfix,
|
|
1150
|
+
dependency: equivalentRoot.function?.name,
|
|
1151
|
+
},
|
|
1152
|
+
});
|
|
1153
|
+
schema[newSchemaPath] = postfixValue;
|
|
1106
1154
|
}
|
|
1107
|
-
|
|
1108
|
-
transformationTracer.operation(rootScopeName, {
|
|
1109
|
-
operation: 'mergePostfix',
|
|
1110
|
-
stage: 'merged',
|
|
1111
|
-
path: newSchemaPath,
|
|
1112
|
-
before: existingType,
|
|
1113
|
-
after: postfixValue,
|
|
1114
|
-
context: {
|
|
1115
|
-
schemaRootPath: equivalentRoot.schemaRootPath,
|
|
1116
|
-
postfix: relevantPostfix,
|
|
1117
|
-
dependency: equivalentRoot.function?.name,
|
|
1118
|
-
},
|
|
1119
|
-
});
|
|
1120
|
-
schema[newSchemaPath] = postfixValue;
|
|
1155
|
+
schemasToClean.add(schema);
|
|
1121
1156
|
}
|
|
1122
|
-
schemasToClean.add(schema);
|
|
1123
1157
|
}
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1158
|
+
const postfixElapsed = Date.now() - mergeStartTime;
|
|
1159
|
+
// Batch-clean all modified schemas once (instead of once per root per ESP entry)
|
|
1160
|
+
for (const schema of schemasToClean) {
|
|
1161
|
+
cleanSchema(schema, { stage: 'afterMergePostfix' });
|
|
1162
|
+
}
|
|
1163
|
+
const cleanElapsed = Date.now() - mergeStartTime;
|
|
1164
|
+
// Propagate equivalency-derived attributes to generic function call variants.
|
|
1165
|
+
// When attributes are traced via equivalencies (e.g., fileComparisons from buildDataMap.signature[2]),
|
|
1166
|
+
// they get written to non-generic paths (returnValue.data.x or funcName().functionCallReturnValue.data.x).
|
|
1167
|
+
// If the ORIGINAL input schema has generic variants (funcName<T>().functionCallReturnValue.data),
|
|
1168
|
+
// we need to copy the attributes to those paths too.
|
|
1169
|
+
for (const filePath in mergedDataStructure.dependencySchemas) {
|
|
1170
|
+
for (const depName in mergedDataStructure.dependencySchemas[filePath]) {
|
|
1171
|
+
const depSchema = mergedDataStructure.dependencySchemas[filePath][depName];
|
|
1172
|
+
const returnValueSchema = depSchema.returnValueSchema;
|
|
1173
|
+
// Look at the ORIGINAL input dependencySchemas for generic variants,
|
|
1174
|
+
// since the merged schema may have lost them during equivalency processing
|
|
1175
|
+
const originalSchema = dependencySchemas?.[filePath]?.[depName];
|
|
1176
|
+
const schemaToSearchForGenericVariants = originalSchema?.returnValueSchema || returnValueSchema;
|
|
1177
|
+
// Find all unique generic variants of this function
|
|
1178
|
+
// e.g., useFetcher<BranchEntityDiffResult>() from useFetcher<BranchEntityDiffResult>().functionCallReturnValue.data
|
|
1179
|
+
const genericVariants = new Set();
|
|
1180
|
+
const genericRegex = new RegExp(`^${depName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}<[^>]+>\\(\\)`);
|
|
1181
|
+
for (const path in schemaToSearchForGenericVariants) {
|
|
1182
|
+
checkDeadline();
|
|
1183
|
+
const match = path.match(genericRegex);
|
|
1184
|
+
if (match) {
|
|
1185
|
+
genericVariants.add(match[0]);
|
|
1186
|
+
}
|
|
1153
1187
|
}
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1188
|
+
if (genericVariants.size === 0)
|
|
1189
|
+
continue;
|
|
1190
|
+
// For each returnValue. path or non-generic function call path,
|
|
1191
|
+
// create corresponding paths for each generic variant
|
|
1192
|
+
const pathsToAdd = [];
|
|
1193
|
+
for (const path in returnValueSchema) {
|
|
1194
|
+
checkDeadline();
|
|
1195
|
+
const value = returnValueSchema[path];
|
|
1196
|
+
// Handle returnValue. paths
|
|
1197
|
+
if (path.startsWith('returnValue.')) {
|
|
1198
|
+
const suffix = path.slice('returnValue.'.length);
|
|
1199
|
+
for (const genericVariant of genericVariants) {
|
|
1200
|
+
const genericPath = `${genericVariant}.functionCallReturnValue.${suffix}`;
|
|
1201
|
+
if (!(genericPath in returnValueSchema)) {
|
|
1202
|
+
pathsToAdd.push([genericPath, value]);
|
|
1203
|
+
}
|
|
1170
1204
|
}
|
|
1171
1205
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1206
|
+
// Handle non-generic function call paths like depName().functionCallReturnValue.x
|
|
1207
|
+
else if (path.startsWith(`${depName}().functionCallReturnValue.`)) {
|
|
1208
|
+
const suffix = path.slice(`${depName}().functionCallReturnValue.`.length);
|
|
1209
|
+
for (const genericVariant of genericVariants) {
|
|
1210
|
+
const genericPath = `${genericVariant}.functionCallReturnValue.${suffix}`;
|
|
1211
|
+
if (!(genericPath in returnValueSchema)) {
|
|
1212
|
+
pathsToAdd.push([genericPath, value]);
|
|
1213
|
+
}
|
|
1180
1214
|
}
|
|
1181
1215
|
}
|
|
1182
1216
|
}
|
|
1217
|
+
// Add the new generic variant paths
|
|
1218
|
+
for (const [path, value] of pathsToAdd) {
|
|
1219
|
+
returnValueSchema[path] = value;
|
|
1220
|
+
}
|
|
1183
1221
|
}
|
|
1184
|
-
// Add the new generic variant paths
|
|
1185
|
-
for (const [path, value] of pathsToAdd) {
|
|
1186
|
-
returnValueSchema[path] = value;
|
|
1187
|
-
}
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
// For mocked dependencies: copy paths from dependencySchemas (usage info) and normalize
|
|
1191
|
-
// returnValue. paths that were created by equivalency processing.
|
|
1192
|
-
// This ensures all paths use the consistent functionName().functionCallReturnValue. format.
|
|
1193
|
-
for (const dependency of importedExports) {
|
|
1194
|
-
if (!dependency.isMocked)
|
|
1195
|
-
continue;
|
|
1196
|
-
const srcSchema = dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
1197
|
-
if (!srcSchema?.returnValueSchema)
|
|
1198
|
-
continue;
|
|
1199
|
-
const depSchema = findOrCreateDependentSchemas({
|
|
1200
|
-
filePath: dependency.filePath,
|
|
1201
|
-
name: dependency.name,
|
|
1202
|
-
});
|
|
1203
|
-
// First, normalize any returnValue paths that were written by equivalency processing
|
|
1204
|
-
// to the standard functionName().functionCallReturnValue format.
|
|
1205
|
-
// This includes both returnValue. (dot) and returnValue[ (array) paths.
|
|
1206
|
-
const pathsToNormalize = [];
|
|
1207
|
-
for (const path in depSchema.returnValueSchema) {
|
|
1208
|
-
checkDeadline();
|
|
1209
|
-
if (path === 'returnValue' ||
|
|
1210
|
-
path.startsWith('returnValue.') ||
|
|
1211
|
-
path.startsWith('returnValue[')) {
|
|
1212
|
-
pathsToNormalize.push([path, depSchema.returnValueSchema[path]]);
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
for (const [path, value] of pathsToNormalize) {
|
|
1216
|
-
delete depSchema.returnValueSchema[path];
|
|
1217
|
-
let normalizedPath;
|
|
1218
|
-
if (path === 'returnValue') {
|
|
1219
|
-
normalizedPath = `${dependency.name}().functionCallReturnValue`;
|
|
1220
|
-
}
|
|
1221
|
-
else if (path.startsWith('returnValue.')) {
|
|
1222
|
-
normalizedPath = path.replace(/^returnValue\./, `${dependency.name}().functionCallReturnValue.`);
|
|
1223
|
-
}
|
|
1224
|
-
else {
|
|
1225
|
-
// path.startsWith('returnValue[')
|
|
1226
|
-
// e.g., returnValue[] -> getOptions().functionCallReturnValue[]
|
|
1227
|
-
// e.g., returnValue[].label -> getOptions().functionCallReturnValue[].label
|
|
1228
|
-
normalizedPath = path.replace(/^returnValue/, `${dependency.name}().functionCallReturnValue`);
|
|
1229
|
-
}
|
|
1230
|
-
transformationTracer.operation(rootScopeName, {
|
|
1231
|
-
operation: 'normalizeReturnValuePath',
|
|
1232
|
-
stage: 'merged',
|
|
1233
|
-
path: normalizedPath,
|
|
1234
|
-
before: path,
|
|
1235
|
-
after: normalizedPath,
|
|
1236
|
-
context: { dependency: dependency.name, value },
|
|
1237
|
-
});
|
|
1238
|
-
depSchema.returnValueSchema[normalizedPath] = value;
|
|
1239
1222
|
}
|
|
1240
|
-
//
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
// 'returnValue.foo' -> 'dependencyName().functionCallReturnValue.foo'
|
|
1246
|
-
// This ensures consistency across the codebase and allows constructMockCode
|
|
1247
|
-
// and gatherDataForMocks to work correctly.
|
|
1248
|
-
if (path === 'returnValue' || path.startsWith('returnValue.')) {
|
|
1249
|
-
// Convert 'returnValue' -> 'name().functionCallReturnValue'
|
|
1250
|
-
// Convert 'returnValue.foo' -> 'name().functionCallReturnValue.foo'
|
|
1251
|
-
const normalizedPath = path === 'returnValue'
|
|
1252
|
-
? `${dependency.name}().functionCallReturnValue`
|
|
1253
|
-
: path.replace(/^returnValue\./, `${dependency.name}().functionCallReturnValue.`);
|
|
1254
|
-
// Always write srcSchema values - they take precedence over equivalency-derived values
|
|
1255
|
-
depSchema.returnValueSchema[normalizedPath] = value;
|
|
1223
|
+
// For mocked dependencies: copy paths from dependencySchemas (usage info) and normalize
|
|
1224
|
+
// returnValue. paths that were created by equivalency processing.
|
|
1225
|
+
// This ensures all paths use the consistent functionName().functionCallReturnValue. format.
|
|
1226
|
+
for (const dependency of importedExports) {
|
|
1227
|
+
if (!dependency.isMocked)
|
|
1256
1228
|
continue;
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
// These are needed for constructMockCode to build the proper mock data hierarchy
|
|
1260
|
-
// Example: supabase.auth.getSession().functionCallReturnValue.data.session
|
|
1261
|
-
if (path.includes('.functionCallReturnValue')) {
|
|
1262
|
-
// Always write srcSchema values - they take precedence over equivalency-derived values
|
|
1263
|
-
depSchema.returnValueSchema[path] = value;
|
|
1229
|
+
const srcSchema = dependencySchemas?.[dependency.filePath]?.[dependency.name];
|
|
1230
|
+
if (!srcSchema?.returnValueSchema)
|
|
1264
1231
|
continue;
|
|
1232
|
+
const depSchema = findOrCreateDependentSchemas({
|
|
1233
|
+
filePath: dependency.filePath,
|
|
1234
|
+
name: dependency.name,
|
|
1235
|
+
});
|
|
1236
|
+
// First, normalize any returnValue paths that were written by equivalency processing
|
|
1237
|
+
// to the standard functionName().functionCallReturnValue format.
|
|
1238
|
+
// This includes both returnValue. (dot) and returnValue[ (array) paths.
|
|
1239
|
+
const pathsToNormalize = [];
|
|
1240
|
+
for (const path in depSchema.returnValueSchema) {
|
|
1241
|
+
checkDeadline();
|
|
1242
|
+
if (path === 'returnValue' ||
|
|
1243
|
+
path.startsWith('returnValue.') ||
|
|
1244
|
+
path.startsWith('returnValue[')) {
|
|
1245
|
+
pathsToNormalize.push([path, depSchema.returnValueSchema[path]]);
|
|
1246
|
+
}
|
|
1265
1247
|
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
!path.startsWith('returnValue')) {
|
|
1275
|
-
if (!(path in depSchema.returnValueSchema)) {
|
|
1276
|
-
depSchema.returnValueSchema[path] = value;
|
|
1248
|
+
for (const [path, value] of pathsToNormalize) {
|
|
1249
|
+
delete depSchema.returnValueSchema[path];
|
|
1250
|
+
let normalizedPath;
|
|
1251
|
+
if (path === 'returnValue') {
|
|
1252
|
+
normalizedPath = `${dependency.name}().functionCallReturnValue`;
|
|
1253
|
+
}
|
|
1254
|
+
else if (path.startsWith('returnValue.')) {
|
|
1255
|
+
normalizedPath = path.replace(/^returnValue\./, `${dependency.name}().functionCallReturnValue.`);
|
|
1277
1256
|
}
|
|
1257
|
+
else {
|
|
1258
|
+
// path.startsWith('returnValue[')
|
|
1259
|
+
// e.g., returnValue[] -> getOptions().functionCallReturnValue[]
|
|
1260
|
+
// e.g., returnValue[].label -> getOptions().functionCallReturnValue[].label
|
|
1261
|
+
normalizedPath = path.replace(/^returnValue/, `${dependency.name}().functionCallReturnValue`);
|
|
1262
|
+
}
|
|
1263
|
+
transformationTracer.operation(rootScopeName, {
|
|
1264
|
+
operation: 'normalizeReturnValuePath',
|
|
1265
|
+
stage: 'merged',
|
|
1266
|
+
path: normalizedPath,
|
|
1267
|
+
before: path,
|
|
1268
|
+
after: normalizedPath,
|
|
1269
|
+
context: { dependency: dependency.name, value },
|
|
1270
|
+
});
|
|
1271
|
+
depSchema.returnValueSchema[normalizedPath] = value;
|
|
1278
1272
|
}
|
|
1279
|
-
//
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
// Only keep object paths that are within functionCallReturnValue
|
|
1296
|
-
if (isFunctionStyleDependency &&
|
|
1297
|
-
!path.includes('.functionCallReturnValue')) {
|
|
1273
|
+
// Now copy paths from the source schema (dependencySchemas)
|
|
1274
|
+
for (const path in srcSchema.returnValueSchema) {
|
|
1275
|
+
checkDeadline();
|
|
1276
|
+
const value = srcSchema.returnValueSchema[path];
|
|
1277
|
+
// Normalize paths starting with 'returnValue' to use the standard format:
|
|
1278
|
+
// 'returnValue.foo' -> 'dependencyName().functionCallReturnValue.foo'
|
|
1279
|
+
// This ensures consistency across the codebase and allows constructMockCode
|
|
1280
|
+
// and gatherDataForMocks to work correctly.
|
|
1281
|
+
if (path === 'returnValue' || path.startsWith('returnValue.')) {
|
|
1282
|
+
// Convert 'returnValue' -> 'name().functionCallReturnValue'
|
|
1283
|
+
// Convert 'returnValue.foo' -> 'name().functionCallReturnValue.foo'
|
|
1284
|
+
const normalizedPath = path === 'returnValue'
|
|
1285
|
+
? `${dependency.name}().functionCallReturnValue`
|
|
1286
|
+
: path.replace(/^returnValue\./, `${dependency.name}().functionCallReturnValue.`);
|
|
1287
|
+
// Always write srcSchema values - they take precedence over equivalency-derived values
|
|
1288
|
+
depSchema.returnValueSchema[normalizedPath] = value;
|
|
1298
1289
|
continue;
|
|
1299
1290
|
}
|
|
1300
|
-
|
|
1291
|
+
// Copy paths containing functionCallReturnValue (return value structures)
|
|
1292
|
+
// These are needed for constructMockCode to build the proper mock data hierarchy
|
|
1293
|
+
// Example: supabase.auth.getSession().functionCallReturnValue.data.session
|
|
1294
|
+
if (path.includes('.functionCallReturnValue')) {
|
|
1295
|
+
// Always write srcSchema values - they take precedence over equivalency-derived values
|
|
1301
1296
|
depSchema.returnValueSchema[path] = value;
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
cleanSchema(depSchema.returnValueSchema, {
|
|
1306
|
-
stage: 'afterMockedDependencyMerge',
|
|
1307
|
-
dependency: dependency.name,
|
|
1308
|
-
});
|
|
1309
|
-
// Pull signature requirements from downstream functions into the mocked return value.
|
|
1310
|
-
// When a mocked function's return flows into another function's signature (via usageEquivalencies),
|
|
1311
|
-
// we need to include that function's signature requirements in the mock.
|
|
1312
|
-
//
|
|
1313
|
-
// Example: fromE5() returns a currency object that flows to calculateTotalPrice(price, quantity).
|
|
1314
|
-
// calculateTotalPrice's signatureSchema shows signature[0].multiply() is required.
|
|
1315
|
-
// We need to add multiply() to fromE5's mock return value.
|
|
1316
|
-
const usageEquivalencies = srcSchema.usageEquivalencies ?? {};
|
|
1317
|
-
for (const [returnPath, equivalencies] of Object.entries(usageEquivalencies)) {
|
|
1318
|
-
// Only process return value paths (functionCallReturnValue)
|
|
1319
|
-
if (!returnPath.includes('.functionCallReturnValue'))
|
|
1320
|
-
continue;
|
|
1321
|
-
for (const equiv of equivalencies) {
|
|
1322
|
-
// Check if this equivalency points to a signature path
|
|
1323
|
-
const signatureMatch = equiv.schemaPath.match(/\.signature\[(\d+)\]$/);
|
|
1324
|
-
if (!signatureMatch)
|
|
1325
1297
|
continue;
|
|
1326
|
-
const targetFunctionName = cleanFunctionName(equiv.scopeNodeName);
|
|
1327
|
-
const signatureIndex = signatureMatch[1];
|
|
1328
|
-
// Look up the target function's analysis to get its signature requirements
|
|
1329
|
-
// First try dependentAnalyses, then dependencySchemas
|
|
1330
|
-
let targetSignatureSchema;
|
|
1331
|
-
// Check dependentAnalyses first (has the full merged analysis)
|
|
1332
|
-
for (const depFilePath in dependentAnalyses) {
|
|
1333
|
-
const analysis = dependentAnalyses[depFilePath]?.[targetFunctionName];
|
|
1334
|
-
if (analysis?.metadata?.mergedDataStructure?.signatureSchema) {
|
|
1335
|
-
targetSignatureSchema =
|
|
1336
|
-
analysis.metadata.mergedDataStructure.signatureSchema;
|
|
1337
|
-
break;
|
|
1338
|
-
}
|
|
1339
1298
|
}
|
|
1340
|
-
//
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1299
|
+
// Copy function-typed paths that end with () (are function calls)
|
|
1300
|
+
// These include:
|
|
1301
|
+
// - Function stubs without functionCallReturnValue (like onAuthStateChange)
|
|
1302
|
+
// - Function markers with async-function type (like getSession(): async-function)
|
|
1303
|
+
// which are needed for constructMockCode to know to generate async functions
|
|
1304
|
+
// Skip paths starting with 'returnValue' - they were already handled above
|
|
1305
|
+
if (['function', 'async-function'].includes(value) &&
|
|
1306
|
+
path.endsWith(')') &&
|
|
1307
|
+
!path.startsWith('returnValue')) {
|
|
1308
|
+
if (!(path in depSchema.returnValueSchema)) {
|
|
1309
|
+
depSchema.returnValueSchema[path] = value;
|
|
1348
1310
|
}
|
|
1349
1311
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
//
|
|
1353
|
-
//
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1312
|
+
// Copy object-typed paths for chained API access patterns (like trpc.customer.getCustomersByOrg)
|
|
1313
|
+
// These intermediate paths are needed for constructMockCode to build the nested mock structure.
|
|
1314
|
+
// Example: for trpc.customer.getCustomersByOrg.useQuery().functionCallReturnValue.data,
|
|
1315
|
+
// we need 'trpc', 'trpc.customer', 'trpc.customer.getCustomersByOrg' all typed as 'object'.
|
|
1316
|
+
// Skip paths starting with 'returnValue' - they were already handled above
|
|
1317
|
+
//
|
|
1318
|
+
// EXCEPTION: For function-style dependencies like getSupabase(), skip intermediate object
|
|
1319
|
+
// paths like 'getSupabase().auth' that are just property access after a function call.
|
|
1320
|
+
// These aren't needed because constructMockCode can infer the structure from the actual
|
|
1321
|
+
// function call paths like 'getSupabase().auth.getUser()'. We only need object paths
|
|
1322
|
+
// for object-style dependencies like 'supabase.auth' where the dependency itself is an object.
|
|
1323
|
+
if (value === 'object' && !path.startsWith('returnValue')) {
|
|
1324
|
+
// Check if this is a function-style dependency (path starts with name() or name<T>())
|
|
1325
|
+
const isFunctionStyleDependency = path.startsWith(`${dependency.name}()`) ||
|
|
1326
|
+
path.match(new RegExp(`^${dependency.name}<[^>]+>\\(\\)`));
|
|
1327
|
+
// For function-style dependencies, skip intermediate object paths
|
|
1328
|
+
// Only keep object paths that are within functionCallReturnValue
|
|
1329
|
+
if (isFunctionStyleDependency &&
|
|
1330
|
+
!path.includes('.functionCallReturnValue')) {
|
|
1357
1331
|
continue;
|
|
1358
|
-
|
|
1359
|
-
if (
|
|
1360
|
-
|
|
1361
|
-
// Extract the suffix after signature[N] (e.g., ".multiply(quantity)")
|
|
1362
|
-
const suffix = sigPath.slice(signaturePrefix.length);
|
|
1363
|
-
// Build the path for the mocked return value
|
|
1364
|
-
// e.g., fromE5(priceE5).functionCallReturnValue.multiply(quantity)
|
|
1365
|
-
const returnValuePath = returnPath + suffix;
|
|
1366
|
-
// Add to the mocked dependency's return value schema if not already present
|
|
1367
|
-
if (!(returnValuePath in depSchema.returnValueSchema)) {
|
|
1368
|
-
depSchema.returnValueSchema[returnValuePath] = sigType;
|
|
1332
|
+
}
|
|
1333
|
+
if (!(path in depSchema.returnValueSchema)) {
|
|
1334
|
+
depSchema.returnValueSchema[path] = value;
|
|
1369
1335
|
}
|
|
1370
1336
|
}
|
|
1371
1337
|
}
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
// This ensures the parent entity's direct usage of dependencies takes precedence.
|
|
1380
|
-
// When both parent and child use the same dependency (e.g., useLoaderData),
|
|
1381
|
-
// the parent's schema paths are preserved, and child's paths are merged in later.
|
|
1382
|
-
//
|
|
1383
|
-
// Some dependencies (like .d.ts type declaration files) may not have:
|
|
1384
|
-
// - Equivalencies with the root scope
|
|
1385
|
-
// - A dependent analysis (they're just type declarations)
|
|
1386
|
-
// - Be marked as mocked
|
|
1387
|
-
// Without this, their schemas would be lost entirely.
|
|
1388
|
-
for (const filePath in dependencySchemas) {
|
|
1389
|
-
for (const name in dependencySchemas[filePath]) {
|
|
1390
|
-
const srcSchema = dependencySchemas[filePath][name];
|
|
1391
|
-
if (!srcSchema)
|
|
1392
|
-
continue;
|
|
1393
|
-
// Skip mocked dependencies - they were already processed above with path normalization
|
|
1394
|
-
if (mockedDependencies.has(`${filePath}::${name}`)) {
|
|
1395
|
-
continue;
|
|
1396
|
-
}
|
|
1397
|
-
// Check if this dependency was already processed by equivalencies
|
|
1398
|
-
const existingSchema = mergedDataStructure.dependencySchemas[filePath]?.[name];
|
|
1399
|
-
// Only add if no existing schema (equivalencies didn't process it)
|
|
1400
|
-
if (!existingSchema) {
|
|
1401
|
-
const depSchema = findOrCreateDependentSchemas({ filePath, name });
|
|
1402
|
-
for (const path in srcSchema.returnValueSchema) {
|
|
1403
|
-
checkDeadline();
|
|
1404
|
-
depSchema.returnValueSchema[path] = srcSchema.returnValueSchema[path];
|
|
1405
|
-
}
|
|
1406
|
-
for (const path in srcSchema.signatureSchema) {
|
|
1407
|
-
checkDeadline();
|
|
1408
|
-
depSchema.signatureSchema[path] = srcSchema.signatureSchema[path];
|
|
1409
|
-
}
|
|
1410
|
-
// Clean known object functions (like String.prototype.replace, Array.prototype.map)
|
|
1411
|
-
// from the copied schema. Without this, method call paths on primitives like
|
|
1412
|
-
// "projectSlug.replace(...)" would cause convertDotNotation to create nested
|
|
1413
|
-
// object structures instead of preserving the primitive type.
|
|
1414
|
-
cleanSchema(depSchema.returnValueSchema, {
|
|
1415
|
-
stage: 'afterDependencySchemaCopy',
|
|
1416
|
-
filePath,
|
|
1417
|
-
dependency: name,
|
|
1418
|
-
});
|
|
1419
|
-
}
|
|
1420
|
-
// TYPE REFINEMENT: Check if dependentAnalyses has a more specific type for this dependency.
|
|
1421
|
-
// When a parent passes `entity.filePath` (string | undefined) to a child component
|
|
1422
|
-
// that requires `filePath: string`, we should use the child's more specific type.
|
|
1423
|
-
// This prevents mock data from having undefined values for required props.
|
|
1338
|
+
cleanSchema(depSchema.returnValueSchema, {
|
|
1339
|
+
stage: 'afterMockedDependencyMerge',
|
|
1340
|
+
dependency: dependency.name,
|
|
1341
|
+
});
|
|
1342
|
+
// Pull signature requirements from downstream functions into the mocked return value.
|
|
1343
|
+
// When a mocked function's return flows into another function's signature (via usageEquivalencies),
|
|
1344
|
+
// we need to include that function's signature requirements in the mock.
|
|
1424
1345
|
//
|
|
1425
|
-
//
|
|
1426
|
-
//
|
|
1427
|
-
//
|
|
1428
|
-
const
|
|
1429
|
-
const
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
const
|
|
1436
|
-
if (
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1346
|
+
// Example: fromE5() returns a currency object that flows to calculateTotalPrice(price, quantity).
|
|
1347
|
+
// calculateTotalPrice's signatureSchema shows signature[0].multiply() is required.
|
|
1348
|
+
// We need to add multiply() to fromE5's mock return value.
|
|
1349
|
+
const usageEquivalencies = srcSchema.usageEquivalencies ?? {};
|
|
1350
|
+
for (const [returnPath, equivalencies] of Object.entries(usageEquivalencies)) {
|
|
1351
|
+
// Only process return value paths (functionCallReturnValue)
|
|
1352
|
+
if (!returnPath.includes('.functionCallReturnValue'))
|
|
1353
|
+
continue;
|
|
1354
|
+
for (const equiv of equivalencies) {
|
|
1355
|
+
// Check if this equivalency points to a signature path
|
|
1356
|
+
const signatureMatch = equiv.schemaPath.match(/\.signature\[(\d+)\]$/);
|
|
1357
|
+
if (!signatureMatch)
|
|
1358
|
+
continue;
|
|
1359
|
+
const targetFunctionName = cleanFunctionName(equiv.scopeNodeName);
|
|
1360
|
+
const signatureIndex = signatureMatch[1];
|
|
1361
|
+
// Look up the target function's analysis to get its signature requirements
|
|
1362
|
+
// First try dependentAnalyses, then dependencySchemas
|
|
1363
|
+
let targetSignatureSchema;
|
|
1364
|
+
// Check dependentAnalyses first (has the full merged analysis)
|
|
1365
|
+
for (const depFilePath in dependentAnalyses) {
|
|
1366
|
+
const analysis = dependentAnalyses[depFilePath]?.[targetFunctionName];
|
|
1367
|
+
if (analysis?.metadata?.mergedDataStructure?.signatureSchema) {
|
|
1368
|
+
targetSignatureSchema =
|
|
1369
|
+
analysis.metadata.mergedDataStructure.signatureSchema;
|
|
1370
|
+
break;
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
// Fallback to dependencySchemas if not found
|
|
1374
|
+
if (!targetSignatureSchema) {
|
|
1375
|
+
for (const depFilePath in dependencySchemas) {
|
|
1376
|
+
const schema = dependencySchemas[depFilePath]?.[targetFunctionName];
|
|
1377
|
+
if (schema?.signatureSchema) {
|
|
1378
|
+
targetSignatureSchema = schema.signatureSchema;
|
|
1379
|
+
break;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
if (!targetSignatureSchema)
|
|
1384
|
+
continue;
|
|
1385
|
+
// Find all paths in the target's signatureSchema that extend from signature[N]
|
|
1386
|
+
// e.g., signature[0].multiply(quantity) -> .multiply(quantity)
|
|
1387
|
+
const signaturePrefix = `signature[${signatureIndex}]`;
|
|
1388
|
+
for (const [sigPath, sigType] of Object.entries(targetSignatureSchema)) {
|
|
1389
|
+
if (!sigPath.startsWith(signaturePrefix))
|
|
1390
|
+
continue;
|
|
1391
|
+
// Skip the base signature[N] path itself - we only want the method/property extensions
|
|
1392
|
+
if (sigPath === signaturePrefix)
|
|
1393
|
+
continue;
|
|
1394
|
+
// Extract the suffix after signature[N] (e.g., ".multiply(quantity)")
|
|
1395
|
+
const suffix = sigPath.slice(signaturePrefix.length);
|
|
1396
|
+
// Build the path for the mocked return value
|
|
1397
|
+
// e.g., fromE5(priceE5).functionCallReturnValue.multiply(quantity)
|
|
1398
|
+
const returnValuePath = returnPath + suffix;
|
|
1399
|
+
// Add to the mocked dependency's return value schema if not already present
|
|
1400
|
+
if (!(returnValuePath in depSchema.returnValueSchema)) {
|
|
1401
|
+
depSchema.returnValueSchema[returnValuePath] = sigType;
|
|
1444
1402
|
}
|
|
1445
1403
|
}
|
|
1446
1404
|
}
|
|
1447
1405
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1406
|
+
cleanSchema(depSchema.returnValueSchema, {
|
|
1407
|
+
stage: 'afterSignatureRequirementsMerge',
|
|
1408
|
+
dependency: dependency.name,
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
// Process the input dependencySchemas FIRST (before child dependentAnalyses).
|
|
1412
|
+
// This ensures the parent entity's direct usage of dependencies takes precedence.
|
|
1413
|
+
// When both parent and child use the same dependency (e.g., useLoaderData),
|
|
1414
|
+
// the parent's schema paths are preserved, and child's paths are merged in later.
|
|
1415
|
+
//
|
|
1416
|
+
// Some dependencies (like .d.ts type declaration files) may not have:
|
|
1417
|
+
// - Equivalencies with the root scope
|
|
1418
|
+
// - A dependent analysis (they're just type declarations)
|
|
1419
|
+
// - Be marked as mocked
|
|
1420
|
+
// Without this, their schemas would be lost entirely.
|
|
1421
|
+
for (const filePath in dependencySchemas) {
|
|
1422
|
+
for (const name in dependencySchemas[filePath]) {
|
|
1423
|
+
const srcSchema = dependencySchemas[filePath][name];
|
|
1424
|
+
if (!srcSchema)
|
|
1425
|
+
continue;
|
|
1426
|
+
// Skip mocked dependencies - they were already processed above with path normalization
|
|
1427
|
+
if (mockedDependencies.has(`${filePath}::${name}`)) {
|
|
1428
|
+
continue;
|
|
1464
1429
|
}
|
|
1465
|
-
//
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
});
|
|
1471
|
-
// Copy only paths that belong to this variant
|
|
1430
|
+
// Check if this dependency was already processed by equivalencies
|
|
1431
|
+
const existingSchema = mergedDataStructure.dependencySchemas[filePath]?.[name];
|
|
1432
|
+
// Only add if no existing schema (equivalencies didn't process it)
|
|
1433
|
+
if (!existingSchema) {
|
|
1434
|
+
const depSchema = findOrCreateDependentSchemas({ filePath, name });
|
|
1472
1435
|
for (const path in srcSchema.returnValueSchema) {
|
|
1473
1436
|
checkDeadline();
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1437
|
+
depSchema.returnValueSchema[path] =
|
|
1438
|
+
srcSchema.returnValueSchema[path];
|
|
1439
|
+
}
|
|
1440
|
+
for (const path in srcSchema.signatureSchema) {
|
|
1441
|
+
checkDeadline();
|
|
1442
|
+
depSchema.signatureSchema[path] = srcSchema.signatureSchema[path];
|
|
1478
1443
|
}
|
|
1479
|
-
|
|
1480
|
-
|
|
1444
|
+
// Clean known object functions (like String.prototype.replace, Array.prototype.map)
|
|
1445
|
+
// from the copied schema. Without this, method call paths on primitives like
|
|
1446
|
+
// "projectSlug.replace(...)" would cause convertDotNotation to create nested
|
|
1447
|
+
// object structures instead of preserving the primitive type.
|
|
1448
|
+
cleanSchema(depSchema.returnValueSchema, {
|
|
1449
|
+
stage: 'afterDependencySchemaCopy',
|
|
1481
1450
|
filePath,
|
|
1482
1451
|
dependency: name,
|
|
1483
|
-
variant,
|
|
1484
1452
|
});
|
|
1485
1453
|
}
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
for (const name in dependentAnalyses[filePath]) {
|
|
1495
|
-
checkDeadline();
|
|
1496
|
-
const dependentMergedDataStructure = dependentAnalyses[filePath][name].metadata?.mergedDataStructure;
|
|
1497
|
-
if (!dependentMergedDataStructure)
|
|
1498
|
-
continue;
|
|
1499
|
-
const isMocked = mockedDependencies.has(`${filePath}::${name}`);
|
|
1500
|
-
// For mocked dependencies: ONLY copy nested dependencySchemas (skip internal implementation)
|
|
1501
|
-
// For non-mocked dependencies: copy everything (signature, returnValue, and nested dependencySchemas)
|
|
1502
|
-
if (!isMocked) {
|
|
1503
|
-
// Create the dependency schema entry if it doesn't exist
|
|
1454
|
+
// TYPE REFINEMENT: Check if dependentAnalyses has a more specific type for this dependency.
|
|
1455
|
+
// When a parent passes `entity.filePath` (string | undefined) to a child component
|
|
1456
|
+
// that requires `filePath: string`, we should use the child's more specific type.
|
|
1457
|
+
// This prevents mock data from having undefined values for required props.
|
|
1458
|
+
//
|
|
1459
|
+
// This runs REGARDLESS of whether equivalencies already processed the schema,
|
|
1460
|
+
// because equivalencies copy the parent's type (string | undefined), not the child's
|
|
1461
|
+
// required type (string).
|
|
1504
1462
|
const depSchema = findOrCreateDependentSchemas({ filePath, name });
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1463
|
+
const childAnalysis = dependentAnalyses[filePath]?.[name];
|
|
1464
|
+
const childSignatureSchema = childAnalysis?.metadata?.mergedDataStructure?.signatureSchema;
|
|
1465
|
+
if (childSignatureSchema) {
|
|
1466
|
+
for (const path in depSchema.signatureSchema) {
|
|
1467
|
+
checkDeadline();
|
|
1468
|
+
const parentType = depSchema.signatureSchema[path];
|
|
1469
|
+
const childType = childSignatureSchema[path];
|
|
1470
|
+
if (parentType && childType) {
|
|
1471
|
+
// Check if parent has optional type and child has required type
|
|
1472
|
+
const parentIsOptional = parentType.includes('| undefined') ||
|
|
1473
|
+
parentType.includes('| null');
|
|
1474
|
+
const childIsOptional = childType.includes('| undefined') ||
|
|
1475
|
+
childType.includes('| null');
|
|
1476
|
+
// If child requires a more specific type (not optional), use it
|
|
1477
|
+
if (parentIsOptional && !childIsOptional) {
|
|
1478
|
+
depSchema.signatureSchema[path] = childType;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1513
1481
|
}
|
|
1514
1482
|
}
|
|
1515
|
-
//
|
|
1516
|
-
for
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1483
|
+
// For functions with multiple different type parameters, also create separate entries
|
|
1484
|
+
// for each type-parameterized variant. This allows gatherDataForMocks to look up
|
|
1485
|
+
// the specific schema for each call signature.
|
|
1486
|
+
// This runs regardless of whether the base entry already existed, since we need
|
|
1487
|
+
// the separate variant entries for proper schema lookup.
|
|
1488
|
+
const baseName = cleanFunctionName(name);
|
|
1489
|
+
if (functionsWithMultipleTypeParams.has(baseName)) {
|
|
1490
|
+
// Find all unique type-parameterized call signatures in the schema
|
|
1491
|
+
const typeParamVariants = new Set();
|
|
1492
|
+
for (const path of Object.keys(srcSchema.returnValueSchema)) {
|
|
1493
|
+
const parts = splitOutsideParenthesesAndArrays(path);
|
|
1494
|
+
if (parts.length > 0 &&
|
|
1495
|
+
parts[0].includes('<') &&
|
|
1496
|
+
parts[0].endsWith(')')) {
|
|
1497
|
+
typeParamVariants.add(parts[0]);
|
|
1498
|
+
}
|
|
1522
1499
|
}
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
for (const depFilePath in dependentMergedDataStructure.dependencySchemas) {
|
|
1529
|
-
for (const depName in dependentMergedDataStructure.dependencySchemas[depFilePath]) {
|
|
1530
|
-
const nestedDepSchema = dependentMergedDataStructure.dependencySchemas[depFilePath][depName];
|
|
1531
|
-
const targetDepSchema = findOrCreateDependentSchemas({
|
|
1532
|
-
filePath: depFilePath,
|
|
1533
|
-
name: depName,
|
|
1500
|
+
// Create a separate entry for each type-parameterized variant
|
|
1501
|
+
for (const variant of typeParamVariants) {
|
|
1502
|
+
const variantSchema = findOrCreateDependentSchemas({
|
|
1503
|
+
filePath,
|
|
1504
|
+
name: variant,
|
|
1534
1505
|
});
|
|
1535
|
-
//
|
|
1536
|
-
for (const path in
|
|
1506
|
+
// Copy only paths that belong to this variant
|
|
1507
|
+
for (const path in srcSchema.returnValueSchema) {
|
|
1537
1508
|
checkDeadline();
|
|
1538
|
-
if (
|
|
1539
|
-
|
|
1540
|
-
|
|
1509
|
+
if (path.startsWith(variant)) {
|
|
1510
|
+
variantSchema.returnValueSchema[path] =
|
|
1511
|
+
srcSchema.returnValueSchema[path];
|
|
1541
1512
|
}
|
|
1542
1513
|
}
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1514
|
+
cleanSchema(variantSchema.returnValueSchema, {
|
|
1515
|
+
stage: 'afterTypeVariantCopy',
|
|
1516
|
+
filePath,
|
|
1517
|
+
dependency: name,
|
|
1518
|
+
variant,
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
// Ensure ALL dependencies from dependentAnalyses are included in dependencySchemas,
|
|
1525
|
+
// even if they have no equivalencies with the root scope.
|
|
1526
|
+
// This preserves nested functionCallReturnValue paths that would otherwise be lost.
|
|
1527
|
+
// EXCEPT: Skip mocked dependencies - we don't want their internal implementation details.
|
|
1528
|
+
for (const filePath in dependentAnalyses) {
|
|
1529
|
+
for (const name in dependentAnalyses[filePath]) {
|
|
1530
|
+
checkDeadline();
|
|
1531
|
+
const dependentMergedDataStructure = dependentAnalyses[filePath][name].metadata?.mergedDataStructure;
|
|
1532
|
+
if (!dependentMergedDataStructure)
|
|
1533
|
+
continue;
|
|
1534
|
+
const isMocked = mockedDependencies.has(`${filePath}::${name}`);
|
|
1535
|
+
// For mocked dependencies: ONLY copy nested dependencySchemas (skip internal implementation)
|
|
1536
|
+
// For non-mocked dependencies: copy everything (signature, returnValue, and nested dependencySchemas)
|
|
1537
|
+
if (!isMocked) {
|
|
1538
|
+
// Create the dependency schema entry if it doesn't exist
|
|
1539
|
+
const depSchema = findOrCreateDependentSchemas({ filePath, name });
|
|
1540
|
+
// Copy over paths from the dependent's returnValueSchema.
|
|
1541
|
+
// Only add paths that don't already exist (don't overwrite values set by equivalencies).
|
|
1542
|
+
// Skip if either source or target exceeds the cap — copying 2,531 paths from
|
|
1543
|
+
// ArticleTable with translatePath on each takes ~1.5s for one entity.
|
|
1544
|
+
const srcRetSize = Object.keys(dependentMergedDataStructure.returnValueSchema || {}).length;
|
|
1545
|
+
if (srcRetSize > SCHEMA_KEY_CAP ||
|
|
1546
|
+
Object.keys(depSchema.returnValueSchema).length > SCHEMA_KEY_CAP)
|
|
1547
|
+
continue;
|
|
1548
|
+
for (const path in dependentMergedDataStructure.returnValueSchema) {
|
|
1549
|
+
// Fast path: only call translatePath when the path starts with the
|
|
1550
|
+
// dependency name (e.g., "ArticleTable().functionCallReturnValue.x").
|
|
1551
|
+
// Most paths start with "returnValue" or "signature" and don't need translation.
|
|
1552
|
+
const translatedPath = path.startsWith(name)
|
|
1553
|
+
? translatePath(path, name)
|
|
1554
|
+
: path;
|
|
1555
|
+
if (!(translatedPath in depSchema.returnValueSchema)) {
|
|
1556
|
+
depSchema.returnValueSchema[translatedPath] =
|
|
1557
|
+
dependentMergedDataStructure.returnValueSchema[path];
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
// Copy over signature schema as well
|
|
1561
|
+
for (const path in dependentMergedDataStructure.signatureSchema) {
|
|
1562
|
+
const translatedPath = path.startsWith(name)
|
|
1563
|
+
? translatePath(path, name)
|
|
1564
|
+
: path;
|
|
1565
|
+
if (!(translatedPath in depSchema.signatureSchema)) {
|
|
1566
|
+
depSchema.signatureSchema[translatedPath] =
|
|
1567
|
+
dependentMergedDataStructure.signatureSchema[path];
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
// Copy nested dependencySchemas for ALL entities (including mocked ones)
|
|
1572
|
+
// This represents what dependencies THIS entity uses, not its internal implementation
|
|
1573
|
+
if (dependentMergedDataStructure.dependencySchemas) {
|
|
1574
|
+
for (const depFilePath in dependentMergedDataStructure.dependencySchemas) {
|
|
1575
|
+
for (const depName in dependentMergedDataStructure
|
|
1576
|
+
.dependencySchemas[depFilePath]) {
|
|
1577
|
+
const nestedDepSchema = dependentMergedDataStructure.dependencySchemas[depFilePath][depName];
|
|
1578
|
+
const targetDepSchema = findOrCreateDependentSchemas({
|
|
1579
|
+
filePath: depFilePath,
|
|
1580
|
+
name: depName,
|
|
1581
|
+
});
|
|
1582
|
+
// Merge in the nested dependency schemas
|
|
1583
|
+
for (const path in nestedDepSchema.returnValueSchema) {
|
|
1584
|
+
checkDeadline();
|
|
1585
|
+
if (!(path in targetDepSchema.returnValueSchema)) {
|
|
1586
|
+
const value = nestedDepSchema.returnValueSchema[path];
|
|
1587
|
+
targetDepSchema.returnValueSchema[path] = value;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
for (const path in nestedDepSchema.signatureSchema) {
|
|
1591
|
+
checkDeadline();
|
|
1592
|
+
if (!(path in targetDepSchema.signatureSchema)) {
|
|
1593
|
+
targetDepSchema.signatureSchema[path] =
|
|
1594
|
+
nestedDepSchema.signatureSchema[path];
|
|
1595
|
+
}
|
|
1548
1596
|
}
|
|
1549
1597
|
}
|
|
1550
1598
|
}
|
|
1551
1599
|
}
|
|
1552
1600
|
}
|
|
1553
1601
|
}
|
|
1602
|
+
const totalElapsed = Date.now() - mergeStartTime;
|
|
1603
|
+
const retKeys = Object.keys(mergedDataStructure.returnValueSchema).length;
|
|
1604
|
+
// Only log phase breakdown for slow merges (>2s)
|
|
1605
|
+
if (totalElapsed > 2000) {
|
|
1606
|
+
console.log(`CodeYam Log Level 2: ${rootScopeName} merge phases: gather=${gatherElapsed}ms mergeESP=${mergeEspElapsed - gatherElapsed}ms postfix=${postfixElapsed - mergeEspElapsed}ms clean=${cleanElapsed - postfixElapsed}ms depCopy=${totalElapsed - cleanElapsed}ms total=${totalElapsed}ms ret=${retKeys}`);
|
|
1607
|
+
}
|
|
1608
|
+
return mergedDataStructure;
|
|
1554
1609
|
}
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1610
|
+
catch (error) {
|
|
1611
|
+
if (error instanceof DataStructureTimeoutError) {
|
|
1612
|
+
// Return partial results instead of propagating the timeout.
|
|
1613
|
+
// By this point, mergedDataStructure has valid data from completed phases
|
|
1614
|
+
// (gather + mergeESP complete in <1s, postfix/clean/depCopy may be partial).
|
|
1615
|
+
const retKeys = Object.keys(mergedDataStructure.returnValueSchema).length;
|
|
1616
|
+
console.log(`CodeYam Log Level 1: ${rootScopeName} merge timed out — returning partial results (${retKeys} ret keys, ${Math.round((Date.now() - mergeStartTime) / 1000)}s)`);
|
|
1617
|
+
mergedDataStructure.timedOut = true;
|
|
1618
|
+
return mergedDataStructure;
|
|
1619
|
+
}
|
|
1620
|
+
throw error;
|
|
1560
1621
|
}
|
|
1561
|
-
return mergedDataStructure;
|
|
1562
1622
|
}
|
|
1563
1623
|
//# sourceMappingURL=mergeInDependentDataStructure.js.map
|