feat: port rule @typescript-eslint/prefer-optional-chain#458
feat: port rule @typescript-eslint/prefer-optional-chain#458
Conversation
Summary of ChangesHello @fansenze, 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 a new linting rule, Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This is an impressive and comprehensive port of the @typescript-eslint/prefer-optional-chain rule. The implementation covers a wide range of complex scenarios, including different logical operators, comparison types, type-aware checks, and even DeMorgan's law for || chains. The test suite is extensive and provides great confidence in the rule's correctness. I've found a few areas for potential improvement regarding code clarity and a couple of possible bugs, which I've detailed in the specific comments. Overall, this is a high-quality contribution.
internal/plugins/typescript/rules/prefer_optional_chain/analyze_chain.go
Show resolved
Hide resolved
internal/plugins/typescript/rules/prefer_optional_chain/gather_logical_operands.go
Outdated
Show resolved
Hide resolved
internal/plugins/typescript/rules/prefer_optional_chain/gather_logical_operands.go
Outdated
Show resolved
Hide resolved
7a74ba6 to
fbef01d
Compare
Port the prefer-optional-chain rule from TypeScript-ESLint to Go. Key implementation details: - Gather and classify logical operands from && and || chains - Compare AST nodes to detect subset/prefix relationships for chain building - Generate optional chain expressions with correct ?. placement - Handle DeMorgan inversion for || chains (restore original operators for output) - Type-aware fix vs suggestion decision based on expression return types - Support type parameters in call expressions (e.g., foo?.<string>()?.bar) - Handle all comparison types: boolean, loose/strict null/undefined equality Includes comprehensive Go unit tests and TypeScript integration tests covering base cases, hand-crafted edge cases, and generated permutations.
fbef01d to
911891c
Compare
Summary
Port the
@typescript-eslint/prefer-optional-chainrule from TypeScript-ESLint to Go.This rule enforces using concise optional chain expressions (
?.) instead of chained logical ANDs (&&), negated logical ORs (||), or empty object coalescing patterns ((foo || {}).bar).Implementation highlights
&&/||chains into operands, classifying each as boolean, strict/loose null/undefined equality, typeof, or negationfoo<string>()), type references (foo<T>()), and qualified names||chains, correctly inverts comparison operators during classification and restores original operators for output generation?.placement, preserving type arguments, call arguments, element access, and commentscheckAny,checkUnknown,checkString,checkNumber,checkBoolean,checkBigInt,requireNullish,allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing)Related Links
Checklist