octocode-cli 1.2.6 ā 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 +85 -142
- package/out/octocode-cli.js +7026 -6945
- 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,648 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* AST-aware structural code search powered by @ast-grep/napi.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* node scripts/ast/search.js --pattern 'console.log($$$ARGS)' --root ./src
|
|
7
|
+
* node scripts/ast/search.js --preset empty-catch --root ./packages
|
|
8
|
+
* node scripts/ast/search.js --kind function_declaration --root ./src --json
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { js as astJs, ts as astTs, tsx as astTsx } from '@ast-grep/napi';
|
|
15
|
+
|
|
16
|
+
import { isDirectRun } from '../common/is-direct-run.js';
|
|
17
|
+
import { ALLOWED_EXTS } from '../types/index.js';
|
|
18
|
+
|
|
19
|
+
import type { NapiConfig, SgNode, SgRoot } from '@ast-grep/napi';
|
|
20
|
+
|
|
21
|
+
export interface AstSearchOptions {
|
|
22
|
+
root: string;
|
|
23
|
+
pattern: string | null;
|
|
24
|
+
kind: string | null;
|
|
25
|
+
preset: string | null;
|
|
26
|
+
rule: NapiConfig | null;
|
|
27
|
+
json: boolean;
|
|
28
|
+
limit: number;
|
|
29
|
+
includeTests: boolean;
|
|
30
|
+
ignoreDirs: Set<string>;
|
|
31
|
+
context: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface AstMatch {
|
|
35
|
+
file: string;
|
|
36
|
+
kind: string;
|
|
37
|
+
text: string;
|
|
38
|
+
lineStart: number;
|
|
39
|
+
lineEnd: number;
|
|
40
|
+
columnStart: number;
|
|
41
|
+
columnEnd: number;
|
|
42
|
+
metaVariables?: Record<string, string>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AstSearchResult {
|
|
46
|
+
query: string;
|
|
47
|
+
queryType: 'pattern' | 'kind' | 'preset' | 'rule';
|
|
48
|
+
totalMatches: number;
|
|
49
|
+
totalFiles: number;
|
|
50
|
+
matches: AstMatch[];
|
|
51
|
+
/** Source lines keyed by relative file path ā only populated when context > 0 */
|
|
52
|
+
_sourceByFile?: Map<string, string[]>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type PresetRule = NapiConfig & { description: string };
|
|
56
|
+
|
|
57
|
+
export const PRESETS: Record<string, PresetRule> = {
|
|
58
|
+
'empty-catch': {
|
|
59
|
+
rule: {
|
|
60
|
+
kind: 'catch_clause',
|
|
61
|
+
has: {
|
|
62
|
+
kind: 'statement_block',
|
|
63
|
+
regex: '^\\{\\s*\\}$',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
description: 'Empty catch blocks that silently swallow errors',
|
|
67
|
+
},
|
|
68
|
+
'console-log': {
|
|
69
|
+
rule: {
|
|
70
|
+
pattern: 'console.log($$$ARGS)',
|
|
71
|
+
},
|
|
72
|
+
description: 'console.log calls left in production code',
|
|
73
|
+
},
|
|
74
|
+
'console-any': {
|
|
75
|
+
rule: {
|
|
76
|
+
pattern: 'console.$METHOD($$$ARGS)',
|
|
77
|
+
},
|
|
78
|
+
description: 'Any console method call (log, warn, error, debug, etc.)',
|
|
79
|
+
},
|
|
80
|
+
debugger: {
|
|
81
|
+
rule: {
|
|
82
|
+
kind: 'debugger_statement',
|
|
83
|
+
},
|
|
84
|
+
description: 'Debugger statements left in code',
|
|
85
|
+
},
|
|
86
|
+
'todo-fixme': {
|
|
87
|
+
rule: {
|
|
88
|
+
kind: 'comment',
|
|
89
|
+
regex: '(?i)(TODO|FIXME|HACK|XXX|BUG)',
|
|
90
|
+
},
|
|
91
|
+
description: 'TODO, FIXME, HACK, XXX, BUG comments',
|
|
92
|
+
},
|
|
93
|
+
'any-type': {
|
|
94
|
+
rule: {
|
|
95
|
+
kind: 'predefined_type',
|
|
96
|
+
regex: '^any$',
|
|
97
|
+
},
|
|
98
|
+
description: 'Explicit `any` type annotations',
|
|
99
|
+
},
|
|
100
|
+
'type-assertion': {
|
|
101
|
+
rule: {
|
|
102
|
+
kind: 'as_expression',
|
|
103
|
+
},
|
|
104
|
+
description: 'TypeScript type assertions (as X)',
|
|
105
|
+
},
|
|
106
|
+
'non-null-assertion': {
|
|
107
|
+
rule: {
|
|
108
|
+
kind: 'non_null_expression',
|
|
109
|
+
},
|
|
110
|
+
description: 'Non-null assertions (x!)',
|
|
111
|
+
},
|
|
112
|
+
'fat-arrow-body': {
|
|
113
|
+
rule: {
|
|
114
|
+
kind: 'arrow_function',
|
|
115
|
+
has: {
|
|
116
|
+
kind: 'statement_block',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
description:
|
|
120
|
+
'Arrow functions with statement block bodies (could be expression)',
|
|
121
|
+
},
|
|
122
|
+
'nested-ternary': {
|
|
123
|
+
rule: {
|
|
124
|
+
kind: 'ternary_expression',
|
|
125
|
+
has: {
|
|
126
|
+
kind: 'ternary_expression',
|
|
127
|
+
stopBy: 'end',
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
description: 'Nested ternary expressions (hard to read)',
|
|
131
|
+
},
|
|
132
|
+
'throw-string': {
|
|
133
|
+
rule: {
|
|
134
|
+
kind: 'throw_statement',
|
|
135
|
+
has: {
|
|
136
|
+
kind: 'string',
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
description: 'Throwing string literals instead of Error objects',
|
|
140
|
+
},
|
|
141
|
+
'switch-no-default': {
|
|
142
|
+
rule: {
|
|
143
|
+
kind: 'switch_statement',
|
|
144
|
+
not: {
|
|
145
|
+
has: {
|
|
146
|
+
kind: 'switch_default',
|
|
147
|
+
stopBy: 'end',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
description: 'Switch statements without a default case',
|
|
152
|
+
},
|
|
153
|
+
'class-declaration': {
|
|
154
|
+
rule: {
|
|
155
|
+
kind: 'class_declaration',
|
|
156
|
+
},
|
|
157
|
+
description: 'All class declarations',
|
|
158
|
+
},
|
|
159
|
+
'async-function': {
|
|
160
|
+
rule: {
|
|
161
|
+
kind: 'function_declaration',
|
|
162
|
+
regex: '^async ',
|
|
163
|
+
},
|
|
164
|
+
description: 'Async function declarations',
|
|
165
|
+
},
|
|
166
|
+
'export-default': {
|
|
167
|
+
rule: {
|
|
168
|
+
kind: 'export_statement',
|
|
169
|
+
has: {
|
|
170
|
+
field: 'default',
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
description: 'Default exports',
|
|
174
|
+
},
|
|
175
|
+
'import-star': {
|
|
176
|
+
rule: {
|
|
177
|
+
kind: 'import_statement',
|
|
178
|
+
has: {
|
|
179
|
+
kind: 'namespace_import',
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
description: 'Namespace imports (import * as X)',
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
function isTestFile(filePath: string): boolean {
|
|
187
|
+
const base = path.basename(filePath);
|
|
188
|
+
return (
|
|
189
|
+
/\.(test|spec)\.(ts|tsx|js|jsx|mjs|cjs)$/.test(base) ||
|
|
190
|
+
base.startsWith('test_') ||
|
|
191
|
+
filePath.includes('__tests__')
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function collectSearchFiles(
|
|
196
|
+
root: string,
|
|
197
|
+
opts: Pick<AstSearchOptions, 'includeTests' | 'ignoreDirs'>
|
|
198
|
+
): string[] {
|
|
199
|
+
const files: string[] = [];
|
|
200
|
+
const walk = (dir: string): void => {
|
|
201
|
+
let entries: fs.Dirent[];
|
|
202
|
+
try {
|
|
203
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
204
|
+
} catch {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
208
|
+
for (const entry of entries) {
|
|
209
|
+
if (opts.ignoreDirs.has(entry.name)) continue;
|
|
210
|
+
if (entry.isSymbolicLink()) continue;
|
|
211
|
+
const next = path.join(dir, entry.name);
|
|
212
|
+
if (entry.isDirectory()) {
|
|
213
|
+
walk(next);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (!entry.isFile()) continue;
|
|
217
|
+
if (entry.name.endsWith('.d.ts')) continue;
|
|
218
|
+
const ext = path.extname(entry.name);
|
|
219
|
+
if (!ALLOWED_EXTS.has(ext)) continue;
|
|
220
|
+
if (!opts.includeTests && isTestFile(next)) continue;
|
|
221
|
+
files.push(next);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
walk(root);
|
|
225
|
+
return files;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
type AstParser = { parse(src: string): SgRoot };
|
|
229
|
+
|
|
230
|
+
function parserForExt(ext: string): AstParser {
|
|
231
|
+
switch (ext) {
|
|
232
|
+
case '.tsx':
|
|
233
|
+
return astTsx;
|
|
234
|
+
case '.jsx':
|
|
235
|
+
return astJs;
|
|
236
|
+
case '.js':
|
|
237
|
+
case '.mjs':
|
|
238
|
+
case '.cjs':
|
|
239
|
+
return astJs;
|
|
240
|
+
case '.ts':
|
|
241
|
+
default:
|
|
242
|
+
return astTs;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function extractMetaVars(
|
|
247
|
+
node: SgNode,
|
|
248
|
+
pattern: string
|
|
249
|
+
): Record<string, string> {
|
|
250
|
+
const vars: Record<string, string> = {};
|
|
251
|
+
let match: RegExpExecArray | null;
|
|
252
|
+
|
|
253
|
+
const triplePattern = /\$\$\$([A-Z_][A-Z0-9_]*)/g;
|
|
254
|
+
const triplNames = new Set<string>();
|
|
255
|
+
while ((match = triplePattern.exec(pattern)) !== null) {
|
|
256
|
+
const name = match[1];
|
|
257
|
+
triplNames.add(name);
|
|
258
|
+
const multiMatch = node.getMultipleMatches(name);
|
|
259
|
+
if (multiMatch.length > 0) {
|
|
260
|
+
vars[`$$$${name}`] = multiMatch.map(n => n.text()).join(', ');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const singlePattern = /(?<!\$)\$([A-Z_][A-Z0-9_]*)(?!\$)/g;
|
|
265
|
+
while ((match = singlePattern.exec(pattern)) !== null) {
|
|
266
|
+
const name = match[1];
|
|
267
|
+
if (triplNames.has(name)) continue;
|
|
268
|
+
const matchNode = node.getMatch(name);
|
|
269
|
+
if (matchNode) vars[`$${name}`] = matchNode.text();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return vars;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function nodeToMatch(
|
|
276
|
+
node: SgNode,
|
|
277
|
+
file: string,
|
|
278
|
+
pattern: string | null
|
|
279
|
+
): AstMatch {
|
|
280
|
+
const range = node.range();
|
|
281
|
+
const result: AstMatch = {
|
|
282
|
+
file,
|
|
283
|
+
kind: String(node.kind()),
|
|
284
|
+
text: node.text(),
|
|
285
|
+
lineStart: range.start.line + 1,
|
|
286
|
+
lineEnd: range.end.line + 1,
|
|
287
|
+
columnStart: range.start.column,
|
|
288
|
+
columnEnd: range.end.column,
|
|
289
|
+
};
|
|
290
|
+
if (pattern) {
|
|
291
|
+
const vars = extractMetaVars(node, pattern);
|
|
292
|
+
if (Object.keys(vars).length > 0) result.metaVariables = vars;
|
|
293
|
+
}
|
|
294
|
+
return result;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function searchFile(
|
|
298
|
+
filePath: string,
|
|
299
|
+
source: string,
|
|
300
|
+
matcher: string | number | NapiConfig,
|
|
301
|
+
patternStr: string | null,
|
|
302
|
+
limit: number
|
|
303
|
+
): AstMatch[] {
|
|
304
|
+
const ext = path.extname(filePath);
|
|
305
|
+
const parser = parserForExt(ext);
|
|
306
|
+
let nodes: SgNode[];
|
|
307
|
+
try {
|
|
308
|
+
const root = parser.parse(source).root();
|
|
309
|
+
nodes = root.findAll(matcher);
|
|
310
|
+
} catch {
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
const matches: AstMatch[] = [];
|
|
314
|
+
for (const node of nodes) {
|
|
315
|
+
if (matches.length >= limit) break;
|
|
316
|
+
matches.push(nodeToMatch(node, filePath, patternStr));
|
|
317
|
+
}
|
|
318
|
+
return matches;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function runSearch(
|
|
322
|
+
files: string[],
|
|
323
|
+
opts: AstSearchOptions,
|
|
324
|
+
root: string
|
|
325
|
+
): AstSearchResult {
|
|
326
|
+
let matcher: string | NapiConfig;
|
|
327
|
+
let queryLabel: string;
|
|
328
|
+
let queryType: AstSearchResult['queryType'];
|
|
329
|
+
let patternStr: string | null = null;
|
|
330
|
+
|
|
331
|
+
if (opts.preset) {
|
|
332
|
+
const preset = PRESETS[opts.preset];
|
|
333
|
+
if (!preset) {
|
|
334
|
+
const available = Object.keys(PRESETS).join(', ');
|
|
335
|
+
throw new Error(
|
|
336
|
+
`Unknown preset: "${opts.preset}". Available: ${available}`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
matcher = preset;
|
|
340
|
+
queryLabel = `preset:${opts.preset} ā ${preset.description}`;
|
|
341
|
+
queryType = 'preset';
|
|
342
|
+
} else if (opts.rule) {
|
|
343
|
+
matcher = opts.rule;
|
|
344
|
+
queryLabel = `rule:${JSON.stringify(opts.rule)}`;
|
|
345
|
+
queryType = 'rule';
|
|
346
|
+
} else if (opts.kind) {
|
|
347
|
+
matcher = { rule: { kind: opts.kind } } as NapiConfig;
|
|
348
|
+
queryLabel = `kind:${opts.kind}`;
|
|
349
|
+
queryType = 'kind';
|
|
350
|
+
} else if (opts.pattern) {
|
|
351
|
+
matcher = opts.pattern;
|
|
352
|
+
patternStr = opts.pattern;
|
|
353
|
+
queryLabel = `pattern:${opts.pattern}`;
|
|
354
|
+
queryType = 'pattern';
|
|
355
|
+
} else {
|
|
356
|
+
throw new Error('Must provide --pattern, --kind, --preset, or --rule');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const allMatches: AstMatch[] = [];
|
|
360
|
+
const filesWithMatches = new Set<string>();
|
|
361
|
+
const sourceByFile =
|
|
362
|
+
opts.context > 0 ? new Map<string, string[]>() : undefined;
|
|
363
|
+
|
|
364
|
+
for (const filePath of files) {
|
|
365
|
+
if (allMatches.length >= opts.limit) break;
|
|
366
|
+
let source: string;
|
|
367
|
+
try {
|
|
368
|
+
source = fs.readFileSync(filePath, 'utf8');
|
|
369
|
+
} catch {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const relFile = path.relative(root, filePath);
|
|
373
|
+
const remaining = opts.limit - allMatches.length;
|
|
374
|
+
const fileMatches = searchFile(
|
|
375
|
+
relFile,
|
|
376
|
+
source,
|
|
377
|
+
matcher,
|
|
378
|
+
patternStr,
|
|
379
|
+
remaining
|
|
380
|
+
);
|
|
381
|
+
if (fileMatches.length > 0) {
|
|
382
|
+
filesWithMatches.add(relFile);
|
|
383
|
+
allMatches.push(...fileMatches);
|
|
384
|
+
if (sourceByFile) sourceByFile.set(relFile, source.split('\n'));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const result: AstSearchResult = {
|
|
389
|
+
query: queryLabel,
|
|
390
|
+
queryType,
|
|
391
|
+
totalMatches: allMatches.length,
|
|
392
|
+
totalFiles: filesWithMatches.size,
|
|
393
|
+
matches: allMatches,
|
|
394
|
+
};
|
|
395
|
+
if (sourceByFile) result._sourceByFile = sourceByFile;
|
|
396
|
+
return result;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
interface ParsedSearchArgs {
|
|
400
|
+
opts: AstSearchOptions;
|
|
401
|
+
listPresets: boolean;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function parseSearchArgs(argv: string[]): ParsedSearchArgs {
|
|
405
|
+
const opts: AstSearchOptions = {
|
|
406
|
+
root: process.cwd(),
|
|
407
|
+
pattern: null,
|
|
408
|
+
kind: null,
|
|
409
|
+
preset: null,
|
|
410
|
+
rule: null,
|
|
411
|
+
json: false,
|
|
412
|
+
limit: 500,
|
|
413
|
+
includeTests: false,
|
|
414
|
+
ignoreDirs: new Set([
|
|
415
|
+
'.git',
|
|
416
|
+
'.next',
|
|
417
|
+
'.yarn',
|
|
418
|
+
'.cache',
|
|
419
|
+
'.octocode',
|
|
420
|
+
'node_modules',
|
|
421
|
+
'dist',
|
|
422
|
+
'coverage',
|
|
423
|
+
'out',
|
|
424
|
+
]),
|
|
425
|
+
context: 0,
|
|
426
|
+
};
|
|
427
|
+
let listPresets = false;
|
|
428
|
+
|
|
429
|
+
for (let i = 0; i < argv.length; i++) {
|
|
430
|
+
const arg = argv[i];
|
|
431
|
+
if (arg === '--pattern' || arg === '-p') {
|
|
432
|
+
opts.pattern = argv[++i];
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (arg.startsWith('--pattern=')) {
|
|
436
|
+
opts.pattern = arg.slice('--pattern='.length);
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (arg === '--kind' || arg === '-k') {
|
|
440
|
+
opts.kind = argv[++i];
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (arg.startsWith('--kind=')) {
|
|
444
|
+
opts.kind = arg.slice('--kind='.length);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (arg === '--preset') {
|
|
448
|
+
opts.preset = argv[++i];
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (arg.startsWith('--preset=')) {
|
|
452
|
+
opts.preset = arg.slice('--preset='.length);
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (arg === '--rule') {
|
|
456
|
+
const raw = argv[++i];
|
|
457
|
+
try {
|
|
458
|
+
opts.rule = JSON.parse(raw) as NapiConfig;
|
|
459
|
+
} catch {
|
|
460
|
+
throw new Error(
|
|
461
|
+
`Invalid --rule JSON: ${raw?.slice(0, 100) ?? '(empty)'}`
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (arg === '--root') {
|
|
467
|
+
opts.root = path.resolve(argv[++i]);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (arg.startsWith('--root=')) {
|
|
471
|
+
opts.root = path.resolve(arg.slice('--root='.length));
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
if (arg === '--json') {
|
|
475
|
+
opts.json = true;
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (arg === '--limit') {
|
|
479
|
+
opts.limit = parseInt(argv[++i], 10);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (arg === '--include-tests') {
|
|
483
|
+
opts.includeTests = true;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (arg === '--context' || arg === '-C') {
|
|
487
|
+
opts.context = parseInt(argv[++i], 10);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (arg === '--list-presets') {
|
|
491
|
+
listPresets = true;
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (arg === '--help' || arg === '-h') {
|
|
495
|
+
printSearchHelp();
|
|
496
|
+
process.exit(0);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (Number.isNaN(opts.limit)) opts.limit = 500;
|
|
501
|
+
if (Number.isNaN(opts.context)) opts.context = 0;
|
|
502
|
+
|
|
503
|
+
return { opts, listPresets };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function printSearchHelp(): void {
|
|
507
|
+
console.log(`
|
|
508
|
+
ast-search ā Structural code search powered by ast-grep
|
|
509
|
+
|
|
510
|
+
Usage:
|
|
511
|
+
node scripts/ast/search.js [options]
|
|
512
|
+
|
|
513
|
+
Search modes (pick one):
|
|
514
|
+
--pattern, -p <code> Match code structurally (e.g. 'console.log($$$ARGS)')
|
|
515
|
+
--kind, -k <kind> Match AST node kind (e.g. 'function_declaration')
|
|
516
|
+
--preset <name> Use a built-in search preset (e.g. 'empty-catch')
|
|
517
|
+
--rule <json> Raw ast-grep rule object as JSON
|
|
518
|
+
|
|
519
|
+
Options:
|
|
520
|
+
--root <path> Search root directory (default: cwd)
|
|
521
|
+
--json Output as JSON
|
|
522
|
+
--limit N Max matches (default: 500)
|
|
523
|
+
--include-tests Include test files
|
|
524
|
+
--context, -C N Lines of context around matches (text output only)
|
|
525
|
+
--list-presets Show available presets and exit
|
|
526
|
+
--help, -h Show this message
|
|
527
|
+
|
|
528
|
+
Pattern wildcards:
|
|
529
|
+
$NAME Match any single AST node
|
|
530
|
+
$$$NAME Match zero or more nodes (variadic)
|
|
531
|
+
|
|
532
|
+
Examples:
|
|
533
|
+
node scripts/ast/search.js -p 'console.log($$$ARGS)' --root ./src
|
|
534
|
+
node scripts/ast/search.js --preset empty-catch --root ./packages
|
|
535
|
+
node scripts/ast/search.js -k function_declaration --json --limit 20
|
|
536
|
+
node scripts/ast/search.js --preset todo-fixme --include-tests
|
|
537
|
+
node scripts/ast/search.js -p 'if ($COND) { return $VAL }' --root ./src
|
|
538
|
+
node scripts/ast/search.js --rule '{"rule":{"kind":"catch_clause"}}' --root ./src
|
|
539
|
+
|
|
540
|
+
Presets:
|
|
541
|
+
${Object.entries(PRESETS)
|
|
542
|
+
.map(([name, p]) => ` ${name.padEnd(22)} ${p.description}`)
|
|
543
|
+
.join('\n')}
|
|
544
|
+
`);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export function formatTextOutput(
|
|
548
|
+
result: AstSearchResult,
|
|
549
|
+
opts: AstSearchOptions,
|
|
550
|
+
_root: string
|
|
551
|
+
): string {
|
|
552
|
+
const lines: string[] = [];
|
|
553
|
+
lines.push(`\nš ${result.query}`);
|
|
554
|
+
lines.push(
|
|
555
|
+
` ${result.totalMatches} matches across ${result.totalFiles} files\n`
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
const ctx = opts.context;
|
|
559
|
+
const sourceMap = result._sourceByFile;
|
|
560
|
+
|
|
561
|
+
let currentFile = '';
|
|
562
|
+
for (const m of result.matches) {
|
|
563
|
+
if (m.file !== currentFile) {
|
|
564
|
+
currentFile = m.file;
|
|
565
|
+
lines.push(`\nāā ${currentFile} āā`);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (ctx > 0 && sourceMap) {
|
|
569
|
+
const srcLines = sourceMap.get(m.file);
|
|
570
|
+
if (srcLines) {
|
|
571
|
+
const start = Math.max(0, m.lineStart - 1 - ctx);
|
|
572
|
+
const end = Math.min(srcLines.length, m.lineEnd + ctx);
|
|
573
|
+
for (let i = start; i < end; i++) {
|
|
574
|
+
const lineNum = i + 1;
|
|
575
|
+
const marker =
|
|
576
|
+
lineNum >= m.lineStart && lineNum <= m.lineEnd ? '>' : ' ';
|
|
577
|
+
lines.push(
|
|
578
|
+
` ${marker} ${String(lineNum).padStart(4)} | ${srcLines[i]}`
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
lines.push('');
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const truncatedText =
|
|
587
|
+
m.text.length > 200 ? m.text.slice(0, 200) + 'ā¦' : m.text;
|
|
588
|
+
const singleLine = truncatedText.replace(/\n/g, 'āµ').replace(/\s+/g, ' ');
|
|
589
|
+
lines.push(
|
|
590
|
+
` L${m.lineStart}:${m.columnStart} [${m.kind}] ${singleLine}`
|
|
591
|
+
);
|
|
592
|
+
|
|
593
|
+
if (m.metaVariables && Object.keys(m.metaVariables).length > 0) {
|
|
594
|
+
for (const [k, v] of Object.entries(m.metaVariables)) {
|
|
595
|
+
const truncV = v.length > 80 ? v.slice(0, 80) + 'ā¦' : v;
|
|
596
|
+
lines.push(` ${k} = ${truncV}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
lines.push('');
|
|
602
|
+
return lines.join('\n');
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
async function main(): Promise<void> {
|
|
606
|
+
const { opts, listPresets } = parseSearchArgs(process.argv.slice(2));
|
|
607
|
+
|
|
608
|
+
if (listPresets) {
|
|
609
|
+
if (opts.json) {
|
|
610
|
+
console.log(JSON.stringify(PRESETS));
|
|
611
|
+
} else {
|
|
612
|
+
console.log('\nAvailable presets:\n');
|
|
613
|
+
for (const [name, preset] of Object.entries(PRESETS)) {
|
|
614
|
+
console.log(` ${name.padEnd(22)} ${preset.description}`);
|
|
615
|
+
}
|
|
616
|
+
console.log('');
|
|
617
|
+
}
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (!opts.pattern && !opts.kind && !opts.preset && !opts.rule) {
|
|
622
|
+
console.error('Error: Must provide --pattern, --kind, --preset, or --rule');
|
|
623
|
+
console.error('Run with --help for usage information.');
|
|
624
|
+
process.exit(1);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const files = collectSearchFiles(opts.root, opts);
|
|
628
|
+
|
|
629
|
+
if (files.length === 0) {
|
|
630
|
+
console.error(`No files found in ${opts.root}`);
|
|
631
|
+
process.exit(1);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const result = runSearch(files, opts, opts.root);
|
|
635
|
+
|
|
636
|
+
if (opts.json) {
|
|
637
|
+
console.log(JSON.stringify(result));
|
|
638
|
+
} else {
|
|
639
|
+
console.log(formatTextOutput(result, opts, opts.root));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (isDirectRun(import.meta.url)) {
|
|
644
|
+
main().catch((error: unknown) => {
|
|
645
|
+
console.error(error);
|
|
646
|
+
process.exit(1);
|
|
647
|
+
});
|
|
648
|
+
}
|