Skip to content

refactor: extract shared base class for JavaScript and TypeScript analyzers#266

Merged
askpt merged 4 commits intomainfrom
repo-assist/improve-js-ts-base-class-8a6b08afff0d8e17
Apr 22, 2026
Merged

refactor: extract shared base class for JavaScript and TypeScript analyzers#266
askpt merged 4 commits intomainfrom
repo-assist/improve-js-ts-base-class-8a6b08afff0d8e17

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

JavaScriptMetricsAnalyzer and TypeScriptMetricsAnalyzer were nearly identical — ~400 lines of duplicated analysis logic. This PR extracts the shared algorithm into a new JsLikeMetricsAnalyzer base class, reducing the two files to thin wrappers that only supply the language-specific Tree-sitter parser.

What Changed

File Change
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts New — shared base class with all analysis logic
src/metricsAnalyzer/languages/javascriptAnalyzer.ts Refactored — extends base class, ~426→51 lines
src/metricsAnalyzer/languages/typescriptAnalyzer.ts Refactored — extends base class, ~441→56 lines

Net change: -821 lines added, +444 lines (saves ~380 lines of duplication).

No Behaviour Changes

  • The public analyzeFile() API of both classes is unchanged
  • The return type JsLikeFunctionMetrics is identical in shape to the previous private JavaScriptFunctionMetrics / TypeScriptFunctionMetrics interfaces
  • The MetricsAnalyzerFactory registers both analyzers identically — no changes needed there
  • All existing extension activation events (onLanguage:javascript, onLanguage:typescript, etc.) remain unchanged

Trade-offs

  • Adding a new supported JS-like language in future only requires extending JsLikeMetricsAnalyzer and setting up a parser — the analysis logic is inherited automatically
  • The base class interfaces (JsLikeMetricsDetail, JsLikeFunctionMetrics) are now exported, making them available for future test files without duplication

Test Status

  • npm run compile — no errors
  • npm run lint — no warnings
  • ⚠️ npm test (vscode-test) — requires VS Code download; not available in this sandboxed environment (known infrastructure limitation). Tests run correctly in Codespaces/local environments.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@aae6af93b4035df74630817afa13c528d6b74fe6

…lyzers

The JavaScriptMetricsAnalyzer and TypeScriptMetricsAnalyzer classes were
nearly identical (~400 lines of duplicated logic). Extract the common
analysis algorithm into a new JsLikeMetricsAnalyzer base class in
jsLikeAnalyzer.ts. Both analyzers now extend this base, providing only
the language-specific Tree-sitter parser instance.

No behaviour changes — the public API and analysis output are identical.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt
Copy link
Copy Markdown
Owner

askpt commented Apr 22, 2026

@copilot resolve the merge conflicts in this pull request

… class

Merge origin/main into PR branch. Conflicts in javascriptAnalyzer.ts and
typescriptAnalyzer.ts resolved by keeping the thin-wrapper versions from
this branch. The for_of_statement bug fix from PR #267 is ported into the
shared base class jsLikeAnalyzer.ts.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [Repo Assist] refactor: extract shared base class for JavaScript and TypeScript analyzers refactor: extract shared base class for JavaScript and TypeScript analyzers Apr 22, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 90.74492% with 41 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.92%. Comparing base (533b8fe) to head (a66540a).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts 90.16% 40 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #266      +/-   ##
==========================================
- Coverage   68.81%   66.92%   -1.89%     
==========================================
  Files           8        9       +1     
  Lines        2937     2561     -376     
  Branches      279      222      -57     
==========================================
- Hits         2021     1714     -307     
+ Misses        914      846      -68     
+ Partials        2        1       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@askpt askpt marked this pull request as ready for review April 22, 2026 16:15
@askpt askpt self-requested a review as a code owner April 22, 2026 16:15
Copilot AI review requested due to automatic review settings April 22, 2026 16:15
askpt and others added 2 commits April 22, 2026 17:18
Add dedicated test suite for the shared base class exercising:
- Nested method_definition and function_declaration reason strings
- Anonymous function expression naming ('(anonymous)')
- Unlabeled continue (no complexity)
- for...in vs for...of detection
- Deep nesting with mixed control flow
- Catch clause nesting
- do...while inside for loop
- Nested ternary expressions
- Logical operator combinations inside control flow
- Nested function complexity isolation
- Empty/edge cases (empty body, empty source, positions)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
function_declaration is not in isNestedFunction(), so it is analyzed as
part of the outer function body rather than collected as a separate function.
Fix the test to assert 1 result instead of 2.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the JS/TS cognitive complexity analyzers by extracting their shared Tree-sitter traversal and scoring logic into a new JsLikeMetricsAnalyzer base class, leaving JavaScriptMetricsAnalyzer and TypeScriptMetricsAnalyzer as thin parser-configuring wrappers.

Changes:

  • Added JsLikeMetricsAnalyzer (and shared metric interfaces) to centralize JS-like analysis logic.
  • Updated JavaScriptMetricsAnalyzer and TypeScriptMetricsAnalyzer to extend the shared base class.
  • Simplified wrapper analyzers’ analyzeFile implementations to delegate directly to the base implementation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts Introduces the shared JS/TS analysis implementation and shared metric types.
src/metricsAnalyzer/languages/javascriptAnalyzer.ts Refactored into a thin wrapper that configures the JS parser and delegates to the base class.
src/metricsAnalyzer/languages/typescriptAnalyzer.ts Refactored into a thin wrapper that configures the TS parser and delegates to the base class.

Comment thread src/metricsAnalyzer/languages/jsLikeAnalyzer.ts
@askpt askpt merged commit 9d73035 into main Apr 22, 2026
7 checks passed
@askpt askpt deleted the repo-assist/improve-js-ts-base-class-8a6b08afff0d8e17 branch April 22, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants