-
-
Notifications
You must be signed in to change notification settings - Fork 856
feat(linter/prefer-optional-chain): add rule #17831
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
Merged
graphite-app
merged 1 commit into
main
from
c/01-09-feat_linter_prefer-optional-chain_add_rule
Jan 9, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // prefer-optional-chain: The pattern `foo && foo.bar` should use optional chaining `foo?.bar` | ||
| declare const fooOptC: { bar: number } | null | undefined; | ||
| fooOptC && fooOptC.bar; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
crates/oxc_linter/src/rules/typescript/prefer_optional_chain.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| use oxc_macros::declare_oxc_lint; | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::rule::{DefaultRuleConfig, Rule}; | ||
|
|
||
| #[derive(Debug, Default, Clone, Deserialize)] | ||
| pub struct PreferOptionalChain(Box<PreferOptionalChainConfig>); | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] | ||
| #[serde(rename_all = "camelCase", default)] | ||
| pub struct PreferOptionalChainConfig { | ||
| /// Allow autofixers that will change the return type of the expression. | ||
| /// This option is considered unsafe as it may break the build. | ||
| pub allow_potentially_unsafe_fixes_that_modify_the_return_type_i_know_what_im_doing: bool, | ||
|
|
||
| /// Check operands that are typed as `any` when inspecting "loose boolean" operands. | ||
| pub check_any: bool, | ||
|
|
||
| /// Check operands that are typed as `bigint` when inspecting "loose boolean" operands. | ||
| pub check_big_int: bool, | ||
|
|
||
| /// Check operands that are typed as `boolean` when inspecting "loose boolean" operands. | ||
| pub check_boolean: bool, | ||
|
|
||
| /// Check operands that are typed as `number` when inspecting "loose boolean" operands. | ||
| pub check_number: bool, | ||
|
|
||
| /// Check operands that are typed as `string` when inspecting "loose boolean" operands. | ||
| pub check_string: bool, | ||
|
|
||
| /// Check operands that are typed as `unknown` when inspecting "loose boolean" operands. | ||
| pub check_unknown: bool, | ||
|
|
||
| /// Skip operands that are not typed with `null` and/or `undefined` when inspecting | ||
| /// "loose boolean" operands. | ||
| pub require_nullish: bool, | ||
| } | ||
|
|
||
| impl Default for PreferOptionalChainConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| allow_potentially_unsafe_fixes_that_modify_the_return_type_i_know_what_im_doing: false, | ||
| check_any: true, | ||
| check_big_int: true, | ||
| check_boolean: true, | ||
| check_number: true, | ||
| check_string: true, | ||
| check_unknown: true, | ||
| require_nullish: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// Enforce using concise optional chain expressions instead of chained logical AND | ||
| /// operators, negated logical OR operators, or empty objects. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// TypeScript 3.7 introduced optional chaining (`?.`) which provides a more concise | ||
| /// and readable way to access properties on potentially nullish values. Using optional | ||
| /// chaining instead of logical AND chains (`&&`) or other patterns improves code clarity. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```ts | ||
| /// foo && foo.bar; | ||
| /// foo && foo.bar && foo.bar.baz; | ||
| /// foo && foo['bar']; | ||
| /// foo && foo.bar && foo.bar.baz && foo.bar.baz.buzz; | ||
| /// foo && foo.bar && foo.bar.baz.buzz; | ||
| /// foo && foo.bar.baz && foo.bar.baz.buzz; | ||
| /// (foo || {}).bar; | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```ts | ||
| /// foo?.bar; | ||
| /// foo?.bar?.baz; | ||
| /// foo?.['bar']; | ||
| /// foo?.bar?.baz?.buzz; | ||
| /// foo?.bar?.baz.buzz; | ||
| /// foo?.bar.baz?.buzz; | ||
| /// foo?.bar; | ||
| /// ``` | ||
| PreferOptionalChain(tsgolint), | ||
| typescript, | ||
| style, | ||
| fix, | ||
| config = PreferOptionalChainConfig, | ||
| ); | ||
|
|
||
| impl Rule for PreferOptionalChain { | ||
| fn from_configuration(value: serde_json::Value) -> Result<Self, serde_json::error::Error> { | ||
| Ok(serde_json::from_value::<DefaultRuleConfig<Self>>(value) | ||
| .unwrap_or_default() | ||
| .into_inner()) | ||
| } | ||
|
|
||
| fn to_configuration(&self) -> Option<Result<serde_json::Value, serde_json::Error>> { | ||
| Some(serde_json::to_value(&*self.0)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.