Skip to content

feat: port rule no-this-before-super#539

Merged
chenjiahan merged 1 commit intomainfrom
feat/port-rule-no-this-before-super-20260317
Mar 27, 2026
Merged

feat: port rule no-this-before-super#539
chenjiahan merged 1 commit intomainfrom
feat/port-rule-no-this-before-super-20260317

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Mar 17, 2026

Summary

Port the no-this-before-super rule from ESLint to rslint.

Disallow this/super before calling super() in constructors

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 introduces the no-this-before-super linting rule, a critical addition for maintaining correct JavaScript class inheritance patterns. The rule prevents common runtime errors by ensuring that this and super property access are only used after the super() constructor has been invoked in derived classes. The implementation meticulously handles diverse code structures and control flows to provide robust and accurate static analysis.

Highlights

  • New Rule: no-this-before-super: A new linting rule, no-this-before-super, has been ported from ESLint to rslint. This rule enforces that this or super (for property access) are not used in derived class constructors before super() has been called.
  • Comprehensive Control Flow Analysis: The rule implementation includes sophisticated logic to track the super() call status across various control flow statements, such as if/else, switch, try/catch/finally, and loops, ensuring accurate violation detection even in complex scenarios.
  • AST Traversal and Scope Management: The rule correctly identifies class and function boundaries to prevent reporting violations in nested scopes where this or super might be valid, and distinguishes between super() calls and super property access.
  • Extensive Test Coverage: Both Go-based unit tests and JavaScript-based rule tester examples, along with snapshot tests, have been added to validate the rule's behavior for various valid and invalid code patterns.
Changelog
  • internal/config/config.go
    • Imported the new no_this_before_super rule package.
    • Registered the no-this-before-super rule with the global rule registry.
  • internal/rules/no_this_before_super/no_this_before_super.go
    • Added the core implementation for the no-this-before-super rule.
    • Implemented functions to identify class nodes, check for valid extends clauses, and determine function/class scope boundaries.
    • Introduced superStatus enum to track super() call state across code paths.
    • Developed recursive functions (checkStatements, checkStatement, checkExpressionStatement, etc.) to traverse the AST and detect this/super usage before super().
    • Included helper functions (isSuperCall, expressionContainsSuperCall, nodeContainsSuperCall) to detect super() calls within expressions and statements.
    • Added logic to check arguments of super() calls for violations.
  • internal/rules/no_this_before_super/no_this_before_super.md
    • Added documentation for the no-this-before-super rule.
    • Provided examples of both incorrect and correct code usage for the rule.
  • internal/rules/no_this_before_super/no_this_before_super_test.go
    • Added Go-based unit tests for the no-this-before-super rule.
    • Included a comprehensive set of valid and invalid test cases covering various scenarios.
  • packages/rslint-test-tools/rstest.config.mts
    • Updated the test configuration to include the new no-this-before-super.test.ts file.
  • packages/rslint-test-tools/tests/eslint/rules/snapshots/no-this-before-super.test.ts.snap
    • Added snapshot test results for the no-this-before-super rule.
  • packages/rslint-test-tools/tests/eslint/rules/no-this-before-super.test.ts
    • Added JavaScript-based test cases for the no-this-before-super rule using the RuleTester.
Activity
  • The author has indicated that tests have been updated.
  • The author has indicated that documentation has been updated.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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 ports the no-this-before-super rule from ESLint. The implementation is comprehensive, covering various control flow scenarios. However, I've found a critical logic error in the control flow analysis concerning nested blocks. The function responsible for analyzing statements within a block incorrectly resets the analysis state, leading to incorrect diagnostics in many common scenarios like if statements, try-catch blocks, and loops. I've provided a series of suggestions to correct this fundamental issue.

@fansenze fansenze force-pushed the feat/port-rule-no-this-before-super-20260317 branch from f3b1be7 to dcd909a Compare March 17, 2026 11:36
@fansenze fansenze force-pushed the feat/port-rule-no-this-before-super-20260317 branch 5 times, most recently from 4fdd1a8 to 0fa716f Compare March 18, 2026 07:28
@fansenze fansenze force-pushed the feat/port-rule-no-this-before-super-20260317 branch from 0fa716f to 1af1580 Compare March 27, 2026 08:17
@fansenze fansenze force-pushed the feat/port-rule-no-this-before-super-20260317 branch from 1af1580 to 09df4dd Compare March 27, 2026 08:35
@chenjiahan chenjiahan enabled auto-merge (squash) March 27, 2026 09:01
@chenjiahan chenjiahan merged commit 2ebdfff into main Mar 27, 2026
10 checks passed
@chenjiahan chenjiahan deleted the feat/port-rule-no-this-before-super-20260317 branch March 27, 2026 09:04
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