Skip to content

fix: align no-unused-vars with ESLint implementation#558

Merged
chenjiahan merged 1 commit intomainfrom
fix/no-unused-vars-declare-function-20260320
Mar 23, 2026
Merged

fix: align no-unused-vars with ESLint implementation#558
chenjiahan merged 1 commit intomainfrom
fix/no-unused-vars-declare-function-20260320

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Mar 20, 2026

Summary

Fixes #555

Aligns @typescript-eslint/no-unused-vars with the original typescript-eslint rule by fixing all known input/output mismatches. Verified against the rsbuild real-world codebase with identical results to ESLint.

Core fixes (issue #555)

  • declare function / overload signature parameters: no longer falsely reported as unused
  • Unused declare function / overloads: correctly reported (with dedup via symbol pointer)

New declaration type support

  • class / interface / type / enum: added KindClassDeclaration, KindInterfaceDeclaration, KindTypeAliasDeclaration, KindEnumDeclaration listeners
  • declare namespace / declare module: added KindModuleDeclaration listener; members not individually reported; declare global and module augmentations skipped; namespace augmentation (multi-declaration symbol) skipped
  • Imports: added KindImportSpecifier, KindImportClause, KindNamespaceImport, KindImportEqualsDeclaration listeners
  • Destructuring: processBindingName recursively handles ObjectBindingPattern / ArrayBindingPattern for both variable declarations and parameter destructuring

Behavioral fixes

  • Scope-aware symbol resolution: replaced name-based map[string] with map[*ast.Symbol] using TypeChecker.GetSymbolAtLocation + SkipAlias, eliminating cross-scope false negatives
  • Shorthand property: { stats } correctly counts as usage via GetShorthandAssignmentValueSymbol
  • isExported over-matching: parameters inside export class/export function are no longer treated as exported
  • isInTypeContext: removed AsExpression/TypeAssertionExpression/SatisfiesExpression from type context (their expression operand is a value context)
  • Type declarations as type-only usage: interface/type/enum/imports used in type context are valid usage, not reported as "only used as type"
  • .d.ts files: skipped entirely (all declarations are ambient)

Configuration fixes

  • parseOptions array format: handles ESLint's [{ option: value }] convention
  • args: "after-used": implemented — only reports unused params after the last used one; default-value params act as boundary
  • varsIgnorePattern scope: no longer applies to parameters or caught errors
  • argsIgnorePattern / args: "none": correctly applies to destructured parameter elements
  • reportUsedIgnorePattern: now applies to all three ignore patterns (vars/args/caughtErrors)
  • ignoreRestSiblings: implemented for destructuring patterns
  • hasTypeAnnotation false positive: removed incorrect logic that treated type-annotated variables as "used"
  • Regexp pre-compilation: patterns compiled once at parse time instead of per-match

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the @typescript-eslint/no-unused-vars rule by bringing its behavior closer to the original ESLint implementation. The changes focus on improving the accuracy of unused variable detection, particularly for TypeScript-specific constructs like declare function overloads and declare namespace members. It addresses several false positives and negatives, introduces new functionality for parameter handling, and optimizes internal processes, resulting in a more reliable and consistent linting experience.

Highlights

  • Improved declare function and Overload Handling: Corrected false positives for declare function and overload signature parameters, ensuring they are no longer reported as unused. Conversely, truly unused declare function and overloads are now correctly identified and reported.
  • Enhanced declare namespace Processing: Refined the handling of declare namespace members; individual members are no longer reported, but the unused namespace name itself is now correctly flagged via a new KindModuleDeclaration handler.
  • Fixed isExported Over-matching: Resolved an issue where parameters inside export class or export function were incorrectly treated as exported, leading to false negatives.
  • Migrated to Scope-Aware Symbol Resolution: Replaced the previous name-based map[string] for variable usage tracking with a more robust map[*ast.Symbol] approach, utilizing TypeChecker.GetSymbolAtLocation and SkipAlias to eliminate cross-scope false negatives.
  • Supported ESLint's Array Option Format: Updated the parseOptions function to correctly handle ESLint's [{ option: value }] convention for rule configurations.
  • Implemented args: "after-used" Option: Added support for the args: "after-used" option, which only reports unused parameters that appear after the last used parameter in a function signature.
  • Refined Ignore Pattern Scoping: Adjusted the varsIgnorePattern to no longer apply to parameters or caught errors, ensuring more precise ignore behavior. Additionally, reportUsedIgnorePattern now correctly applies to all three ignore patterns (vars, args, caughtErrors).
  • Removed hasTypeAnnotation False Positive: Eliminated incorrect logic that treated type-annotated variables as implicitly 'used', preventing false positives for unused variables with type annotations.
  • Optimized Regex Pre-compilation: Improved performance by pre-compiling regex patterns once at parse time, rather than compiling them per-match.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly refactors the no-unused-vars rule to align its behavior with the original ESLint implementation, particularly for TypeScript-specific constructs. The core changes involve moving from name-based variable usage tracking to a more robust symbol-based resolution, which correctly handles scoping, aliases, and various declaration types like declare function overloads and declare namespace members. The introduction of compilePatterns for regex pre-compilation and the matchesIgnorePattern function centralizes and improves the handling of ignore patterns. Additionally, the after-used parameter logic has been correctly implemented, and false positives related to type annotations have been removed. The comprehensive new test cases thoroughly validate these improvements.

@fansenze fansenze force-pushed the fix/no-unused-vars-declare-function-20260320 branch 12 times, most recently from c1f88a7 to 1902b10 Compare March 21, 2026 02:14
@fansenze fansenze force-pushed the fix/no-unused-vars-declare-function-20260320 branch from 1902b10 to 1ac3fb1 Compare March 21, 2026 04:05
@chenjiahan chenjiahan merged commit 06f4f3f into main Mar 23, 2026
14 checks passed
@chenjiahan chenjiahan deleted the fix/no-unused-vars-declare-function-20260320 branch March 23, 2026 02:20
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.

[Bug]: @typescript-eslint/no-unused-vars reports false positives

2 participants