Skip to content

feat: port rule no-unsafe-optional-chaining#519

Open
fansenze wants to merge 1 commit intomainfrom
feat/port-rule-no-unsafe-optional-chaining-20260317
Open

feat: port rule no-unsafe-optional-chaining#519
fansenze wants to merge 1 commit intomainfrom
feat/port-rule-no-unsafe-optional-chaining-20260317

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Mar 17, 2026

Summary

Port the no-unsafe-optional-chaining rule from ESLint to rslint.

Disallow use of optional chaining in contexts where the undefined value is not allowed

Related Links

Checklist

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

@gemini-code-assist
Copy link
Copy Markdown
Contributor

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-unsafe-optional-chaining rule, a direct port from ESLint, into the rslint codebase. The primary goal of this rule is to enhance code robustness by identifying and preventing scenarios where optional chaining expressions might unexpectedly resolve to undefined and subsequently cause runtime TypeError exceptions or other undesirable behavior when used in contexts that do not tolerate undefined values. The implementation includes a detailed AST traversal to detect these patterns and offers a configurable option to also flag arithmetic operations that could result in NaN.

Highlights

  • New Rule Implementation: The no-unsafe-optional-chaining rule has been ported from ESLint, aiming to prevent runtime TypeError issues arising from optional chaining expressions evaluating to undefined in incompatible contexts.
  • Comprehensive Detection: The rule identifies unsafe optional chaining usage across various JavaScript constructs, including call expressions, property/element access, new expressions, destructuring assignments, for-of loops, in/instanceof operators, and class extends clauses.
  • Configurable Arithmetic Check: An optional disallowArithmeticOperators setting allows the rule to also flag arithmetic operations on optional chaining results that could lead to NaN, providing enhanced safety.
Changelog
  • internal/config/config.go
    • Imported the new no_unsafe_optional_chaining rule package.
    • Registered the no-unsafe-optional-chaining rule in the global rule registry.
  • internal/rules/no_unsafe_optional_chaining/no_unsafe_optional_chaining.go
    • Added the core implementation of the no-unsafe-optional-chaining rule.
    • Defined two rule messages: unsafeOptionalChain for TypeError risks and unsafeArithmetic for NaN risks.
    • Implemented listeners for various AST node kinds (CallExpression, NewExpression, BinaryExpression, etc.) to detect unsafe optional chaining.
    • Included helper functions hasOptionalChain to recursively check for optional chains and isDestructuringPattern to identify destructuring assignments.
    • Added parseOptions to handle the disallowArithmeticOperators configuration.
  • internal/rules/no_unsafe_optional_chaining/no_unsafe_optional_chaining.md
    • Created documentation for the no-unsafe-optional-chaining rule.
    • Provided detailed explanations of the rule's purpose and examples of both incorrect and correct code.
    • Documented the disallowArithmeticOperators option and its impact.
  • internal/rules/no_unsafe_optional_chaining/no_unsafe_optional_chaining_test.go
    • Added comprehensive unit tests for the no-unsafe-optional-chaining rule.
    • Included a wide range of valid and invalid test cases covering different JavaScript constructs and the disallowArithmeticOperators option.
  • packages/rslint-test-tools/rstest.config.mts
    • Updated the test configuration to include the new no-unsafe-optional-chaining.test.ts file for ESLint rules.
    • Added the new test file to the TypeScript ESLint rules section as well.
  • packages/rslint-test-tools/tests/eslint/rules/snapshots/no-unsafe-optional-chaining.test.ts.snap
    • Generated snapshot tests for the no-unsafe-optional-chaining rule's invalid cases.
  • packages/rslint-test-tools/tests/eslint/rules/no-unsafe-optional-chaining.test.ts
    • Added basic test cases for the no-unsafe-optional-chaining rule using RuleTester.
Activity
  • The no-unsafe-optional-chaining rule has been implemented and integrated.
  • Comprehensive test cases have been added to validate the rule's behavior, covering various scenarios and options.
  • Documentation for the new rule, including its purpose, examples, and configuration options, has been created.
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
Contributor

@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-unsafe-optional-chaining rule from ESLint. The implementation is comprehensive, covering many edge cases, and includes good documentation and tests.

I've found a couple of issues:

  • A correctness bug in the hasOptionalChain helper function which fails to correctly handle TSNonNullExpression.
  • A redundant test file entry in the test runner configuration.

Details and suggestions are in the review comments. Overall, great work on porting this complex rule.

@fansenze fansenze force-pushed the feat/port-rule-no-unsafe-optional-chaining-20260317 branch 2 times, most recently from 8c72da0 to c39695f Compare March 17, 2026 10:29
@fansenze fansenze force-pushed the feat/port-rule-no-unsafe-optional-chaining-20260317 branch 4 times, most recently from 1462c63 to 57f67ba Compare March 18, 2026 02:44
@fansenze fansenze force-pushed the feat/port-rule-no-unsafe-optional-chaining-20260317 branch from 57f67ba to b2ae2a8 Compare March 18, 2026 02:58
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.

1 participant