octocode-cli 1.2.5 → 1.2.7
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/LICENSE +21 -63
- package/README.md +86 -109
- package/out/octocode-cli.js +7027 -7014
- package/package.json +8 -6
- package/skills/README.md +97 -120
- package/skills/octocode-code-engineer/.claude/settings.local.json +18 -0
- package/skills/octocode-code-engineer/.octocode/rfc/RFC-code-engineer-weakness-fixes.md +255 -0
- package/skills/octocode-code-engineer/.plan/VALIDATED_PLAN.md +223 -0
- package/skills/octocode-code-engineer/README.md +178 -0
- package/skills/octocode-code-engineer/SKILL.md +418 -0
- package/skills/octocode-code-engineer/coverage/architecture.ts.html +7828 -0
- package/skills/octocode-code-engineer/coverage/ast-helpers.ts.html +211 -0
- package/skills/octocode-code-engineer/coverage/ast-search.ts.html +1795 -0
- package/skills/octocode-code-engineer/coverage/base.css +224 -0
- package/skills/octocode-code-engineer/coverage/block-navigation.js +87 -0
- package/skills/octocode-code-engineer/coverage/cache.ts.html +376 -0
- package/skills/octocode-code-engineer/coverage/cli.ts.html +982 -0
- package/skills/octocode-code-engineer/coverage/clover.xml +3217 -0
- package/skills/octocode-code-engineer/coverage/collect-effects.ts.html +664 -0
- package/skills/octocode-code-engineer/coverage/collect-input-sources.ts.html +577 -0
- package/skills/octocode-code-engineer/coverage/collect-performance.ts.html +331 -0
- package/skills/octocode-code-engineer/coverage/collect-prototype-pollution.ts.html +421 -0
- package/skills/octocode-code-engineer/coverage/collect-security.ts.html +604 -0
- package/skills/octocode-code-engineer/coverage/collect-test-profile.ts.html +589 -0
- package/skills/octocode-code-engineer/coverage/coverage-final.json +30 -0
- package/skills/octocode-code-engineer/coverage/dependencies.ts.html +997 -0
- package/skills/octocode-code-engineer/coverage/dependency-summary.ts.html +688 -0
- package/skills/octocode-code-engineer/coverage/discovery.ts.html +322 -0
- package/skills/octocode-code-engineer/coverage/favicon.png +0 -0
- package/skills/octocode-code-engineer/coverage/graph-analytics.ts.html +1510 -0
- package/skills/octocode-code-engineer/coverage/index.html +536 -0
- package/skills/octocode-code-engineer/coverage/index.ts.html +826 -0
- package/skills/octocode-code-engineer/coverage/metrics.ts.html +553 -0
- package/skills/octocode-code-engineer/coverage/pipeline.ts.html +2044 -0
- package/skills/octocode-code-engineer/coverage/prettify.css +1 -0
- package/skills/octocode-code-engineer/coverage/prettify.js +2 -0
- package/skills/octocode-code-engineer/coverage/report-analysis.ts.html +1570 -0
- package/skills/octocode-code-engineer/coverage/report-writer.ts.html +1102 -0
- package/skills/octocode-code-engineer/coverage/security-detectors.ts.html +1747 -0
- package/skills/octocode-code-engineer/coverage/semantic-detectors.ts.html +2152 -0
- package/skills/octocode-code-engineer/coverage/semantic.ts.html +1897 -0
- package/skills/octocode-code-engineer/coverage/sort-arrow-sprite.png +0 -0
- package/skills/octocode-code-engineer/coverage/sorter.js +210 -0
- package/skills/octocode-code-engineer/coverage/summary-md.ts.html +1222 -0
- package/skills/octocode-code-engineer/coverage/test-quality-detectors.ts.html +1039 -0
- package/skills/octocode-code-engineer/coverage/tree-sitter-analyzer.ts.html +955 -0
- package/skills/octocode-code-engineer/coverage/ts-analyzer.ts.html +1213 -0
- package/skills/octocode-code-engineer/coverage/types.ts.html +2473 -0
- package/skills/octocode-code-engineer/coverage/utils.ts.html +820 -0
- package/skills/octocode-code-engineer/eslint.config.mjs +54 -0
- package/skills/octocode-code-engineer/minify-scripts.mjs +32 -0
- package/skills/octocode-code-engineer/package.json +54 -0
- package/skills/octocode-code-engineer/references/agent-ast-reading-rfc.md +95 -0
- package/skills/octocode-code-engineer/references/architecture-techniques.md +121 -0
- package/skills/octocode-code-engineer/references/ast-search.md +210 -0
- package/skills/octocode-code-engineer/references/ast-tree-search.md +151 -0
- package/skills/octocode-code-engineer/references/cli-reference.md +167 -0
- package/skills/octocode-code-engineer/references/concepts.md +107 -0
- package/skills/octocode-code-engineer/references/finding-categories.md +128 -0
- package/skills/octocode-code-engineer/references/improvement-roadmap.md +304 -0
- package/skills/octocode-code-engineer/references/output-files.md +144 -0
- package/skills/octocode-code-engineer/references/playbooks.md +204 -0
- package/skills/octocode-code-engineer/references/present-results.md +136 -0
- package/skills/octocode-code-engineer/references/tool-workflows.md +566 -0
- package/skills/octocode-code-engineer/references/validate-investigate.md +225 -0
- package/skills/octocode-code-engineer/scripts/analysis/dependencies.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/dependency-summary.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/discovery.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/graph-analytics.js +1 -0
- package/skills/octocode-code-engineer/scripts/analysis/semantic.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/helpers.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/metrics.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/search.js +2 -0
- package/skills/octocode-code-engineer/scripts/ast/tree-search.js +2 -0
- package/skills/octocode-code-engineer/scripts/ast/tree-sitter.js +1 -0
- package/skills/octocode-code-engineer/scripts/ast/ts-analyzer.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/chains.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/effects.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/input-sources.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/performance.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/prototype-pollution.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/security.js +1 -0
- package/skills/octocode-code-engineer/scripts/collectors/test-profile.js +1 -0
- package/skills/octocode-code-engineer/scripts/common/is-direct-run.js +1 -0
- package/skills/octocode-code-engineer/scripts/common/utils.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/code-quality.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/cohesion.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/coupling.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/cycle.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/dead-code.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/import-style.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/security.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/semantic.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/shared.js +1 -0
- package/skills/octocode-code-engineer/scripts/detectors/test-quality.js +1 -0
- package/skills/octocode-code-engineer/scripts/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/cache.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/cli.js +1 -0
- package/skills/octocode-code-engineer/scripts/pipeline/main.js +2 -0
- package/skills/octocode-code-engineer/scripts/reporting/analysis.js +1 -0
- package/skills/octocode-code-engineer/scripts/reporting/summary-md.js +1 -0
- package/skills/octocode-code-engineer/scripts/reporting/writer.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/constants.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/index.js +1 -0
- package/skills/octocode-code-engineer/scripts/types/interfaces.js +1 -0
- package/skills/octocode-code-engineer/src/analysis/dependencies.test.ts +545 -0
- package/skills/octocode-code-engineer/src/analysis/dependencies.ts +406 -0
- package/skills/octocode-code-engineer/src/analysis/dependency-summary.test.ts +566 -0
- package/skills/octocode-code-engineer/src/analysis/dependency-summary.ts +257 -0
- package/skills/octocode-code-engineer/src/analysis/discovery.test.ts +420 -0
- package/skills/octocode-code-engineer/src/analysis/discovery.ts +87 -0
- package/skills/octocode-code-engineer/src/analysis/graph-analytics.test.ts +449 -0
- package/skills/octocode-code-engineer/src/analysis/graph-analytics.ts +534 -0
- package/skills/octocode-code-engineer/src/analysis/semantic.test.ts +1533 -0
- package/skills/octocode-code-engineer/src/analysis/semantic.ts +830 -0
- package/skills/octocode-code-engineer/src/ast/helpers.test.ts +185 -0
- package/skills/octocode-code-engineer/src/ast/helpers.ts +62 -0
- package/skills/octocode-code-engineer/src/ast/metrics.test.ts +304 -0
- package/skills/octocode-code-engineer/src/ast/metrics.ts +204 -0
- package/skills/octocode-code-engineer/src/ast/search.test.ts +647 -0
- package/skills/octocode-code-engineer/src/ast/search.ts +648 -0
- package/skills/octocode-code-engineer/src/ast/tree-search.test.ts +199 -0
- package/skills/octocode-code-engineer/src/ast/tree-search.ts +392 -0
- package/skills/octocode-code-engineer/src/ast/tree-sitter.test.ts +407 -0
- package/skills/octocode-code-engineer/src/ast/tree-sitter.ts +402 -0
- package/skills/octocode-code-engineer/src/ast/ts-analyzer.test.ts +1864 -0
- package/skills/octocode-code-engineer/src/ast/ts-analyzer.ts +509 -0
- package/skills/octocode-code-engineer/src/collectors/chains.ts +74 -0
- package/skills/octocode-code-engineer/src/collectors/effects.test.ts +490 -0
- package/skills/octocode-code-engineer/src/collectors/effects.ts +332 -0
- package/skills/octocode-code-engineer/src/collectors/input-sources.test.ts +144 -0
- package/skills/octocode-code-engineer/src/collectors/input-sources.ts +196 -0
- package/skills/octocode-code-engineer/src/collectors/performance.test.ts +82 -0
- package/skills/octocode-code-engineer/src/collectors/performance.ts +141 -0
- package/skills/octocode-code-engineer/src/collectors/prototype-pollution.test.ts +55 -0
- package/skills/octocode-code-engineer/src/collectors/prototype-pollution.ts +162 -0
- package/skills/octocode-code-engineer/src/collectors/security.test.ts +124 -0
- package/skills/octocode-code-engineer/src/collectors/security.ts +309 -0
- package/skills/octocode-code-engineer/src/collectors/test-profile.test.ts +97 -0
- package/skills/octocode-code-engineer/src/collectors/test-profile.ts +269 -0
- package/skills/octocode-code-engineer/src/common/is-direct-run.test.ts +32 -0
- package/skills/octocode-code-engineer/src/common/is-direct-run.ts +13 -0
- package/skills/octocode-code-engineer/src/common/utils.test.ts +463 -0
- package/skills/octocode-code-engineer/src/common/utils.ts +304 -0
- package/skills/octocode-code-engineer/src/detectors/code-quality.ts +966 -0
- package/skills/octocode-code-engineer/src/detectors/cohesion.ts +539 -0
- package/skills/octocode-code-engineer/src/detectors/coupling.ts +323 -0
- package/skills/octocode-code-engineer/src/detectors/cycle.ts +349 -0
- package/skills/octocode-code-engineer/src/detectors/dead-code.ts +320 -0
- package/skills/octocode-code-engineer/src/detectors/import-style.ts +376 -0
- package/skills/octocode-code-engineer/src/detectors/index.test.ts +3061 -0
- package/skills/octocode-code-engineer/src/detectors/index.ts +88 -0
- package/skills/octocode-code-engineer/src/detectors/security.test.ts +882 -0
- package/skills/octocode-code-engineer/src/detectors/security.ts +821 -0
- package/skills/octocode-code-engineer/src/detectors/semantic.ts +758 -0
- package/skills/octocode-code-engineer/src/detectors/shared.ts +49 -0
- package/skills/octocode-code-engineer/src/detectors/test-quality.test.ts +388 -0
- package/skills/octocode-code-engineer/src/detectors/test-quality.ts +367 -0
- package/skills/octocode-code-engineer/src/index.test.ts +4425 -0
- package/skills/octocode-code-engineer/src/index.ts +403 -0
- package/skills/octocode-code-engineer/src/pipeline/cache.test.ts +199 -0
- package/skills/octocode-code-engineer/src/pipeline/cache.ts +130 -0
- package/skills/octocode-code-engineer/src/pipeline/cli.test.ts +493 -0
- package/skills/octocode-code-engineer/src/pipeline/cli.ts +344 -0
- package/skills/octocode-code-engineer/src/pipeline/main.test.ts +174 -0
- package/skills/octocode-code-engineer/src/pipeline/main.ts +1074 -0
- package/skills/octocode-code-engineer/src/pipeline.test.ts +84 -0
- package/skills/octocode-code-engineer/src/reporting/analysis.test.ts +782 -0
- package/skills/octocode-code-engineer/src/reporting/analysis.ts +688 -0
- package/skills/octocode-code-engineer/src/reporting/output-contract.test.ts +463 -0
- package/skills/octocode-code-engineer/src/reporting/summary-md.test.ts +421 -0
- package/skills/octocode-code-engineer/src/reporting/summary-md.ts +714 -0
- package/skills/octocode-code-engineer/src/reporting/writer.ts +430 -0
- package/skills/octocode-code-engineer/src/sanity.test.ts +47 -0
- package/skills/octocode-code-engineer/src/types/constants.ts +248 -0
- package/skills/octocode-code-engineer/src/types/index.ts +80 -0
- package/skills/octocode-code-engineer/src/types/interfaces.ts +682 -0
- package/skills/octocode-code-engineer/tsconfig.json +17 -0
- package/skills/octocode-code-engineer/vitest.config.ts +8 -0
- package/skills/octocode-documentation-writer/README.md +113 -0
- package/skills/octocode-documentation-writer/SKILL.md +886 -0
- package/skills/octocode-documentation-writer/references/agent-discovery-analysis.md +453 -0
- package/skills/octocode-documentation-writer/references/agent-documentation-writer.md +255 -0
- package/skills/octocode-documentation-writer/references/agent-engineer-questions.md +247 -0
- package/skills/octocode-documentation-writer/references/agent-orchestrator.md +370 -0
- package/skills/octocode-documentation-writer/references/agent-qa-validator.md +227 -0
- package/skills/octocode-documentation-writer/references/agent-researcher.md +250 -0
- package/skills/octocode-documentation-writer/schemas/analysis-schema.json +886 -0
- package/skills/octocode-documentation-writer/schemas/discovery-tasks.json +96 -0
- package/skills/octocode-documentation-writer/schemas/documentation-structure.json +373 -0
- package/skills/octocode-documentation-writer/schemas/partial-discovery-schema.json +102 -0
- package/skills/octocode-documentation-writer/schemas/partial-research-schema.json +98 -0
- package/skills/octocode-documentation-writer/schemas/qa-results-schema.json +113 -0
- package/skills/octocode-documentation-writer/schemas/questions-schema.json +228 -0
- package/skills/octocode-documentation-writer/schemas/research-schema.json +104 -0
- package/skills/octocode-documentation-writer/schemas/state-schema.json +222 -0
- package/skills/octocode-documentation-writer/schemas/work-assignments-schema.json +74 -0
- package/skills/octocode-plan/SKILL.md +122 -116
- package/skills/octocode-prompt-optimizer/SKILL.md +617 -0
- package/skills/octocode-pull-request-reviewer/README.md +249 -0
- package/skills/octocode-pull-request-reviewer/SKILL.md +479 -0
- package/skills/octocode-pull-request-reviewer/references/dependency-check.md +74 -0
- package/skills/octocode-pull-request-reviewer/references/domain-reviewers.md +24 -0
- package/skills/octocode-pull-request-reviewer/references/execution-lifecycle.md +441 -0
- package/skills/octocode-pull-request-reviewer/references/flow-analysis-protocol.md +64 -0
- package/skills/octocode-pull-request-reviewer/references/output-template.md +174 -0
- package/skills/octocode-pull-request-reviewer/references/parallel-agent-protocol.md +182 -0
- package/skills/octocode-pull-request-reviewer/references/review-guidelines.md +26 -0
- package/skills/octocode-pull-request-reviewer/references/verification-checklist.md +40 -0
- package/skills/octocode-research/.claude/settings.local.json +46 -0
- package/skills/octocode-research/.octocode/plan/code-review-fixes/plan.md +312 -0
- package/skills/octocode-research/.octocode/plan/code-review-fixes/research.md +212 -0
- package/skills/octocode-research/.octocode/plans/NODE_SERVER_START_PLAN.md +755 -0
- package/skills/octocode-research/.octocode/research/code-review/research.md +371 -0
- package/skills/octocode-research/.octocode/review/IMPROVEMENTS.md +391 -0
- package/skills/octocode-research/.octocode/review/REVIEW_PLAN.md +289 -0
- package/skills/octocode-research/.octocode/review/REVIEW_REPORT.md +356 -0
- package/skills/octocode-research/AGENTS.md +349 -0
- package/skills/octocode-research/README.md +494 -0
- package/skills/octocode-research/SKILL.md +652 -274
- package/skills/octocode-research/docs/API_REFERENCE.md +562 -0
- package/skills/octocode-research/docs/ARCHITECTURE.md +554 -0
- package/skills/octocode-research/docs/FLOWS.md +577 -0
- package/skills/octocode-research/docs/OVERVIEW.md +564 -0
- package/skills/octocode-research/docs/SERVER_FLOWS.md +631 -0
- package/skills/octocode-research/ecosystem.config.cjs +88 -0
- package/skills/octocode-research/eslint.config.mjs +27 -0
- package/skills/octocode-research/package.json +84 -0
- package/skills/octocode-research/references/GUARDRAILS.md +40 -0
- package/skills/octocode-research/references/PARALLEL_AGENT_PROTOCOL.md +178 -0
- package/skills/octocode-research/references/roast-prompt.md +149 -0
- package/skills/octocode-research/scripts/server-init.d.ts +2 -0
- package/skills/octocode-research/scripts/server-init.js +2 -0
- package/skills/octocode-research/scripts/server.d.ts +8 -0
- package/skills/octocode-research/scripts/server.js +445 -0
- package/skills/octocode-research/src/__tests__/integration/circuitBreaker.test.ts +205 -0
- package/skills/octocode-research/src/__tests__/integration/routes.test.ts +374 -0
- package/skills/octocode-research/src/__tests__/unit/circuitBreaker.test.ts +245 -0
- package/skills/octocode-research/src/__tests__/unit/errorHandler.test.ts +183 -0
- package/skills/octocode-research/src/__tests__/unit/httpPreprocess.test.ts +157 -0
- package/skills/octocode-research/src/__tests__/unit/logger.test.ts +143 -0
- package/skills/octocode-research/src/__tests__/unit/queryParser.test.ts +130 -0
- package/skills/octocode-research/src/__tests__/unit/responseBuilder.test.ts +469 -0
- package/skills/octocode-research/src/__tests__/unit/retry.test.ts +205 -0
- package/skills/octocode-research/src/index.ts +186 -0
- package/skills/octocode-research/src/mcpCache.ts +49 -0
- package/skills/octocode-research/src/middleware/errorHandler.ts +65 -0
- package/skills/octocode-research/src/middleware/logger.ts +61 -0
- package/skills/octocode-research/src/middleware/queryParser.ts +115 -0
- package/skills/octocode-research/src/middleware/readiness.ts +17 -0
- package/skills/octocode-research/src/routes/github.ts +197 -0
- package/skills/octocode-research/src/routes/local.ts +175 -0
- package/skills/octocode-research/src/routes/lsp.ts +177 -0
- package/skills/octocode-research/src/routes/package.ts +127 -0
- package/skills/octocode-research/src/routes/prompts.ts +138 -0
- package/skills/octocode-research/src/routes/tools.ts +677 -0
- package/skills/octocode-research/src/server-init.ts +363 -0
- package/skills/octocode-research/src/server.ts +285 -0
- package/skills/octocode-research/src/types/errorGuards.ts +151 -0
- package/skills/octocode-research/src/types/express.d.ts +76 -0
- package/skills/octocode-research/src/types/guards.ts +98 -0
- package/skills/octocode-research/src/types/mcp.ts +119 -0
- package/skills/octocode-research/src/types/responses.ts +199 -0
- package/skills/octocode-research/src/types/toolTypes.ts +33 -0
- package/skills/octocode-research/src/utils/asyncTimeout.ts +116 -0
- package/skills/octocode-research/src/utils/circuitBreaker.ts +492 -0
- package/skills/octocode-research/src/utils/colors.ts +53 -0
- package/skills/octocode-research/src/utils/errorQueue.ts +71 -0
- package/skills/octocode-research/src/utils/logEmoji.ts +103 -0
- package/skills/octocode-research/src/utils/logger.ts +413 -0
- package/skills/octocode-research/src/utils/resilience.ts +169 -0
- package/skills/octocode-research/src/utils/responseBuilder.ts +495 -0
- package/skills/octocode-research/src/utils/responseFactory.ts +100 -0
- package/skills/octocode-research/src/utils/responseParser.ts +272 -0
- package/skills/octocode-research/src/utils/retry.ts +280 -0
- package/skills/octocode-research/src/utils/routeFactory.ts +117 -0
- package/skills/octocode-research/src/utils/url.ts +20 -0
- package/skills/octocode-research/src/validation/httpPreprocess.ts +155 -0
- package/skills/octocode-research/src/validation/index.ts +2 -0
- package/skills/octocode-research/src/validation/schemas.ts +578 -0
- package/skills/octocode-research/src/validation/toolCallSchema.ts +132 -0
- package/skills/octocode-research/tsconfig.json +21 -0
- package/skills/octocode-research/tsdown.config.ts +42 -0
- package/skills/octocode-research/vitest.config.ts +20 -0
- package/skills/octocode-researcher/SKILL.md +461 -0
- package/skills/octocode-researcher/references/fallbacks.md +120 -0
- package/skills/{octocode-local-search → octocode-researcher}/references/tool-reference.md +132 -49
- package/skills/{octocode-local-search → octocode-researcher}/references/workflow-patterns.md +204 -4
- package/skills/octocode-rfc-generator/SKILL.md +223 -0
- package/skills/octocode-rfc-generator/references/rfc-template.md +193 -0
- package/skills/octocode-roast/SKILL.md +63 -21
- package/skills/octocode-implement/SKILL.md +0 -293
- package/skills/octocode-implement/references/execution-phases.md +0 -317
- package/skills/octocode-implement/references/tool-reference.md +0 -403
- package/skills/octocode-implement/references/workflow-patterns.md +0 -385
- package/skills/octocode-local-search/SKILL.md +0 -449
- package/skills/octocode-pr-review/SKILL.md +0 -391
- package/skills/octocode-pr-review/references/domain-reviewers.md +0 -105
- package/skills/octocode-pr-review/references/execution-lifecycle.md +0 -116
- package/skills/octocode-pr-review/references/research-flows.md +0 -75
- package/skills/octocode-research/references/tool-reference.md +0 -304
- package/skills/octocode-research/references/workflow-patterns.md +0 -325
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import * as ts from 'typescript';
|
|
5
|
+
|
|
6
|
+
import { isTestFile } from '../common/utils.js';
|
|
7
|
+
|
|
8
|
+
import type { DependencyState, FileEntry } from '../types/index.js';
|
|
9
|
+
|
|
10
|
+
export interface SemanticContext {
|
|
11
|
+
service: ts.LanguageService;
|
|
12
|
+
checker: ts.TypeChecker;
|
|
13
|
+
program: ts.Program;
|
|
14
|
+
root: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface UnusedParam {
|
|
18
|
+
functionName: string;
|
|
19
|
+
paramName: string;
|
|
20
|
+
lineStart: number;
|
|
21
|
+
lineEnd: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface InterfaceImpl {
|
|
25
|
+
interfaceName: string;
|
|
26
|
+
className: string;
|
|
27
|
+
classFile: string;
|
|
28
|
+
classLine: number;
|
|
29
|
+
missingMembers: string[];
|
|
30
|
+
anycastMembers: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TypeHierarchyInfo {
|
|
34
|
+
name: string;
|
|
35
|
+
depth: number;
|
|
36
|
+
chain: string[];
|
|
37
|
+
lineStart: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface OverrideChainInfo {
|
|
41
|
+
methodName: string;
|
|
42
|
+
className: string;
|
|
43
|
+
depth: number;
|
|
44
|
+
chain: string[];
|
|
45
|
+
lineStart: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ExportRefInfo {
|
|
49
|
+
count: number;
|
|
50
|
+
uniqueFiles: number;
|
|
51
|
+
lineStart: number;
|
|
52
|
+
lineEnd: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface LeakyReturn {
|
|
56
|
+
functionName: string;
|
|
57
|
+
returnType: string;
|
|
58
|
+
sourceFile: string;
|
|
59
|
+
lineStart: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface NarrowableParam {
|
|
63
|
+
functionName: string;
|
|
64
|
+
paramName: string;
|
|
65
|
+
declaredType: string;
|
|
66
|
+
actualTypes: string[];
|
|
67
|
+
narrowedType: string;
|
|
68
|
+
lineStart: number;
|
|
69
|
+
lineEnd: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface SemanticProfile {
|
|
73
|
+
file: string;
|
|
74
|
+
referenceCountByExport: Map<string, ExportRefInfo>;
|
|
75
|
+
unusedParams: UnusedParam[];
|
|
76
|
+
interfaceImpls: InterfaceImpl[];
|
|
77
|
+
typeHierarchyDepth: number;
|
|
78
|
+
typeHierarchies: TypeHierarchyInfo[];
|
|
79
|
+
overrideChains: OverrideChainInfo[];
|
|
80
|
+
abstractnessRatio: number;
|
|
81
|
+
unusedImports: Array<{ name: string; lineStart: number }>;
|
|
82
|
+
concreteImports: Array<{
|
|
83
|
+
name: string;
|
|
84
|
+
targetFile: string;
|
|
85
|
+
lineStart: number;
|
|
86
|
+
}>;
|
|
87
|
+
leakyReturns: LeakyReturn[];
|
|
88
|
+
narrowableParams: NarrowableParam[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function findTsConfig(root: string): ts.ParsedCommandLine | null {
|
|
92
|
+
const configPath = ts.findConfigFile(
|
|
93
|
+
root,
|
|
94
|
+
ts.sys.fileExists,
|
|
95
|
+
'tsconfig.json'
|
|
96
|
+
);
|
|
97
|
+
if (!configPath) return null;
|
|
98
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
99
|
+
if (configFile.error) return null;
|
|
100
|
+
return ts.parseJsonConfigFileContent(
|
|
101
|
+
configFile.config,
|
|
102
|
+
ts.sys,
|
|
103
|
+
path.dirname(configPath)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createSemanticContext(
|
|
108
|
+
files: string[],
|
|
109
|
+
root: string
|
|
110
|
+
): SemanticContext {
|
|
111
|
+
const parsed = findTsConfig(root);
|
|
112
|
+
const compilerOptions: ts.CompilerOptions = parsed?.options ?? {
|
|
113
|
+
target: ts.ScriptTarget.ES2022,
|
|
114
|
+
module: ts.ModuleKind.ESNext,
|
|
115
|
+
moduleResolution: ts.ModuleResolutionKind.Node10,
|
|
116
|
+
strict: true,
|
|
117
|
+
esModuleInterop: true,
|
|
118
|
+
skipLibCheck: true,
|
|
119
|
+
allowJs: true,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const fileSet = new Set(files);
|
|
123
|
+
const fileContents = new Map<string, string>();
|
|
124
|
+
for (const f of files) {
|
|
125
|
+
try {
|
|
126
|
+
fileContents.set(f, fs.readFileSync(f, 'utf8'));
|
|
127
|
+
} catch {
|
|
128
|
+
void 0;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const host: ts.LanguageServiceHost = {
|
|
133
|
+
getScriptFileNames: () => [...fileSet],
|
|
134
|
+
getScriptVersion: () => '1',
|
|
135
|
+
getScriptSnapshot: fileName => {
|
|
136
|
+
const content = fileContents.get(fileName);
|
|
137
|
+
if (content != null) return ts.ScriptSnapshot.fromString(content);
|
|
138
|
+
try {
|
|
139
|
+
const text = fs.readFileSync(fileName, 'utf8');
|
|
140
|
+
return ts.ScriptSnapshot.fromString(text);
|
|
141
|
+
} catch {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
getCurrentDirectory: () => root,
|
|
146
|
+
getCompilationSettings: () => compilerOptions,
|
|
147
|
+
getDefaultLibFileName: ts.getDefaultLibFilePath,
|
|
148
|
+
fileExists: ts.sys.fileExists,
|
|
149
|
+
readFile: ts.sys.readFile,
|
|
150
|
+
readDirectory: ts.sys.readDirectory,
|
|
151
|
+
directoryExists: ts.sys.directoryExists,
|
|
152
|
+
getDirectories: ts.sys.getDirectories,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const service = ts.createLanguageService(host, ts.createDocumentRegistry());
|
|
156
|
+
const program = service.getProgram()!;
|
|
157
|
+
const checker = program.getTypeChecker();
|
|
158
|
+
|
|
159
|
+
return { service, checker, program, root };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function getExportReferenceInfo(
|
|
163
|
+
ctx: SemanticContext,
|
|
164
|
+
filePath: string,
|
|
165
|
+
exportName: string,
|
|
166
|
+
exportLine: number,
|
|
167
|
+
includeTests: boolean
|
|
168
|
+
): { count: number; uniqueFiles: number } {
|
|
169
|
+
const sourceFile = ctx.program.getSourceFile(filePath);
|
|
170
|
+
if (!sourceFile) return { count: -1, uniqueFiles: 0 };
|
|
171
|
+
|
|
172
|
+
const node = findSymbolAtLine(sourceFile, exportName, exportLine);
|
|
173
|
+
if (!node) return { count: -1, uniqueFiles: 0 };
|
|
174
|
+
|
|
175
|
+
const refs = ctx.service.findReferences(filePath, node.getStart(sourceFile));
|
|
176
|
+
if (!refs) return { count: 0, uniqueFiles: 0 };
|
|
177
|
+
|
|
178
|
+
let count = 0;
|
|
179
|
+
const files = new Set<string>();
|
|
180
|
+
for (const group of refs) {
|
|
181
|
+
for (const ref of group.references) {
|
|
182
|
+
if (ref.isDefinition) continue;
|
|
183
|
+
if (!includeTests && isTestFile(ref.fileName)) continue;
|
|
184
|
+
count++;
|
|
185
|
+
files.add(ref.fileName);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return { count, uniqueFiles: files.size };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function findSymbolAtLine(
|
|
192
|
+
sourceFile: ts.SourceFile,
|
|
193
|
+
name: string,
|
|
194
|
+
line: number
|
|
195
|
+
): ts.Node | undefined {
|
|
196
|
+
const lineStart = sourceFile.getPositionOfLineAndCharacter(
|
|
197
|
+
Math.max(0, line - 1),
|
|
198
|
+
0
|
|
199
|
+
);
|
|
200
|
+
const lineEnd =
|
|
201
|
+
line <
|
|
202
|
+
sourceFile.getLineAndCharacterOfPosition(sourceFile.getEnd()).line + 1
|
|
203
|
+
? sourceFile.getPositionOfLineAndCharacter(line, 0)
|
|
204
|
+
: sourceFile.getEnd();
|
|
205
|
+
|
|
206
|
+
let found: ts.Node | undefined;
|
|
207
|
+
const visit = (node: ts.Node): void => {
|
|
208
|
+
if (found) return;
|
|
209
|
+
if (node.getStart(sourceFile) >= lineStart && node.getEnd() <= lineEnd) {
|
|
210
|
+
if (ts.isIdentifier(node) && node.text === name) {
|
|
211
|
+
found = node;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
ts.forEachChild(node, visit);
|
|
216
|
+
};
|
|
217
|
+
ts.forEachChild(sourceFile, visit);
|
|
218
|
+
return found;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function getInheritanceDepth(
|
|
222
|
+
checker: ts.TypeChecker,
|
|
223
|
+
type: ts.Type
|
|
224
|
+
): { depth: number; chain: string[] } {
|
|
225
|
+
const chain: string[] = [];
|
|
226
|
+
let current = type;
|
|
227
|
+
let depth = 0;
|
|
228
|
+
const seen = new Set<string>();
|
|
229
|
+
|
|
230
|
+
while (true) {
|
|
231
|
+
const bases = current.getBaseTypes?.() ?? [];
|
|
232
|
+
if (bases.length === 0) break;
|
|
233
|
+
const base = bases[0];
|
|
234
|
+
const name = checker.typeToString(base);
|
|
235
|
+
if (seen.has(name)) break;
|
|
236
|
+
seen.add(name);
|
|
237
|
+
chain.push(name);
|
|
238
|
+
depth++;
|
|
239
|
+
current = base;
|
|
240
|
+
if (depth > 20) break;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return { depth, chain };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function collectExportReferences(
|
|
247
|
+
ctx: SemanticContext,
|
|
248
|
+
filePath: string,
|
|
249
|
+
fileEntry: FileEntry,
|
|
250
|
+
includeTests: boolean
|
|
251
|
+
): Map<string, ExportRefInfo> {
|
|
252
|
+
const result = new Map<string, ExportRefInfo>();
|
|
253
|
+
const exports = fileEntry.dependencyProfile?.declaredExports ?? [];
|
|
254
|
+
for (const exp of exports) {
|
|
255
|
+
if (exp.lineStart == null) continue;
|
|
256
|
+
const refInfo = getExportReferenceInfo(
|
|
257
|
+
ctx,
|
|
258
|
+
filePath,
|
|
259
|
+
exp.name,
|
|
260
|
+
exp.lineStart,
|
|
261
|
+
includeTests
|
|
262
|
+
);
|
|
263
|
+
result.set(exp.name, {
|
|
264
|
+
count: refInfo.count,
|
|
265
|
+
uniqueFiles: refInfo.uniqueFiles,
|
|
266
|
+
lineStart: exp.lineStart,
|
|
267
|
+
lineEnd: exp.lineEnd ?? exp.lineStart,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function findFunctionNode(
|
|
274
|
+
sourceFile: ts.SourceFile,
|
|
275
|
+
fnName: string,
|
|
276
|
+
lineStart: number,
|
|
277
|
+
lineEnd: number
|
|
278
|
+
): ts.FunctionLikeDeclaration | undefined {
|
|
279
|
+
const fnPos = sourceFile.getPositionOfLineAndCharacter(
|
|
280
|
+
Math.max(0, lineStart - 1),
|
|
281
|
+
0
|
|
282
|
+
);
|
|
283
|
+
let fnNode: ts.Node | undefined;
|
|
284
|
+
const findFn = (node: ts.Node): void => {
|
|
285
|
+
if (fnNode) return;
|
|
286
|
+
if (
|
|
287
|
+
node.getStart(sourceFile) >= fnPos &&
|
|
288
|
+
node.getEnd() <=
|
|
289
|
+
sourceFile.getPositionOfLineAndCharacter(
|
|
290
|
+
Math.min(
|
|
291
|
+
lineEnd,
|
|
292
|
+
sourceFile.getLineAndCharacterOfPosition(sourceFile.getEnd())
|
|
293
|
+
.line + 1
|
|
294
|
+
) - 1,
|
|
295
|
+
0
|
|
296
|
+
) +
|
|
297
|
+
200
|
|
298
|
+
) {
|
|
299
|
+
if (
|
|
300
|
+
ts.isFunctionDeclaration(node) ||
|
|
301
|
+
ts.isMethodDeclaration(node) ||
|
|
302
|
+
ts.isArrowFunction(node) ||
|
|
303
|
+
ts.isFunctionExpression(node)
|
|
304
|
+
) {
|
|
305
|
+
const name =
|
|
306
|
+
node.name && ts.isIdentifier(node.name) ? node.name.text : '';
|
|
307
|
+
if (name === fnName || fnName === '<anonymous>') {
|
|
308
|
+
fnNode = node;
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
ts.forEachChild(node, findFn);
|
|
314
|
+
};
|
|
315
|
+
ts.forEachChild(sourceFile, findFn);
|
|
316
|
+
return fnNode as ts.FunctionLikeDeclaration | undefined;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function collectUnusedParams(
|
|
320
|
+
ctx: SemanticContext,
|
|
321
|
+
filePath: string,
|
|
322
|
+
sourceFile: ts.SourceFile,
|
|
323
|
+
fileEntry: FileEntry
|
|
324
|
+
): UnusedParam[] {
|
|
325
|
+
const results: UnusedParam[] = [];
|
|
326
|
+
for (const fn of fileEntry.functions) {
|
|
327
|
+
if (!fn.params || fn.params === 0) continue;
|
|
328
|
+
if (fn.name === '<anonymous>' || fn.name === '') continue;
|
|
329
|
+
|
|
330
|
+
const fnNode = findFunctionNode(sourceFile, fn.name, fn.lineStart, fn.lineEnd);
|
|
331
|
+
if (
|
|
332
|
+
!fnNode ||
|
|
333
|
+
!(
|
|
334
|
+
ts.isFunctionDeclaration(fnNode) ||
|
|
335
|
+
ts.isMethodDeclaration(fnNode) ||
|
|
336
|
+
ts.isArrowFunction(fnNode) ||
|
|
337
|
+
ts.isFunctionExpression(fnNode)
|
|
338
|
+
)
|
|
339
|
+
)
|
|
340
|
+
continue;
|
|
341
|
+
|
|
342
|
+
for (const param of fnNode.parameters) {
|
|
343
|
+
if (!ts.isIdentifier(param.name)) continue;
|
|
344
|
+
const paramName = param.name.text;
|
|
345
|
+
if (paramName.startsWith('_')) continue;
|
|
346
|
+
|
|
347
|
+
const refs = ctx.service.findReferences(
|
|
348
|
+
filePath,
|
|
349
|
+
param.name.getStart(sourceFile)
|
|
350
|
+
);
|
|
351
|
+
let usageCount = 0;
|
|
352
|
+
if (refs) {
|
|
353
|
+
for (const group of refs) {
|
|
354
|
+
for (const ref of group.references) {
|
|
355
|
+
if (!ref.isDefinition) usageCount++;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (usageCount === 0) {
|
|
360
|
+
results.push({
|
|
361
|
+
functionName: fn.name,
|
|
362
|
+
paramName,
|
|
363
|
+
lineStart: fn.lineStart,
|
|
364
|
+
lineEnd: fn.lineEnd,
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return results;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function analyzeTypeHierarchy(
|
|
373
|
+
ctx: SemanticContext,
|
|
374
|
+
sourceFile: ts.SourceFile,
|
|
375
|
+
fileEntry: FileEntry,
|
|
376
|
+
profile: SemanticProfile
|
|
377
|
+
): number {
|
|
378
|
+
let abstractCount = 0;
|
|
379
|
+
let totalExportTypes = 0;
|
|
380
|
+
const exports = fileEntry.dependencyProfile?.declaredExports ?? [];
|
|
381
|
+
|
|
382
|
+
const visit = (node: ts.Node): void => {
|
|
383
|
+
if (ts.isClassDeclaration(node) && node.name) {
|
|
384
|
+
const type = ctx.checker.getTypeAtLocation(node);
|
|
385
|
+
const { depth, chain } = getInheritanceDepth(ctx.checker, type);
|
|
386
|
+
if (depth > 0) {
|
|
387
|
+
profile.typeHierarchies.push({
|
|
388
|
+
name: node.name.text,
|
|
389
|
+
depth,
|
|
390
|
+
chain,
|
|
391
|
+
lineStart:
|
|
392
|
+
sourceFile.getLineAndCharacterOfPosition(
|
|
393
|
+
node.getStart(sourceFile)
|
|
394
|
+
).line + 1,
|
|
395
|
+
});
|
|
396
|
+
if (depth > profile.typeHierarchyDepth) {
|
|
397
|
+
profile.typeHierarchyDepth = depth;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (node.heritageClauses) {
|
|
402
|
+
for (const clause of node.heritageClauses) {
|
|
403
|
+
if (clause.token === ts.SyntaxKind.ImplementsKeyword) {
|
|
404
|
+
for (const expr of clause.types) {
|
|
405
|
+
const ifaceType = ctx.checker.getTypeAtLocation(expr);
|
|
406
|
+
const ifaceName = ctx.checker.typeToString(ifaceType);
|
|
407
|
+
const classType = ctx.checker.getTypeAtLocation(node);
|
|
408
|
+
const ifaceProps = ifaceType.getProperties?.() ?? [];
|
|
409
|
+
const classProps = new Set(
|
|
410
|
+
(classType.getProperties?.() ?? []).map(p => p.name)
|
|
411
|
+
);
|
|
412
|
+
const missing = ifaceProps
|
|
413
|
+
.filter(p => !classProps.has(p.name))
|
|
414
|
+
.map(p => p.name);
|
|
415
|
+
|
|
416
|
+
const anycastMembers: string[] = [];
|
|
417
|
+
for (const prop of ifaceProps) {
|
|
418
|
+
if (classProps.has(prop.name)) {
|
|
419
|
+
const classProp = classType.getProperty?.(prop.name);
|
|
420
|
+
if (classProp) {
|
|
421
|
+
const classPropType = ctx.checker.getTypeOfSymbolAtLocation(
|
|
422
|
+
classProp,
|
|
423
|
+
node
|
|
424
|
+
);
|
|
425
|
+
const typeStr = ctx.checker.typeToString(classPropType);
|
|
426
|
+
if (typeStr === 'any') anycastMembers.push(prop.name);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (missing.length > 0 || anycastMembers.length > 0) {
|
|
432
|
+
profile.interfaceImpls.push({
|
|
433
|
+
interfaceName: ifaceName,
|
|
434
|
+
className: node.name!.text,
|
|
435
|
+
classFile: fileEntry.file,
|
|
436
|
+
classLine:
|
|
437
|
+
sourceFile.getLineAndCharacterOfPosition(
|
|
438
|
+
node.getStart(sourceFile)
|
|
439
|
+
).line + 1,
|
|
440
|
+
missingMembers: missing,
|
|
441
|
+
anycastMembers,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (node.modifiers?.some(m => m.kind === ts.SyntaxKind.AbstractKeyword)) {
|
|
450
|
+
abstractCount++;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const baseMethods = new Map<string, number>();
|
|
454
|
+
const classType = ctx.checker.getTypeAtLocation(node);
|
|
455
|
+
let currentType = classType;
|
|
456
|
+
let baseDepth = 0;
|
|
457
|
+
while (true) {
|
|
458
|
+
const bases = currentType.getBaseTypes?.() ?? [];
|
|
459
|
+
if (bases.length === 0) break;
|
|
460
|
+
baseDepth++;
|
|
461
|
+
const baseType = bases[0];
|
|
462
|
+
for (const prop of baseType.getProperties?.() ?? []) {
|
|
463
|
+
const decl = prop.getDeclarations?.()?.[0];
|
|
464
|
+
if (
|
|
465
|
+
decl &&
|
|
466
|
+
(ts.isMethodDeclaration(decl) || ts.isMethodSignature(decl))
|
|
467
|
+
) {
|
|
468
|
+
baseMethods.set(
|
|
469
|
+
prop.name,
|
|
470
|
+
Math.max(baseMethods.get(prop.name) ?? 0, baseDepth)
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
currentType = baseType;
|
|
475
|
+
if (baseDepth > 20) break;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
for (const member of node.members) {
|
|
479
|
+
if (
|
|
480
|
+
ts.isMethodDeclaration(member) &&
|
|
481
|
+
member.name &&
|
|
482
|
+
ts.isIdentifier(member.name)
|
|
483
|
+
) {
|
|
484
|
+
const methodName = member.name.text;
|
|
485
|
+
const overrideDepth = baseMethods.get(methodName);
|
|
486
|
+
if (overrideDepth != null && overrideDepth > 0) {
|
|
487
|
+
profile.overrideChains.push({
|
|
488
|
+
methodName,
|
|
489
|
+
className: node.name!.text,
|
|
490
|
+
depth: overrideDepth,
|
|
491
|
+
chain: [],
|
|
492
|
+
lineStart:
|
|
493
|
+
sourceFile.getLineAndCharacterOfPosition(
|
|
494
|
+
member.getStart(sourceFile)
|
|
495
|
+
).line + 1,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (ts.isInterfaceDeclaration(node)) {
|
|
503
|
+
abstractCount++;
|
|
504
|
+
totalExportTypes++;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (ts.isTypeAliasDeclaration(node)) {
|
|
508
|
+
totalExportTypes++;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
ts.forEachChild(node, visit);
|
|
512
|
+
};
|
|
513
|
+
ts.forEachChild(sourceFile, visit);
|
|
514
|
+
|
|
515
|
+
totalExportTypes += exports.length;
|
|
516
|
+
return totalExportTypes > 0 ? abstractCount / totalExportTypes : 0;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function analyzeImports(
|
|
520
|
+
ctx: SemanticContext,
|
|
521
|
+
filePath: string,
|
|
522
|
+
sourceFile: ts.SourceFile,
|
|
523
|
+
fileEntry: FileEntry
|
|
524
|
+
): {
|
|
525
|
+
unusedImports: Array<{ name: string; lineStart: number }>;
|
|
526
|
+
concreteImports: Array<{ name: string; targetFile: string; lineStart: number }>;
|
|
527
|
+
} {
|
|
528
|
+
const unusedImports: Array<{ name: string; lineStart: number }> = [];
|
|
529
|
+
const concreteImports: Array<{ name: string; targetFile: string; lineStart: number }> = [];
|
|
530
|
+
|
|
531
|
+
const imports = fileEntry.dependencyProfile?.importedSymbols ?? [];
|
|
532
|
+
for (const imp of imports) {
|
|
533
|
+
if (imp.lineStart == null) continue;
|
|
534
|
+
const node = findSymbolAtLine(sourceFile, imp.localName, imp.lineStart);
|
|
535
|
+
if (!node) continue;
|
|
536
|
+
|
|
537
|
+
const refs = ctx.service.findReferences(
|
|
538
|
+
filePath,
|
|
539
|
+
node.getStart(sourceFile)
|
|
540
|
+
);
|
|
541
|
+
let sameFileUsageCount = 0;
|
|
542
|
+
if (refs) {
|
|
543
|
+
for (const group of refs) {
|
|
544
|
+
for (const ref of group.references) {
|
|
545
|
+
if (ref.isDefinition) continue;
|
|
546
|
+
if (ref.fileName !== filePath) continue;
|
|
547
|
+
sameFileUsageCount++;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (sameFileUsageCount === 0) {
|
|
553
|
+
unusedImports.push({ name: imp.localName, lineStart: imp.lineStart });
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (imp.resolvedModule) {
|
|
557
|
+
const targetSrc = ctx.program.getSourceFile(
|
|
558
|
+
path.resolve(ctx.root, imp.resolvedModule)
|
|
559
|
+
);
|
|
560
|
+
if (targetSrc) {
|
|
561
|
+
const sym = ctx.checker.getSymbolAtLocation(node);
|
|
562
|
+
if (sym) {
|
|
563
|
+
const aliased =
|
|
564
|
+
sym.flags & ts.SymbolFlags.Alias
|
|
565
|
+
? ctx.checker.getAliasedSymbol(sym)
|
|
566
|
+
: sym;
|
|
567
|
+
const decl = aliased.getDeclarations?.()?.[0];
|
|
568
|
+
if (decl && ts.isClassDeclaration(decl)) {
|
|
569
|
+
const isAbstract = decl.modifiers?.some(
|
|
570
|
+
m => m.kind === ts.SyntaxKind.AbstractKeyword
|
|
571
|
+
);
|
|
572
|
+
if (!isAbstract) {
|
|
573
|
+
concreteImports.push({
|
|
574
|
+
name: imp.localName,
|
|
575
|
+
targetFile: imp.resolvedModule,
|
|
576
|
+
lineStart: imp.lineStart,
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return { unusedImports, concreteImports };
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function detectLeakyReturns(
|
|
588
|
+
ctx: SemanticContext,
|
|
589
|
+
filePath: string,
|
|
590
|
+
sourceFile: ts.SourceFile,
|
|
591
|
+
fileEntry: FileEntry
|
|
592
|
+
): LeakyReturn[] {
|
|
593
|
+
const results: LeakyReturn[] = [];
|
|
594
|
+
const exportedFns = fileEntry.functions.filter(fn =>
|
|
595
|
+
fileEntry.dependencyProfile?.declaredExports?.some(e => e.name === fn.name)
|
|
596
|
+
);
|
|
597
|
+
for (const fn of exportedFns) {
|
|
598
|
+
const fnPos = sourceFile.getPositionOfLineAndCharacter(
|
|
599
|
+
Math.max(0, fn.lineStart - 1),
|
|
600
|
+
0
|
|
601
|
+
);
|
|
602
|
+
let fnNode: ts.FunctionDeclaration | ts.MethodDeclaration | undefined;
|
|
603
|
+
const findFnDecl = (node: ts.Node): void => {
|
|
604
|
+
if (fnNode) return;
|
|
605
|
+
if (
|
|
606
|
+
(ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node)) &&
|
|
607
|
+
node.name &&
|
|
608
|
+
ts.isIdentifier(node.name) &&
|
|
609
|
+
node.name.text === fn.name &&
|
|
610
|
+
node.getStart(sourceFile) >= fnPos
|
|
611
|
+
) {
|
|
612
|
+
fnNode = node;
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
ts.forEachChild(node, findFnDecl);
|
|
616
|
+
};
|
|
617
|
+
ts.forEachChild(sourceFile, findFnDecl);
|
|
618
|
+
|
|
619
|
+
if (fnNode) {
|
|
620
|
+
const sig = ctx.checker.getSignatureFromDeclaration(fnNode);
|
|
621
|
+
if (sig) {
|
|
622
|
+
const retType = ctx.checker.getReturnTypeOfSignature(sig);
|
|
623
|
+
const retSymbol = retType.symbol || retType.aliasSymbol;
|
|
624
|
+
if (retSymbol?.declarations?.[0]) {
|
|
625
|
+
const retDeclFile =
|
|
626
|
+
retSymbol.declarations[0].getSourceFile().fileName;
|
|
627
|
+
const relRetFile = path.relative(ctx.root, retDeclFile);
|
|
628
|
+
if (
|
|
629
|
+
retDeclFile !== filePath &&
|
|
630
|
+
!relRetFile.startsWith('node_modules') &&
|
|
631
|
+
!retDeclFile.includes('lib.')
|
|
632
|
+
) {
|
|
633
|
+
results.push({
|
|
634
|
+
functionName: fn.name,
|
|
635
|
+
returnType: ctx.checker.typeToString(retType),
|
|
636
|
+
sourceFile: relRetFile,
|
|
637
|
+
lineStart: fn.lineStart,
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return results;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function detectNarrowableParams(
|
|
648
|
+
ctx: SemanticContext,
|
|
649
|
+
filePath: string,
|
|
650
|
+
sourceFile: ts.SourceFile,
|
|
651
|
+
fileEntry: FileEntry
|
|
652
|
+
): NarrowableParam[] {
|
|
653
|
+
const results: NarrowableParam[] = [];
|
|
654
|
+
const exportedFns = fileEntry.functions.filter(fn =>
|
|
655
|
+
fileEntry.dependencyProfile?.declaredExports?.some(e => e.name === fn.name)
|
|
656
|
+
);
|
|
657
|
+
for (const fn of exportedFns) {
|
|
658
|
+
if (!fn.params || fn.params < 1) continue;
|
|
659
|
+
const fnPos = sourceFile.getPositionOfLineAndCharacter(
|
|
660
|
+
Math.max(0, fn.lineStart - 1),
|
|
661
|
+
0
|
|
662
|
+
);
|
|
663
|
+
let fnDeclNode: ts.FunctionDeclaration | undefined;
|
|
664
|
+
const findDecl = (node: ts.Node): void => {
|
|
665
|
+
if (fnDeclNode) return;
|
|
666
|
+
if (
|
|
667
|
+
ts.isFunctionDeclaration(node) &&
|
|
668
|
+
node.name?.text === fn.name &&
|
|
669
|
+
node.getStart(sourceFile) >= fnPos
|
|
670
|
+
) {
|
|
671
|
+
fnDeclNode = node;
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
ts.forEachChild(node, findDecl);
|
|
675
|
+
};
|
|
676
|
+
ts.forEachChild(sourceFile, findDecl);
|
|
677
|
+
if (!fnDeclNode) continue;
|
|
678
|
+
|
|
679
|
+
for (const param of fnDeclNode.parameters) {
|
|
680
|
+
if (!ts.isIdentifier(param.name)) continue;
|
|
681
|
+
const paramType = ctx.checker.getTypeAtLocation(param);
|
|
682
|
+
if (
|
|
683
|
+
!paramType.isUnion() &&
|
|
684
|
+
!(paramType.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
|
|
685
|
+
)
|
|
686
|
+
continue;
|
|
687
|
+
|
|
688
|
+
const declaredStr = ctx.checker.typeToString(paramType);
|
|
689
|
+
const refs = ctx.service.findReferences(
|
|
690
|
+
filePath,
|
|
691
|
+
fnDeclNode.name!.getStart(sourceFile)
|
|
692
|
+
);
|
|
693
|
+
if (!refs) continue;
|
|
694
|
+
|
|
695
|
+
const argTypes: string[] = [];
|
|
696
|
+
let allNarrow = true;
|
|
697
|
+
let callSiteCount = 0;
|
|
698
|
+
|
|
699
|
+
for (const group of refs) {
|
|
700
|
+
for (const ref of group.references) {
|
|
701
|
+
if (ref.isDefinition) continue;
|
|
702
|
+
const refSrc = ctx.program.getSourceFile(ref.fileName);
|
|
703
|
+
if (!refSrc) continue;
|
|
704
|
+
let tokenNode: ts.Node = refSrc;
|
|
705
|
+
const findToken = (n: ts.Node): void => {
|
|
706
|
+
if (
|
|
707
|
+
n.getStart(refSrc!) <= ref.textSpan.start &&
|
|
708
|
+
n.getEnd() >= ref.textSpan.start + ref.textSpan.length
|
|
709
|
+
) {
|
|
710
|
+
tokenNode = n;
|
|
711
|
+
ts.forEachChild(n, findToken);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
ts.forEachChild(refSrc, findToken);
|
|
715
|
+
let callExpr: ts.CallExpression | undefined;
|
|
716
|
+
let ancestor: ts.Node | undefined = tokenNode;
|
|
717
|
+
while (ancestor) {
|
|
718
|
+
if (ts.isCallExpression(ancestor)) {
|
|
719
|
+
callExpr = ancestor;
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
ancestor = ancestor.parent;
|
|
723
|
+
}
|
|
724
|
+
if (!callExpr?.arguments) continue;
|
|
725
|
+
|
|
726
|
+
const paramIdx = fnDeclNode.parameters.indexOf(param);
|
|
727
|
+
if (paramIdx < 0 || paramIdx >= callExpr.arguments.length) {
|
|
728
|
+
allNarrow = false;
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const argType = ctx.checker.getTypeAtLocation(
|
|
733
|
+
callExpr.arguments[paramIdx]
|
|
734
|
+
);
|
|
735
|
+
const argStr = ctx.checker.typeToString(argType);
|
|
736
|
+
argTypes.push(argStr);
|
|
737
|
+
callSiteCount++;
|
|
738
|
+
|
|
739
|
+
if (
|
|
740
|
+
argStr === declaredStr ||
|
|
741
|
+
argStr === 'any' ||
|
|
742
|
+
argStr === 'unknown'
|
|
743
|
+
) {
|
|
744
|
+
allNarrow = false;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
if (allNarrow && callSiteCount >= 2 && argTypes.length > 0) {
|
|
750
|
+
const uniqueArgTypes = [...new Set(argTypes)];
|
|
751
|
+
if (uniqueArgTypes.length <= 2) {
|
|
752
|
+
results.push({
|
|
753
|
+
functionName: fn.name,
|
|
754
|
+
paramName: param.name.text,
|
|
755
|
+
declaredType: declaredStr,
|
|
756
|
+
actualTypes: uniqueArgTypes,
|
|
757
|
+
narrowedType:
|
|
758
|
+
uniqueArgTypes.length === 1
|
|
759
|
+
? uniqueArgTypes[0]
|
|
760
|
+
: uniqueArgTypes.join(' | '),
|
|
761
|
+
lineStart: fn.lineStart,
|
|
762
|
+
lineEnd: fn.lineEnd,
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return results;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
export function analyzeSemanticProfile(
|
|
772
|
+
ctx: SemanticContext,
|
|
773
|
+
filePath: string,
|
|
774
|
+
fileEntry: FileEntry,
|
|
775
|
+
includeTests = true
|
|
776
|
+
): SemanticProfile {
|
|
777
|
+
const profile: SemanticProfile = {
|
|
778
|
+
file: fileEntry.file,
|
|
779
|
+
referenceCountByExport: new Map(),
|
|
780
|
+
unusedParams: [],
|
|
781
|
+
interfaceImpls: [],
|
|
782
|
+
typeHierarchyDepth: 0,
|
|
783
|
+
typeHierarchies: [],
|
|
784
|
+
overrideChains: [],
|
|
785
|
+
abstractnessRatio: 0,
|
|
786
|
+
unusedImports: [],
|
|
787
|
+
concreteImports: [],
|
|
788
|
+
leakyReturns: [],
|
|
789
|
+
narrowableParams: [],
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
const sourceFile = ctx.program.getSourceFile(filePath);
|
|
793
|
+
if (!sourceFile) return profile;
|
|
794
|
+
|
|
795
|
+
profile.referenceCountByExport = collectExportReferences(
|
|
796
|
+
ctx, filePath, fileEntry, includeTests
|
|
797
|
+
);
|
|
798
|
+
profile.unusedParams = collectUnusedParams(ctx, filePath, sourceFile, fileEntry);
|
|
799
|
+
profile.abstractnessRatio = analyzeTypeHierarchy(ctx, sourceFile, fileEntry, profile);
|
|
800
|
+
|
|
801
|
+
const importResults = analyzeImports(ctx, filePath, sourceFile, fileEntry);
|
|
802
|
+
profile.unusedImports = importResults.unusedImports;
|
|
803
|
+
profile.concreteImports = importResults.concreteImports;
|
|
804
|
+
|
|
805
|
+
profile.leakyReturns = detectLeakyReturns(ctx, filePath, sourceFile, fileEntry);
|
|
806
|
+
profile.narrowableParams = detectNarrowableParams(ctx, filePath, sourceFile, fileEntry);
|
|
807
|
+
|
|
808
|
+
return profile;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
export function collectAllAbsoluteFiles(
|
|
812
|
+
fileSummaries: FileEntry[],
|
|
813
|
+
dependencyState: DependencyState,
|
|
814
|
+
root: string
|
|
815
|
+
): string[] {
|
|
816
|
+
const files = new Set<string>();
|
|
817
|
+
for (const entry of fileSummaries) {
|
|
818
|
+
files.add(path.resolve(root, entry.file));
|
|
819
|
+
}
|
|
820
|
+
for (const relFile of dependencyState.files) {
|
|
821
|
+
files.add(path.resolve(root, relFile));
|
|
822
|
+
}
|
|
823
|
+
return [...files].filter(f => {
|
|
824
|
+
try {
|
|
825
|
+
return fs.statSync(f).isFile();
|
|
826
|
+
} catch {
|
|
827
|
+
return false;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}
|