Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [prefer-optional-chain] handle cases where the first operands are unrelated to the rest of the chain and add type info#6397
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
32 commits Select commitHold shift + click to select a range
a657697
feat(eslint-plugin): [prefer-optional-chain] support more cases
bradzacher0eaad01
wip
bradzacherbbf5788
WIP
bradzachera519c00
WIP
bradzachera354d0a
temp
bradzachere7efa91
Merge branch 'main' into 6332-refactor-prefer-optional-chain
bradzacherb6fcab3
tests are passing without fixers
bradzachere51128a
add more test
bradzacher34671ec
fix lint errors across codebase revealed by changes
bradzachere1c5eee
more test cases
bradzacher3c387a9
more test cases
bradzachercb28981
Merge branch 'v6' into 6332-refactor-prefer-optional-chain
bradzacher74d0d0b
lint after rebase
bradzacher74ff6d0
make it type-aware
bradzachercff21e5
Merge branch 'v6' into 6332-refactor-prefer-optional-chain
bradzacher2f66664
update configs
bradzacher1b17c93
update docs
bradzacherf45a30f
schema snap
bradzacher0a3a548
Merge branch 'v6' into 6332-refactor-prefer-optional-chain
bradzacherd90a7c5
WIP add fixer back
bradzacherd0a67ae
algorithm improvements and some test fixes
bradzacher3071db7
modularise the rule
bradzacher709939d
more work
bradzacher1a04ff7
Merge branch 'v6' into 6332-refactor-prefer-optional-chain
bradzachera66a4a5
finished tests
bradzacher52ca2e1
few more tests to ensure coverage
bradzacherc58118c
remove name cos spelling
bradzacher6e558a4
add docs for the new flag
bradzacher7d08e25
update schema snapshot
bradzacherd37c271
Merge branch 'v6' into 6332-refactor-prefer-optional-chain
bradzacher3e22d5f
review comments
bradzacher4c36891
configs
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletionspackages/ast-spec/src/expression/BinaryExpression/BinaryOperatorToText.ts
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,35 @@ | ||
import type { SyntaxKind } from 'typescript'; | ||
// the members of ts.BinaryOperator | ||
export interface BinaryOperatorToText { | ||
[SyntaxKind.InstanceOfKeyword]: 'instanceof'; | ||
[SyntaxKind.InKeyword]: 'in'; | ||
// math | ||
[SyntaxKind.AsteriskAsteriskToken]: '**'; | ||
[SyntaxKind.AsteriskToken]: '*'; | ||
[SyntaxKind.SlashToken]: '/'; | ||
[SyntaxKind.PercentToken]: '%'; | ||
[SyntaxKind.PlusToken]: '+'; | ||
[SyntaxKind.MinusToken]: '-'; | ||
// bitwise | ||
[SyntaxKind.AmpersandToken]: '&'; | ||
[SyntaxKind.BarToken]: '|'; | ||
[SyntaxKind.CaretToken]: '^'; | ||
[SyntaxKind.LessThanLessThanToken]: '<<'; | ||
[SyntaxKind.GreaterThanGreaterThanToken]: '>>'; | ||
[SyntaxKind.GreaterThanGreaterThanGreaterThanToken]: '>>>'; | ||
// logical | ||
[SyntaxKind.AmpersandAmpersandToken]: '&&'; | ||
[SyntaxKind.BarBarToken]: '||'; | ||
[SyntaxKind.LessThanToken]: '<'; | ||
[SyntaxKind.LessThanEqualsToken]: '<='; | ||
[SyntaxKind.GreaterThanToken]: '>'; | ||
[SyntaxKind.GreaterThanEqualsToken]: '>='; | ||
[SyntaxKind.EqualsEqualsToken]: '=='; | ||
[SyntaxKind.EqualsEqualsEqualsToken]: '==='; | ||
[SyntaxKind.ExclamationEqualsEqualsToken]: '!=='; | ||
[SyntaxKind.ExclamationEqualsToken]: '!='; | ||
} |
6 changes: 5 additions & 1 deletionpackages/ast-spec/src/expression/BinaryExpression/spec.ts
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
20 changes: 20 additions & 0 deletionspackages/ast-spec/tests/BinaryOperatorToText.type-test.ts
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,20 @@ | ||
import type { | ||
AssignmentOperator, | ||
BinaryOperator, | ||
SyntaxKind, | ||
} from 'typescript'; | ||
import type { BinaryOperatorToText } from '../src'; | ||
type BinaryOperatorWithoutInvalidTypes = Exclude< | ||
BinaryOperator, | ||
| AssignmentOperator // --> AssignmentExpression | ||
| SyntaxKind.CommaToken // -> SequenceExpression | ||
| SyntaxKind.QuestionQuestionToken // -> LogicalExpression | ||
>; | ||
type _Test = { | ||
readonly [T in BinaryOperatorWithoutInvalidTypes]: BinaryOperatorToText[T]; | ||
// If there are any BinaryOperator members that don't have a corresponding | ||
// BinaryOperatorToText, then this line will error with "Type 'T' cannot | ||
// be used to index type 'BinaryOperatorToText'." | ||
}; |
199 changes: 195 additions & 4 deletionspackages/eslint-plugin/docs/rules/prefer-optional-chain.md
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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/package.json
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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/disable-type-checked.ts
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
1 change: 0 additions & 1 deletionpackages/eslint-plugin/src/configs/stylistic.ts
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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/rules/class-literal-property-style.ts
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
2 changes: 1 addition & 1 deletionpackages/eslint-plugin/src/rules/explicit-module-boundary-types.ts
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
4 changes: 2 additions & 2 deletionspackages/eslint-plugin/src/rules/indent.ts
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
5 changes: 2 additions & 3 deletionspackages/eslint-plugin/src/rules/no-extraneous-class.ts
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
3 changes: 1 addition & 2 deletionspackages/eslint-plugin/src/rules/prefer-includes.ts
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
14 changes: 14 additions & 0 deletionspackages/eslint-plugin/src/rules/prefer-optional-chain-utils/PreferOptionalChainOptions.ts
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,14 @@ | ||
export type PreferOptionalChainMessageIds = | ||
| 'preferOptionalChain' | ||
| 'optionalChainSuggest'; | ||
export interface PreferOptionalChainOptions { | ||
checkAny?: boolean; | ||
checkUnknown?: boolean; | ||
checkString?: boolean; | ||
checkNumber?: boolean; | ||
checkBoolean?: boolean; | ||
checkBigInt?: boolean; | ||
requireNullish?: boolean; | ||
allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing?: boolean; | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.