@codeyam/codeyam-cli 0.1.0-staging.1 → 0.1.0-staging.28f73cf
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/package.json +8 -7
- package/analyzer-template/packages/ai/index.ts +2 -1
- package/analyzer-template/packages/ai/package.json +2 -2
- package/analyzer-template/packages/ai/scripts/ai-test-matrix.mjs +424 -0
- package/analyzer-template/packages/ai/src/lib/analyzeScope.ts +24 -0
- package/analyzer-template/packages/ai/src/lib/astScopes/astScopeAnalyzer.ts +6 -16
- package/analyzer-template/packages/ai/src/lib/astScopes/methodSemantics.ts +197 -0
- package/analyzer-template/packages/ai/src/lib/astScopes/paths.ts +28 -2
- package/analyzer-template/packages/ai/src/lib/astScopes/processExpression.ts +127 -4
- package/analyzer-template/packages/ai/src/lib/checkAllAttributes.ts +1 -3
- package/analyzer-template/packages/ai/src/lib/dataStructure/ScopeDataStructure.ts +1821 -542
- package/analyzer-template/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.ts +138 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.ts +1 -1
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.ts +139 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/DebugTracer.ts +224 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/PathManager.ts +203 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/README.md +294 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.ts +161 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.ts +235 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.ts +14 -6
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/selectBestValue.ts +70 -0
- package/analyzer-template/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.ts +113 -0
- package/analyzer-template/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.ts +36 -0
- package/analyzer-template/packages/ai/src/lib/generateChangesEntityDocumentation.ts +20 -2
- package/analyzer-template/packages/ai/src/lib/generateChangesEntityKeyAttributes.ts +51 -107
- package/analyzer-template/packages/ai/src/lib/generateChangesEntityScenarioData.ts +56 -160
- package/analyzer-template/packages/ai/src/lib/generateChangesEntityScenarios.ts +79 -265
- package/analyzer-template/packages/ai/src/lib/generateEntityDocumentation.ts +16 -2
- package/analyzer-template/packages/ai/src/lib/generateEntityKeyAttributes.ts +53 -176
- package/analyzer-template/packages/ai/src/lib/generateEntityScenarioData.ts +53 -154
- package/analyzer-template/packages/ai/src/lib/generateEntityScenarios.ts +84 -254
- package/analyzer-template/packages/ai/src/lib/generateStatementAnalysis.ts +48 -71
- package/analyzer-template/packages/ai/src/lib/getConditionalUsagesFromCode.ts +27 -6
- package/analyzer-template/packages/ai/src/lib/getLLMCallStats.ts +0 -14
- package/analyzer-template/packages/ai/src/lib/modelInfo.ts +15 -0
- package/analyzer-template/packages/ai/src/lib/promptGenerators/gatherAttributesMap.ts +42 -4
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.ts +8 -33
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.ts +54 -62
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.ts +93 -109
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.ts +8 -27
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.ts +33 -38
- package/analyzer-template/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.ts +30 -30
- package/analyzer-template/packages/ai/src/lib/types/index.ts +2 -0
- package/analyzer-template/packages/ai/src/lib/worker/SerializableDataStructure.ts +39 -0
- package/analyzer-template/packages/analyze/src/lib/FileAnalyzer.ts +52 -6
- package/analyzer-template/packages/analyze/src/lib/asts/nodes/index.ts +2 -1
- package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getAllDeclaredEntityNodes.ts +238 -0
- package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/getDeclaredEntityNode.ts +25 -0
- package/analyzer-template/packages/analyze/src/lib/asts/sourceFiles/index.ts +2 -0
- package/analyzer-template/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.ts +8 -10
- package/analyzer-template/packages/analyze/src/lib/files/analyze/analyzeEntities.ts +6 -1
- package/analyzer-template/packages/analyze/src/lib/files/analyze/gatherEntityMap.ts +8 -6
- package/analyzer-template/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.ts +5 -13
- package/analyzer-template/packages/analyze/src/lib/files/analyzeChange.ts +34 -15
- package/analyzer-template/packages/analyze/src/lib/files/analyzeEntity.ts +17 -3
- package/analyzer-template/packages/analyze/src/lib/files/analyzeInitial.ts +35 -16
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateDataStructure.ts +7 -1
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.ts +9 -1
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateScenarioData.ts +6 -1
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/generateScenarios.ts +9 -1
- package/analyzer-template/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.ts +15 -7
- package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.d.ts +23 -0
- package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.d.ts.map +1 -0
- package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js +30 -0
- package/analyzer-template/packages/aws/dist/src/lib/s3/getPresignedUrl.js.map +1 -0
- package/analyzer-template/packages/aws/package.json +5 -4
- package/analyzer-template/packages/aws/s3/index.ts +4 -0
- package/analyzer-template/packages/aws/src/lib/s3/getPresignedUrl.ts +62 -0
- package/analyzer-template/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.ts +28 -21
- package/analyzer-template/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.ts +18 -11
- package/analyzer-template/packages/generate/src/lib/scenarioComponent.ts +6 -3
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js +28 -21
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js.map +1 -1
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js +18 -11
- package/analyzer-template/packages/github/dist/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js.map +1 -1
- package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.js +5 -3
- package/analyzer-template/packages/github/dist/generate/src/lib/scenarioComponent.js.map +1 -1
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.d.ts +2 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.js +3 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/db.js.map +1 -1
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tableRelations.d.ts +2 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tableRelations.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.d.ts +37 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.d.ts.map +1 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.js +27 -0
- package/analyzer-template/packages/github/dist/supabase/src/lib/kysely/tables/debugReportsTable.js.map +1 -0
- package/analyzer-template/packages/github/dist/utils/index.d.ts +2 -0
- package/analyzer-template/packages/github/dist/utils/index.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/utils/index.js +2 -0
- package/analyzer-template/packages/github/dist/utils/index.js.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.d.ts +25 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.d.ts.map +1 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.js +40 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/Semaphore.js.map +1 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.js +5 -3
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.d.ts.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts +12 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts.map +1 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
- package/analyzer-template/packages/github/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
- package/analyzer-template/packages/supabase/src/lib/kysely/db.ts +6 -0
- package/analyzer-template/packages/supabase/src/lib/kysely/tableRelations.ts +3 -0
- package/analyzer-template/packages/supabase/src/lib/kysely/tables/debugReportsTable.ts +61 -0
- package/analyzer-template/packages/ui-components/src/scenario-editor/components/DataItemEditor.tsx +1 -1
- package/analyzer-template/packages/utils/dist/utils/index.d.ts +2 -0
- package/analyzer-template/packages/utils/dist/utils/index.d.ts.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/index.js +2 -0
- package/analyzer-template/packages/utils/dist/utils/index.js.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.d.ts +25 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.d.ts.map +1 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.js +40 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/Semaphore.js.map +1 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.d.ts.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.js +5 -3
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.d.ts.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.d.ts.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.d.ts.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts +12 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.d.ts.map +1 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
- package/analyzer-template/packages/utils/dist/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
- package/analyzer-template/packages/utils/index.ts +2 -0
- package/analyzer-template/packages/utils/src/lib/Semaphore.ts +42 -0
- package/analyzer-template/packages/utils/src/lib/frameworks/getNextRoutePath.ts +8 -3
- package/analyzer-template/packages/utils/src/lib/frameworks/getRemixRoutePath.ts +2 -1
- package/analyzer-template/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.ts +2 -1
- package/analyzer-template/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.ts +1 -0
- package/analyzer-template/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.ts +33 -0
- package/analyzer-template/project/constructMockCode.ts +170 -6
- package/analyzer-template/project/reconcileMockDataKeys.ts +13 -0
- package/analyzer-template/project/start.ts +1 -11
- package/analyzer-template/project/startScenarioCapture.ts +24 -0
- package/analyzer-template/project/trackGeneratedFiles.ts +41 -0
- package/analyzer-template/project/writeMockDataTsx.ts +125 -4
- package/analyzer-template/project/writeScenarioComponents.ts +199 -45
- package/analyzer-template/project/writeUniversalMocks.ts +72 -10
- package/background/src/lib/virtualized/project/constructMockCode.js +158 -7
- package/background/src/lib/virtualized/project/constructMockCode.js.map +1 -1
- package/background/src/lib/virtualized/project/reconcileMockDataKeys.js +12 -0
- package/background/src/lib/virtualized/project/reconcileMockDataKeys.js.map +1 -1
- package/background/src/lib/virtualized/project/start.js +1 -8
- package/background/src/lib/virtualized/project/start.js.map +1 -1
- package/background/src/lib/virtualized/project/startScenarioCapture.js +18 -0
- package/background/src/lib/virtualized/project/startScenarioCapture.js.map +1 -1
- package/background/src/lib/virtualized/project/trackGeneratedFiles.js +30 -0
- package/background/src/lib/virtualized/project/trackGeneratedFiles.js.map +1 -0
- package/background/src/lib/virtualized/project/writeMockDataTsx.js +95 -3
- package/background/src/lib/virtualized/project/writeMockDataTsx.js.map +1 -1
- package/background/src/lib/virtualized/project/writeScenarioComponents.js +144 -28
- package/background/src/lib/virtualized/project/writeScenarioComponents.js.map +1 -1
- package/background/src/lib/virtualized/project/writeUniversalMocks.js +59 -9
- package/background/src/lib/virtualized/project/writeUniversalMocks.js.map +1 -1
- package/codeyam-cli/scripts/apply-setup.js +288 -0
- package/codeyam-cli/scripts/apply-setup.js.map +1 -0
- package/codeyam-cli/scripts/extract-setup.js +130 -0
- package/codeyam-cli/scripts/extract-setup.js.map +1 -0
- package/codeyam-cli/scripts/fixtures/cal.com/universal-mocks/packages/prisma/index.js +238 -0
- package/codeyam-cli/scripts/fixtures/cal.com/universal-mocks/packages/prisma/index.js.map +1 -0
- package/codeyam-cli/src/cli.js +6 -0
- package/codeyam-cli/src/cli.js.map +1 -1
- package/codeyam-cli/src/codeyam-cli.js +0 -0
- package/codeyam-cli/src/commands/debug.js +221 -0
- package/codeyam-cli/src/commands/debug.js.map +1 -0
- package/codeyam-cli/src/commands/init.js +4 -23
- package/codeyam-cli/src/commands/init.js.map +1 -1
- package/codeyam-cli/src/commands/report.js +102 -0
- package/codeyam-cli/src/commands/report.js.map +1 -0
- package/codeyam-cli/src/commands/setup-sandbox.js +164 -0
- package/codeyam-cli/src/commands/setup-sandbox.js.map +1 -0
- package/codeyam-cli/src/utils/__tests__/cleanupAnalysisFiles.test.js +6 -6
- package/codeyam-cli/src/utils/__tests__/cleanupAnalysisFiles.test.js.map +1 -1
- package/codeyam-cli/src/utils/__tests__/setupClaudeCodeSettings.test.js +8 -0
- package/codeyam-cli/src/utils/__tests__/setupClaudeCodeSettings.test.js.map +1 -1
- package/codeyam-cli/src/utils/analysisRunner.js +4 -3
- package/codeyam-cli/src/utils/analysisRunner.js.map +1 -1
- package/codeyam-cli/src/utils/analyzer.js +30 -0
- package/codeyam-cli/src/utils/analyzer.js.map +1 -1
- package/codeyam-cli/src/utils/backgroundServer.js +25 -5
- package/codeyam-cli/src/utils/backgroundServer.js.map +1 -1
- package/codeyam-cli/src/utils/cleanupAnalysisFiles.js +2 -2
- package/codeyam-cli/src/utils/cleanupAnalysisFiles.js.map +1 -1
- package/codeyam-cli/src/utils/fileWatcher.js +75 -5
- package/codeyam-cli/src/utils/fileWatcher.js.map +1 -1
- package/codeyam-cli/src/utils/generateReport.js +219 -0
- package/codeyam-cli/src/utils/generateReport.js.map +1 -0
- package/codeyam-cli/src/utils/install-skills.js +7 -0
- package/codeyam-cli/src/utils/install-skills.js.map +1 -1
- package/codeyam-cli/src/utils/queue/__tests__/job.pidTracking.test.js +1 -0
- package/codeyam-cli/src/utils/queue/__tests__/job.pidTracking.test.js.map +1 -1
- package/codeyam-cli/src/utils/queue/job.js +8 -3
- package/codeyam-cli/src/utils/queue/job.js.map +1 -1
- package/codeyam-cli/src/utils/setupClaudeCodeSettings.js +4 -0
- package/codeyam-cli/src/utils/setupClaudeCodeSettings.js.map +1 -1
- package/codeyam-cli/src/utils/webappDetection.js +2 -1
- package/codeyam-cli/src/utils/webappDetection.js.map +1 -1
- package/codeyam-cli/src/webserver/app/lib/database.js +63 -2
- package/codeyam-cli/src/webserver/app/lib/database.js.map +1 -1
- package/codeyam-cli/src/webserver/backgroundServer.js +15 -35
- package/codeyam-cli/src/webserver/backgroundServer.js.map +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/EntityTypeIcon-D5ZHFomX.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{InteractivePreview-Dh-FldQK.js → InteractivePreview-XDSzQLOY.js} +3 -3
- package/codeyam-cli/src/webserver/build/client/assets/LibraryFunctionPreview-BYVx9KFp.js +3 -0
- package/codeyam-cli/src/webserver/build/client/assets/{LogViewer-Dp6DC845.js → LogViewer-CRcT5fOZ.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/ReportIssueModal-BORLgi0X.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/SafeScreenshot-Bual6h18.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-Bi-YUMa-.js +6 -0
- package/codeyam-cli/src/webserver/build/client/assets/ScenarioViewer-4D2vLLJz.js +5 -0
- package/codeyam-cli/src/webserver/build/client/assets/_index-BC200mfN.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-CxvZPkCv.js +10 -0
- package/codeyam-cli/src/webserver/build/client/assets/api.generate-report-l0sNRNKZ.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{chart-column-B2I7jQx2.js → chart-column-B8fb6wnw.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/chunk-WWGJGFF6-De6i8FUT.js +26 -0
- package/codeyam-cli/src/webserver/build/client/assets/{circle-alert-GwwOAbhw.js → circle-alert-IdsgAK39.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/circle-check-BACUUf75.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/clock-vWeoCemX.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/createLucideIcon-CS7XDrKv.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/dev.empty-DIOEw_3i.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{entity._sha._-1Z6D0fLM.js → entity._sha._-8Els_3Wb.js} +10 -10
- package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.create-scenario-C3FZJx1w.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.edit._scenarioId-YJz_igar.js +5 -0
- package/codeyam-cli/src/webserver/build/client/assets/entityStatus-BEqj2qBy.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{entityVersioning-DO2gCvXv.js → entityVersioning-Bk_YB1jM.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/entry.client-DiP0q291.js +5 -0
- package/codeyam-cli/src/webserver/build/client/assets/file-text-LM0mgxXE.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/files-Dxh9CcaV.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/git-BXmqrWCH.js +12 -0
- package/codeyam-cli/src/webserver/build/client/assets/globals-BGS74ED-.css +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/html2canvas-pro.esm-XQCGvadH.js +5 -0
- package/codeyam-cli/src/webserver/build/client/assets/index-D-zYbzFZ.js +8 -0
- package/codeyam-cli/src/webserver/build/client/assets/{loader-circle-DN7Vr40D.js → loader-circle-BXPKbHEb.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/manifest-1af162d4.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/root-DB7VgjCY.js +16 -0
- package/codeyam-cli/src/webserver/build/client/assets/{settings-MZc4XdmE.js → settings-5zF_GOcS.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/settings-Dc4MlMpK.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/simulations-BQ-02-jB.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/triangle-alert-D7k-ArFa.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{useLastLogLine-BBlyqxij.js → useLastLogLine-AlhS7g5F.js} +1 -1
- package/codeyam-cli/src/webserver/build/client/assets/useToast-Ddo4UQv7.js +1 -0
- package/codeyam-cli/src/webserver/build/client/assets/{zap-B4gsLUZQ.js → zap-_jw-9DCp.js} +1 -1
- package/codeyam-cli/src/webserver/build/server/assets/index-D4JpXSIO.js +1 -0
- package/codeyam-cli/src/webserver/build/server/assets/server-build-vwbN7n65.js +169 -0
- 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/server.js +1 -1
- package/codeyam-cli/src/webserver/server.js.map +1 -1
- package/codeyam-cli/templates/codeyam-setup-skill.md +85 -94
- package/codeyam-cli/templates/debug-command.md +125 -0
- package/package.json +9 -11
- package/packages/ai/index.js +1 -2
- package/packages/ai/index.js.map +1 -1
- package/packages/ai/src/lib/analyzeScope.js +13 -0
- package/packages/ai/src/lib/analyzeScope.js.map +1 -1
- package/packages/ai/src/lib/astScopes/astScopeAnalyzer.js +6 -15
- package/packages/ai/src/lib/astScopes/astScopeAnalyzer.js.map +1 -1
- package/packages/ai/src/lib/astScopes/methodSemantics.js +134 -0
- package/packages/ai/src/lib/astScopes/methodSemantics.js.map +1 -1
- package/packages/ai/src/lib/astScopes/paths.js +28 -3
- package/packages/ai/src/lib/astScopes/paths.js.map +1 -1
- package/packages/ai/src/lib/astScopes/processExpression.js +111 -3
- package/packages/ai/src/lib/astScopes/processExpression.js.map +1 -1
- package/packages/ai/src/lib/checkAllAttributes.js +1 -3
- package/packages/ai/src/lib/checkAllAttributes.js.map +1 -1
- package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js +1320 -396
- package/packages/ai/src/lib/dataStructure/ScopeDataStructure.js.map +1 -1
- package/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.js +137 -1
- package/packages/ai/src/lib/dataStructure/equivalencyManagers/FunctionCallManager.js.map +1 -1
- package/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.js +1 -1
- package/packages/ai/src/lib/dataStructure/equivalencyManagers/frameworks/ReactFrameworkManager.js.map +1 -1
- package/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.js +112 -0
- package/packages/ai/src/lib/dataStructure/helpers/BatchSchemaProcessor.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/DebugTracer.js +176 -0
- package/packages/ai/src/lib/dataStructure/helpers/DebugTracer.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/PathManager.js +178 -0
- package/packages/ai/src/lib/dataStructure/helpers/PathManager.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.js +138 -0
- package/packages/ai/src/lib/dataStructure/helpers/ScopeTreeManager.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.js +199 -0
- package/packages/ai/src/lib/dataStructure/helpers/VisitedTracker.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.js +14 -6
- package/packages/ai/src/lib/dataStructure/helpers/fillInSchemaGapsAndUnknowns.js.map +1 -1
- package/packages/ai/src/lib/dataStructure/helpers/selectBestValue.js +62 -0
- package/packages/ai/src/lib/dataStructure/helpers/selectBestValue.js.map +1 -0
- package/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.js +90 -0
- package/packages/ai/src/lib/dataStructure/helpers/uniqueIdUtils.js.map +1 -0
- package/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.js +22 -0
- package/packages/ai/src/lib/gatherRelevantDependentKeyAttributes.js.map +1 -1
- package/packages/ai/src/lib/generateChangesEntityDocumentation.js +19 -1
- package/packages/ai/src/lib/generateChangesEntityDocumentation.js.map +1 -1
- package/packages/ai/src/lib/generateChangesEntityKeyAttributes.js +51 -107
- package/packages/ai/src/lib/generateChangesEntityKeyAttributes.js.map +1 -1
- package/packages/ai/src/lib/generateChangesEntityScenarioData.js +55 -156
- package/packages/ai/src/lib/generateChangesEntityScenarioData.js.map +1 -1
- package/packages/ai/src/lib/generateChangesEntityScenarios.js +79 -262
- package/packages/ai/src/lib/generateChangesEntityScenarios.js.map +1 -1
- package/packages/ai/src/lib/generateEntityDocumentation.js +15 -1
- package/packages/ai/src/lib/generateEntityDocumentation.js.map +1 -1
- package/packages/ai/src/lib/generateEntityKeyAttributes.js +53 -176
- package/packages/ai/src/lib/generateEntityKeyAttributes.js.map +1 -1
- package/packages/ai/src/lib/generateEntityScenarioData.js +52 -152
- package/packages/ai/src/lib/generateEntityScenarioData.js.map +1 -1
- package/packages/ai/src/lib/generateEntityScenarios.js +88 -258
- package/packages/ai/src/lib/generateEntityScenarios.js.map +1 -1
- package/packages/ai/src/lib/generateStatementAnalysis.js +46 -71
- package/packages/ai/src/lib/generateStatementAnalysis.js.map +1 -1
- package/packages/ai/src/lib/getConditionalUsagesFromCode.js +13 -8
- package/packages/ai/src/lib/getConditionalUsagesFromCode.js.map +1 -1
- package/packages/ai/src/lib/getLLMCallStats.js +0 -14
- package/packages/ai/src/lib/getLLMCallStats.js.map +1 -1
- package/packages/ai/src/lib/modelInfo.js +15 -0
- package/packages/ai/src/lib/modelInfo.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/gatherAttributesMap.js +36 -3
- package/packages/ai/src/lib/promptGenerators/gatherAttributesMap.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.js +8 -33
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityDocumentationGenerator.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.js +35 -41
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenarioDataGenerator.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.js +59 -72
- package/packages/ai/src/lib/promptGenerators/generateChangesEntityScenariosGenerator.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.js +8 -27
- package/packages/ai/src/lib/promptGenerators/generateEntityDocumentationGenerator.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.js +24 -27
- package/packages/ai/src/lib/promptGenerators/generateEntityScenarioDataGenerator.js.map +1 -1
- package/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.js +21 -22
- package/packages/ai/src/lib/promptGenerators/generateEntityScenariosGenerator.js.map +1 -1
- package/packages/ai/src/lib/types/index.js +2 -0
- package/packages/ai/src/lib/types/index.js.map +1 -1
- package/packages/ai/src/lib/worker/SerializableDataStructure.js +7 -0
- package/packages/ai/src/lib/worker/SerializableDataStructure.js.map +1 -1
- package/packages/analyze/src/lib/FileAnalyzer.js +45 -5
- package/packages/analyze/src/lib/FileAnalyzer.js.map +1 -1
- package/packages/analyze/src/lib/asts/nodes/index.js +2 -1
- package/packages/analyze/src/lib/asts/nodes/index.js.map +1 -1
- package/packages/analyze/src/lib/asts/sourceFiles/getAllDeclaredEntityNodes.js +191 -0
- package/packages/analyze/src/lib/asts/sourceFiles/getAllDeclaredEntityNodes.js.map +1 -0
- package/packages/analyze/src/lib/asts/sourceFiles/getDeclaredEntityNode.js +16 -0
- package/packages/analyze/src/lib/asts/sourceFiles/getDeclaredEntityNode.js.map +1 -0
- package/packages/analyze/src/lib/asts/sourceFiles/index.js +2 -0
- package/packages/analyze/src/lib/asts/sourceFiles/index.js.map +1 -1
- package/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.js +6 -8
- package/packages/analyze/src/lib/files/analyze/analyzeEntities/prepareDataStructures.js.map +1 -1
- package/packages/analyze/src/lib/files/analyze/analyzeEntities.js +5 -1
- package/packages/analyze/src/lib/files/analyze/analyzeEntities.js.map +1 -1
- package/packages/analyze/src/lib/files/analyze/gatherEntityMap.js +8 -2
- package/packages/analyze/src/lib/files/analyze/gatherEntityMap.js.map +1 -1
- package/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.js +5 -8
- package/packages/analyze/src/lib/files/analyze/trackEntityCircularDependencies.js.map +1 -1
- package/packages/analyze/src/lib/files/analyzeChange.js +21 -9
- package/packages/analyze/src/lib/files/analyzeChange.js.map +1 -1
- package/packages/analyze/src/lib/files/analyzeEntity.js +10 -4
- package/packages/analyze/src/lib/files/analyzeEntity.js.map +1 -1
- package/packages/analyze/src/lib/files/analyzeInitial.js +21 -9
- package/packages/analyze/src/lib/files/analyzeInitial.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js +6 -1
- package/packages/analyze/src/lib/files/scenarios/generateDataStructure.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.js +9 -1
- package/packages/analyze/src/lib/files/scenarios/generateKeyAttributes.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/generateScenarioData.js +5 -1
- package/packages/analyze/src/lib/files/scenarios/generateScenarioData.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/generateScenarios.js +9 -1
- package/packages/analyze/src/lib/files/scenarios/generateScenarios.js.map +1 -1
- package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js +16 -7
- package/packages/analyze/src/lib/files/scenarios/mergeInDependentDataStructure.js.map +1 -1
- package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js +28 -21
- package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageNext.js.map +1 -1
- package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js +18 -11
- package/packages/generate/src/lib/componentScenarioPage/componentScenarioPageRemix.js.map +1 -1
- package/packages/generate/src/lib/scenarioComponent.js +5 -3
- package/packages/generate/src/lib/scenarioComponent.js.map +1 -1
- package/packages/supabase/src/lib/kysely/db.js +3 -0
- package/packages/supabase/src/lib/kysely/db.js.map +1 -1
- package/packages/supabase/src/lib/kysely/tables/debugReportsTable.js +27 -0
- package/packages/supabase/src/lib/kysely/tables/debugReportsTable.js.map +1 -0
- package/packages/utils/index.js +2 -0
- package/packages/utils/index.js.map +1 -1
- package/packages/utils/src/lib/Semaphore.js +40 -0
- package/packages/utils/src/lib/Semaphore.js.map +1 -0
- package/packages/utils/src/lib/frameworks/getNextRoutePath.js +5 -3
- package/packages/utils/src/lib/frameworks/getNextRoutePath.js.map +1 -1
- package/packages/utils/src/lib/frameworks/getRemixRoutePath.js +2 -1
- package/packages/utils/src/lib/frameworks/getRemixRoutePath.js.map +1 -1
- package/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.js +2 -1
- package/packages/utils/src/lib/frameworks/nextRouteFileNameToRoute.js.map +1 -1
- package/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.js +1 -0
- package/packages/utils/src/lib/frameworks/remixRouteFileNameToRoute.js.map +1 -1
- package/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.js +32 -0
- package/packages/utils/src/lib/frameworks/sanitizeNextRouteSegments.js.map +1 -0
- package/analyzer-template/packages/ai/src/lib/generateEntityDataMap.ts +0 -375
- package/codeyam-cli/src/webserver/build/client/assets/EntityTypeIcon-GqWwt5wG.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/LibraryFunctionPreview-p0fuyqGQ.js +0 -3
- package/codeyam-cli/src/webserver/build/client/assets/SafeScreenshot-xwuhwsZH.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/ScenarioPreview-Bl2IRh55.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/ScenarioViewer-M2QuSHKC.js +0 -5
- package/codeyam-cli/src/webserver/build/client/assets/_index-CAVtep9Q.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/activity.(_tab)-CLmzsLsT.js +0 -10
- package/codeyam-cli/src/webserver/build/client/assets/components-CAx5ONX_.js +0 -40
- package/codeyam-cli/src/webserver/build/client/assets/createLucideIcon-CgyOwWip.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/dev.empty-DGy3zrli.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.create-scenario-ChAdTrrU.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/entity._sha_.edit._scenarioId-D9L7267w.js +0 -5
- package/codeyam-cli/src/webserver/build/client/assets/entry.client-C6FRgjPr.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/files-C3-cQjgv.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/git-Dp4EB9nv.js +0 -12
- package/codeyam-cli/src/webserver/build/client/assets/globals-Da3jt49-.css +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/manifest-172a4629.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/root-COyVTsPq.js +0 -16
- package/codeyam-cli/src/webserver/build/client/assets/search-CvyP_1Lo.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/settings-Hbf8b7J_.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/simulations-BMBi0VzO.js +0 -1
- package/codeyam-cli/src/webserver/build/client/assets/useToast-C_VxoXTh.js +0 -1
- package/codeyam-cli/src/webserver/build/client/cy-logo-cli.svg +0 -13
- package/codeyam-cli/src/webserver/build/server/assets/index-eAULANMV.js +0 -1
- package/codeyam-cli/src/webserver/build/server/assets/server-build-lutv16q5.js +0 -161
- package/codeyam-cli/src/webserver/public/cy-logo-cli.svg +0 -13
- package/packages/ai/src/lib/generateEntityDataMap.js +0 -335
- package/packages/ai/src/lib/generateEntityDataMap.js.map +0 -1
- package/packages/ai/src/lib/promptGenerators/generateEntityDataMapGenerator.js +0 -17
- package/packages/ai/src/lib/promptGenerators/generateEntityDataMapGenerator.js.map +0 -1
|
@@ -1,36 +1,104 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* ScopeDataStructure
|
|
2
3
|
*
|
|
3
|
-
* The
|
|
4
|
-
*
|
|
4
|
+
* The core engine for CodeYam's static analysis. Takes AST analysis output and
|
|
5
|
+
* determines the complete data schema for a function by tracking how data flows
|
|
6
|
+
* through the code.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
8
|
+
* ## High-Level Architecture
|
|
7
9
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
+
* ```
|
|
11
|
+
* ┌─────────────────┐ ┌─────────────────────────────────────────────┐
|
|
12
|
+
* │ AST Analyzer │────▶│ ScopeDataStructure │
|
|
13
|
+
* │ (per-scope) │ │ │
|
|
14
|
+
* └─────────────────┘ │ Phase 1: Discovery (onlyEquivalencies) │
|
|
15
|
+
* │ ├─ Process each scope's analysis │
|
|
16
|
+
* │ ├─ Build equivalency relationships │
|
|
17
|
+
* │ └─ Track nested scope hierarchy │
|
|
18
|
+
* │ │
|
|
19
|
+
* │ Phase 2: Schema Building │
|
|
20
|
+
* │ ├─ Follow all equivalencies │
|
|
21
|
+
* │ ├─ Build complete schemas at data sources │
|
|
22
|
+
* │ └─ Populate equivalencyDatabase │
|
|
23
|
+
* └─────────────────────────────────────────────┘
|
|
24
|
+
* │
|
|
25
|
+
* ▼
|
|
26
|
+
* ┌─────────────────────────────────────────────┐
|
|
27
|
+
* │ Output │
|
|
28
|
+
* │ • Function signature schemas │
|
|
29
|
+
* │ • Return value types │
|
|
30
|
+
* │ • External API call schemas │
|
|
31
|
+
* │ • Data flow (source → usage) mappings │
|
|
32
|
+
* └─────────────────────────────────────────────┘
|
|
33
|
+
* ```
|
|
10
34
|
*
|
|
11
|
-
*
|
|
12
|
-
* source of the data (either the function's signature arguments or an external
|
|
13
|
-
* API call made from within the function). While following the equivalencies the
|
|
14
|
-
* second pass populates the equivalencyDatabase which tracks all equivalent values
|
|
15
|
-
* and provides easy access to determine where data comes from and where it is used
|
|
16
|
-
* to call external functions. This makes it possible to combine schemas between
|
|
17
|
-
* functions to create a complete schema for a dependency tree.
|
|
35
|
+
* ## Key Concepts
|
|
18
36
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
37
|
+
* ### Equivalencies
|
|
38
|
+
* When code assigns one variable to another (`const user = props.user`), we create
|
|
39
|
+
* an "equivalency" linking the two paths. Following these links lets us trace data
|
|
40
|
+
* back to its source (function arguments or API returns).
|
|
41
|
+
*
|
|
42
|
+
* ### Schema Paths
|
|
43
|
+
* Dot-separated strings describing data structure:
|
|
44
|
+
* - `signature[0].user.id` - First arg's user.id property
|
|
45
|
+
* - `items[].name` - name property of array elements
|
|
46
|
+
* - `functionCallReturnValue(fetch).data` - Return from fetch() call
|
|
47
|
+
*
|
|
48
|
+
* ### Two-Phase Processing
|
|
49
|
+
* 1. **Phase 1 (Discovery)**: `onlyEquivalencies = true`
|
|
50
|
+
* - Processes scopes, builds equivalency graph
|
|
51
|
+
* - Does NOT populate schemas yet
|
|
52
|
+
*
|
|
53
|
+
* 2. **Phase 2 (Schema Building)**: `captureCompleteSchema()`
|
|
54
|
+
* - Follows equivalencies to build complete schemas
|
|
55
|
+
* - Populates `equivalencyDatabase` for source/usage queries
|
|
56
|
+
*
|
|
57
|
+
* ## Key Methods (Entry Points)
|
|
58
|
+
*
|
|
59
|
+
* - `constructor(scopeName, scopeText, managers, scopeAnalysis)` - Initialize with root scope
|
|
60
|
+
* - `addScopeAnalysis(scopeName, text, analysis)` - Add nested scope
|
|
61
|
+
* - `captureCompleteSchema()` - Trigger Phase 2 processing
|
|
62
|
+
* - `getSchema({scopeName})` - Get schema for a scope
|
|
63
|
+
* - `getFunctionSignature({functionName})` - Get function signature schema
|
|
64
|
+
* - `getSourceEquivalencies()` - Find where data comes from
|
|
65
|
+
* - `getUsageEquivalencies()` - Find where data is used
|
|
66
|
+
*
|
|
67
|
+
* ## Debugging Tips
|
|
68
|
+
*
|
|
69
|
+
* - Enable metrics: `addToSchemaCallCount`, `followEquivalenciesCallCount`
|
|
70
|
+
* - Check `scopeNode.equivalencies` for raw equivalency data
|
|
71
|
+
* - Use `getEquivalenciesDatabaseEntry(scope, path)` to trace a specific path
|
|
72
|
+
* - The `visitedTracker` prevents infinite loops - if data is missing, check if
|
|
73
|
+
* it was already visited with a different value
|
|
74
|
+
*
|
|
75
|
+
* ## Related Files
|
|
76
|
+
*
|
|
77
|
+
* - `helpers/` - Extracted utility modules (PathManager, VisitedTracker, etc.)
|
|
78
|
+
* - `equivalencyManagers/` - Plugins that create equivalencies from AST patterns
|
|
79
|
+
* - `helpers/README.md` - Overview of the helper module architecture
|
|
22
80
|
*/
|
|
23
81
|
|
|
24
82
|
import { ScopeAnalysis } from '~codeyam/types';
|
|
25
|
-
import {
|
|
26
|
-
joinParenthesesAndArrays,
|
|
27
|
-
splitOutsideParenthesesAndArrays,
|
|
28
|
-
} from '../splitOutsideParentheses';
|
|
29
83
|
import { EquivalencyManager } from './equivalencyManagers/EquivalencyManager';
|
|
30
84
|
import fillInSchemaGapsAndUnknowns from './helpers/fillInSchemaGapsAndUnknowns';
|
|
31
85
|
import ensureSchemaConsistency from './helpers/ensureSchemaConsistency';
|
|
32
|
-
import cleanOutBoundary from '../cleanOutBoundary';
|
|
33
86
|
import cleanPath from './helpers/cleanPath';
|
|
87
|
+
import { PathManager } from './helpers/PathManager';
|
|
88
|
+
import {
|
|
89
|
+
uniqueId,
|
|
90
|
+
uniqueScopeVariables,
|
|
91
|
+
uniqueScopeAndPaths,
|
|
92
|
+
} from './helpers/uniqueIdUtils';
|
|
93
|
+
import selectBestValue from './helpers/selectBestValue';
|
|
94
|
+
import { VisitedTracker } from './helpers/VisitedTracker';
|
|
95
|
+
import { DebugTracer } from './helpers/DebugTracer';
|
|
96
|
+
import { BatchSchemaProcessor } from './helpers/BatchSchemaProcessor';
|
|
97
|
+
import {
|
|
98
|
+
ScopeTreeManager,
|
|
99
|
+
ROOT_SCOPE_NAME,
|
|
100
|
+
ScopeTreeNode,
|
|
101
|
+
} from './helpers/ScopeTreeManager';
|
|
34
102
|
import cleanScopeNodeName from './helpers/cleanScopeNodeName';
|
|
35
103
|
import getFunctionCallRoot from './helpers/getFunctionCallRoot';
|
|
36
104
|
import cleanPathOfNonTransformingFunctions from './helpers/cleanPathOfNonTransformingFunctions';
|
|
@@ -42,38 +110,10 @@ import type {
|
|
|
42
110
|
SerializableScopeVariable,
|
|
43
111
|
} from '../worker/SerializableDataStructure';
|
|
44
112
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// equivalentScopeNodeName: string,
|
|
50
|
-
// equivalentPath: string,
|
|
51
|
-
// newEquivalentPath: string,
|
|
52
|
-
// equivalencyReason: string,
|
|
53
|
-
// ) {
|
|
54
|
-
// if ([
|
|
55
|
-
// 'original equivalency'
|
|
56
|
-
// ].includes(equivalencyReason)) {
|
|
57
|
-
// return
|
|
58
|
-
// }
|
|
59
|
-
|
|
60
|
-
// appendFileSync('equivalencies.txt', '\n\n');
|
|
61
|
-
// appendFileSync('equivalencies.txt',
|
|
62
|
-
// JSON.stringify(
|
|
63
|
-
// {
|
|
64
|
-
// newEquivalentPath,
|
|
65
|
-
// scopeNodeName,
|
|
66
|
-
// path,
|
|
67
|
-
// equivalentScopeNodeName,
|
|
68
|
-
// equivalentPath,
|
|
69
|
-
// equivalencyReason,
|
|
70
|
-
// },
|
|
71
|
-
// null,
|
|
72
|
-
// 2,
|
|
73
|
-
// ),
|
|
74
|
-
// );
|
|
75
|
-
// }
|
|
76
|
-
|
|
113
|
+
/**
|
|
114
|
+
* ScopeInfo represents a function scope discovered during AST analysis.
|
|
115
|
+
* Contains the raw analysis data before it's processed into a ScopeNode.
|
|
116
|
+
*/
|
|
77
117
|
export interface ScopeInfo {
|
|
78
118
|
name: string;
|
|
79
119
|
text: string;
|
|
@@ -87,46 +127,94 @@ export interface ScopeInfo {
|
|
|
87
127
|
analysis?: any;
|
|
88
128
|
}
|
|
89
129
|
|
|
130
|
+
/**
|
|
131
|
+
* ScopeNode is the processed representation of a function scope.
|
|
132
|
+
* Contains the schema and equivalencies built during analysis.
|
|
133
|
+
*
|
|
134
|
+
* Key fields:
|
|
135
|
+
* - `schema`: Map of path → type (e.g., `{ 'user.id': 'string' }`)
|
|
136
|
+
* - `equivalencies`: Map of path → equivalent paths in other scopes
|
|
137
|
+
* - `functionCalls`: External function calls made from this scope
|
|
138
|
+
*/
|
|
90
139
|
export interface ScopeNode {
|
|
140
|
+
/** Unique scope name, may include suffix like `____cyScope0A` for disambiguation */
|
|
91
141
|
name: string;
|
|
142
|
+
/** Path from root scope to this scope (e.g., ['outer', 'inner']) */
|
|
92
143
|
tree: string[];
|
|
144
|
+
/** Original source code text of the function */
|
|
93
145
|
text: string;
|
|
146
|
+
/** Raw AST analysis output */
|
|
94
147
|
analysis: ScopeAnalysis;
|
|
148
|
+
/** True if this is a static class method */
|
|
95
149
|
isStatic?: boolean;
|
|
150
|
+
/** The built schema: maps schema paths to type values */
|
|
96
151
|
schema: Record<string, string>;
|
|
97
|
-
|
|
152
|
+
/** Maps local paths to equivalent paths in other scopes (for tracing data flow) */
|
|
153
|
+
equivalencies: Record<string, ScopeVariable[]>;
|
|
154
|
+
/** External function calls made from this scope */
|
|
98
155
|
functionCalls: FunctionCallInfo[];
|
|
156
|
+
/** True if this scope is a function definition (vs. class/module) */
|
|
99
157
|
isFunctionDefinition: boolean;
|
|
100
|
-
|
|
101
|
-
|
|
158
|
+
/** Variables created in this scope (e.g., via `const x = ...`) */
|
|
159
|
+
instantiatedVariables?: string[];
|
|
160
|
+
/** Variables from parent scope accessible in this scope */
|
|
161
|
+
parentInstantiatedVariables?: string[];
|
|
102
162
|
}
|
|
103
163
|
|
|
104
|
-
export
|
|
105
|
-
|
|
106
|
-
children: ScopeTreeNode[];
|
|
107
|
-
}
|
|
164
|
+
// Re-export ScopeTreeNode from ScopeTreeManager for backward compatibility
|
|
165
|
+
export type { ScopeTreeNode };
|
|
108
166
|
|
|
167
|
+
/**
|
|
168
|
+
* ScopeVariable represents a reference to a specific path in a specific scope.
|
|
169
|
+
* Used in equivalency chains to track data flow.
|
|
170
|
+
*/
|
|
109
171
|
export interface ScopeVariable {
|
|
110
|
-
|
|
172
|
+
/** Unique ID to prevent processing the same equivalency twice */
|
|
173
|
+
id: number;
|
|
174
|
+
/** Name of the scope containing this variable */
|
|
111
175
|
scopeNodeName: string;
|
|
176
|
+
/** Schema path (e.g., 'signature[0].user.id') */
|
|
112
177
|
schemaPath: string;
|
|
178
|
+
/** Why this equivalency exists (e.g., 'destructuring', 'assignment') */
|
|
113
179
|
equivalencyReason: string;
|
|
114
180
|
}
|
|
115
181
|
|
|
182
|
+
/**
|
|
183
|
+
* VariableInfo is the public representation of a tracked variable.
|
|
184
|
+
* Returned by getter methods like `getVariableInfo()`.
|
|
185
|
+
*/
|
|
116
186
|
export interface VariableInfo {
|
|
117
187
|
name: string;
|
|
118
188
|
equivalentTo?: ScopeVariable[];
|
|
119
189
|
schema: Record<string, string>;
|
|
120
190
|
}
|
|
121
191
|
|
|
192
|
+
/**
|
|
193
|
+
* FunctionCallInfo tracks an external function call and its data flow.
|
|
194
|
+
* Used to understand what data is passed to external APIs.
|
|
195
|
+
*/
|
|
122
196
|
export interface FunctionCallInfo {
|
|
197
|
+
/** Function name (e.g., 'fetch', 'console.log') */
|
|
123
198
|
name: string;
|
|
199
|
+
/** How the function was called (e.g., 'fetch(url, options)') */
|
|
124
200
|
callSignature: string;
|
|
201
|
+
/** Scope where the call was made */
|
|
125
202
|
callScope: string;
|
|
126
|
-
|
|
127
|
-
|
|
203
|
+
/** Schema of arguments passed to the function */
|
|
204
|
+
schema?: Record<string, string>;
|
|
205
|
+
/** Traces argument data back to original source */
|
|
206
|
+
equivalencies?: Record<string, ScopeVariable[]>;
|
|
128
207
|
}
|
|
129
208
|
|
|
209
|
+
/**
|
|
210
|
+
* EquivalencyValueChainItem tracks a single step in an equivalency chain.
|
|
211
|
+
* Used internally during schema building to trace data from usage back to source.
|
|
212
|
+
*
|
|
213
|
+
* Example chain for `const { user } = props; return user.name;`:
|
|
214
|
+
* 1. returnValue → user.name (reason: 'return statement')
|
|
215
|
+
* 2. user.name → props.user.name (reason: 'destructuring')
|
|
216
|
+
* 3. props.user.name → signature[0].user.name (reason: 'parameter')
|
|
217
|
+
*/
|
|
130
218
|
export interface EquivalencyValueChainItem {
|
|
131
219
|
id: number;
|
|
132
220
|
addToSchemaId?: number;
|
|
@@ -145,41 +233,124 @@ export interface EquivalencyValueChainItem {
|
|
|
145
233
|
traceId?: number;
|
|
146
234
|
}
|
|
147
235
|
|
|
236
|
+
/**
|
|
237
|
+
* EquivalencyDatabaseEntry links data sources to their usages.
|
|
238
|
+
*
|
|
239
|
+
* After analysis, use `getSourceEquivalencies()` and `getUsageEquivalencies()`
|
|
240
|
+
* to query this database and understand data flow:
|
|
241
|
+
* - sourceCandidates: Where the data originates (function args, API returns)
|
|
242
|
+
* - usages: Where the data is consumed (return values, JSX props, API calls)
|
|
243
|
+
* - intermediatesOrder: All paths in the chain, ordered by distance from source
|
|
244
|
+
*/
|
|
148
245
|
export interface EquivalencyDatabaseEntry {
|
|
149
246
|
id: number;
|
|
247
|
+
/** Paths where data originates (signature args, API returns) */
|
|
150
248
|
sourceCandidates: Pick<ScopeVariable, 'schemaPath' | 'scopeNodeName'>[];
|
|
249
|
+
/** Paths where data is consumed (return values, JSX props) */
|
|
151
250
|
usages: Pick<ScopeVariable, 'schemaPath' | 'scopeNodeName'>[];
|
|
251
|
+
/** All intermediate paths with their distance from source (0 = source) */
|
|
152
252
|
intermediatesOrder: Record<string, number>;
|
|
153
|
-
|
|
253
|
+
/** Paths with multiple distinct sources (e.g., object with props from different origins) */
|
|
254
|
+
complexSourceEquivalencies: Set<string>;
|
|
154
255
|
}
|
|
155
256
|
|
|
156
|
-
const ROOT_SCOPE_NAME = 'root';
|
|
157
|
-
|
|
158
257
|
// DEBUG: Performance metrics
|
|
159
258
|
export let maxEquivalencyChainDepth = 0;
|
|
160
259
|
export let addToSchemaCallCount = 0;
|
|
161
260
|
export let followEquivalenciesCallCount = 0;
|
|
261
|
+
export let followEquivalenciesEarlyExitCount = 0;
|
|
262
|
+
export let followEquivalenciesEarlyExitPhase1Count = 0;
|
|
263
|
+
export let followEquivalenciesWithWorkCount = 0;
|
|
264
|
+
export let addEquivalencyCallCount = 0;
|
|
162
265
|
export function resetScopeDataStructureMetrics() {
|
|
163
266
|
maxEquivalencyChainDepth = 0;
|
|
164
267
|
addToSchemaCallCount = 0;
|
|
165
268
|
followEquivalenciesCallCount = 0;
|
|
269
|
+
followEquivalenciesEarlyExitCount = 0;
|
|
270
|
+
followEquivalenciesEarlyExitPhase1Count = 0;
|
|
271
|
+
followEquivalenciesWithWorkCount = 0;
|
|
272
|
+
addEquivalencyCallCount = 0;
|
|
166
273
|
}
|
|
167
274
|
|
|
275
|
+
// Performance: Pre-computed Sets for equivalency reason filtering (O(1) vs O(n))
|
|
276
|
+
const ALLOWED_EQUIVALENCY_REASONS = new Set([
|
|
277
|
+
'original equivalency',
|
|
278
|
+
'implicit parent equivalency',
|
|
279
|
+
'explicit parent equivalency',
|
|
280
|
+
'transformed non-object function equivalency - original equivalency',
|
|
281
|
+
'equivalency to external function call',
|
|
282
|
+
'functionCall to function signature equivalency',
|
|
283
|
+
'explicit parent signature',
|
|
284
|
+
'useState setter call equivalency',
|
|
285
|
+
'non-object function argument function signature equivalency1',
|
|
286
|
+
'non-object function argument function signature equivalency2',
|
|
287
|
+
'returnValue to functionCallReturnValue equivalency',
|
|
288
|
+
'.map() function deconstruction',
|
|
289
|
+
'useMemo return value equivalent to first argument return value',
|
|
290
|
+
'useState value equivalency',
|
|
291
|
+
'Node .then() equivalency',
|
|
292
|
+
'Explicit array deconstruction equivalency value',
|
|
293
|
+
'forwardRef equivalency',
|
|
294
|
+
'forwardRef signature to child signature equivalency',
|
|
295
|
+
'captured function call return value equivalency',
|
|
296
|
+
'original equivalency - rerouted via useCallback',
|
|
297
|
+
'non-object function argument function signature equivalency1 - rerouted via useCallback',
|
|
298
|
+
'non-object function argument function signature equivalency2 - rerouted via useCallback',
|
|
299
|
+
'implicit parent equivalency - rerouted via useCallback',
|
|
300
|
+
'Spread operator equivalency key update: implicit parent equivalency',
|
|
301
|
+
'Array.from() equivalency',
|
|
302
|
+
'propagated sub-property equivalency',
|
|
303
|
+
'propagated function call return sub-property equivalency',
|
|
304
|
+
'where was this function called from', // Added: tracks which scope called an external function
|
|
305
|
+
]);
|
|
306
|
+
|
|
307
|
+
const SILENTLY_IGNORED_EQUIVALENCY_REASONS = new Set([
|
|
308
|
+
'signature of functionCall',
|
|
309
|
+
'transformed non-object function equivalency - implicit parent equivalency',
|
|
310
|
+
'transformed non-object function equivalency - non-object function argument function signature equivalency1',
|
|
311
|
+
'transformed non-object function equivalency - non-object function argument function signature equivalency2',
|
|
312
|
+
'transformed non-object function equivalency - equivalency to external function call',
|
|
313
|
+
'Explicit array deconstruction equivalency key',
|
|
314
|
+
'transformed non-object function equivalency - useState setter call equivalency',
|
|
315
|
+
'transformed non-object function equivalency - implicit parent equivalency - rerouted via useCallback',
|
|
316
|
+
'transformed non-object function equivalency - Array.from() equivalency',
|
|
317
|
+
'Spread operator equivalency key update: Explicit array deconstruction equivalency value',
|
|
318
|
+
]);
|
|
319
|
+
|
|
168
320
|
export class ScopeDataStructure {
|
|
169
321
|
debugCount = 0;
|
|
170
322
|
onlyEquivalencies = true;
|
|
171
323
|
equivalencyManagers: EquivalencyManager[] = [];
|
|
172
324
|
scopeNodes: Record<string, ScopeNode> = {};
|
|
173
|
-
|
|
325
|
+
private scopeTreeManager: ScopeTreeManager = new ScopeTreeManager();
|
|
174
326
|
externalFunctionCalls: FunctionCallInfo[] = [];
|
|
327
|
+
|
|
328
|
+
// Getter for backward compatibility - returns the tree structure
|
|
329
|
+
get scopeTree(): ScopeTreeNode {
|
|
330
|
+
return this.scopeTreeManager.getTree();
|
|
331
|
+
}
|
|
175
332
|
environmentVariables: string[] = [];
|
|
176
333
|
equivalencyDatabase: EquivalencyDatabaseEntry[] = [];
|
|
177
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Conditional usages collected during AST analysis.
|
|
337
|
+
* Maps local variable path to array of usages.
|
|
338
|
+
*/
|
|
339
|
+
private rawConditionalUsages: Record<
|
|
340
|
+
string,
|
|
341
|
+
Array<{
|
|
342
|
+
path: string;
|
|
343
|
+
conditionType: 'truthiness' | 'comparison' | 'switch';
|
|
344
|
+
comparedValues?: string[];
|
|
345
|
+
location: 'if' | 'ternary' | 'logical-and' | 'switch';
|
|
346
|
+
}>
|
|
347
|
+
> = {};
|
|
348
|
+
|
|
178
349
|
private lastAddToSchemaId = 0;
|
|
179
350
|
private lastEquivalencyId = 0;
|
|
180
351
|
private lastEquivalencyDatabaseId = 0;
|
|
181
|
-
private
|
|
182
|
-
private
|
|
352
|
+
private pathManager: PathManager = new PathManager();
|
|
353
|
+
private visitedTracker: VisitedTracker = new VisitedTracker();
|
|
183
354
|
private equivalencyDatabaseCache: Map<
|
|
184
355
|
string,
|
|
185
356
|
EquivalencyDatabaseEntry | undefined
|
|
@@ -187,7 +358,31 @@ export class ScopeDataStructure {
|
|
|
187
358
|
// Inverted index: maps uniqueId -> EquivalencyDatabaseEntry for O(1) lookup
|
|
188
359
|
private intermediatesOrderIndex: Map<string, EquivalencyDatabaseEntry> =
|
|
189
360
|
new Map();
|
|
190
|
-
|
|
361
|
+
|
|
362
|
+
// Index for O(1) lookup of external function calls by name
|
|
363
|
+
// Invalidated by setting to null; rebuilt lazily on next access
|
|
364
|
+
private externalFunctionCallsIndex: Map<string, FunctionCallInfo> | null =
|
|
365
|
+
null;
|
|
366
|
+
|
|
367
|
+
// Debug tracer for selective path/scope tracing
|
|
368
|
+
// Enable via: CODEYAM_DEBUG=true CODEYAM_DEBUG_PATHS="user.*,signature" npm test
|
|
369
|
+
private tracer: DebugTracer = new DebugTracer({
|
|
370
|
+
enabled: process.env.CODEYAM_DEBUG === 'true',
|
|
371
|
+
pathPatterns: process.env.CODEYAM_DEBUG_PATHS
|
|
372
|
+
? process.env.CODEYAM_DEBUG_PATHS.split(',').map((p) => new RegExp(p))
|
|
373
|
+
: [],
|
|
374
|
+
scopePatterns: process.env.CODEYAM_DEBUG_SCOPES
|
|
375
|
+
? process.env.CODEYAM_DEBUG_SCOPES.split(',').map((p) => new RegExp(p))
|
|
376
|
+
: [],
|
|
377
|
+
maxDepth: 20,
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// Batch processor for converting recursive addToSchema calls to iterative processing
|
|
381
|
+
// This eliminates deep recursion and allows for better work deduplication
|
|
382
|
+
private batchProcessor: BatchSchemaProcessor | null = null;
|
|
383
|
+
// Track items already in the batch queue to prevent duplicates
|
|
384
|
+
// For external function calls, uses path-only keys (no value) for more aggressive deduplication
|
|
385
|
+
private batchQueuedSet: Set<string> | null = null;
|
|
191
386
|
|
|
192
387
|
constructor(
|
|
193
388
|
managers: EquivalencyManager[],
|
|
@@ -196,7 +391,7 @@ export class ScopeDataStructure {
|
|
|
196
391
|
scopeAnalysis: ScopeAnalysis,
|
|
197
392
|
) {
|
|
198
393
|
this.equivalencyManagers.push(...managers);
|
|
199
|
-
this.
|
|
394
|
+
this.scopeTreeManager.setRootName(scopeName);
|
|
200
395
|
this.addScopeAnalysis(scopeName, scopeText, scopeAnalysis);
|
|
201
396
|
}
|
|
202
397
|
|
|
@@ -208,8 +403,11 @@ export class ScopeDataStructure {
|
|
|
208
403
|
isStatic?: boolean,
|
|
209
404
|
) {
|
|
210
405
|
try {
|
|
211
|
-
if (
|
|
212
|
-
|
|
406
|
+
if (
|
|
407
|
+
scopeNodeName !== this.scopeTreeManager.getRootName() &&
|
|
408
|
+
!parentScopeName
|
|
409
|
+
) {
|
|
410
|
+
parentScopeName = this.scopeTreeManager.getRootName();
|
|
213
411
|
}
|
|
214
412
|
|
|
215
413
|
let tree: any[] = [];
|
|
@@ -262,7 +460,7 @@ export class ScopeDataStructure {
|
|
|
262
460
|
'CodeYam Error: Failed to add scope analysis for scope:',
|
|
263
461
|
JSON.stringify(
|
|
264
462
|
{
|
|
265
|
-
rootScopeName: this.
|
|
463
|
+
rootScopeName: this.scopeTreeManager.getRootName(),
|
|
266
464
|
scopeNodeName,
|
|
267
465
|
scopeNodeText: scopeText,
|
|
268
466
|
isolatedStructure: scopeAnalysis.isolatedStructure,
|
|
@@ -277,30 +475,51 @@ export class ScopeDataStructure {
|
|
|
277
475
|
}
|
|
278
476
|
}
|
|
279
477
|
|
|
478
|
+
/**
|
|
479
|
+
* Phase 2: Build complete schemas by following all equivalencies.
|
|
480
|
+
*
|
|
481
|
+
* This is the main entry point for schema building. Call this after all
|
|
482
|
+
* scopes have been added via `addScopeAnalysis()`.
|
|
483
|
+
*
|
|
484
|
+
* ## What Happens
|
|
485
|
+
*
|
|
486
|
+
* 1. Resets state (visited tracker, schemas, database)
|
|
487
|
+
* 2. For each scope, follows equivalencies to build complete schemas
|
|
488
|
+
* 3. Filters out internal function calls (keeps only external-facing)
|
|
489
|
+
* 4. Propagates source/usage relationships for querying
|
|
490
|
+
*
|
|
491
|
+
* ## After Calling
|
|
492
|
+
*
|
|
493
|
+
* You can then use:
|
|
494
|
+
* - `getSchema()` - Get the built schema for a scope
|
|
495
|
+
* - `getSourceEquivalencies()` - Find where data comes from
|
|
496
|
+
* - `getUsageEquivalencies()` - Find where data is used
|
|
497
|
+
*/
|
|
280
498
|
async captureCompleteSchema() {
|
|
499
|
+
// Reset state for Phase 2
|
|
281
500
|
this.onlyEquivalencies = false;
|
|
282
501
|
this.equivalencyDatabase = [];
|
|
283
|
-
this.equivalencyDatabaseCache.clear();
|
|
284
|
-
this.intermediatesOrderIndex.clear();
|
|
285
|
-
this.
|
|
502
|
+
this.equivalencyDatabaseCache.clear();
|
|
503
|
+
this.intermediatesOrderIndex.clear();
|
|
504
|
+
this.visitedTracker.resetGlobalVisited();
|
|
286
505
|
|
|
287
506
|
for (const externalFunctionCall of this.externalFunctionCalls) {
|
|
288
507
|
externalFunctionCall.schema = {};
|
|
289
508
|
}
|
|
290
509
|
|
|
291
|
-
// for
|
|
510
|
+
// Build schemas for all scopes in parallel
|
|
292
511
|
await Promise.all(
|
|
293
512
|
Object.values(this.scopeNodes).map(async (scopeNode) => {
|
|
294
513
|
scopeNode.schema = {};
|
|
295
514
|
await this.determineEquivalenciesAndBuildSchema(scopeNode);
|
|
296
515
|
}),
|
|
297
516
|
);
|
|
298
|
-
|
|
517
|
+
|
|
299
518
|
const validExternalFacingScopeNames = new Set<string>([
|
|
300
|
-
this.
|
|
519
|
+
this.scopeTreeManager.getRootName(),
|
|
301
520
|
]);
|
|
302
521
|
this.externalFunctionCalls = this.externalFunctionCalls.filter((efc) => {
|
|
303
|
-
const efcName = efc.name
|
|
522
|
+
const efcName = this.pathManager.stripGenerics(efc.name);
|
|
304
523
|
for (const manager of this.equivalencyManagers) {
|
|
305
524
|
if (manager.internalFunctions.has(efcName)) {
|
|
306
525
|
return false;
|
|
@@ -309,65 +528,71 @@ export class ScopeDataStructure {
|
|
|
309
528
|
validExternalFacingScopeNames.add(efcName);
|
|
310
529
|
return true;
|
|
311
530
|
});
|
|
531
|
+
this.invalidateExternalFunctionCallsIndex();
|
|
532
|
+
|
|
533
|
+
// Pre-compute array methods list once (avoids Array.from() in hot loop)
|
|
534
|
+
const arrayMethodsList = Array.from(arrayMethodsSet);
|
|
535
|
+
const containsArrayMethod = (path: string) =>
|
|
536
|
+
arrayMethodsList.some((method) => path.includes(`.${method}(`));
|
|
312
537
|
|
|
313
538
|
for (const entry of this.equivalencyDatabase) {
|
|
314
|
-
entry.usages = entry.usages.filter(
|
|
315
|
-
(usage)
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
) &&
|
|
539
|
+
entry.usages = entry.usages.filter((usage) => {
|
|
540
|
+
const baseName = this.pathManager.stripGenerics(usage.scopeNodeName);
|
|
541
|
+
return (
|
|
542
|
+
validExternalFacingScopeNames.has(baseName) &&
|
|
319
543
|
(usage.schemaPath.startsWith('returnValue') ||
|
|
320
|
-
usage.schemaPath.startsWith(
|
|
321
|
-
!
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
)
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
) &&
|
|
544
|
+
usage.schemaPath.startsWith(baseName)) &&
|
|
545
|
+
!containsArrayMethod(usage.schemaPath)
|
|
546
|
+
);
|
|
547
|
+
});
|
|
548
|
+
entry.sourceCandidates = entry.sourceCandidates.filter((candidate) => {
|
|
549
|
+
const baseName = this.pathManager.stripGenerics(
|
|
550
|
+
candidate.scopeNodeName,
|
|
551
|
+
);
|
|
552
|
+
return (
|
|
553
|
+
validExternalFacingScopeNames.has(baseName) &&
|
|
330
554
|
(candidate.schemaPath.startsWith('signature[') ||
|
|
331
|
-
candidate.schemaPath.startsWith(
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
candidate.schemaPath.includes(`.${method}(`),
|
|
336
|
-
),
|
|
337
|
-
);
|
|
555
|
+
candidate.schemaPath.startsWith(baseName)) &&
|
|
556
|
+
!containsArrayMethod(candidate.schemaPath)
|
|
557
|
+
);
|
|
558
|
+
});
|
|
338
559
|
}
|
|
339
560
|
|
|
340
561
|
this.propagateSourceAndUsageEquivalencies(
|
|
341
|
-
this.scopeNodes[this.
|
|
562
|
+
this.scopeNodes[this.scopeTreeManager.getRootName()],
|
|
342
563
|
);
|
|
343
564
|
|
|
344
565
|
for (const externalFunctionCall of this.externalFunctionCalls) {
|
|
345
|
-
const t = new Date().getTime();
|
|
346
|
-
// console.info("Start Propagating", JSON.stringify({
|
|
347
|
-
// externalFunctionCallName: externalFunctionCall.name,
|
|
348
|
-
// // sourceEquivalencies: externalFunctionCall.sourceEquivalencies,
|
|
349
|
-
// }, null, 2));
|
|
350
|
-
|
|
351
566
|
this.propagateSourceAndUsageEquivalencies(externalFunctionCall);
|
|
352
|
-
|
|
353
|
-
const elapsedTime = new Date().getTime() - t;
|
|
354
|
-
if (elapsedTime > 100) {
|
|
355
|
-
console.warn('Long Propagation Time', {
|
|
356
|
-
externalFunctionCallName: externalFunctionCall.name,
|
|
357
|
-
time: elapsedTime,
|
|
358
|
-
});
|
|
359
|
-
// throw new Error("STOP - LONG PROPAGATION TIME")
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// console.info("Done Propagating", JSON.stringify({
|
|
363
|
-
// externalFunctionCallName: externalFunctionCall.name,
|
|
364
|
-
// // sourceEquivalencies: externalFunctionCall.sourceEquivalencies,
|
|
365
|
-
// // usageEquivalencies: externalFunctionCall.usageEquivalencies,
|
|
366
|
-
// time: elapsedTime
|
|
367
|
-
// }, null, 2))
|
|
368
567
|
}
|
|
369
568
|
}
|
|
370
569
|
|
|
570
|
+
/**
|
|
571
|
+
* Core method: Adds a path/value to a scope's schema and follows equivalencies.
|
|
572
|
+
*
|
|
573
|
+
* This is the heart of schema building. For each path, it:
|
|
574
|
+
* 1. Validates the path and checks if already visited
|
|
575
|
+
* 2. Adds the path → value mapping to the scope's schema
|
|
576
|
+
* 3. Follows any equivalencies to propagate the schema to linked paths
|
|
577
|
+
* 4. Tracks the equivalency chain for source/usage database
|
|
578
|
+
*
|
|
579
|
+
* ## Cycle Detection
|
|
580
|
+
*
|
|
581
|
+
* Uses `visitedTracker.checkAndMarkGlobalVisited()` to prevent infinite loops.
|
|
582
|
+
* A path is considered visited if the exact (scope, path, value) tuple has
|
|
583
|
+
* been processed before.
|
|
584
|
+
*
|
|
585
|
+
* ## Equivalency Following
|
|
586
|
+
*
|
|
587
|
+
* When a path has equivalencies (e.g., `user` → `props.user`), this method
|
|
588
|
+
* recursively calls itself to add the equivalent path to its scope's schema.
|
|
589
|
+
*
|
|
590
|
+
* @param path - Schema path to add (e.g., 'user.id')
|
|
591
|
+
* @param value - Type value (e.g., 'string', 'number')
|
|
592
|
+
* @param scopeNode - The scope to add to
|
|
593
|
+
* @param equivalencyValueChain - Tracks the chain for database population
|
|
594
|
+
* @param traceId - Debug ID for tracing specific paths
|
|
595
|
+
*/
|
|
371
596
|
addToSchema({
|
|
372
597
|
path,
|
|
373
598
|
value,
|
|
@@ -382,11 +607,34 @@ export class ScopeDataStructure {
|
|
|
382
607
|
traceId?: number;
|
|
383
608
|
}) {
|
|
384
609
|
addToSchemaCallCount++;
|
|
610
|
+
|
|
611
|
+
// Trace entry for debugging
|
|
612
|
+
this.tracer.traceEnter('addToSchema', {
|
|
613
|
+
path,
|
|
614
|
+
scope: scopeNode?.name,
|
|
615
|
+
value,
|
|
616
|
+
chainDepth: equivalencyValueChain.length,
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
// Safety: Detect runaway recursion
|
|
620
|
+
if (addToSchemaCallCount > 500000) {
|
|
621
|
+
console.error('INFINITE LOOP DETECTED in addToSchema', {
|
|
622
|
+
callCount: addToSchemaCallCount,
|
|
623
|
+
path,
|
|
624
|
+
value,
|
|
625
|
+
scopeNodeName: scopeNode?.name,
|
|
626
|
+
});
|
|
627
|
+
throw new Error(
|
|
628
|
+
`Infinite loop detected: addToSchema called ${addToSchemaCallCount} times`,
|
|
629
|
+
);
|
|
630
|
+
}
|
|
385
631
|
// Track max chain depth
|
|
386
632
|
if (equivalencyValueChain.length > maxEquivalencyChainDepth) {
|
|
387
633
|
maxEquivalencyChainDepth = equivalencyValueChain.length;
|
|
388
634
|
}
|
|
389
|
-
if (!scopeNode)
|
|
635
|
+
if (!scopeNode) {
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
390
638
|
|
|
391
639
|
if (!this.isValidPath(path)) {
|
|
392
640
|
if (traceId) {
|
|
@@ -395,6 +643,7 @@ export class ScopeDataStructure {
|
|
|
395
643
|
return;
|
|
396
644
|
}
|
|
397
645
|
|
|
646
|
+
// Update chain metadata for database tracking
|
|
398
647
|
if (equivalencyValueChain.length > 0) {
|
|
399
648
|
equivalencyValueChain[equivalencyValueChain.length - 1].addToSchemaId =
|
|
400
649
|
++this.lastAddToSchemaId;
|
|
@@ -405,78 +654,18 @@ export class ScopeDataStructure {
|
|
|
405
654
|
};
|
|
406
655
|
}
|
|
407
656
|
|
|
408
|
-
//
|
|
409
|
-
|
|
410
|
-
// traceId += 1;
|
|
411
|
-
// if (traceId) {
|
|
412
|
-
// console.info(JSON.stringify({ traceId, path, value, scopeNodeName: scopeNode.name }, null, 2));
|
|
413
|
-
// }
|
|
414
|
-
// if (traceId > 30) {
|
|
415
|
-
// console.warn(
|
|
416
|
-
// 'HIGH TRACEID',
|
|
417
|
-
// JSON.stringify(
|
|
418
|
-
// {
|
|
419
|
-
// path,
|
|
420
|
-
// value,
|
|
421
|
-
// scopeNodeName: scopeNode.name,
|
|
422
|
-
// equivalencyValueChain,
|
|
423
|
-
// },
|
|
424
|
-
// null,
|
|
425
|
-
// 2,
|
|
426
|
-
// ),
|
|
427
|
-
// );
|
|
428
|
-
// throw new Error('STOP - HIGH TRACEID');
|
|
429
|
-
// }
|
|
430
|
-
|
|
431
|
-
// Use this debugging to trace a specific path through the propagation
|
|
432
|
-
const debugLevel = 0; // 0 for minimal, 1 for executed paths, 2 for all paths info
|
|
433
|
-
// if ((!this.onlyEquivalencies && path === 'entity.sha') || traceId) {
|
|
434
|
-
// traceId ||= 0;
|
|
435
|
-
// traceId += 1;
|
|
436
|
-
// if (traceId === 1) {
|
|
437
|
-
// this.debugCount += 1;
|
|
438
|
-
// if (this.debugCount > 5) throw new Error('STOP - HIGH DEBUG COUNT');
|
|
439
|
-
// console.warn('START');
|
|
440
|
-
// }
|
|
441
|
-
|
|
442
|
-
// console.info(
|
|
443
|
-
// 'Debug Propagation: Adding to schema',
|
|
444
|
-
// JSON.stringify(
|
|
445
|
-
// {
|
|
446
|
-
// // traceId,
|
|
447
|
-
// path,
|
|
448
|
-
// value,
|
|
449
|
-
// scopeNodeName: scopeNode.name,
|
|
450
|
-
// reason:
|
|
451
|
-
// equivalencyValueChain[equivalencyValueChain.length - 1]?.reason,
|
|
452
|
-
// // previous:
|
|
453
|
-
// // equivalencyValueChain[equivalencyValueChain.length - 1],
|
|
454
|
-
// text: scopeNode.text,
|
|
455
|
-
// // tree: scopeNode.tree,
|
|
456
|
-
// // onlyEquivalencies: this.onlyEquivalencies,
|
|
457
|
-
// // equivalencyValueChain,
|
|
458
|
-
// // scopeNode
|
|
459
|
-
// // instatiatedVeriables: scopeNode.instantiatedVariables,
|
|
460
|
-
// // parentInstantiatedVariables: scopeNode.parentInstantiatedVariables,
|
|
461
|
-
// // isolatedStructure: scopeNode.analysis.isolatedStructure,
|
|
462
|
-
// // isolatedEquivalencies: scopeNode.analysis.isolatedEquivalentVariables,
|
|
463
|
-
// // equivalencies: traceId === 1 ? scopeNode.equivalencies : 'skipped',
|
|
464
|
-
// // functionCalls: scopeNode.functionCalls,
|
|
465
|
-
// // externalFunctionCalls: this.externalFunctionCalls.filter(fc => fc.callScope.includes(scopeNode.name))
|
|
466
|
-
// },
|
|
467
|
-
// null,
|
|
468
|
-
// 2,
|
|
469
|
-
// ),
|
|
470
|
-
// );
|
|
471
|
-
// // if (traceId > 1) traceId = undefined;
|
|
472
|
-
// throw new Error('STOP ON ADD');
|
|
473
|
-
// }
|
|
657
|
+
// Debug level: 0=minimal, 1=executed paths, 2=all paths (set via traceId)
|
|
658
|
+
const debugLevel = 0;
|
|
474
659
|
|
|
660
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
661
|
+
// SECTION 1: Handle complex source paths (paths with multiple data origins)
|
|
662
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
475
663
|
const equivalenciesDatabaseEntry = this.getEquivalenciesDatabaseEntry(
|
|
476
664
|
scopeNode.name,
|
|
477
665
|
path,
|
|
478
666
|
);
|
|
479
667
|
|
|
668
|
+
// Process complex source paths (before visited check, per original design)
|
|
480
669
|
this.addComplexSourcePathVariables(
|
|
481
670
|
equivalenciesDatabaseEntry,
|
|
482
671
|
scopeNode,
|
|
@@ -485,23 +674,19 @@ export class ScopeDataStructure {
|
|
|
485
674
|
traceId,
|
|
486
675
|
);
|
|
487
676
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
677
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
678
|
+
// SECTION 2: Cycle detection - skip if already visited with this value
|
|
679
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
680
|
+
if (
|
|
681
|
+
this.visitedTracker.checkAndMarkGlobalVisited(scopeNode.name, path, value)
|
|
682
|
+
) {
|
|
491
683
|
if (traceId) {
|
|
492
|
-
console.info(
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
{
|
|
496
|
-
uniqueIdentifier,
|
|
497
|
-
// equivalencyValueChain,
|
|
498
|
-
},
|
|
499
|
-
null,
|
|
500
|
-
2,
|
|
501
|
-
),
|
|
502
|
-
);
|
|
684
|
+
console.info('Debug: already visited', {
|
|
685
|
+
key: `${scopeNode.name}::${path}::${value}`,
|
|
686
|
+
});
|
|
503
687
|
}
|
|
504
688
|
|
|
689
|
+
// Still record in database even if visited (captures full chain)
|
|
505
690
|
if (!this.onlyEquivalencies) {
|
|
506
691
|
this.addToEquivalencyDatabase(equivalencyValueChain, traceId);
|
|
507
692
|
}
|
|
@@ -509,8 +694,7 @@ export class ScopeDataStructure {
|
|
|
509
694
|
return;
|
|
510
695
|
}
|
|
511
696
|
|
|
512
|
-
|
|
513
|
-
|
|
697
|
+
// Continue complex source processing after visited check
|
|
514
698
|
this.captureComplexSourcePaths(
|
|
515
699
|
equivalenciesDatabaseEntry,
|
|
516
700
|
scopeNode,
|
|
@@ -519,6 +703,10 @@ export class ScopeDataStructure {
|
|
|
519
703
|
traceId,
|
|
520
704
|
);
|
|
521
705
|
|
|
706
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
707
|
+
// SECTION 3: Process each subpath to follow equivalencies
|
|
708
|
+
// For path "user.profile.name", processes: "user", "user.profile", "user.profile.name"
|
|
709
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
522
710
|
const pathParts = this.splitPath(path);
|
|
523
711
|
|
|
524
712
|
this.checkForArrayItemPath(pathParts, scopeNode, equivalencyValueChain);
|
|
@@ -637,6 +825,11 @@ export class ScopeDataStructure {
|
|
|
637
825
|
}
|
|
638
826
|
}
|
|
639
827
|
|
|
828
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
829
|
+
// SECTION 4: Write to schema if appropriate
|
|
830
|
+
// Only writes if: (a) path not set or is 'unknown', AND
|
|
831
|
+
// (b) path belongs to this scope (instantiated, signature, or return)
|
|
832
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
640
833
|
const writeToCurrentScopeSchema =
|
|
641
834
|
(!scopeNode.schema[path] || scopeNode.schema[path] === 'unknown') &&
|
|
642
835
|
(!scopeNode.instantiatedVariables ||
|
|
@@ -644,50 +837,35 @@ export class ScopeDataStructure {
|
|
|
644
837
|
pathParts[0].startsWith('signature[') ||
|
|
645
838
|
pathParts[0].startsWith('returnValue'));
|
|
646
839
|
|
|
647
|
-
//
|
|
840
|
+
// Early exit if schema already has this exact value
|
|
648
841
|
if (scopeNode.schema[path] === value && value !== 'unknown') {
|
|
649
842
|
return;
|
|
650
843
|
}
|
|
651
844
|
|
|
652
845
|
if (writeToCurrentScopeSchema) {
|
|
653
846
|
if (traceId && debugLevel > 0) {
|
|
654
|
-
console.info('Debug
|
|
655
|
-
traceId,
|
|
847
|
+
console.info('Debug: writing schema', {
|
|
656
848
|
path,
|
|
657
|
-
|
|
658
|
-
|
|
849
|
+
value,
|
|
850
|
+
scope: scopeNode.name,
|
|
659
851
|
});
|
|
660
852
|
}
|
|
661
853
|
scopeNode.schema[path] = value;
|
|
662
854
|
}
|
|
663
855
|
|
|
856
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
857
|
+
// SECTION 5: Record in equivalency database (Phase 2 only)
|
|
858
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
664
859
|
if (!this.onlyEquivalencies) {
|
|
665
860
|
this.addToEquivalencyDatabase(equivalencyValueChain, traceId);
|
|
666
861
|
}
|
|
667
862
|
|
|
668
863
|
if (traceId) {
|
|
669
|
-
console.info(
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
scopeNodeName: scopeNode.name,
|
|
675
|
-
path,
|
|
676
|
-
value,
|
|
677
|
-
// schemaValue: scopeNode.schema[path],
|
|
678
|
-
// propagated,
|
|
679
|
-
// scopeInfoText: scopeNode.text,
|
|
680
|
-
// equivalencyValueChain//: equivalencyValueChain.map(
|
|
681
|
-
// (ev) => ev.currentPath.schemaPath,
|
|
682
|
-
// ),
|
|
683
|
-
// sourceEquivalencies: scopeNode.sourceEquivalencies[path] ?? [],
|
|
684
|
-
// usageEquivalencies: scopeNode.usageEquivalencies[path] ?? [],
|
|
685
|
-
// checkpoints,
|
|
686
|
-
},
|
|
687
|
-
null,
|
|
688
|
-
2,
|
|
689
|
-
),
|
|
690
|
-
);
|
|
864
|
+
console.info('Debug: addToSchema complete', {
|
|
865
|
+
scope: scopeNode.name,
|
|
866
|
+
path,
|
|
867
|
+
value,
|
|
868
|
+
});
|
|
691
869
|
}
|
|
692
870
|
}
|
|
693
871
|
|
|
@@ -708,53 +886,30 @@ export class ScopeDataStructure {
|
|
|
708
886
|
equivalencyValueChain?: EquivalencyValueChainItem[],
|
|
709
887
|
traceId?: number,
|
|
710
888
|
) {
|
|
711
|
-
//
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
'Explicit array deconstruction equivalency value', // 1 failure
|
|
730
|
-
'forwardRef equivalency',
|
|
731
|
-
'forwardRef signature to child signature equivalency', // 1 failure
|
|
732
|
-
'captured function call return value equivalency', // 1 failure (in ScopeDataStructure.test.ts)
|
|
733
|
-
'original equivalency - rerouted via useCallback', // 1 failure
|
|
734
|
-
'non-object function argument function signature equivalency1 - rerouted via useCallback', // 1 failure
|
|
735
|
-
'non-object function argument function signature equivalency2 - rerouted via useCallback', // 1 failure
|
|
736
|
-
'implicit parent equivalency - rerouted via useCallback', // 1 failure
|
|
737
|
-
'Spread operator equivalency key update: implicit parent equivalency', // 1 failure
|
|
738
|
-
'Array.from() equivalency',
|
|
739
|
-
].includes(equivalencyReason)
|
|
740
|
-
) {
|
|
741
|
-
if (
|
|
742
|
-
[
|
|
743
|
-
'signature of functionCall',
|
|
744
|
-
'transformed non-object function equivalency - implicit parent equivalency',
|
|
745
|
-
'transformed non-object function equivalency - non-object function argument function signature equivalency1',
|
|
746
|
-
'transformed non-object function equivalency - non-object function argument function signature equivalency2',
|
|
747
|
-
'transformed non-object function equivalency - equivalency to external function call',
|
|
748
|
-
'Explicit array deconstruction equivalency key',
|
|
749
|
-
'transformed non-object function equivalency - useState setter call equivalency',
|
|
750
|
-
'transformed non-object function equivalency - implicit parent equivalency - rerouted via useCallback',
|
|
751
|
-
'transformed non-object function equivalency - Array.from() equivalency',
|
|
752
|
-
'Spread operator equivalency key update: Explicit array deconstruction equivalency value',
|
|
753
|
-
].includes(equivalencyReason)
|
|
754
|
-
) {
|
|
889
|
+
// DEBUG: Detect infinite loops
|
|
890
|
+
addEquivalencyCallCount++;
|
|
891
|
+
if (addEquivalencyCallCount > 50000) {
|
|
892
|
+
console.error('INFINITE LOOP DETECTED in addEquivalency', {
|
|
893
|
+
callCount: addEquivalencyCallCount,
|
|
894
|
+
path,
|
|
895
|
+
equivalentPath,
|
|
896
|
+
equivalentScopeName,
|
|
897
|
+
scopeNodeName: scopeNode.name,
|
|
898
|
+
equivalencyReason,
|
|
899
|
+
});
|
|
900
|
+
throw new Error(
|
|
901
|
+
`Infinite loop detected: addEquivalency called ${addEquivalencyCallCount} times`,
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
// Filter equivalency reasons - use pre-computed Sets for O(1) lookup
|
|
905
|
+
if (!ALLOWED_EQUIVALENCY_REASONS.has(equivalencyReason)) {
|
|
906
|
+
if (SILENTLY_IGNORED_EQUIVALENCY_REASONS.has(equivalencyReason)) {
|
|
755
907
|
return;
|
|
756
908
|
} else {
|
|
909
|
+
// Log and skip - if an equivalency reason isn't in ALLOWED or SILENTLY_IGNORED,
|
|
910
|
+
// it shouldn't be stored (was previously missing the return)
|
|
757
911
|
console.info('Not tracked equivalency reason', { equivalencyReason });
|
|
912
|
+
return;
|
|
758
913
|
}
|
|
759
914
|
}
|
|
760
915
|
|
|
@@ -771,13 +926,47 @@ export class ScopeDataStructure {
|
|
|
771
926
|
scopeNode.equivalencies[path] ||= [];
|
|
772
927
|
|
|
773
928
|
const existing = scopeNode.equivalencies[path];
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
929
|
+
const existingEquivalency = existing.find(
|
|
930
|
+
(v) =>
|
|
931
|
+
v.schemaPath === equivalentPath &&
|
|
932
|
+
v.scopeNodeName === equivalentScopeName,
|
|
933
|
+
);
|
|
934
|
+
if (existingEquivalency) {
|
|
935
|
+
// During Phase 2 (onlyEquivalencies=false), we need to still process the equivalency
|
|
936
|
+
// to build the chain and add to the database, even if the equivalency already exists
|
|
937
|
+
if (!this.onlyEquivalencies) {
|
|
938
|
+
const equivalentScopeNode = this.getScopeOrFunctionCallInfo(
|
|
939
|
+
equivalentScopeName,
|
|
940
|
+
) as ScopeNode;
|
|
941
|
+
if (equivalentScopeNode) {
|
|
942
|
+
// Extract function call name from path if it looks like a function call path
|
|
943
|
+
// e.g., "ChildComponent().signature[0].dataItem" -> "ChildComponent"
|
|
944
|
+
const pathMatch = path.match(/^([^().]+)\(\)/);
|
|
945
|
+
const previousPathScopeNodeName = pathMatch
|
|
946
|
+
? pathMatch[1]
|
|
947
|
+
: scopeNode.name;
|
|
948
|
+
|
|
949
|
+
this.addToSchema({
|
|
950
|
+
path: equivalentPath,
|
|
951
|
+
value: 'unknown',
|
|
952
|
+
scopeNode: equivalentScopeNode,
|
|
953
|
+
equivalencyValueChain: [
|
|
954
|
+
...(equivalencyValueChain ?? []),
|
|
955
|
+
{
|
|
956
|
+
id: existingEquivalency.id,
|
|
957
|
+
source: 'duplicate equivalency - Phase 2',
|
|
958
|
+
reason: equivalencyReason,
|
|
959
|
+
previousPath: {
|
|
960
|
+
scopeNodeName: previousPathScopeNodeName,
|
|
961
|
+
schemaPath: path,
|
|
962
|
+
value: scopeNode.schema[path],
|
|
963
|
+
},
|
|
964
|
+
},
|
|
965
|
+
],
|
|
966
|
+
traceId,
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
}
|
|
781
970
|
return;
|
|
782
971
|
}
|
|
783
972
|
|
|
@@ -884,9 +1073,9 @@ export class ScopeDataStructure {
|
|
|
884
1073
|
functionCallInfo.equivalencies = {};
|
|
885
1074
|
}
|
|
886
1075
|
|
|
887
|
-
const
|
|
888
|
-
|
|
889
|
-
|
|
1076
|
+
const searchKey = getFunctionCallRoot(functionCallInfo.callSignature);
|
|
1077
|
+
const existingFunctionCall =
|
|
1078
|
+
this.getExternalFunctionCallsIndex().get(searchKey);
|
|
890
1079
|
if (existingFunctionCall) {
|
|
891
1080
|
existingFunctionCall.schema = {
|
|
892
1081
|
...existingFunctionCall.schema,
|
|
@@ -909,6 +1098,7 @@ export class ScopeDataStructure {
|
|
|
909
1098
|
|
|
910
1099
|
if (isExternal) {
|
|
911
1100
|
this.externalFunctionCalls.push(functionCallInfo);
|
|
1101
|
+
this.invalidateExternalFunctionCallsIndex();
|
|
912
1102
|
}
|
|
913
1103
|
}
|
|
914
1104
|
}
|
|
@@ -935,7 +1125,7 @@ export class ScopeDataStructure {
|
|
|
935
1125
|
// }
|
|
936
1126
|
|
|
937
1127
|
const getRootOrExternalFunctionCallInfo = (name: string) => {
|
|
938
|
-
return name === this.
|
|
1128
|
+
return name === this.scopeTreeManager.getRootName()
|
|
939
1129
|
? this.getScopeNode()
|
|
940
1130
|
: this.getExternalFunctionCallInfo(name);
|
|
941
1131
|
};
|
|
@@ -966,6 +1156,40 @@ export class ScopeDataStructure {
|
|
|
966
1156
|
},
|
|
967
1157
|
);
|
|
968
1158
|
|
|
1159
|
+
// PERF: Build a Map for O(1) lookup instead of O(n) inner loop
|
|
1160
|
+
// Map from "remaining parts joined" to equivalentSchemaPath
|
|
1161
|
+
const equivalentSchemaPathMap = new Map<string, string>();
|
|
1162
|
+
for (const equivalentSchemaPath of Object.keys(
|
|
1163
|
+
equivalentScopeNode.schema,
|
|
1164
|
+
)) {
|
|
1165
|
+
// Quick string check before expensive path splitting
|
|
1166
|
+
if (
|
|
1167
|
+
!(
|
|
1168
|
+
equivalentSchemaPath.startsWith(equivalentPath) ||
|
|
1169
|
+
equivalentSchemaPath === equivalentPath ||
|
|
1170
|
+
equivalentPath.startsWith(equivalentSchemaPath)
|
|
1171
|
+
)
|
|
1172
|
+
) {
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
const equivalentSchemaPathParts = this.splitPath(equivalentSchemaPath);
|
|
1177
|
+
|
|
1178
|
+
if (
|
|
1179
|
+
!equivalentPathParts.every(
|
|
1180
|
+
(p, i) => equivalentSchemaPathParts[i] === p,
|
|
1181
|
+
)
|
|
1182
|
+
) {
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
const remainingEquivalentSchemaPathParts =
|
|
1187
|
+
equivalentSchemaPathParts.slice(equivalentPathParts.length);
|
|
1188
|
+
// Use | as separator since it won't appear in path parts
|
|
1189
|
+
const key = remainingEquivalentSchemaPathParts.join('|');
|
|
1190
|
+
equivalentSchemaPathMap.set(key, equivalentSchemaPath);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
969
1193
|
for (const schemaPath of relevantSchemaPaths) {
|
|
970
1194
|
const schemaPathParts = this.splitPath(schemaPath);
|
|
971
1195
|
|
|
@@ -977,106 +1201,29 @@ export class ScopeDataStructure {
|
|
|
977
1201
|
pathParts.length,
|
|
978
1202
|
);
|
|
979
1203
|
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
const relevantEquivalentSchemaPaths = Object.keys(
|
|
984
|
-
equivalentScopeNode.schema,
|
|
985
|
-
).filter((equivalentSchemaPath) => {
|
|
986
|
-
// Quick string check before expensive path splitting
|
|
987
|
-
return (
|
|
988
|
-
equivalentSchemaPath.startsWith(equivalentPath) ||
|
|
989
|
-
equivalentSchemaPath === equivalentPath ||
|
|
990
|
-
equivalentPath.startsWith(equivalentSchemaPath)
|
|
991
|
-
);
|
|
992
|
-
});
|
|
993
|
-
|
|
994
|
-
for (const equivalentSchemaPath of relevantEquivalentSchemaPaths) {
|
|
995
|
-
const equivalentSchemaPathParts =
|
|
996
|
-
this.splitPath(equivalentSchemaPath);
|
|
997
|
-
|
|
998
|
-
if (
|
|
999
|
-
!equivalentPathParts.every(
|
|
1000
|
-
(p, i) => equivalentSchemaPathParts[i] === p,
|
|
1001
|
-
)
|
|
1002
|
-
) {
|
|
1003
|
-
continue;
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
const remainingEquivalentSchemaPathParts =
|
|
1007
|
-
equivalentSchemaPathParts.slice(equivalentPathParts.length);
|
|
1008
|
-
|
|
1009
|
-
if (
|
|
1010
|
-
remainingSchemaPathParts.length !==
|
|
1011
|
-
remainingEquivalentSchemaPathParts.length ||
|
|
1012
|
-
!remainingEquivalentSchemaPathParts.every(
|
|
1013
|
-
(p, i) => p === remainingSchemaPathParts[i],
|
|
1014
|
-
)
|
|
1015
|
-
) {
|
|
1016
|
-
missingEquivalentPath = true;
|
|
1017
|
-
continue;
|
|
1018
|
-
}
|
|
1019
|
-
missingEquivalentPath = false;
|
|
1204
|
+
// PERF: O(1) Map lookup instead of O(n) inner loop
|
|
1205
|
+
const remainingKey = remainingSchemaPathParts.join('|');
|
|
1206
|
+
const equivalentSchemaPath = equivalentSchemaPathMap.get(remainingKey);
|
|
1020
1207
|
|
|
1208
|
+
if (equivalentSchemaPath) {
|
|
1021
1209
|
const value1 = scopeNode.schema[schemaPath];
|
|
1022
1210
|
const value2 = equivalentScopeNode.schema[equivalentSchemaPath];
|
|
1023
1211
|
|
|
1024
|
-
|
|
1025
|
-
if (
|
|
1026
|
-
value2 &&
|
|
1027
|
-
((bestValue === 'unknown' && value2 !== 'unknown') ||
|
|
1028
|
-
(bestValue.includes('unknown') && !value2.includes('unknown')))
|
|
1029
|
-
) {
|
|
1030
|
-
bestValue = value2;
|
|
1031
|
-
}
|
|
1212
|
+
const bestValue = selectBestValue(value1, value2);
|
|
1032
1213
|
|
|
1033
1214
|
scopeNode.schema[schemaPath] = bestValue;
|
|
1034
1215
|
equivalentScopeNode.schema[equivalentSchemaPath] = bestValue;
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
// traceId,
|
|
1039
|
-
// sourceScopeNodeName: scopeNode.name,
|
|
1040
|
-
// path,
|
|
1041
|
-
// schemaPath,
|
|
1042
|
-
// usageScopeNodeName: equivalentScopeNode.name,
|
|
1043
|
-
// equivalentPath,
|
|
1044
|
-
// equivalentSchemaPath,
|
|
1045
|
-
// remainingSchemaPathParts,
|
|
1046
|
-
// remainingEquivalentSchemaPathParts,
|
|
1047
|
-
// value1,
|
|
1048
|
-
// value2,
|
|
1049
|
-
// bestValue,
|
|
1050
|
-
// });
|
|
1051
|
-
// }
|
|
1052
|
-
break;
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
if (
|
|
1056
|
-
missingEquivalentPath &&
|
|
1057
|
-
(scopeNode.name.includes('____cyScope') ||
|
|
1058
|
-
!('instantiatedVariables' in equivalentScopeNode))
|
|
1216
|
+
} else if (
|
|
1217
|
+
scopeNode.name.includes('____cyScope') ||
|
|
1218
|
+
!('instantiatedVariables' in equivalentScopeNode)
|
|
1059
1219
|
) {
|
|
1220
|
+
// No matching equivalent path found - create one if needed
|
|
1060
1221
|
// Only necessary for internal function scopes or external function scopes
|
|
1061
1222
|
const newEquivalentPath = this.joinPathParts([
|
|
1062
1223
|
equivalentPath,
|
|
1063
1224
|
...remainingSchemaPathParts,
|
|
1064
1225
|
]);
|
|
1065
1226
|
|
|
1066
|
-
// if (traceId) {
|
|
1067
|
-
// console.info('Debug Propagate: missing equivalent path', {
|
|
1068
|
-
// traceId,
|
|
1069
|
-
// scopeNodeName: scopeNode.name,
|
|
1070
|
-
// path,
|
|
1071
|
-
// equivalentPath,
|
|
1072
|
-
// schemaPath,
|
|
1073
|
-
// remainingSchemaPathParts,
|
|
1074
|
-
// equivalentScopeNodeName: equivalentScopeNode.name,
|
|
1075
|
-
// // equivalentScopeNodeSchema: equivalentScopeNode.schema,
|
|
1076
|
-
// newEquivalentPath,
|
|
1077
|
-
// });
|
|
1078
|
-
// }
|
|
1079
|
-
|
|
1080
1227
|
equivalentScopeNode.schema[newEquivalentPath] =
|
|
1081
1228
|
scopeNode.schema[schemaPath];
|
|
1082
1229
|
}
|
|
@@ -1118,111 +1265,40 @@ export class ScopeDataStructure {
|
|
|
1118
1265
|
}
|
|
1119
1266
|
}
|
|
1120
1267
|
|
|
1121
|
-
splitPath(path: string): string[] {
|
|
1122
|
-
|
|
1123
|
-
return structuredClone(this.pathPartsCache.get(path)!);
|
|
1124
|
-
}
|
|
1125
|
-
const pathParts = splitOutsideParenthesesAndArrays(path);
|
|
1126
|
-
this.pathPartsCache.set(path, structuredClone(pathParts));
|
|
1127
|
-
return pathParts;
|
|
1268
|
+
splitPath(path: string): readonly string[] {
|
|
1269
|
+
return this.pathManager.splitPath(path);
|
|
1128
1270
|
}
|
|
1129
1271
|
|
|
1130
|
-
joinPathParts(pathParts: string[]): string {
|
|
1131
|
-
|
|
1132
|
-
if (this.pathJoinCache.has(cacheKey)) {
|
|
1133
|
-
return this.pathJoinCache.get(cacheKey)!;
|
|
1134
|
-
}
|
|
1135
|
-
const result = joinParenthesesAndArrays(pathParts);
|
|
1136
|
-
this.pathJoinCache.set(cacheKey, result);
|
|
1137
|
-
return result;
|
|
1272
|
+
joinPathParts(pathParts: readonly string[]): string {
|
|
1273
|
+
return this.pathManager.joinPathParts(pathParts);
|
|
1138
1274
|
}
|
|
1139
1275
|
|
|
1140
1276
|
// PRIVATE METHODS //
|
|
1141
1277
|
|
|
1142
|
-
private uniqueId({
|
|
1143
|
-
scopeNodeName,
|
|
1144
|
-
schemaPath,
|
|
1145
|
-
value,
|
|
1146
|
-
}: {
|
|
1278
|
+
private uniqueId(params: {
|
|
1147
1279
|
scopeNodeName: string;
|
|
1148
1280
|
schemaPath: string;
|
|
1149
1281
|
value?: string;
|
|
1150
1282
|
}): string {
|
|
1151
|
-
|
|
1152
|
-
return parts.join('::');
|
|
1283
|
+
return uniqueId(params);
|
|
1153
1284
|
}
|
|
1154
1285
|
|
|
1155
1286
|
private uniqueScopeVariables(scopeVariables: ScopeVariable[]) {
|
|
1156
|
-
return
|
|
1287
|
+
return uniqueScopeVariables(scopeVariables);
|
|
1157
1288
|
}
|
|
1158
1289
|
|
|
1159
1290
|
private uniqueScopeAndPaths(
|
|
1160
1291
|
scopeVariables: Pick<ScopeVariable, 'scopeNodeName' | 'schemaPath'>[],
|
|
1161
1292
|
) {
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
// Optimize from O(n²) to O(n) using Set for deduplication
|
|
1165
|
-
const seen = new Set<string>();
|
|
1166
|
-
return scopeVariables.filter((varItem) => {
|
|
1167
|
-
const key = `${varItem.scopeNodeName}::${varItem.schemaPath}`;
|
|
1168
|
-
if (seen.has(key)) {
|
|
1169
|
-
return false;
|
|
1170
|
-
}
|
|
1171
|
-
seen.add(key);
|
|
1172
|
-
return true;
|
|
1173
|
-
});
|
|
1293
|
+
return uniqueScopeAndPaths(scopeVariables);
|
|
1174
1294
|
}
|
|
1175
1295
|
|
|
1176
1296
|
private isValidPath(path: string): boolean {
|
|
1177
|
-
|
|
1178
|
-
return false;
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
if (!path) {
|
|
1182
|
-
return false;
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
if (path === 'true' || path === 'false') {
|
|
1186
|
-
return false;
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
if (!isNaN(Number(path))) {
|
|
1190
|
-
return false;
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
if (path.match(/^['"]/)) {
|
|
1194
|
-
return false;
|
|
1195
|
-
}
|
|
1196
|
-
|
|
1197
|
-
return this.splitPath(path).every((part) => {
|
|
1198
|
-
if (
|
|
1199
|
-
cleanOutBoundary(
|
|
1200
|
-
cleanOutBoundary(cleanOutBoundary(part, '<', '>'), '[', ']'),
|
|
1201
|
-
).match(/\s/)
|
|
1202
|
-
) {
|
|
1203
|
-
return false;
|
|
1204
|
-
}
|
|
1205
|
-
return true;
|
|
1206
|
-
});
|
|
1297
|
+
return this.pathManager.isValidPath(path);
|
|
1207
1298
|
}
|
|
1208
1299
|
|
|
1209
1300
|
private addToTree(pathParts: string[]) {
|
|
1210
|
-
|
|
1211
|
-
for (const pathPart of pathParts) {
|
|
1212
|
-
const existingChild = scopeTreeNode.children.find(
|
|
1213
|
-
(child) => child.name === pathPart,
|
|
1214
|
-
);
|
|
1215
|
-
if (existingChild) {
|
|
1216
|
-
scopeTreeNode = existingChild;
|
|
1217
|
-
} else {
|
|
1218
|
-
const childScopeTreeNode = {
|
|
1219
|
-
name: pathPart,
|
|
1220
|
-
children: [] as any[],
|
|
1221
|
-
};
|
|
1222
|
-
scopeTreeNode.children.push(childScopeTreeNode);
|
|
1223
|
-
scopeTreeNode = childScopeTreeNode;
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1301
|
+
this.scopeTreeManager.addPath(pathParts);
|
|
1226
1302
|
}
|
|
1227
1303
|
|
|
1228
1304
|
private setInstantiatedVariables(scopeNode: ScopeNode) {
|
|
@@ -1317,9 +1393,92 @@ export class ScopeDataStructure {
|
|
|
1317
1393
|
scopeNode,
|
|
1318
1394
|
'original equivalency',
|
|
1319
1395
|
);
|
|
1396
|
+
|
|
1397
|
+
// Propagate sub-property equivalencies when the equivalentValue is a simple variable
|
|
1398
|
+
// that has sub-properties defined in the isolatedEquivalentVariables.
|
|
1399
|
+
// This handles cases like: dataItem={{ structure: completeDataStructure }}
|
|
1400
|
+
// where completeDataStructure has sub-properties like completeDataStructure['Function Arguments']
|
|
1401
|
+
// We need to propagate these to create: dataItem.structure['Function Arguments'] equivalencies
|
|
1402
|
+
const isSimpleVariable =
|
|
1403
|
+
!equivalentValue.startsWith('signature[') &&
|
|
1404
|
+
!equivalentValue.includes('functionCallReturnValue') &&
|
|
1405
|
+
!equivalentValue.includes('.') &&
|
|
1406
|
+
!equivalentValue.includes('[');
|
|
1407
|
+
|
|
1408
|
+
if (isSimpleVariable) {
|
|
1409
|
+
// Look in current scope and all parent scopes for sub-properties
|
|
1410
|
+
const scopesToCheck = [scopeNode.name, ...scopeNode.tree];
|
|
1411
|
+
for (const scopeName of scopesToCheck) {
|
|
1412
|
+
const checkScope = this.scopeNodes[scopeName];
|
|
1413
|
+
if (!checkScope?.analysis?.isolatedEquivalentVariables) continue;
|
|
1414
|
+
|
|
1415
|
+
for (const [subPath, subValue] of Object.entries(
|
|
1416
|
+
checkScope.analysis.isolatedEquivalentVariables,
|
|
1417
|
+
)) {
|
|
1418
|
+
// Check if this is a sub-property of the equivalentValue variable
|
|
1419
|
+
// e.g., completeDataStructure['Function Arguments'] or completeDataStructure.foo
|
|
1420
|
+
const matchesDot = subPath.startsWith(equivalentValue + '.');
|
|
1421
|
+
const matchesBracket = subPath.startsWith(equivalentValue + '[');
|
|
1422
|
+
if (matchesDot || matchesBracket) {
|
|
1423
|
+
const subPropertyPath = subPath.substring(
|
|
1424
|
+
equivalentValue.length,
|
|
1425
|
+
);
|
|
1426
|
+
const newPath = cleanPath(path + subPropertyPath, allPaths);
|
|
1427
|
+
const newEquivalentValue = cleanPath(
|
|
1428
|
+
(subValue as string).replace(/::cyDuplicateKey\d+::/g, ''),
|
|
1429
|
+
allPaths,
|
|
1430
|
+
);
|
|
1431
|
+
|
|
1432
|
+
if (
|
|
1433
|
+
newEquivalentValue &&
|
|
1434
|
+
this.isValidPath(newEquivalentValue)
|
|
1435
|
+
) {
|
|
1436
|
+
this.addEquivalency(
|
|
1437
|
+
newPath,
|
|
1438
|
+
newEquivalentValue,
|
|
1439
|
+
checkScope.name, // Use the scope where the sub-property was found
|
|
1440
|
+
scopeNode,
|
|
1441
|
+
'propagated sub-property equivalency',
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
// Also check if equivalentValue itself maps to a functionCallReturnValue
|
|
1447
|
+
// e.g., result = useMemo(...).functionCallReturnValue
|
|
1448
|
+
if (
|
|
1449
|
+
subPath === equivalentValue &&
|
|
1450
|
+
typeof subValue === 'string' &&
|
|
1451
|
+
subValue.endsWith('.functionCallReturnValue')
|
|
1452
|
+
) {
|
|
1453
|
+
this.propagateFunctionCallReturnSubProperties(
|
|
1454
|
+
path,
|
|
1455
|
+
subValue,
|
|
1456
|
+
scopeNode,
|
|
1457
|
+
allPaths,
|
|
1458
|
+
);
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
// Handle function call return values by propagating returnValue.* sub-properties
|
|
1465
|
+
// from the callback scope to the usage path
|
|
1466
|
+
if (equivalentValue.endsWith('.functionCallReturnValue')) {
|
|
1467
|
+
this.propagateFunctionCallReturnSubProperties(
|
|
1468
|
+
path,
|
|
1469
|
+
equivalentValue,
|
|
1470
|
+
scopeNode,
|
|
1471
|
+
allPaths,
|
|
1472
|
+
);
|
|
1473
|
+
}
|
|
1320
1474
|
}
|
|
1321
1475
|
}
|
|
1322
1476
|
|
|
1477
|
+
// PERF: Enable batch processing to convert recursive addToSchema calls to iterative
|
|
1478
|
+
// This eliminates deep call stacks and improves deduplication
|
|
1479
|
+
this.batchProcessor = new BatchSchemaProcessor();
|
|
1480
|
+
this.batchQueuedSet = new Set();
|
|
1481
|
+
|
|
1323
1482
|
for (const key of Array.from(allPaths)) {
|
|
1324
1483
|
let value = isolatedStructure[key] ?? 'unknown';
|
|
1325
1484
|
|
|
@@ -1327,7 +1486,6 @@ export class ScopeDataStructure {
|
|
|
1327
1486
|
value = 'unknown';
|
|
1328
1487
|
}
|
|
1329
1488
|
|
|
1330
|
-
const startTime = new Date().getTime();
|
|
1331
1489
|
this.addToSchema({
|
|
1332
1490
|
path: cleanPath(key, allPaths),
|
|
1333
1491
|
value,
|
|
@@ -1341,31 +1499,308 @@ export class ScopeDataStructure {
|
|
|
1341
1499
|
],
|
|
1342
1500
|
});
|
|
1343
1501
|
|
|
1344
|
-
//
|
|
1345
|
-
|
|
1346
|
-
// console.info('SLOW', {
|
|
1347
|
-
// timeDiff,
|
|
1348
|
-
// root: this.scopeTree.name,
|
|
1349
|
-
// scopeNodeName: scopeNode.name,
|
|
1350
|
-
// schemaPath: key,
|
|
1351
|
-
// onlyEquivalencies: this.onlyEquivalencies,
|
|
1352
|
-
// });
|
|
1353
|
-
// }
|
|
1502
|
+
// Process any work queued by followEquivalencies
|
|
1503
|
+
this.processBatchQueue();
|
|
1354
1504
|
}
|
|
1355
1505
|
|
|
1506
|
+
// Final pass to ensure all queued work is processed
|
|
1507
|
+
this.processBatchQueue();
|
|
1508
|
+
|
|
1509
|
+
// Clean up batch processor
|
|
1510
|
+
this.batchProcessor = null;
|
|
1511
|
+
this.batchQueuedSet = null;
|
|
1512
|
+
|
|
1356
1513
|
this.validateSchema(scopeNode, false, false);
|
|
1357
1514
|
}
|
|
1358
1515
|
|
|
1516
|
+
/**
|
|
1517
|
+
* Process all items in the batch queue.
|
|
1518
|
+
* Each item may queue more work via followEquivalencies.
|
|
1519
|
+
*/
|
|
1520
|
+
private processBatchQueue(): void {
|
|
1521
|
+
if (!this.batchProcessor) return;
|
|
1522
|
+
|
|
1523
|
+
while (this.batchProcessor.hasWork()) {
|
|
1524
|
+
const item = this.batchProcessor.getNextWork();
|
|
1525
|
+
if (!item) break;
|
|
1526
|
+
|
|
1527
|
+
const scopeNode = this.getScopeOrFunctionCallInfo(
|
|
1528
|
+
item.scopeNodeName,
|
|
1529
|
+
) as ScopeNode;
|
|
1530
|
+
if (!scopeNode) continue;
|
|
1531
|
+
|
|
1532
|
+
this.addToSchema({
|
|
1533
|
+
path: item.path,
|
|
1534
|
+
value: item.value,
|
|
1535
|
+
scopeNode,
|
|
1536
|
+
equivalencyValueChain:
|
|
1537
|
+
item.equivalencyValueChain as EquivalencyValueChainItem[],
|
|
1538
|
+
traceId: item.traceId,
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
/**
|
|
1544
|
+
* Propagates returnValue.* sub-properties from a callback scope to the usage path.
|
|
1545
|
+
*
|
|
1546
|
+
* When we have an equivalency like:
|
|
1547
|
+
* DataItemEditor().signature[0].structure -> getData().functionCallReturnValue
|
|
1548
|
+
*
|
|
1549
|
+
* And the callback scope for getData has:
|
|
1550
|
+
* returnValue.args -> dataStructure.arguments
|
|
1551
|
+
*
|
|
1552
|
+
* This method creates the transitive equivalency:
|
|
1553
|
+
* DataItemEditor().signature[0].structure.args -> signature[0].dataStructure.arguments
|
|
1554
|
+
*
|
|
1555
|
+
* It also resolves variable references through parent scopes (e.g., dataStructure -> signature[0].dataStructure)
|
|
1556
|
+
* and ensures the database entry is updated with the usage path.
|
|
1557
|
+
*/
|
|
1558
|
+
private propagateFunctionCallReturnSubProperties(
|
|
1559
|
+
path: string,
|
|
1560
|
+
equivalentValue: string,
|
|
1561
|
+
scopeNode: ScopeNode,
|
|
1562
|
+
allPaths: string[],
|
|
1563
|
+
) {
|
|
1564
|
+
// Try to find the callback scope name from different patterns:
|
|
1565
|
+
// 1. "getData().functionCallReturnValue" → look up getData variable to find scope
|
|
1566
|
+
// 2. "useMemo(cyScope1(), [...]).functionCallReturnValue" → extract cyScope1 directly
|
|
1567
|
+
let callbackScopeName: string | undefined;
|
|
1568
|
+
|
|
1569
|
+
// Pattern 1: Simple function call like getData().functionCallReturnValue
|
|
1570
|
+
const simpleFunctionMatch = equivalentValue.match(
|
|
1571
|
+
/^(\w+)\(\)\.functionCallReturnValue$/,
|
|
1572
|
+
);
|
|
1573
|
+
if (simpleFunctionMatch) {
|
|
1574
|
+
const functionName = simpleFunctionMatch[1];
|
|
1575
|
+
// Find the function reference in current or parent scopes
|
|
1576
|
+
const scopesToCheck = [scopeNode.name, ...scopeNode.tree];
|
|
1577
|
+
for (const scopeName of scopesToCheck) {
|
|
1578
|
+
const checkScope = this.scopeNodes[scopeName];
|
|
1579
|
+
if (!checkScope?.analysis?.isolatedEquivalentVariables) continue;
|
|
1580
|
+
|
|
1581
|
+
const functionRef =
|
|
1582
|
+
checkScope.analysis.isolatedEquivalentVariables[functionName];
|
|
1583
|
+
if (typeof functionRef === 'string' && functionRef.endsWith('F')) {
|
|
1584
|
+
callbackScopeName = functionRef.slice(0, -1);
|
|
1585
|
+
break;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// Pattern 2: useMemo/useCallback with callback scope
|
|
1591
|
+
// e.g., "useMemo(cyScope1(), [deps]).functionCallReturnValue"
|
|
1592
|
+
if (!callbackScopeName) {
|
|
1593
|
+
const useMemoMatch = equivalentValue.match(
|
|
1594
|
+
/^useMemo\((\w+)\(\),\s*\[.*\]\)\.functionCallReturnValue$/,
|
|
1595
|
+
);
|
|
1596
|
+
if (useMemoMatch) {
|
|
1597
|
+
callbackScopeName = useMemoMatch[1];
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
if (!callbackScopeName) return;
|
|
1602
|
+
|
|
1603
|
+
const callbackScope = this.scopeNodes[callbackScopeName];
|
|
1604
|
+
if (!callbackScope) return;
|
|
1605
|
+
|
|
1606
|
+
// Look for returnValue.* sub-properties in the callback scope
|
|
1607
|
+
if (!callbackScope.analysis?.isolatedEquivalentVariables) return;
|
|
1608
|
+
|
|
1609
|
+
const isolatedVars = callbackScope.analysis.isolatedEquivalentVariables;
|
|
1610
|
+
|
|
1611
|
+
// First, check if returnValue is an alias to another variable (e.g., returnValue = intermediate)
|
|
1612
|
+
// If so, we need to look for that variable's sub-properties too
|
|
1613
|
+
const returnValueAlias =
|
|
1614
|
+
typeof isolatedVars.returnValue === 'string' &&
|
|
1615
|
+
!isolatedVars.returnValue.includes('.')
|
|
1616
|
+
? isolatedVars.returnValue
|
|
1617
|
+
: undefined;
|
|
1618
|
+
|
|
1619
|
+
// Pattern 3: Object.keys(X).reduce() - the reduce result has the same sub-properties as X
|
|
1620
|
+
// When returnValue = "Object.keys(source).reduce(...).functionCallReturnValue", look for source.* sub-properties
|
|
1621
|
+
let reduceSourceVar: string | undefined;
|
|
1622
|
+
if (typeof isolatedVars.returnValue === 'string') {
|
|
1623
|
+
const reduceMatch = isolatedVars.returnValue.match(
|
|
1624
|
+
/^Object\.keys\((\w+)\)\.reduce\(.*\)\.functionCallReturnValue$/,
|
|
1625
|
+
);
|
|
1626
|
+
if (reduceMatch) {
|
|
1627
|
+
reduceSourceVar = reduceMatch[1];
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
for (const [subPath, subValue] of Object.entries(isolatedVars)) {
|
|
1632
|
+
// Check for direct returnValue.* sub-properties
|
|
1633
|
+
const isReturnValueSub =
|
|
1634
|
+
subPath.startsWith('returnValue.') ||
|
|
1635
|
+
subPath.startsWith('returnValue[');
|
|
1636
|
+
|
|
1637
|
+
// Also check for alias.* sub-properties (e.g., intermediate.args when returnValue = intermediate)
|
|
1638
|
+
const isAliasSub =
|
|
1639
|
+
returnValueAlias &&
|
|
1640
|
+
(subPath.startsWith(returnValueAlias + '.') ||
|
|
1641
|
+
subPath.startsWith(returnValueAlias + '['));
|
|
1642
|
+
|
|
1643
|
+
// Also check for reduce source.* sub-properties (e.g., source['Function Arguments'] when returnValue = Object.keys(source).reduce())
|
|
1644
|
+
const isReduceSourceSub =
|
|
1645
|
+
reduceSourceVar &&
|
|
1646
|
+
(subPath.startsWith(reduceSourceVar + '.') ||
|
|
1647
|
+
subPath.startsWith(reduceSourceVar + '['));
|
|
1648
|
+
|
|
1649
|
+
if (
|
|
1650
|
+
typeof subValue !== 'string' ||
|
|
1651
|
+
(!isReturnValueSub && !isAliasSub && !isReduceSourceSub)
|
|
1652
|
+
)
|
|
1653
|
+
continue;
|
|
1654
|
+
|
|
1655
|
+
// Convert alias/reduceSource paths to returnValue paths
|
|
1656
|
+
let effectiveSubPath = subPath;
|
|
1657
|
+
if (isAliasSub && !isReturnValueSub) {
|
|
1658
|
+
// Replace the alias prefix with returnValue
|
|
1659
|
+
effectiveSubPath =
|
|
1660
|
+
'returnValue' + subPath.substring(returnValueAlias!.length);
|
|
1661
|
+
} else if (isReduceSourceSub && !isReturnValueSub && !isAliasSub) {
|
|
1662
|
+
// Replace the reduce source prefix with returnValue
|
|
1663
|
+
effectiveSubPath =
|
|
1664
|
+
'returnValue' + subPath.substring(reduceSourceVar!.length);
|
|
1665
|
+
}
|
|
1666
|
+
const subPropertyPath = effectiveSubPath.substring('returnValue'.length);
|
|
1667
|
+
const newPath = cleanPath(path + subPropertyPath, allPaths);
|
|
1668
|
+
let newEquivalentValue = cleanPath(
|
|
1669
|
+
subValue.replace(/::cyDuplicateKey\d+::/g, ''),
|
|
1670
|
+
allPaths,
|
|
1671
|
+
);
|
|
1672
|
+
|
|
1673
|
+
// Resolve variable references through parent scope equivalencies
|
|
1674
|
+
const resolved = this.resolveVariableThroughParentScopes(
|
|
1675
|
+
newEquivalentValue,
|
|
1676
|
+
callbackScope,
|
|
1677
|
+
allPaths,
|
|
1678
|
+
);
|
|
1679
|
+
newEquivalentValue = resolved.resolvedPath;
|
|
1680
|
+
const equivalentScopeName = resolved.scopeName;
|
|
1681
|
+
|
|
1682
|
+
if (!newEquivalentValue || !this.isValidPath(newEquivalentValue))
|
|
1683
|
+
continue;
|
|
1684
|
+
|
|
1685
|
+
this.addEquivalency(
|
|
1686
|
+
newPath,
|
|
1687
|
+
newEquivalentValue,
|
|
1688
|
+
equivalentScopeName,
|
|
1689
|
+
scopeNode,
|
|
1690
|
+
'propagated function call return sub-property equivalency',
|
|
1691
|
+
);
|
|
1692
|
+
|
|
1693
|
+
// Ensure the database entry has the usage path
|
|
1694
|
+
this.addUsageToEquivalencyDatabaseEntry(
|
|
1695
|
+
newPath,
|
|
1696
|
+
newEquivalentValue,
|
|
1697
|
+
equivalentScopeName,
|
|
1698
|
+
scopeNode.name,
|
|
1699
|
+
);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
/**
|
|
1704
|
+
* Resolves a variable path through parent scope equivalencies.
|
|
1705
|
+
*
|
|
1706
|
+
* For example, if the path is "dataStructure.arguments" and the parent scope has
|
|
1707
|
+
* dataStructure -> signature[0].dataStructure, this returns:
|
|
1708
|
+
* { resolvedPath: "signature[0].dataStructure.arguments", scopeName: "ParentScope" }
|
|
1709
|
+
*/
|
|
1710
|
+
private resolveVariableThroughParentScopes(
|
|
1711
|
+
path: string | undefined,
|
|
1712
|
+
callbackScope: ScopeNode,
|
|
1713
|
+
allPaths: string[],
|
|
1714
|
+
): { resolvedPath: string | undefined; scopeName: string } {
|
|
1715
|
+
if (!path)
|
|
1716
|
+
return { resolvedPath: undefined, scopeName: callbackScope.name };
|
|
1717
|
+
|
|
1718
|
+
// Extract the root variable name
|
|
1719
|
+
const dotIndex = path.indexOf('.');
|
|
1720
|
+
const bracketIndex = path.indexOf('[');
|
|
1721
|
+
let firstDelimiter = -1;
|
|
1722
|
+
if (dotIndex > -1 && bracketIndex > -1) {
|
|
1723
|
+
firstDelimiter = Math.min(dotIndex, bracketIndex);
|
|
1724
|
+
} else {
|
|
1725
|
+
firstDelimiter = dotIndex > -1 ? dotIndex : bracketIndex;
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
if (firstDelimiter === -1)
|
|
1729
|
+
return { resolvedPath: path, scopeName: callbackScope.name };
|
|
1730
|
+
|
|
1731
|
+
const rootVar = path.substring(0, firstDelimiter);
|
|
1732
|
+
const restOfPath = path.substring(firstDelimiter);
|
|
1733
|
+
|
|
1734
|
+
// Look in parent scopes for the root variable's equivalency
|
|
1735
|
+
for (const parentScopeName of callbackScope.tree || []) {
|
|
1736
|
+
const parentScope = this.scopeNodes[parentScopeName];
|
|
1737
|
+
if (!parentScope?.analysis?.isolatedEquivalentVariables) continue;
|
|
1738
|
+
|
|
1739
|
+
const rootEquiv =
|
|
1740
|
+
parentScope.analysis.isolatedEquivalentVariables[rootVar];
|
|
1741
|
+
if (typeof rootEquiv === 'string') {
|
|
1742
|
+
return {
|
|
1743
|
+
resolvedPath: cleanPath(rootEquiv + restOfPath, allPaths),
|
|
1744
|
+
scopeName: parentScopeName,
|
|
1745
|
+
};
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
return { resolvedPath: path, scopeName: callbackScope.name };
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* Adds a usage path to the equivalency database entry that has the given source.
|
|
1754
|
+
*
|
|
1755
|
+
* The addEquivalency call creates the equivalency but doesn't always properly
|
|
1756
|
+
* link the usage in the database, so we need to do it explicitly.
|
|
1757
|
+
*/
|
|
1758
|
+
private addUsageToEquivalencyDatabaseEntry(
|
|
1759
|
+
usagePath: string,
|
|
1760
|
+
sourcePath: string,
|
|
1761
|
+
sourceScopeName: string,
|
|
1762
|
+
fallbackScopeName: string,
|
|
1763
|
+
) {
|
|
1764
|
+
const usageEntry = {
|
|
1765
|
+
scopeNodeName: usagePath.match(/^([^().]+)\(\)/)
|
|
1766
|
+
? (usagePath.match(/^([^().]+)\(\)/)?.[1] ?? fallbackScopeName)
|
|
1767
|
+
: fallbackScopeName,
|
|
1768
|
+
schemaPath: usagePath,
|
|
1769
|
+
};
|
|
1770
|
+
|
|
1771
|
+
const sourceKey = `${sourceScopeName}::${sourcePath}`;
|
|
1772
|
+
for (const entry of this.equivalencyDatabase) {
|
|
1773
|
+
if (
|
|
1774
|
+
entry.intermediatesOrder[sourceKey] === 0 ||
|
|
1775
|
+
entry.sourceCandidates.some(
|
|
1776
|
+
(sc) =>
|
|
1777
|
+
sc.scopeNodeName === sourceScopeName &&
|
|
1778
|
+
sc.schemaPath === sourcePath,
|
|
1779
|
+
)
|
|
1780
|
+
) {
|
|
1781
|
+
const usageExists = entry.usages.some(
|
|
1782
|
+
(u) =>
|
|
1783
|
+
u.scopeNodeName === usageEntry.scopeNodeName &&
|
|
1784
|
+
u.schemaPath === usageEntry.schemaPath,
|
|
1785
|
+
);
|
|
1786
|
+
if (!usageExists) {
|
|
1787
|
+
entry.usages.push(usageEntry);
|
|
1788
|
+
}
|
|
1789
|
+
break;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1359
1794
|
private checkForArrayItemPath(
|
|
1360
|
-
pathParts: string[],
|
|
1795
|
+
pathParts: readonly string[],
|
|
1361
1796
|
scopeNode: ScopeNode,
|
|
1362
1797
|
equivalencyValueChain: EquivalencyValueChainItem[],
|
|
1363
1798
|
) {
|
|
1364
|
-
const arrayItemPath =
|
|
1799
|
+
const arrayItemPath = this.joinPathParts([...pathParts, '[]']);
|
|
1365
1800
|
if (scopeNode.equivalencies[arrayItemPath]) {
|
|
1366
1801
|
const firstEquivalency = equivalencyValueChain[0].currentPath;
|
|
1367
|
-
const newFirstEquivalencyPath =
|
|
1368
|
-
...
|
|
1802
|
+
const newFirstEquivalencyPath = this.joinPathParts([
|
|
1803
|
+
...this.splitPath(firstEquivalency.schemaPath),
|
|
1369
1804
|
'[]',
|
|
1370
1805
|
]);
|
|
1371
1806
|
this.addToSchema({
|
|
@@ -1383,41 +1818,95 @@ export class ScopeDataStructure {
|
|
|
1383
1818
|
}
|
|
1384
1819
|
}
|
|
1385
1820
|
|
|
1821
|
+
/**
|
|
1822
|
+
* Follows equivalencies for a subpath and recursively calls addToSchema.
|
|
1823
|
+
*
|
|
1824
|
+
* This is the recursive heart of schema propagation. When we have:
|
|
1825
|
+
* `user` equivalent to `props.user`
|
|
1826
|
+
* And we're processing `user.id`, this method will:
|
|
1827
|
+
* 1. Find the equivalency `user` → `props.user`
|
|
1828
|
+
* 2. Reconstruct the full path: `props.user.id`
|
|
1829
|
+
* 3. Recursively call addToSchema for the equivalent scope
|
|
1830
|
+
*
|
|
1831
|
+
* ## Function-Ambivalent Paths
|
|
1832
|
+
*
|
|
1833
|
+
* Handles paths like `data.map(fn)` by also checking equivalencies
|
|
1834
|
+
* for `data.map` (without the function argument). This allows
|
|
1835
|
+
* array method chains to propagate correctly.
|
|
1836
|
+
*
|
|
1837
|
+
* @param scopeNode - Current scope being processed
|
|
1838
|
+
* @param path - Full original path (e.g., 'user.profile.name')
|
|
1839
|
+
* @param pathParts - Split version of path
|
|
1840
|
+
* @param subPath - Current subpath being checked (e.g., 'user.profile')
|
|
1841
|
+
* @param subPathParts - Split version of subPath
|
|
1842
|
+
* @param value - Type value to propagate
|
|
1843
|
+
* @param equivalencyValueChain - Chain tracking for database
|
|
1844
|
+
* @param traceId - Debug ID
|
|
1845
|
+
* @param debugLevel - 0=minimal, 1=executed, 2=verbose
|
|
1846
|
+
*/
|
|
1386
1847
|
private followEquivalencies(
|
|
1387
1848
|
scopeNode: ScopeNode,
|
|
1388
1849
|
path: string,
|
|
1389
|
-
pathParts: string[],
|
|
1850
|
+
pathParts: readonly string[],
|
|
1390
1851
|
subPath: string,
|
|
1391
|
-
subPathParts: string[],
|
|
1852
|
+
subPathParts: readonly string[],
|
|
1392
1853
|
value: string,
|
|
1393
1854
|
equivalencyValueChain: EquivalencyValueChainItem[],
|
|
1394
1855
|
traceId?: number,
|
|
1395
1856
|
debugLevel = 0,
|
|
1396
1857
|
) {
|
|
1397
1858
|
followEquivalenciesCallCount++;
|
|
1859
|
+
|
|
1860
|
+
// Trace for debugging
|
|
1861
|
+
this.tracer.trace('followEquivalencies', {
|
|
1862
|
+
path,
|
|
1863
|
+
scope: scopeNode.name,
|
|
1864
|
+
subPath,
|
|
1865
|
+
hasEquivalencies: (scopeNode.equivalencies[subPath]?.length ?? 0) > 0,
|
|
1866
|
+
});
|
|
1867
|
+
|
|
1398
1868
|
let propagated = false;
|
|
1869
|
+
|
|
1870
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
1871
|
+
// Step 1: Collect equivalencies (including function-ambivalent paths)
|
|
1872
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
1399
1873
|
let equivalentValues = scopeNode.equivalencies[subPath] ?? [];
|
|
1400
1874
|
let functionAmbivalentSubPath = subPath;
|
|
1401
1875
|
let functionAmbivalentSubPathParts = subPathParts;
|
|
1402
1876
|
|
|
1403
|
-
|
|
1877
|
+
// For paths like `data.map(fn)`, also check `data.map` equivalencies
|
|
1878
|
+
// PERF: Only do the expensive function-ambivalent processing if:
|
|
1879
|
+
// 1. No equivalencies found yet, AND
|
|
1880
|
+
// 2. The subpath ends with a function call (contains '(')
|
|
1881
|
+
const lastPart = subPathParts[subPathParts.length - 1];
|
|
1882
|
+
if (lastPart.indexOf('(') > -1) {
|
|
1883
|
+
// Check function-ambivalent path for equivalencies
|
|
1404
1884
|
functionAmbivalentSubPathParts = [
|
|
1405
1885
|
...subPathParts.slice(0, -1),
|
|
1406
|
-
|
|
1886
|
+
lastPart.split('(')[0],
|
|
1407
1887
|
];
|
|
1408
1888
|
functionAmbivalentSubPath = this.joinPathParts(
|
|
1409
1889
|
functionAmbivalentSubPathParts,
|
|
1410
1890
|
);
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1891
|
+
const functionAmbivalentEquivalencies =
|
|
1892
|
+
scopeNode.equivalencies[functionAmbivalentSubPath];
|
|
1893
|
+
if (functionAmbivalentEquivalencies?.length > 0) {
|
|
1894
|
+
equivalentValues = [
|
|
1895
|
+
...equivalentValues,
|
|
1896
|
+
...functionAmbivalentEquivalencies,
|
|
1897
|
+
];
|
|
1898
|
+
}
|
|
1415
1899
|
}
|
|
1416
1900
|
|
|
1417
1901
|
// Early exit optimization: if no equivalencies to follow, skip expensive processing
|
|
1418
1902
|
if (equivalentValues.length === 0) {
|
|
1903
|
+
followEquivalenciesEarlyExitCount++;
|
|
1904
|
+
if (this.onlyEquivalencies) {
|
|
1905
|
+
followEquivalenciesEarlyExitPhase1Count++;
|
|
1906
|
+
}
|
|
1419
1907
|
return false;
|
|
1420
1908
|
}
|
|
1909
|
+
followEquivalenciesWithWorkCount++;
|
|
1421
1910
|
|
|
1422
1911
|
if (traceId && debugLevel > 1) {
|
|
1423
1912
|
console.info(
|
|
@@ -1443,12 +1932,21 @@ export class ScopeDataStructure {
|
|
|
1443
1932
|
|
|
1444
1933
|
const uniqueEquivalentValues = this.uniqueScopeVariables(equivalentValues);
|
|
1445
1934
|
|
|
1935
|
+
// PERF: Only create Set when chain is non-empty (common case avoids allocation)
|
|
1936
|
+
// Pre-compute Set of chain IDs for O(1) cycle detection instead of O(n) .some()
|
|
1937
|
+
const chainIdSet =
|
|
1938
|
+
equivalencyValueChain.length > 0
|
|
1939
|
+
? new Set(equivalencyValueChain.map((ev) => ev.id))
|
|
1940
|
+
: null;
|
|
1941
|
+
|
|
1446
1942
|
for (let index = 0; index < uniqueEquivalentValues.length; ++index) {
|
|
1447
1943
|
const equivalentValue = uniqueEquivalentValues[index];
|
|
1448
1944
|
|
|
1945
|
+
// O(1) cycle check using Set instead of O(n) .some()
|
|
1449
1946
|
if (
|
|
1947
|
+
chainIdSet &&
|
|
1450
1948
|
equivalentValue.id !== -1 &&
|
|
1451
|
-
|
|
1949
|
+
chainIdSet.has(equivalentValue.id)
|
|
1452
1950
|
) {
|
|
1453
1951
|
if (traceId) {
|
|
1454
1952
|
console.info(
|
|
@@ -1499,8 +1997,12 @@ export class ScopeDataStructure {
|
|
|
1499
1997
|
schemaPathParts[schemaPathParts.length - 1] ===
|
|
1500
1998
|
lastRelevantSubPathPart.split('(')[0]
|
|
1501
1999
|
) {
|
|
1502
|
-
|
|
1503
|
-
|
|
2000
|
+
// PERF: Don't mutate the array - create a new one to avoid requiring structuredClone
|
|
2001
|
+
const updatedSchemaPathParts = [
|
|
2002
|
+
...schemaPathParts.slice(0, -1),
|
|
2003
|
+
lastRelevantSubPathPart,
|
|
2004
|
+
];
|
|
2005
|
+
schemaPath = this.joinPathParts(updatedSchemaPathParts);
|
|
1504
2006
|
}
|
|
1505
2007
|
|
|
1506
2008
|
const remainingPathParts = pathParts.slice(relevantSubPathParts.length);
|
|
@@ -1560,25 +2062,27 @@ export class ScopeDataStructure {
|
|
|
1560
2062
|
);
|
|
1561
2063
|
}
|
|
1562
2064
|
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
2065
|
+
// PERF: Use spread instead of structuredClone - the chain items are immutable
|
|
2066
|
+
// so shallow copy is sufficient and much faster (structuredClone is expensive)
|
|
2067
|
+
const localEquivalencyValueChain = [
|
|
2068
|
+
...equivalencyValueChain,
|
|
2069
|
+
{
|
|
2070
|
+
id: equivalentValue.id,
|
|
2071
|
+
source: 'equivalence',
|
|
2072
|
+
previousPath: {
|
|
2073
|
+
scopeNodeName: scopeNode.name,
|
|
2074
|
+
schemaPath: path,
|
|
2075
|
+
value,
|
|
2076
|
+
},
|
|
2077
|
+
currentPath: {
|
|
2078
|
+
scopeNodeName: equivalentScopeNode.name,
|
|
2079
|
+
schemaPath: newEquivalentPath,
|
|
2080
|
+
value,
|
|
2081
|
+
},
|
|
2082
|
+
reason: equivalentValue.equivalencyReason,
|
|
2083
|
+
traceId,
|
|
1578
2084
|
},
|
|
1579
|
-
|
|
1580
|
-
traceId,
|
|
1581
|
-
});
|
|
2085
|
+
];
|
|
1582
2086
|
|
|
1583
2087
|
// writeEquivalencyToFile(
|
|
1584
2088
|
// scopeNode.name,
|
|
@@ -1590,13 +2094,57 @@ export class ScopeDataStructure {
|
|
|
1590
2094
|
// )
|
|
1591
2095
|
|
|
1592
2096
|
propagated = true;
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
2097
|
+
|
|
2098
|
+
// PERF: If batch processor is active, queue work instead of recursing
|
|
2099
|
+
// This converts deep recursion to iterative processing
|
|
2100
|
+
if (this.batchProcessor) {
|
|
2101
|
+
// PERF OPTIMIZATION: For external function calls, use path-only key (ignore value)
|
|
2102
|
+
// External FCs don't have internals to analyze, so processing the same path
|
|
2103
|
+
// with different values is redundant - we're just tracking data flow
|
|
2104
|
+
const isExternalFc =
|
|
2105
|
+
equivalentValue.equivalencyReason ===
|
|
2106
|
+
'equivalency to external function call' ||
|
|
2107
|
+
this.getExternalFunctionCallInfo(equivalentScopeNode.name) !==
|
|
2108
|
+
undefined;
|
|
2109
|
+
const queueKey = isExternalFc
|
|
2110
|
+
? `${equivalentScopeNode.name}::${newEquivalentPath}` // path-only for external FC
|
|
2111
|
+
: `${equivalentScopeNode.name}::${newEquivalentPath}::${value}`; // full key otherwise
|
|
2112
|
+
|
|
2113
|
+
// Always check visited - it catches already-processed items
|
|
2114
|
+
const alreadyVisited = this.visitedTracker.hasGlobalVisited(
|
|
2115
|
+
equivalentScopeNode.name,
|
|
2116
|
+
newEquivalentPath,
|
|
2117
|
+
value,
|
|
2118
|
+
);
|
|
2119
|
+
// For external FCs, queueKey is path-only so this catches any-value duplicates
|
|
2120
|
+
const alreadyQueued = this.batchQueuedSet?.has(queueKey);
|
|
2121
|
+
|
|
2122
|
+
if (alreadyVisited || alreadyQueued) {
|
|
2123
|
+
// Still record in database to capture the chain (as addToSchema does)
|
|
2124
|
+
this.addToEquivalencyDatabase(
|
|
2125
|
+
localEquivalencyValueChain as EquivalencyValueChainItem[],
|
|
2126
|
+
traceId,
|
|
2127
|
+
);
|
|
2128
|
+
} else {
|
|
2129
|
+
// Mark as queued to prevent duplicate queue entries
|
|
2130
|
+
this.batchQueuedSet?.add(queueKey);
|
|
2131
|
+
this.batchProcessor.addWork({
|
|
2132
|
+
scopeNodeName: equivalentScopeNode.name,
|
|
2133
|
+
path: newEquivalentPath,
|
|
2134
|
+
value,
|
|
2135
|
+
equivalencyValueChain: localEquivalencyValueChain,
|
|
2136
|
+
traceId,
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
} else {
|
|
2140
|
+
this.addToSchema({
|
|
2141
|
+
path: newEquivalentPath,
|
|
2142
|
+
value,
|
|
2143
|
+
scopeNode: equivalentScopeNode,
|
|
2144
|
+
equivalencyValueChain: localEquivalencyValueChain,
|
|
2145
|
+
traceId,
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
1600
2148
|
}
|
|
1601
2149
|
}
|
|
1602
2150
|
|
|
@@ -1635,6 +2183,17 @@ export class ScopeDataStructure {
|
|
|
1635
2183
|
usageEquivalency.scopeNodeName,
|
|
1636
2184
|
) as ScopeNode;
|
|
1637
2185
|
|
|
2186
|
+
// Guard against infinite recursion by tracking which paths we've already
|
|
2187
|
+
// added from addComplexSourcePathVariables
|
|
2188
|
+
if (
|
|
2189
|
+
this.visitedTracker.checkAndMarkComplexSourceVisited(
|
|
2190
|
+
usageScopeNode.name,
|
|
2191
|
+
newUsageEquivalentPath,
|
|
2192
|
+
)
|
|
2193
|
+
) {
|
|
2194
|
+
continue;
|
|
2195
|
+
}
|
|
2196
|
+
|
|
1638
2197
|
this.addToSchema({
|
|
1639
2198
|
path: newUsageEquivalentPath,
|
|
1640
2199
|
value: 'unknown',
|
|
@@ -1785,6 +2344,7 @@ export class ScopeDataStructure {
|
|
|
1785
2344
|
|
|
1786
2345
|
for (let i = 0; i < cleanEquivalencyValueChain.length; ++i) {
|
|
1787
2346
|
const pathInfo = cleanEquivalencyValueChain[i].currentPath;
|
|
2347
|
+
const previousPath = cleanEquivalencyValueChain[i].previousPath;
|
|
1788
2348
|
|
|
1789
2349
|
const schemaPathParts = this.splitPath(pathInfo.schemaPath);
|
|
1790
2350
|
const isSignaturePath =
|
|
@@ -1798,6 +2358,21 @@ export class ScopeDataStructure {
|
|
|
1798
2358
|
databaseEntry.usages.push(pathInfo);
|
|
1799
2359
|
}
|
|
1800
2360
|
|
|
2361
|
+
// Also add previousPath as a usage if it matches the criteria
|
|
2362
|
+
// This handles propagated sub-property equivalencies where the JSX prop path
|
|
2363
|
+
// is in previousPath and should be tracked as a usage
|
|
2364
|
+
if (previousPath) {
|
|
2365
|
+
const prevSchemaPathParts = this.splitPath(previousPath.schemaPath);
|
|
2366
|
+
const isPrevSignaturePath =
|
|
2367
|
+
prevSchemaPathParts.findIndex((p) => p.startsWith('signature[')) > 0;
|
|
2368
|
+
if (
|
|
2369
|
+
prevSchemaPathParts[0].startsWith('returnValue') ||
|
|
2370
|
+
isPrevSignaturePath
|
|
2371
|
+
) {
|
|
2372
|
+
databaseEntry.usages.push(previousPath);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
|
|
1801
2376
|
const pathId = this.uniqueId(pathInfo);
|
|
1802
2377
|
let intermediateIndex = cleanEquivalencyValueChain.length - i - 1;
|
|
1803
2378
|
const existingIntermediateIndex =
|
|
@@ -1905,6 +2480,7 @@ export class ScopeDataStructure {
|
|
|
1905
2480
|
if (functionScope) {
|
|
1906
2481
|
functionScope.functionCalls.push(externalFunctionCallInfo);
|
|
1907
2482
|
this.externalFunctionCalls.splice(i, 1);
|
|
2483
|
+
this.invalidateExternalFunctionCallsIndex();
|
|
1908
2484
|
|
|
1909
2485
|
for (const equivalentPath in externalFunctionCallInfo.equivalencies) {
|
|
1910
2486
|
let localEquivalentPath = equivalentPath;
|
|
@@ -1972,18 +2548,21 @@ export class ScopeDataStructure {
|
|
|
1972
2548
|
for (const key in scopeNode.equivalencies) {
|
|
1973
2549
|
const keyParts = this.splitPath(key);
|
|
1974
2550
|
if (keyParts.length > 1) {
|
|
1975
|
-
|
|
2551
|
+
// PERF: Don't mutate the array - use indexing instead of pop() to avoid requiring structuredClone
|
|
2552
|
+
const lastPart = keyParts[keyParts.length - 1];
|
|
1976
2553
|
if (lastPart.startsWith('signature[')) {
|
|
1977
|
-
const
|
|
2554
|
+
const keyPartsWithoutLast = keyParts.slice(0, -1);
|
|
2555
|
+
const functionCall =
|
|
2556
|
+
keyPartsWithoutLast[keyPartsWithoutLast.length - 1];
|
|
1978
2557
|
const argumentsIndex = functionCall.indexOf('(');
|
|
1979
2558
|
const functionName = this.joinPathParts([
|
|
1980
|
-
...
|
|
2559
|
+
...keyPartsWithoutLast.slice(0, -1),
|
|
1981
2560
|
functionCall.slice(
|
|
1982
2561
|
0,
|
|
1983
2562
|
argumentsIndex === -1 ? undefined : argumentsIndex,
|
|
1984
2563
|
),
|
|
1985
2564
|
]);
|
|
1986
|
-
const callSignature = this.joinPathParts(
|
|
2565
|
+
const callSignature = this.joinPathParts(keyPartsWithoutLast);
|
|
1987
2566
|
|
|
1988
2567
|
const functionCallInfo = {
|
|
1989
2568
|
name: functionName,
|
|
@@ -2000,19 +2579,21 @@ export class ScopeDataStructure {
|
|
|
2000
2579
|
equivalentValues.schemaPath,
|
|
2001
2580
|
);
|
|
2002
2581
|
if (equivalentValueParts.length > 1) {
|
|
2003
|
-
|
|
2582
|
+
// PERF: Don't mutate the array - use indexing instead of pop() to avoid requiring structuredClone
|
|
2583
|
+
const lastPart =
|
|
2584
|
+
equivalentValueParts[equivalentValueParts.length - 1];
|
|
2004
2585
|
if (lastPart.startsWith('functionCallReturnValue')) {
|
|
2005
|
-
const
|
|
2006
|
-
|
|
2586
|
+
const partsWithoutLast = equivalentValueParts.slice(0, -1);
|
|
2587
|
+
const functionCall = partsWithoutLast[partsWithoutLast.length - 1];
|
|
2007
2588
|
const argumentsIndex = functionCall.indexOf('(');
|
|
2008
2589
|
const functionName = this.joinPathParts([
|
|
2009
|
-
...
|
|
2590
|
+
...partsWithoutLast.slice(0, -1),
|
|
2010
2591
|
functionCall.slice(
|
|
2011
2592
|
0,
|
|
2012
2593
|
argumentsIndex === -1 ? undefined : argumentsIndex,
|
|
2013
2594
|
),
|
|
2014
2595
|
]);
|
|
2015
|
-
const callSignature = this.joinPathParts(
|
|
2596
|
+
const callSignature = this.joinPathParts(partsWithoutLast);
|
|
2016
2597
|
|
|
2017
2598
|
const functionCallInfo = {
|
|
2018
2599
|
name: functionName,
|
|
@@ -2213,7 +2794,8 @@ export class ScopeDataStructure {
|
|
|
2213
2794
|
}
|
|
2214
2795
|
|
|
2215
2796
|
getScopeNode(scopeName?: string): ScopeNode | undefined {
|
|
2216
|
-
const scopeNode =
|
|
2797
|
+
const scopeNode =
|
|
2798
|
+
this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()];
|
|
2217
2799
|
if (scopeNode) return scopeNode;
|
|
2218
2800
|
|
|
2219
2801
|
for (const scopeNodeName in this.scopeNodes) {
|
|
@@ -2223,18 +2805,54 @@ export class ScopeDataStructure {
|
|
|
2223
2805
|
}
|
|
2224
2806
|
}
|
|
2225
2807
|
|
|
2808
|
+
/**
|
|
2809
|
+
* Gets or builds the external function calls index for O(1) lookup.
|
|
2810
|
+
* The index maps both the full name and the name without generics to the FunctionCallInfo.
|
|
2811
|
+
*/
|
|
2812
|
+
private getExternalFunctionCallsIndex(): Map<string, FunctionCallInfo> {
|
|
2813
|
+
if (this.externalFunctionCallsIndex === null) {
|
|
2814
|
+
this.externalFunctionCallsIndex = new Map();
|
|
2815
|
+
for (const efc of this.externalFunctionCalls) {
|
|
2816
|
+
this.externalFunctionCallsIndex.set(efc.name, efc);
|
|
2817
|
+
// Also index by name without generics (e.g., 'MyFunction<T>' -> 'MyFunction')
|
|
2818
|
+
const nameWithoutGenerics = this.pathManager.stripGenerics(efc.name);
|
|
2819
|
+
if (nameWithoutGenerics !== efc.name) {
|
|
2820
|
+
this.externalFunctionCallsIndex.set(nameWithoutGenerics, efc);
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
return this.externalFunctionCallsIndex;
|
|
2825
|
+
}
|
|
2826
|
+
|
|
2827
|
+
/**
|
|
2828
|
+
* Invalidates the external function calls index.
|
|
2829
|
+
* Call this after any mutation to externalFunctionCalls array.
|
|
2830
|
+
*/
|
|
2831
|
+
private invalidateExternalFunctionCallsIndex(): void {
|
|
2832
|
+
this.externalFunctionCallsIndex = null;
|
|
2833
|
+
}
|
|
2834
|
+
|
|
2226
2835
|
getExternalFunctionCallInfo(
|
|
2227
2836
|
functionName: string,
|
|
2228
2837
|
): FunctionCallInfo | undefined {
|
|
2838
|
+
const searchKey = getFunctionCallRoot(functionName);
|
|
2839
|
+
const index = this.getExternalFunctionCallsIndex();
|
|
2840
|
+
|
|
2841
|
+
// First try exact match
|
|
2842
|
+
const exact = index.get(searchKey);
|
|
2843
|
+
if (exact) return exact;
|
|
2844
|
+
|
|
2845
|
+
// Fallback to the original find for edge cases not covered by index
|
|
2229
2846
|
return this.externalFunctionCalls.find(
|
|
2230
2847
|
(efc) =>
|
|
2231
|
-
efc.name ===
|
|
2232
|
-
efc.name
|
|
2848
|
+
efc.name === searchKey ||
|
|
2849
|
+
this.pathManager.stripGenerics(efc.name) === searchKey,
|
|
2233
2850
|
);
|
|
2234
2851
|
}
|
|
2235
2852
|
|
|
2236
2853
|
getScopeAnalysis(scopeName?: string): ScopeAnalysis | undefined {
|
|
2237
|
-
return this.scopeNodes[scopeName ?? this.
|
|
2854
|
+
return this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()]
|
|
2855
|
+
.analysis;
|
|
2238
2856
|
}
|
|
2239
2857
|
|
|
2240
2858
|
getAnalysisTree(): ScopeTreeNode {
|
|
@@ -2248,12 +2866,13 @@ export class ScopeDataStructure {
|
|
|
2248
2866
|
scopeName?: string;
|
|
2249
2867
|
fillInUnknowns?: boolean;
|
|
2250
2868
|
}): any {
|
|
2251
|
-
const scopeNode =
|
|
2869
|
+
const scopeNode =
|
|
2870
|
+
this.scopeNodes[scopeName ?? this.scopeTreeManager.getRootName()];
|
|
2252
2871
|
|
|
2253
2872
|
if (!scopeNode) {
|
|
2254
|
-
const
|
|
2255
|
-
|
|
2256
|
-
|
|
2873
|
+
const searchKey = getFunctionCallRoot(scopeName);
|
|
2874
|
+
const externalFunctionCallSchema =
|
|
2875
|
+
this.getExternalFunctionCallsIndex().get(searchKey)?.schema;
|
|
2257
2876
|
|
|
2258
2877
|
if (!externalFunctionCallSchema) return;
|
|
2259
2878
|
|
|
@@ -2286,9 +2905,9 @@ export class ScopeDataStructure {
|
|
|
2286
2905
|
return this.equivalencyDatabaseCache.get(cacheKey);
|
|
2287
2906
|
}
|
|
2288
2907
|
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
);
|
|
2908
|
+
// PERF: Use inverted index for O(1) lookup instead of O(n) array scan
|
|
2909
|
+
// The intermediatesOrderIndex maps pathId (scopeNodeName::schemaPath) to entry
|
|
2910
|
+
const result = this.intermediatesOrderIndex.get(cacheKey);
|
|
2292
2911
|
|
|
2293
2912
|
this.equivalencyDatabaseCache.set(cacheKey, result);
|
|
2294
2913
|
return result;
|
|
@@ -2392,7 +3011,7 @@ export class ScopeDataStructure {
|
|
|
2392
3011
|
}
|
|
2393
3012
|
|
|
2394
3013
|
const tempScopeNode = this.createTempScopeNode(
|
|
2395
|
-
functionName ?? this.
|
|
3014
|
+
functionName ?? this.scopeTreeManager.getRootName(),
|
|
2396
3015
|
signatureInSchema,
|
|
2397
3016
|
equivalencies,
|
|
2398
3017
|
);
|
|
@@ -2409,7 +3028,7 @@ export class ScopeDataStructure {
|
|
|
2409
3028
|
functionName?: string;
|
|
2410
3029
|
fillInUnknowns?: boolean;
|
|
2411
3030
|
}) {
|
|
2412
|
-
const scopeName = functionName ?? this.
|
|
3031
|
+
const scopeName = functionName ?? this.scopeTreeManager.getRootName();
|
|
2413
3032
|
const scopeNode = this.scopeNodes[scopeName];
|
|
2414
3033
|
|
|
2415
3034
|
let schema: ScopeNode['schema'] = {};
|
|
@@ -2434,14 +3053,7 @@ export class ScopeDataStructure {
|
|
|
2434
3053
|
for (const schemaPath in externalFunctionCall.schema) {
|
|
2435
3054
|
const value1 = schema[schemaPath];
|
|
2436
3055
|
const value2 = externalFunctionCall.schema[schemaPath];
|
|
2437
|
-
|
|
2438
|
-
if (
|
|
2439
|
-
bestValue === 'unknown' ||
|
|
2440
|
-
(bestValue.includes('unknown') && !value2.includes('unknown'))
|
|
2441
|
-
) {
|
|
2442
|
-
bestValue = value2;
|
|
2443
|
-
}
|
|
2444
|
-
schema[schemaPath] = bestValue;
|
|
3056
|
+
schema[schemaPath] = selectBestValue(value1, value2, value2);
|
|
2445
3057
|
}
|
|
2446
3058
|
}
|
|
2447
3059
|
}
|
|
@@ -2567,7 +3179,7 @@ export class ScopeDataStructure {
|
|
|
2567
3179
|
}
|
|
2568
3180
|
|
|
2569
3181
|
getEquivalentSignatureVariables() {
|
|
2570
|
-
const scopeNode = this.scopeNodes[this.
|
|
3182
|
+
const scopeNode = this.scopeNodes[this.scopeTreeManager.getRootName()];
|
|
2571
3183
|
|
|
2572
3184
|
const equivalentSignatureVariables: Record<string, string> = {};
|
|
2573
3185
|
for (const [path, equivalentValues] of Object.entries(
|
|
@@ -2589,7 +3201,7 @@ export class ScopeDataStructure {
|
|
|
2589
3201
|
final?: boolean,
|
|
2590
3202
|
): VariableInfo | undefined {
|
|
2591
3203
|
const scopeNode = this.getScopeOrFunctionCallInfo(
|
|
2592
|
-
scopeName ?? this.
|
|
3204
|
+
scopeName ?? this.scopeTreeManager.getRootName(),
|
|
2593
3205
|
);
|
|
2594
3206
|
if (!scopeNode) return;
|
|
2595
3207
|
|
|
@@ -2626,7 +3238,7 @@ export class ScopeDataStructure {
|
|
|
2626
3238
|
);
|
|
2627
3239
|
|
|
2628
3240
|
const tempScopeNode = this.createTempScopeNode(
|
|
2629
|
-
scopeName ?? this.
|
|
3241
|
+
scopeName ?? this.scopeTreeManager.getRootName(),
|
|
2630
3242
|
relevantSchema,
|
|
2631
3243
|
);
|
|
2632
3244
|
|
|
@@ -2647,6 +3259,87 @@ export class ScopeDataStructure {
|
|
|
2647
3259
|
return this.environmentVariables;
|
|
2648
3260
|
}
|
|
2649
3261
|
|
|
3262
|
+
/**
|
|
3263
|
+
* Add conditional usages from AST analysis.
|
|
3264
|
+
* Called during scope analysis to collect all conditionals.
|
|
3265
|
+
*/
|
|
3266
|
+
addConditionalUsages(
|
|
3267
|
+
usages: Record<
|
|
3268
|
+
string,
|
|
3269
|
+
Array<{
|
|
3270
|
+
path: string;
|
|
3271
|
+
conditionType: 'truthiness' | 'comparison' | 'switch';
|
|
3272
|
+
comparedValues?: string[];
|
|
3273
|
+
location: 'if' | 'ternary' | 'logical-and' | 'switch';
|
|
3274
|
+
}>
|
|
3275
|
+
>,
|
|
3276
|
+
): void {
|
|
3277
|
+
for (const [path, pathUsages] of Object.entries(usages)) {
|
|
3278
|
+
if (!this.rawConditionalUsages[path]) {
|
|
3279
|
+
this.rawConditionalUsages[path] = [];
|
|
3280
|
+
}
|
|
3281
|
+
// Deduplicate usages
|
|
3282
|
+
for (const usage of pathUsages) {
|
|
3283
|
+
const exists = this.rawConditionalUsages[path].some(
|
|
3284
|
+
(existing) =>
|
|
3285
|
+
existing.location === usage.location &&
|
|
3286
|
+
existing.conditionType === usage.conditionType &&
|
|
3287
|
+
JSON.stringify(existing.comparedValues) ===
|
|
3288
|
+
JSON.stringify(usage.comparedValues),
|
|
3289
|
+
);
|
|
3290
|
+
if (!exists) {
|
|
3291
|
+
this.rawConditionalUsages[path].push(usage);
|
|
3292
|
+
}
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
|
|
3297
|
+
/**
|
|
3298
|
+
* Get enriched conditional usages with source tracing.
|
|
3299
|
+
* Uses explainPath to trace each local variable back to its data source.
|
|
3300
|
+
*/
|
|
3301
|
+
getEnrichedConditionalUsages(): Record<
|
|
3302
|
+
string,
|
|
3303
|
+
Array<{
|
|
3304
|
+
path: string;
|
|
3305
|
+
conditionType: 'truthiness' | 'comparison' | 'switch';
|
|
3306
|
+
comparedValues?: string[];
|
|
3307
|
+
location: 'if' | 'ternary' | 'logical-and' | 'switch';
|
|
3308
|
+
sourceDataPath?: string;
|
|
3309
|
+
}>
|
|
3310
|
+
> {
|
|
3311
|
+
const enriched: Record<
|
|
3312
|
+
string,
|
|
3313
|
+
Array<{
|
|
3314
|
+
path: string;
|
|
3315
|
+
conditionType: 'truthiness' | 'comparison' | 'switch';
|
|
3316
|
+
comparedValues?: string[];
|
|
3317
|
+
location: 'if' | 'ternary' | 'logical-and' | 'switch';
|
|
3318
|
+
sourceDataPath?: string;
|
|
3319
|
+
}>
|
|
3320
|
+
> = {};
|
|
3321
|
+
|
|
3322
|
+
for (const [path, usages] of Object.entries(this.rawConditionalUsages)) {
|
|
3323
|
+
// Try to trace this path back to a data source
|
|
3324
|
+
// First, try the root scope
|
|
3325
|
+
const rootScopeName = this.scopeTreeManager.getTree().name;
|
|
3326
|
+
const explanation = this.explainPath(rootScopeName, path);
|
|
3327
|
+
|
|
3328
|
+
let sourceDataPath: string | undefined;
|
|
3329
|
+
if (explanation.source) {
|
|
3330
|
+
// Build the full data path: scopeName.path
|
|
3331
|
+
sourceDataPath = `${explanation.source.scope}.${explanation.source.path}`;
|
|
3332
|
+
}
|
|
3333
|
+
|
|
3334
|
+
enriched[path] = usages.map((usage) => ({
|
|
3335
|
+
...usage,
|
|
3336
|
+
sourceDataPath,
|
|
3337
|
+
}));
|
|
3338
|
+
}
|
|
3339
|
+
|
|
3340
|
+
return enriched;
|
|
3341
|
+
}
|
|
3342
|
+
|
|
2650
3343
|
toSerializable(): SerializableDataStructure {
|
|
2651
3344
|
// Helper to convert ScopeVariable to SerializableScopeVariable
|
|
2652
3345
|
const toSerializableVariable = (
|
|
@@ -2725,12 +3418,598 @@ export class ScopeDataStructure {
|
|
|
2725
3418
|
|
|
2726
3419
|
const environmentVariables = this.getEnvironmentVariables();
|
|
2727
3420
|
|
|
3421
|
+
// Get enriched conditional usages with source tracing
|
|
3422
|
+
const enrichedConditionalUsages = this.getEnrichedConditionalUsages();
|
|
3423
|
+
const conditionalUsages =
|
|
3424
|
+
Object.keys(enrichedConditionalUsages).length > 0
|
|
3425
|
+
? enrichedConditionalUsages
|
|
3426
|
+
: undefined;
|
|
3427
|
+
|
|
2728
3428
|
return {
|
|
2729
3429
|
externalFunctionCalls,
|
|
2730
3430
|
rootFunction,
|
|
2731
3431
|
functionResults,
|
|
2732
3432
|
equivalentSignatureVariables,
|
|
2733
3433
|
environmentVariables,
|
|
3434
|
+
conditionalUsages,
|
|
3435
|
+
};
|
|
3436
|
+
}
|
|
3437
|
+
|
|
3438
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
3439
|
+
// DEBUGGING HELPERS
|
|
3440
|
+
// These methods help understand what's happening during analysis.
|
|
3441
|
+
// Use them when investigating why a path is missing or has the wrong type.
|
|
3442
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
3443
|
+
|
|
3444
|
+
/**
|
|
3445
|
+
* Explains a path by tracing its equivalencies and showing where data comes from.
|
|
3446
|
+
*
|
|
3447
|
+
* @example
|
|
3448
|
+
* ```typescript
|
|
3449
|
+
* const explanation = sds.explainPath('MyComponent', 'user.id');
|
|
3450
|
+
* console.log(explanation);
|
|
3451
|
+
* // {
|
|
3452
|
+
* // path: 'user.id',
|
|
3453
|
+
* // scope: 'MyComponent',
|
|
3454
|
+
* // schemaValue: 'string',
|
|
3455
|
+
* // equivalencies: [
|
|
3456
|
+
* // { scope: 'MyComponent', path: 'signature[0].user.id' },
|
|
3457
|
+
* // { scope: 'UserProfile', path: 'props.user.id' }
|
|
3458
|
+
* // ],
|
|
3459
|
+
* // source: { scope: 'fetchUser', path: 'functionCallReturnValue(fetch).data.user.id' }
|
|
3460
|
+
* // }
|
|
3461
|
+
* ```
|
|
3462
|
+
*/
|
|
3463
|
+
explainPath(
|
|
3464
|
+
scopeName: string,
|
|
3465
|
+
path: string,
|
|
3466
|
+
): {
|
|
3467
|
+
path: string;
|
|
3468
|
+
scope: string;
|
|
3469
|
+
schemaValue: string | undefined;
|
|
3470
|
+
equivalencies: Array<{ scope: string; path: string; reason?: string }>;
|
|
3471
|
+
databaseEntry: EquivalencyDatabaseEntry | undefined;
|
|
3472
|
+
source: { scope: string; path: string } | undefined;
|
|
3473
|
+
} {
|
|
3474
|
+
const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
|
|
3475
|
+
if (!scopeNode) {
|
|
3476
|
+
return {
|
|
3477
|
+
path,
|
|
3478
|
+
scope: scopeName,
|
|
3479
|
+
schemaValue: undefined,
|
|
3480
|
+
equivalencies: [],
|
|
3481
|
+
databaseEntry: undefined,
|
|
3482
|
+
source: undefined,
|
|
3483
|
+
};
|
|
3484
|
+
}
|
|
3485
|
+
|
|
3486
|
+
const schemaValue = scopeNode.schema?.[path];
|
|
3487
|
+
const rawEquivalencies = scopeNode.equivalencies?.[path] ?? [];
|
|
3488
|
+
|
|
3489
|
+
const equivalencies = rawEquivalencies.map((eq) => ({
|
|
3490
|
+
scope: eq.scopeNodeName,
|
|
3491
|
+
path: eq.schemaPath,
|
|
3492
|
+
reason: eq.equivalencyReason,
|
|
3493
|
+
}));
|
|
3494
|
+
|
|
3495
|
+
const databaseEntry = this.getEquivalenciesDatabaseEntry(scopeName, path);
|
|
3496
|
+
const source =
|
|
3497
|
+
databaseEntry?.sourceCandidates?.[0] &&
|
|
3498
|
+
databaseEntry.sourceCandidates.length > 0
|
|
3499
|
+
? {
|
|
3500
|
+
scope: databaseEntry.sourceCandidates[0].scopeNodeName,
|
|
3501
|
+
path: databaseEntry.sourceCandidates[0].schemaPath,
|
|
3502
|
+
}
|
|
3503
|
+
: undefined;
|
|
3504
|
+
|
|
3505
|
+
return {
|
|
3506
|
+
path,
|
|
3507
|
+
scope: scopeName,
|
|
3508
|
+
schemaValue,
|
|
3509
|
+
equivalencies,
|
|
3510
|
+
databaseEntry,
|
|
3511
|
+
source,
|
|
3512
|
+
};
|
|
3513
|
+
}
|
|
3514
|
+
|
|
3515
|
+
/**
|
|
3516
|
+
* Dumps the current state of analysis for inspection.
|
|
3517
|
+
* Useful for understanding the overall state or finding issues.
|
|
3518
|
+
*
|
|
3519
|
+
* @param options - What to include in the dump
|
|
3520
|
+
* @returns A summary object with requested data
|
|
3521
|
+
*/
|
|
3522
|
+
dumpState(options?: {
|
|
3523
|
+
includeSchemas?: boolean;
|
|
3524
|
+
includeEquivalencies?: boolean;
|
|
3525
|
+
scopeFilter?: string;
|
|
3526
|
+
}): {
|
|
3527
|
+
scopeCount: number;
|
|
3528
|
+
externalCallCount: number;
|
|
3529
|
+
equivalencyDatabaseSize: number;
|
|
3530
|
+
metrics: {
|
|
3531
|
+
addToSchemaCallCount: number;
|
|
3532
|
+
followEquivalenciesCallCount: number;
|
|
3533
|
+
maxEquivalencyChainDepth: number;
|
|
3534
|
+
};
|
|
3535
|
+
scopes: Array<{
|
|
3536
|
+
name: string;
|
|
3537
|
+
schemaKeyCount: number;
|
|
3538
|
+
equivalencyKeyCount: number;
|
|
3539
|
+
functionCallCount: number;
|
|
3540
|
+
schema?: Record<string, string>;
|
|
3541
|
+
equivalencies?: Record<string, Array<{ scope: string; path: string }>>;
|
|
3542
|
+
}>;
|
|
3543
|
+
} {
|
|
3544
|
+
const includeSchemas = options?.includeSchemas ?? false;
|
|
3545
|
+
const includeEquivalencies = options?.includeEquivalencies ?? false;
|
|
3546
|
+
const scopeFilter = options?.scopeFilter;
|
|
3547
|
+
|
|
3548
|
+
const scopeNames = Object.keys(this.scopeNodes).filter(
|
|
3549
|
+
(name) => !scopeFilter || name.includes(scopeFilter),
|
|
3550
|
+
);
|
|
3551
|
+
|
|
3552
|
+
const scopes = scopeNames.map((name) => {
|
|
3553
|
+
const node = this.scopeNodes[name];
|
|
3554
|
+
const result: {
|
|
3555
|
+
name: string;
|
|
3556
|
+
schemaKeyCount: number;
|
|
3557
|
+
equivalencyKeyCount: number;
|
|
3558
|
+
functionCallCount: number;
|
|
3559
|
+
schema?: Record<string, string>;
|
|
3560
|
+
equivalencies?: Record<string, Array<{ scope: string; path: string }>>;
|
|
3561
|
+
} = {
|
|
3562
|
+
name,
|
|
3563
|
+
schemaKeyCount: Object.keys(node.schema ?? {}).length,
|
|
3564
|
+
equivalencyKeyCount: Object.keys(node.equivalencies ?? {}).length,
|
|
3565
|
+
functionCallCount: node.functionCalls?.length ?? 0,
|
|
3566
|
+
};
|
|
3567
|
+
|
|
3568
|
+
if (includeSchemas) {
|
|
3569
|
+
result.schema = node.schema;
|
|
3570
|
+
}
|
|
3571
|
+
|
|
3572
|
+
if (includeEquivalencies) {
|
|
3573
|
+
result.equivalencies = Object.entries(node.equivalencies ?? {}).reduce(
|
|
3574
|
+
(acc, [key, vals]) => {
|
|
3575
|
+
acc[key] = vals.map((v) => ({
|
|
3576
|
+
scope: v.scopeNodeName,
|
|
3577
|
+
path: v.schemaPath,
|
|
3578
|
+
}));
|
|
3579
|
+
return acc;
|
|
3580
|
+
},
|
|
3581
|
+
{} as Record<string, Array<{ scope: string; path: string }>>,
|
|
3582
|
+
);
|
|
3583
|
+
}
|
|
3584
|
+
|
|
3585
|
+
return result;
|
|
3586
|
+
});
|
|
3587
|
+
|
|
3588
|
+
return {
|
|
3589
|
+
scopeCount: Object.keys(this.scopeNodes).length,
|
|
3590
|
+
externalCallCount: this.externalFunctionCalls.length,
|
|
3591
|
+
equivalencyDatabaseSize: this.equivalencyDatabase.length,
|
|
3592
|
+
metrics: {
|
|
3593
|
+
addToSchemaCallCount,
|
|
3594
|
+
followEquivalenciesCallCount,
|
|
3595
|
+
maxEquivalencyChainDepth,
|
|
3596
|
+
},
|
|
3597
|
+
scopes,
|
|
3598
|
+
};
|
|
3599
|
+
}
|
|
3600
|
+
|
|
3601
|
+
/**
|
|
3602
|
+
* Traces the full equivalency chain for a path, showing how data flows.
|
|
3603
|
+
* This is the most detailed debugging tool for understanding data tracing.
|
|
3604
|
+
*
|
|
3605
|
+
* @example
|
|
3606
|
+
* ```typescript
|
|
3607
|
+
* const chain = sds.traceEquivalencyChain('MyComponent', 'user.id');
|
|
3608
|
+
* // Returns array of steps showing how user.id is traced back to its source
|
|
3609
|
+
* ```
|
|
3610
|
+
*/
|
|
3611
|
+
traceEquivalencyChain(
|
|
3612
|
+
scopeName: string,
|
|
3613
|
+
path: string,
|
|
3614
|
+
maxDepth = 20,
|
|
3615
|
+
): Array<{
|
|
3616
|
+
step: number;
|
|
3617
|
+
scope: string;
|
|
3618
|
+
path: string;
|
|
3619
|
+
schemaValue?: string;
|
|
3620
|
+
nextEquivalencies: Array<{ scope: string; path: string; reason?: string }>;
|
|
3621
|
+
}> {
|
|
3622
|
+
const chain: Array<{
|
|
3623
|
+
step: number;
|
|
3624
|
+
scope: string;
|
|
3625
|
+
path: string;
|
|
3626
|
+
schemaValue?: string;
|
|
3627
|
+
nextEquivalencies: Array<{
|
|
3628
|
+
scope: string;
|
|
3629
|
+
path: string;
|
|
3630
|
+
reason?: string;
|
|
3631
|
+
}>;
|
|
3632
|
+
}> = [];
|
|
3633
|
+
|
|
3634
|
+
const visited = new Set<string>();
|
|
3635
|
+
const queue: Array<{ scope: string; path: string; depth: number }> = [
|
|
3636
|
+
{ scope: scopeName, path, depth: 0 },
|
|
3637
|
+
];
|
|
3638
|
+
|
|
3639
|
+
while (queue.length > 0 && chain.length < maxDepth) {
|
|
3640
|
+
const current = queue.shift()!;
|
|
3641
|
+
const key = `${current.scope}::${current.path}`;
|
|
3642
|
+
|
|
3643
|
+
if (visited.has(key)) continue;
|
|
3644
|
+
visited.add(key);
|
|
3645
|
+
|
|
3646
|
+
const scopeNode = this.getScopeOrFunctionCallInfo(current.scope);
|
|
3647
|
+
const schemaValue = scopeNode?.schema?.[current.path];
|
|
3648
|
+
const rawEquivalencies = scopeNode?.equivalencies?.[current.path] ?? [];
|
|
3649
|
+
|
|
3650
|
+
const nextEquivalencies = rawEquivalencies.map((eq) => ({
|
|
3651
|
+
scope: eq.scopeNodeName,
|
|
3652
|
+
path: eq.schemaPath,
|
|
3653
|
+
reason: eq.equivalencyReason,
|
|
3654
|
+
}));
|
|
3655
|
+
|
|
3656
|
+
chain.push({
|
|
3657
|
+
step: chain.length + 1,
|
|
3658
|
+
scope: current.scope,
|
|
3659
|
+
path: current.path,
|
|
3660
|
+
schemaValue,
|
|
3661
|
+
nextEquivalencies,
|
|
3662
|
+
});
|
|
3663
|
+
|
|
3664
|
+
// Add next equivalencies to queue for further tracing
|
|
3665
|
+
for (const eq of nextEquivalencies) {
|
|
3666
|
+
const nextKey = `${eq.scope}::${eq.path}`;
|
|
3667
|
+
if (!visited.has(nextKey)) {
|
|
3668
|
+
queue.push({
|
|
3669
|
+
scope: eq.scope,
|
|
3670
|
+
path: eq.path,
|
|
3671
|
+
depth: current.depth + 1,
|
|
3672
|
+
});
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
|
|
3677
|
+
return chain;
|
|
3678
|
+
}
|
|
3679
|
+
|
|
3680
|
+
/**
|
|
3681
|
+
* Finds all paths in a scope that match a pattern.
|
|
3682
|
+
* Useful for finding related paths when debugging.
|
|
3683
|
+
*
|
|
3684
|
+
* @example
|
|
3685
|
+
* ```typescript
|
|
3686
|
+
* const paths = sds.findPaths('MyComponent', /user/);
|
|
3687
|
+
* // Returns all schema paths containing "user"
|
|
3688
|
+
* ```
|
|
3689
|
+
*/
|
|
3690
|
+
findPaths(
|
|
3691
|
+
scopeName: string,
|
|
3692
|
+
pattern: RegExp | string,
|
|
3693
|
+
): Array<{ path: string; value: string; hasEquivalencies: boolean }> {
|
|
3694
|
+
const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
|
|
3695
|
+
if (!scopeNode) return [];
|
|
3696
|
+
|
|
3697
|
+
const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
|
|
3698
|
+
const results: Array<{
|
|
3699
|
+
path: string;
|
|
3700
|
+
value: string;
|
|
3701
|
+
hasEquivalencies: boolean;
|
|
3702
|
+
}> = [];
|
|
3703
|
+
|
|
3704
|
+
// Search in schema
|
|
3705
|
+
for (const [path, value] of Object.entries(scopeNode.schema ?? {})) {
|
|
3706
|
+
if (regex.test(path)) {
|
|
3707
|
+
results.push({
|
|
3708
|
+
path,
|
|
3709
|
+
value,
|
|
3710
|
+
hasEquivalencies: !!scopeNode.equivalencies?.[path]?.length,
|
|
3711
|
+
});
|
|
3712
|
+
}
|
|
3713
|
+
}
|
|
3714
|
+
|
|
3715
|
+
// Also search equivalency keys that might not be in schema
|
|
3716
|
+
for (const path of Object.keys(scopeNode.equivalencies ?? {})) {
|
|
3717
|
+
if (regex.test(path) && !results.find((r) => r.path === path)) {
|
|
3718
|
+
results.push({
|
|
3719
|
+
path,
|
|
3720
|
+
value: scopeNode.schema?.[path] ?? 'not-in-schema',
|
|
3721
|
+
hasEquivalencies: true,
|
|
3722
|
+
});
|
|
3723
|
+
}
|
|
3724
|
+
}
|
|
3725
|
+
|
|
3726
|
+
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
3727
|
+
}
|
|
3728
|
+
|
|
3729
|
+
/**
|
|
3730
|
+
* Returns a summary of why a path might be missing from the schema.
|
|
3731
|
+
* Checks common issues that cause paths to not appear.
|
|
3732
|
+
*/
|
|
3733
|
+
diagnoseMissingPath(
|
|
3734
|
+
scopeName: string,
|
|
3735
|
+
path: string,
|
|
3736
|
+
): {
|
|
3737
|
+
exists: boolean;
|
|
3738
|
+
inSchema: boolean;
|
|
3739
|
+
inEquivalencies: boolean;
|
|
3740
|
+
possibleReasons: string[];
|
|
3741
|
+
suggestions: string[];
|
|
3742
|
+
} {
|
|
3743
|
+
const scopeNode = this.getScopeOrFunctionCallInfo(scopeName);
|
|
3744
|
+
const possibleReasons: string[] = [];
|
|
3745
|
+
const suggestions: string[] = [];
|
|
3746
|
+
|
|
3747
|
+
if (!scopeNode) {
|
|
3748
|
+
possibleReasons.push(`Scope "${scopeName}" does not exist`);
|
|
3749
|
+
suggestions.push(
|
|
3750
|
+
`Available scopes: ${Object.keys(this.scopeNodes).join(', ')}`,
|
|
3751
|
+
);
|
|
3752
|
+
return {
|
|
3753
|
+
exists: false,
|
|
3754
|
+
inSchema: false,
|
|
3755
|
+
inEquivalencies: false,
|
|
3756
|
+
possibleReasons,
|
|
3757
|
+
suggestions,
|
|
3758
|
+
};
|
|
3759
|
+
}
|
|
3760
|
+
|
|
3761
|
+
const inSchema = path in (scopeNode.schema ?? {});
|
|
3762
|
+
const inEquivalencies = path in (scopeNode.equivalencies ?? {});
|
|
3763
|
+
|
|
3764
|
+
if (inSchema) {
|
|
3765
|
+
return {
|
|
3766
|
+
exists: true,
|
|
3767
|
+
inSchema: true,
|
|
3768
|
+
inEquivalencies,
|
|
3769
|
+
possibleReasons: [],
|
|
3770
|
+
suggestions: [],
|
|
3771
|
+
};
|
|
3772
|
+
}
|
|
3773
|
+
|
|
3774
|
+
// Check for similar paths
|
|
3775
|
+
const allPaths = [
|
|
3776
|
+
...Object.keys(scopeNode.schema ?? {}),
|
|
3777
|
+
...Object.keys(scopeNode.equivalencies ?? {}),
|
|
3778
|
+
];
|
|
3779
|
+
|
|
3780
|
+
const similarPaths = allPaths.filter((p) => {
|
|
3781
|
+
const pathParts = path.split('.');
|
|
3782
|
+
const pParts = p.split('.');
|
|
3783
|
+
return (
|
|
3784
|
+
pathParts.some((part) => pParts.includes(part)) ||
|
|
3785
|
+
p.includes(path) ||
|
|
3786
|
+
path.includes(p)
|
|
3787
|
+
);
|
|
3788
|
+
});
|
|
3789
|
+
|
|
3790
|
+
if (similarPaths.length > 0) {
|
|
3791
|
+
possibleReasons.push(`Path "${path}" not found but similar paths exist`);
|
|
3792
|
+
suggestions.push(`Similar paths: ${similarPaths.slice(0, 5).join(', ')}`);
|
|
3793
|
+
}
|
|
3794
|
+
|
|
3795
|
+
// Check if it's a partial path
|
|
3796
|
+
const parentPath = path.split('.').slice(0, -1).join('.');
|
|
3797
|
+
if (parentPath && scopeNode.schema?.[parentPath]) {
|
|
3798
|
+
possibleReasons.push(
|
|
3799
|
+
`Parent path "${parentPath}" exists with value "${scopeNode.schema[parentPath]}"`,
|
|
3800
|
+
);
|
|
3801
|
+
suggestions.push(
|
|
3802
|
+
`The property might not have been accessed in the analyzed code`,
|
|
3803
|
+
);
|
|
3804
|
+
}
|
|
3805
|
+
|
|
3806
|
+
// Check visited tracker
|
|
3807
|
+
const wasVisited = this.visitedTracker.wasVisited(scopeName, path);
|
|
3808
|
+
if (wasVisited) {
|
|
3809
|
+
possibleReasons.push(`Path was visited during analysis but not written`);
|
|
3810
|
+
suggestions.push(
|
|
3811
|
+
`Check if the path was filtered by isValidPath() or hit cycle detection`,
|
|
3812
|
+
);
|
|
3813
|
+
}
|
|
3814
|
+
|
|
3815
|
+
if (possibleReasons.length === 0) {
|
|
3816
|
+
possibleReasons.push(
|
|
3817
|
+
`Path "${path}" was never encountered during analysis`,
|
|
3818
|
+
);
|
|
3819
|
+
suggestions.push(
|
|
3820
|
+
`Verify the path is accessed in the source code being analyzed`,
|
|
3821
|
+
);
|
|
3822
|
+
}
|
|
3823
|
+
|
|
3824
|
+
return {
|
|
3825
|
+
exists: false,
|
|
3826
|
+
inSchema,
|
|
3827
|
+
inEquivalencies,
|
|
3828
|
+
possibleReasons,
|
|
3829
|
+
suggestions,
|
|
3830
|
+
};
|
|
3831
|
+
}
|
|
3832
|
+
|
|
3833
|
+
/**
|
|
3834
|
+
* Analyzes the stored equivalencies to provide aggregate statistics.
|
|
3835
|
+
* Call this AFTER analysis completes to understand equivalency patterns.
|
|
3836
|
+
*
|
|
3837
|
+
* @example
|
|
3838
|
+
* ```typescript
|
|
3839
|
+
* const analysis = sds.analyzeEquivalencies();
|
|
3840
|
+
* console.log(analysis.byReason); // Count by reason
|
|
3841
|
+
* console.log(analysis.byScope); // Count by scope
|
|
3842
|
+
* console.log(analysis.hotSpots); // Scopes with most equivalencies
|
|
3843
|
+
* ```
|
|
3844
|
+
*/
|
|
3845
|
+
analyzeEquivalencies(): {
|
|
3846
|
+
total: number;
|
|
3847
|
+
byReason: Record<string, number>;
|
|
3848
|
+
byScope: Record<string, number>;
|
|
3849
|
+
hotSpots: Array<{ scope: string; count: number }>;
|
|
3850
|
+
unusedReasons: string[];
|
|
3851
|
+
} {
|
|
3852
|
+
const byReason: Record<string, number> = {};
|
|
3853
|
+
const byScope: Record<string, number> = {};
|
|
3854
|
+
let total = 0;
|
|
3855
|
+
|
|
3856
|
+
// Collect from all scope nodes
|
|
3857
|
+
for (const scopeName of Object.keys(this.scopeNodes)) {
|
|
3858
|
+
const scopeNode = this.scopeNodes[scopeName];
|
|
3859
|
+
const equivalencies = scopeNode.equivalencies ?? {};
|
|
3860
|
+
|
|
3861
|
+
for (const path of Object.keys(equivalencies)) {
|
|
3862
|
+
for (const eq of equivalencies[path]) {
|
|
3863
|
+
total++;
|
|
3864
|
+
const reason = eq.equivalencyReason ?? 'unknown';
|
|
3865
|
+
byReason[reason] = (byReason[reason] ?? 0) + 1;
|
|
3866
|
+
byScope[scopeName] = (byScope[scopeName] ?? 0) + 1;
|
|
3867
|
+
}
|
|
3868
|
+
}
|
|
3869
|
+
}
|
|
3870
|
+
|
|
3871
|
+
// Also check external function calls
|
|
3872
|
+
for (const fc of this.externalFunctionCalls) {
|
|
3873
|
+
const equivalencies = fc.equivalencies ?? {};
|
|
3874
|
+
for (const path of Object.keys(equivalencies)) {
|
|
3875
|
+
for (const eq of equivalencies[path]) {
|
|
3876
|
+
total++;
|
|
3877
|
+
const reason = eq.equivalencyReason ?? 'unknown';
|
|
3878
|
+
byReason[reason] = (byReason[reason] ?? 0) + 1;
|
|
3879
|
+
byScope[fc.name] = (byScope[fc.name] ?? 0) + 1;
|
|
3880
|
+
}
|
|
3881
|
+
}
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3884
|
+
// Find hot spots (scopes with most equivalencies)
|
|
3885
|
+
const hotSpots = Object.entries(byScope)
|
|
3886
|
+
.map(([scope, count]) => ({ scope, count }))
|
|
3887
|
+
.sort((a, b) => b.count - a.count)
|
|
3888
|
+
.slice(0, 10);
|
|
3889
|
+
|
|
3890
|
+
// Find reasons that appear in stored equivalencies but aren't in ALLOWED list
|
|
3891
|
+
const unusedReasons = Object.keys(byReason).filter(
|
|
3892
|
+
(reason) => !ALLOWED_EQUIVALENCY_REASONS.has(reason),
|
|
3893
|
+
);
|
|
3894
|
+
|
|
3895
|
+
return {
|
|
3896
|
+
total,
|
|
3897
|
+
byReason,
|
|
3898
|
+
byScope,
|
|
3899
|
+
hotSpots,
|
|
3900
|
+
unusedReasons,
|
|
2734
3901
|
};
|
|
2735
3902
|
}
|
|
3903
|
+
|
|
3904
|
+
/**
|
|
3905
|
+
* Validates equivalencies and identifies potential problems.
|
|
3906
|
+
* Returns actionable issues that may indicate bugs or optimization opportunities.
|
|
3907
|
+
*
|
|
3908
|
+
* @example
|
|
3909
|
+
* ```typescript
|
|
3910
|
+
* const issues = sds.validateEquivalencies();
|
|
3911
|
+
* if (issues.length > 0) {
|
|
3912
|
+
* console.log('Found issues:', issues);
|
|
3913
|
+
* }
|
|
3914
|
+
* ```
|
|
3915
|
+
*/
|
|
3916
|
+
validateEquivalencies(): Array<{
|
|
3917
|
+
type: 'broken_reference' | 'dead_end' | 'long_chain' | 'circular';
|
|
3918
|
+
severity: 'error' | 'warning';
|
|
3919
|
+
scope: string;
|
|
3920
|
+
path: string;
|
|
3921
|
+
details: string;
|
|
3922
|
+
}> {
|
|
3923
|
+
const issues: Array<{
|
|
3924
|
+
type: 'broken_reference' | 'dead_end' | 'long_chain' | 'circular';
|
|
3925
|
+
severity: 'error' | 'warning';
|
|
3926
|
+
scope: string;
|
|
3927
|
+
path: string;
|
|
3928
|
+
details: string;
|
|
3929
|
+
}> = [];
|
|
3930
|
+
|
|
3931
|
+
const allScopeNames = new Set([
|
|
3932
|
+
...Object.keys(this.scopeNodes),
|
|
3933
|
+
...this.externalFunctionCalls.map((fc) => fc.name),
|
|
3934
|
+
]);
|
|
3935
|
+
|
|
3936
|
+
// Check all scope nodes
|
|
3937
|
+
for (const scopeName of Object.keys(this.scopeNodes)) {
|
|
3938
|
+
const scopeNode = this.scopeNodes[scopeName];
|
|
3939
|
+
const equivalencies = scopeNode.equivalencies ?? {};
|
|
3940
|
+
|
|
3941
|
+
for (const [path, eqs] of Object.entries(equivalencies)) {
|
|
3942
|
+
for (const eq of eqs) {
|
|
3943
|
+
// Check 1: Does the target scope exist?
|
|
3944
|
+
if (!allScopeNames.has(eq.scopeNodeName)) {
|
|
3945
|
+
issues.push({
|
|
3946
|
+
type: 'broken_reference',
|
|
3947
|
+
severity: 'error',
|
|
3948
|
+
scope: scopeName,
|
|
3949
|
+
path,
|
|
3950
|
+
details: `Equivalency points to non-existent scope "${eq.scopeNodeName}"`,
|
|
3951
|
+
});
|
|
3952
|
+
continue;
|
|
3953
|
+
}
|
|
3954
|
+
|
|
3955
|
+
// Check 2: Does the target path exist in target scope's schema or equivalencies?
|
|
3956
|
+
const targetScope = this.getScopeOrFunctionCallInfo(eq.scopeNodeName);
|
|
3957
|
+
if (targetScope) {
|
|
3958
|
+
const hasInSchema = eq.schemaPath in (targetScope.schema ?? {});
|
|
3959
|
+
const hasInEquivalencies =
|
|
3960
|
+
eq.schemaPath in (targetScope.equivalencies ?? {});
|
|
3961
|
+
|
|
3962
|
+
// Only flag as dead end if it's a leaf path (no sub-properties exist)
|
|
3963
|
+
if (!hasInSchema && !hasInEquivalencies) {
|
|
3964
|
+
const hasSubPaths = Object.keys(targetScope.schema ?? {}).some(
|
|
3965
|
+
(p) =>
|
|
3966
|
+
p.startsWith(eq.schemaPath + '.') ||
|
|
3967
|
+
p.startsWith(eq.schemaPath + '['),
|
|
3968
|
+
);
|
|
3969
|
+
if (!hasSubPaths) {
|
|
3970
|
+
issues.push({
|
|
3971
|
+
type: 'dead_end',
|
|
3972
|
+
severity: 'warning',
|
|
3973
|
+
scope: scopeName,
|
|
3974
|
+
path,
|
|
3975
|
+
details: `Equivalency to "${eq.scopeNodeName}::${eq.schemaPath}" - path not in schema`,
|
|
3976
|
+
});
|
|
3977
|
+
}
|
|
3978
|
+
}
|
|
3979
|
+
}
|
|
3980
|
+
}
|
|
3981
|
+
|
|
3982
|
+
// Check 3: Trace chain depth
|
|
3983
|
+
const chain = this.traceEquivalencyChain(scopeName, path, 50);
|
|
3984
|
+
if (chain.length >= 20) {
|
|
3985
|
+
issues.push({
|
|
3986
|
+
type: 'long_chain',
|
|
3987
|
+
severity: 'warning',
|
|
3988
|
+
scope: scopeName,
|
|
3989
|
+
path,
|
|
3990
|
+
details: `Equivalency chain depth is ${chain.length} (may impact performance)`,
|
|
3991
|
+
});
|
|
3992
|
+
}
|
|
3993
|
+
|
|
3994
|
+
// Check for potential cycles (chain that returns to starting point)
|
|
3995
|
+
const visited = new Set<string>();
|
|
3996
|
+
for (const step of chain) {
|
|
3997
|
+
const key = `${step.scope}::${step.path}`;
|
|
3998
|
+
if (visited.has(key)) {
|
|
3999
|
+
issues.push({
|
|
4000
|
+
type: 'circular',
|
|
4001
|
+
severity: 'warning',
|
|
4002
|
+
scope: scopeName,
|
|
4003
|
+
path,
|
|
4004
|
+
details: `Circular reference detected at ${key}`,
|
|
4005
|
+
});
|
|
4006
|
+
break;
|
|
4007
|
+
}
|
|
4008
|
+
visited.add(key);
|
|
4009
|
+
}
|
|
4010
|
+
}
|
|
4011
|
+
}
|
|
4012
|
+
|
|
4013
|
+
return issues;
|
|
4014
|
+
}
|
|
2736
4015
|
}
|