Skip to content

feat(linter): add eslint/class-methods-use-this rule #12977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

PeterCardenas
Copy link
Contributor

@PeterCardenas PeterCardenas commented Aug 11, 2025

Adds the class-methods-use-this rule.

Added a bunch of tests from the eslint repo and the typescript eslint repo, without the ones that rely on particular option configurations.

Works towards #479

@PeterCardenas PeterCardenas requested a review from camc314 as a code owner August 11, 2025 00:00
Copy link
Contributor

graphite-app bot commented Aug 11, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Aug 11, 2025
Copy link

codspeed-hq bot commented Aug 11, 2025

CodSpeed Instrumentation Performance Report

Merging #12977 will not alter performance

Comparing PeterCardenas:class-methods-use-this (7a92472) with main (0b61338)

Summary

✅ 34 untouched benchmarks

@PeterCardenas PeterCardenas force-pushed the class-methods-use-this branch from 6926f50 to b548897 Compare August 11, 2025 01:34
Copy link
Contributor

@huangtiandi1999 huangtiandi1999 left a comment

Choose a reason for hiding this comment

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

I think we can simplify the implementation a bit. I’ve left two comments for changes, and need @camc314 to confirm them.

@PeterCardenas
Copy link
Contributor Author

@huangtiandi1999 fixed in latest commit! code is much easier to reason about now and was able to figure out the last few test cases as well.

@PeterCardenas PeterCardenas force-pushed the class-methods-use-this branch from b1c8e83 to a422933 Compare August 12, 2025 05:30
Copy link
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

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

thanks!

@camc314 camc314 requested review from Copilot and removed request for huangtiandi1999 August 14, 2025 16:41
Copy link
Contributor

@Copilot 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 implements the ESLint class-methods-use-this rule for the Oxc linter. The rule enforces that class methods utilize the this keyword, helping identify methods that could be converted to static methods for better performance and clarity.

Key changes implemented:

  • Added comprehensive rule logic with configuration options for method exceptions, class field enforcement, override method handling, and interface implementation handling
  • Implemented AST visitor pattern to detect this usage within method bodies while properly handling scope boundaries
  • Added extensive test coverage with 422 test cases covering various method types and edge cases

Reviewed Changes

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

File Description
crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs Main rule implementation with configuration parsing, AST analysis, and comprehensive test suite
crates/oxc_linter/src/rules.rs Module registration for the new rule
crates/oxc_linter/src/snapshots/eslint_class_methods_use_this.snap Snapshot test output showing diagnostic messages for various failing test cases

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants