octocode-cli 1.2.6 → 1.2.8
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 +85 -142
- package/out/octocode-cli.js +7063 -6934
- 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,199 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
formatAstTreeSearchOutput,
|
|
9
|
+
parseAstTreeSearchArgs,
|
|
10
|
+
resolveAstTreeInput,
|
|
11
|
+
searchAstTree,
|
|
12
|
+
validateAstTreeSearchOptions,
|
|
13
|
+
} from './tree-search.js';
|
|
14
|
+
|
|
15
|
+
describe('ast-tree-search', () => {
|
|
16
|
+
let tmpDir: string;
|
|
17
|
+
let scanRoot: string;
|
|
18
|
+
let latestScanDir: string;
|
|
19
|
+
let olderScanDir: string;
|
|
20
|
+
let astTreePath: string;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ast-tree-search-'));
|
|
24
|
+
scanRoot = path.join(tmpDir, '.octocode', 'scan');
|
|
25
|
+
olderScanDir = path.join(scanRoot, '2026-03-18T10-00-00-000Z');
|
|
26
|
+
latestScanDir = path.join(scanRoot, '2026-03-19T10-00-00-000Z');
|
|
27
|
+
|
|
28
|
+
fs.mkdirSync(olderScanDir, { recursive: true });
|
|
29
|
+
fs.mkdirSync(latestScanDir, { recursive: true });
|
|
30
|
+
|
|
31
|
+
fs.writeFileSync(path.join(olderScanDir, 'ast-trees.txt'), '## pkg — src/older.ts\nFunctionDeclaration[1:2]\n', 'utf8');
|
|
32
|
+
|
|
33
|
+
astTreePath = path.join(latestScanDir, 'ast-trees.txt');
|
|
34
|
+
fs.writeFileSync(astTreePath, [
|
|
35
|
+
'## pkg — src/main.ts',
|
|
36
|
+
'SourceFile[1:50]',
|
|
37
|
+
' FunctionDeclaration[3:12]',
|
|
38
|
+
' IfStatement[5:7] ...',
|
|
39
|
+
' ClassDeclaration[14:30]',
|
|
40
|
+
'## pkg — src/utils.ts',
|
|
41
|
+
'SourceFile[1:40]',
|
|
42
|
+
' function_declaration[4:8]',
|
|
43
|
+
' ArrowFunction[10:12]',
|
|
44
|
+
' WhileStatement[14:18]',
|
|
45
|
+
'',
|
|
46
|
+
].join('\n'), 'utf8');
|
|
47
|
+
|
|
48
|
+
const olderTime = new Date('2026-03-18T10:00:00.000Z');
|
|
49
|
+
const newerTime = new Date('2026-03-19T10:00:00.000Z');
|
|
50
|
+
fs.utimesSync(path.join(olderScanDir, 'ast-trees.txt'), olderTime, olderTime);
|
|
51
|
+
fs.utimesSync(path.join(latestScanDir, 'ast-trees.txt'), newerTime, newerTime);
|
|
52
|
+
fs.utimesSync(olderScanDir, olderTime, olderTime);
|
|
53
|
+
fs.utimesSync(latestScanDir, newerTime, newerTime);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('parses limit, file, and section flags', () => {
|
|
61
|
+
const { opts } = parseAstTreeSearchArgs([
|
|
62
|
+
'--input', scanRoot,
|
|
63
|
+
'--kind', 'FunctionDeclaration',
|
|
64
|
+
'--file', 'src/main',
|
|
65
|
+
'--section', 'pkg',
|
|
66
|
+
'--limit', '10',
|
|
67
|
+
'--context', '2',
|
|
68
|
+
'--json',
|
|
69
|
+
'--ignore-case',
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
expect(opts.input).toBe(scanRoot);
|
|
73
|
+
expect(opts.kind).toBe('FunctionDeclaration');
|
|
74
|
+
expect(opts.file).toBe('src/main');
|
|
75
|
+
expect(opts.section).toBe('pkg');
|
|
76
|
+
expect(opts.limit).toBe(10);
|
|
77
|
+
expect(opts.context).toBe(2);
|
|
78
|
+
expect(opts.json).toBe(true);
|
|
79
|
+
expect(opts.ignoreCase).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('rejects invalid search options', () => {
|
|
83
|
+
expect(() => validateAstTreeSearchOptions({
|
|
84
|
+
input: scanRoot,
|
|
85
|
+
pattern: null,
|
|
86
|
+
kind: null,
|
|
87
|
+
context: 0,
|
|
88
|
+
json: false,
|
|
89
|
+
ignoreCase: false,
|
|
90
|
+
limit: 50,
|
|
91
|
+
file: null,
|
|
92
|
+
section: null,
|
|
93
|
+
})).toThrow('Must provide --pattern or --kind');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('resolves the latest scan from the scan root', () => {
|
|
97
|
+
const resolved = resolveAstTreeInput(scanRoot);
|
|
98
|
+
expect(resolved.selectionMode).toBe('latest-scan');
|
|
99
|
+
expect(resolved.inputFile).toBe(astTreePath);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('resolves a specific scan directory', () => {
|
|
103
|
+
const resolved = resolveAstTreeInput(latestScanDir);
|
|
104
|
+
expect(resolved.selectionMode).toBe('scan-dir');
|
|
105
|
+
expect(resolved.inputFile).toBe(astTreePath);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('resolves a direct ast-trees.txt input path', () => {
|
|
109
|
+
const resolved = resolveAstTreeInput(astTreePath);
|
|
110
|
+
expect(resolved.selectionMode).toBe('direct-file');
|
|
111
|
+
expect(resolved.inputFile).toBe(astTreePath);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('matches kind queries with PascalCase and snake_case node names', () => {
|
|
115
|
+
const result = searchAstTree(resolveAstTreeInput(scanRoot), {
|
|
116
|
+
input: scanRoot,
|
|
117
|
+
pattern: null,
|
|
118
|
+
kind: 'function_declaration',
|
|
119
|
+
context: 0,
|
|
120
|
+
json: false,
|
|
121
|
+
ignoreCase: false,
|
|
122
|
+
limit: 0,
|
|
123
|
+
file: null,
|
|
124
|
+
section: null,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(result.totalMatches).toBe(2);
|
|
128
|
+
expect(result.matches.map((match) => match.file)).toEqual([
|
|
129
|
+
'src/main.ts',
|
|
130
|
+
'src/utils.ts',
|
|
131
|
+
]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('filters by file and section regexes', () => {
|
|
135
|
+
const result = searchAstTree(resolveAstTreeInput(scanRoot), {
|
|
136
|
+
input: scanRoot,
|
|
137
|
+
pattern: 'FunctionDeclaration|function_declaration|ArrowFunction',
|
|
138
|
+
kind: null,
|
|
139
|
+
context: 0,
|
|
140
|
+
json: false,
|
|
141
|
+
ignoreCase: false,
|
|
142
|
+
limit: 0,
|
|
143
|
+
file: 'src/utils',
|
|
144
|
+
section: 'pkg',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
expect(result.totalMatches).toBe(2);
|
|
148
|
+
expect(result.matches.every((match) => match.file === 'src/utils.ts')).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('enforces the default limit and reports truncation metadata', () => {
|
|
152
|
+
const repeatedAst = [
|
|
153
|
+
'## pkg — src/many.ts',
|
|
154
|
+
'SourceFile[1:200]',
|
|
155
|
+
...Array.from({ length: 60 }, (_, index) => ` FunctionDeclaration[${index + 1}:${index + 2}]`),
|
|
156
|
+
'',
|
|
157
|
+
].join('\n');
|
|
158
|
+
fs.writeFileSync(astTreePath, repeatedAst, 'utf8');
|
|
159
|
+
|
|
160
|
+
const result = searchAstTree(resolveAstTreeInput(astTreePath), {
|
|
161
|
+
input: astTreePath,
|
|
162
|
+
pattern: null,
|
|
163
|
+
kind: 'FunctionDeclaration',
|
|
164
|
+
context: 0,
|
|
165
|
+
json: false,
|
|
166
|
+
ignoreCase: false,
|
|
167
|
+
limit: 50,
|
|
168
|
+
file: null,
|
|
169
|
+
section: null,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
expect(result.totalMatches).toBe(60);
|
|
173
|
+
expect(result.returnedMatches).toBe(50);
|
|
174
|
+
expect(result.truncated).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('formats output with selected file information and compact summary', () => {
|
|
178
|
+
const opts = {
|
|
179
|
+
input: scanRoot,
|
|
180
|
+
pattern: 'IfStatement|WhileStatement',
|
|
181
|
+
kind: null,
|
|
182
|
+
context: 1,
|
|
183
|
+
json: false,
|
|
184
|
+
ignoreCase: false,
|
|
185
|
+
limit: 25,
|
|
186
|
+
file: null,
|
|
187
|
+
section: null,
|
|
188
|
+
};
|
|
189
|
+
const result = searchAstTree(resolveAstTreeInput(scanRoot), opts);
|
|
190
|
+
const text = formatAstTreeSearchOutput(result, opts);
|
|
191
|
+
|
|
192
|
+
expect(text).toContain('Requested input:');
|
|
193
|
+
expect(text).toContain('Selected AST file:');
|
|
194
|
+
expect(text).toContain('Matches: 2 total, showing 2');
|
|
195
|
+
expect(text).toContain('Matched files: 2');
|
|
196
|
+
expect(text).toContain('src/main.ts');
|
|
197
|
+
expect(text).toContain('src/utils.ts');
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
import { isDirectRun } from '../common/is-direct-run.js';
|
|
7
|
+
|
|
8
|
+
export interface AstTreeSearchOptions {
|
|
9
|
+
input: string;
|
|
10
|
+
pattern: string | null;
|
|
11
|
+
kind: string | null;
|
|
12
|
+
context: number;
|
|
13
|
+
json: boolean;
|
|
14
|
+
ignoreCase: boolean;
|
|
15
|
+
limit: number;
|
|
16
|
+
file: string | null;
|
|
17
|
+
section: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AstTreeContextLine {
|
|
21
|
+
lineNumber: number;
|
|
22
|
+
line: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AstTreeMatch {
|
|
26
|
+
section: string;
|
|
27
|
+
file: string | null;
|
|
28
|
+
lineNumber: number;
|
|
29
|
+
line: string;
|
|
30
|
+
context: AstTreeContextLine[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ResolvedAstTreeInput {
|
|
34
|
+
requestedInput: string;
|
|
35
|
+
inputFile: string;
|
|
36
|
+
selectionMode: 'direct-file' | 'scan-dir' | 'latest-scan';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AstTreeSearchResult {
|
|
40
|
+
requestedInput: string;
|
|
41
|
+
inputFile: string;
|
|
42
|
+
selectionMode: ResolvedAstTreeInput['selectionMode'];
|
|
43
|
+
query: string;
|
|
44
|
+
limit: number;
|
|
45
|
+
totalMatches: number;
|
|
46
|
+
returnedMatches: number;
|
|
47
|
+
truncated: boolean;
|
|
48
|
+
uniqueFiles: number;
|
|
49
|
+
matches: AstTreeMatch[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function printAstTreeSearchHelp(): void {
|
|
53
|
+
console.log(`
|
|
54
|
+
ast-tree-search — Search generated ast-trees.txt output
|
|
55
|
+
|
|
56
|
+
Usage:
|
|
57
|
+
node scripts/ast/tree-search.js [options]
|
|
58
|
+
|
|
59
|
+
Options:
|
|
60
|
+
--input, -i <path> Path to ast-trees.txt, a scan directory, or .octocode/scan (default: .octocode/scan)
|
|
61
|
+
--pattern, -p <regex> Regex to match against AST tree lines
|
|
62
|
+
--kind, -k <kind> Match a node kind (supports snake_case or PascalCase)
|
|
63
|
+
--file <regex> Filter matches to section file paths that match the regex
|
|
64
|
+
--section <regex> Filter matches to section headers that match the regex
|
|
65
|
+
--limit <n> Max matches to return (default: 50, 0 = all)
|
|
66
|
+
--context, -C <n> Context lines around each match (default: 0)
|
|
67
|
+
--json Output matches as JSON
|
|
68
|
+
--ignore-case Case-insensitive pattern matching
|
|
69
|
+
--help, -h Show this message
|
|
70
|
+
|
|
71
|
+
Examples:
|
|
72
|
+
node scripts/ast/tree-search.js -i .octocode/scan -k function_declaration --limit 25
|
|
73
|
+
node scripts/ast/tree-search.js -i .octocode/scan/2026-03-18T23-43-21-490Z -k ClassDeclaration --file 'src/index'
|
|
74
|
+
node scripts/ast/tree-search.js -i .octocode/scan -p 'IfStatement|SwitchStatement' --section 'src/'
|
|
75
|
+
`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseArgValue(arg: string, argv: string[], index: number): { value: string; nextIndex: number } {
|
|
79
|
+
const equalsIndex = arg.indexOf('=');
|
|
80
|
+
if (equalsIndex !== -1) {
|
|
81
|
+
return { value: arg.slice(equalsIndex + 1), nextIndex: index };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return { value: argv[index + 1] || '', nextIndex: index + 1 };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function parseAstTreeSearchArgs(argv: string[]): { opts: AstTreeSearchOptions; showHelp: boolean } {
|
|
88
|
+
const opts: AstTreeSearchOptions = {
|
|
89
|
+
input: '.octocode/scan',
|
|
90
|
+
pattern: null,
|
|
91
|
+
kind: null,
|
|
92
|
+
context: 0,
|
|
93
|
+
json: false,
|
|
94
|
+
ignoreCase: false,
|
|
95
|
+
limit: 50,
|
|
96
|
+
file: null,
|
|
97
|
+
section: null,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
let showHelp = false;
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
103
|
+
const arg = argv[i];
|
|
104
|
+
|
|
105
|
+
if (arg === '--json') {
|
|
106
|
+
opts.json = true;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (arg === '--ignore-case') {
|
|
110
|
+
opts.ignoreCase = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (arg === '--help' || arg === '-h') {
|
|
114
|
+
showHelp = true;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (arg === '--input' || arg === '-i' || arg.startsWith('--input=')) {
|
|
119
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
120
|
+
opts.input = parsed.value;
|
|
121
|
+
i = parsed.nextIndex;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (arg === '--pattern' || arg === '-p' || arg.startsWith('--pattern=')) {
|
|
125
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
126
|
+
opts.pattern = parsed.value;
|
|
127
|
+
i = parsed.nextIndex;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (arg === '--kind' || arg === '-k' || arg.startsWith('--kind=')) {
|
|
131
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
132
|
+
opts.kind = parsed.value;
|
|
133
|
+
i = parsed.nextIndex;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (arg === '--file' || arg.startsWith('--file=')) {
|
|
137
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
138
|
+
opts.file = parsed.value;
|
|
139
|
+
i = parsed.nextIndex;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (arg === '--section' || arg.startsWith('--section=')) {
|
|
143
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
144
|
+
opts.section = parsed.value;
|
|
145
|
+
i = parsed.nextIndex;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (arg === '--limit' || arg.startsWith('--limit=')) {
|
|
149
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
150
|
+
const parsedLimit = Number.parseInt(parsed.value, 10);
|
|
151
|
+
opts.limit = Number.isFinite(parsedLimit) ? parsedLimit : 50;
|
|
152
|
+
i = parsed.nextIndex;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (arg === '--context' || arg === '-C' || arg.startsWith('--context=')) {
|
|
156
|
+
const parsed = parseArgValue(arg, argv, i);
|
|
157
|
+
const parsedContext = Number.parseInt(parsed.value, 10);
|
|
158
|
+
opts.context = Number.isFinite(parsedContext) ? parsedContext : 0;
|
|
159
|
+
i = parsed.nextIndex;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
throw new Error(`Unknown argument: ${arg}`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { opts, showHelp };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function validateAstTreeSearchOptions(opts: AstTreeSearchOptions): void {
|
|
170
|
+
if (!opts.pattern && !opts.kind) {
|
|
171
|
+
throw new Error('Must provide --pattern or --kind');
|
|
172
|
+
}
|
|
173
|
+
if (!Number.isFinite(opts.context) || opts.context < 0) {
|
|
174
|
+
throw new Error('--context must be a non-negative integer');
|
|
175
|
+
}
|
|
176
|
+
if (!Number.isFinite(opts.limit) || opts.limit < 0) {
|
|
177
|
+
throw new Error('--limit must be a non-negative integer');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function toSnakeCase(value: string): string {
|
|
182
|
+
return value
|
|
183
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
184
|
+
.replace(/[-\s]+/g, '_')
|
|
185
|
+
.toLowerCase();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function toPascalCase(value: string): string {
|
|
189
|
+
return value
|
|
190
|
+
.split('_')
|
|
191
|
+
.filter(Boolean)
|
|
192
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
193
|
+
.join('');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function escapeRegExp(value: string): string {
|
|
197
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function buildRegex(pattern: string | null, flags: string, label: string): RegExp | null {
|
|
201
|
+
if (!pattern) return null;
|
|
202
|
+
try {
|
|
203
|
+
return new RegExp(pattern, flags);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw new Error(`Invalid ${label} regex: ${(error as Error).message}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function parseSectionFile(section: string): string | null {
|
|
210
|
+
const marker = ' — ';
|
|
211
|
+
const index = section.indexOf(marker);
|
|
212
|
+
if (index === -1) return null;
|
|
213
|
+
return section.slice(index + marker.length).trim() || null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function resolveAstTreeInput(inputPath: string): ResolvedAstTreeInput {
|
|
217
|
+
const requestedInput = path.resolve(inputPath);
|
|
218
|
+
if (!fs.existsSync(requestedInput)) {
|
|
219
|
+
throw new Error(`Input does not exist: ${requestedInput}`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const stat = fs.statSync(requestedInput);
|
|
223
|
+
if (stat.isFile()) {
|
|
224
|
+
return {
|
|
225
|
+
requestedInput,
|
|
226
|
+
inputFile: requestedInput,
|
|
227
|
+
selectionMode: 'direct-file',
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const directAstFile = path.join(requestedInput, 'ast-trees.txt');
|
|
232
|
+
if (fs.existsSync(directAstFile) && fs.statSync(directAstFile).isFile()) {
|
|
233
|
+
return {
|
|
234
|
+
requestedInput,
|
|
235
|
+
inputFile: directAstFile,
|
|
236
|
+
selectionMode: 'scan-dir',
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const candidates = fs.readdirSync(requestedInput, { withFileTypes: true })
|
|
241
|
+
.filter((entry) => entry.isDirectory())
|
|
242
|
+
.map((entry) => path.join(requestedInput, entry.name, 'ast-trees.txt'))
|
|
243
|
+
.filter((candidate) => fs.existsSync(candidate) && fs.statSync(candidate).isFile())
|
|
244
|
+
.sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs);
|
|
245
|
+
|
|
246
|
+
if (candidates.length === 0) {
|
|
247
|
+
throw new Error(`No ast-trees.txt found under: ${requestedInput}`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
requestedInput,
|
|
252
|
+
inputFile: candidates[0],
|
|
253
|
+
selectionMode: 'latest-scan',
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function searchAstTree(
|
|
258
|
+
resolved: ResolvedAstTreeInput,
|
|
259
|
+
opts: AstTreeSearchOptions,
|
|
260
|
+
): AstTreeSearchResult {
|
|
261
|
+
const flags = opts.ignoreCase ? 'i' : '';
|
|
262
|
+
const patternRegex = buildRegex(opts.pattern, flags, 'pattern');
|
|
263
|
+
const fileRegex = buildRegex(opts.file, flags, 'file');
|
|
264
|
+
const sectionRegex = buildRegex(opts.section, flags, 'section');
|
|
265
|
+
|
|
266
|
+
let kindRegex: RegExp | null = null;
|
|
267
|
+
if (opts.kind) {
|
|
268
|
+
const snake = toSnakeCase(opts.kind);
|
|
269
|
+
const pascal = toPascalCase(snake);
|
|
270
|
+
kindRegex = new RegExp(`\\b(?:${escapeRegExp(snake)}|${escapeRegExp(pascal)})\\b`, flags);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const lines = fs.readFileSync(resolved.inputFile, 'utf8').split(/\r?\n/);
|
|
274
|
+
const allMatches: AstTreeMatch[] = [];
|
|
275
|
+
let currentSection = '';
|
|
276
|
+
let currentFile: string | null = null;
|
|
277
|
+
|
|
278
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
279
|
+
const line = lines[index];
|
|
280
|
+
|
|
281
|
+
if (line.startsWith('## ')) {
|
|
282
|
+
currentSection = line.slice(3).trim();
|
|
283
|
+
currentFile = parseSectionFile(currentSection);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (patternRegex && !patternRegex.test(line)) continue;
|
|
287
|
+
if (kindRegex && !kindRegex.test(line)) continue;
|
|
288
|
+
if (sectionRegex && !sectionRegex.test(currentSection)) continue;
|
|
289
|
+
if (fileRegex && !fileRegex.test(currentFile ?? '')) continue;
|
|
290
|
+
|
|
291
|
+
const start = Math.max(0, index - opts.context);
|
|
292
|
+
const end = Math.min(lines.length, index + opts.context + 1);
|
|
293
|
+
|
|
294
|
+
allMatches.push({
|
|
295
|
+
section: currentSection,
|
|
296
|
+
file: currentFile,
|
|
297
|
+
lineNumber: index + 1,
|
|
298
|
+
line,
|
|
299
|
+
context: lines.slice(start, end).map((contextLine, offset) => ({
|
|
300
|
+
lineNumber: start + offset + 1,
|
|
301
|
+
line: contextLine,
|
|
302
|
+
})),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const returnedMatches = opts.limit === 0 ? allMatches : allMatches.slice(0, opts.limit);
|
|
307
|
+
const uniqueFiles = new Set(allMatches.map((match) => match.file).filter(Boolean)).size;
|
|
308
|
+
const queryParts = [
|
|
309
|
+
opts.kind ? `kind=${opts.kind}` : null,
|
|
310
|
+
opts.pattern ? `pattern=${opts.pattern}` : null,
|
|
311
|
+
opts.file ? `file=${opts.file}` : null,
|
|
312
|
+
opts.section ? `section=${opts.section}` : null,
|
|
313
|
+
].filter(Boolean);
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
requestedInput: resolved.requestedInput,
|
|
317
|
+
inputFile: resolved.inputFile,
|
|
318
|
+
selectionMode: resolved.selectionMode,
|
|
319
|
+
query: queryParts.join(', '),
|
|
320
|
+
limit: opts.limit,
|
|
321
|
+
totalMatches: allMatches.length,
|
|
322
|
+
returnedMatches: returnedMatches.length,
|
|
323
|
+
truncated: returnedMatches.length < allMatches.length,
|
|
324
|
+
uniqueFiles,
|
|
325
|
+
matches: returnedMatches,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function formatAstTreeSearchOutput(result: AstTreeSearchResult, opts: AstTreeSearchOptions): string {
|
|
330
|
+
const lines: string[] = [];
|
|
331
|
+
|
|
332
|
+
lines.push(`\nAST tree search: ${result.query}`);
|
|
333
|
+
lines.push(`Requested input: ${result.requestedInput}`);
|
|
334
|
+
lines.push(`Selected AST file: ${result.inputFile} (${result.selectionMode})`);
|
|
335
|
+
lines.push(`Matches: ${result.totalMatches} total, showing ${result.returnedMatches}${result.truncated ? ` (limit ${result.limit})` : ''}`);
|
|
336
|
+
lines.push(`Matched files: ${result.uniqueFiles}\n`);
|
|
337
|
+
|
|
338
|
+
let currentSection = '';
|
|
339
|
+
for (const match of result.matches) {
|
|
340
|
+
if (match.section !== currentSection) {
|
|
341
|
+
currentSection = match.section;
|
|
342
|
+
lines.push(`-- ${currentSection || '(no section)'} --`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (opts.context > 0) {
|
|
346
|
+
for (const contextLine of match.context) {
|
|
347
|
+
const marker = contextLine.lineNumber === match.lineNumber ? '>' : ' ';
|
|
348
|
+
lines.push(` ${marker} ${String(contextLine.lineNumber).padStart(4)} | ${contextLine.line}`);
|
|
349
|
+
}
|
|
350
|
+
lines.push('');
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const fileLabel = match.file ? ` (${match.file})` : '';
|
|
355
|
+
lines.push(` L${match.lineNumber}${fileLabel} ${match.line}`);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (result.totalMatches === 0) {
|
|
359
|
+
lines.push('No matches found.');
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
lines.push('');
|
|
363
|
+
return lines.join('\n');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async function main(): Promise<void> {
|
|
367
|
+
try {
|
|
368
|
+
const { opts, showHelp } = parseAstTreeSearchArgs(process.argv.slice(2));
|
|
369
|
+
if (showHelp) {
|
|
370
|
+
printAstTreeSearchHelp();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
validateAstTreeSearchOptions(opts);
|
|
375
|
+
const resolved = resolveAstTreeInput(opts.input);
|
|
376
|
+
const result = searchAstTree(resolved, opts);
|
|
377
|
+
|
|
378
|
+
if (opts.json) {
|
|
379
|
+
console.log(JSON.stringify(result, null, 2));
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
console.log(formatAstTreeSearchOutput(result, opts));
|
|
384
|
+
} catch (error) {
|
|
385
|
+
console.error((error as Error).message);
|
|
386
|
+
process.exit(1);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (isDirectRun(import.meta.url)) {
|
|
391
|
+
void main();
|
|
392
|
+
}
|