Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): [prefer-optional-chain] handle cases where the f…
…irst operands are unrelated to the rest of the chain and add type info (#6397)
- Loading branch information
1 parent
ca3fc02
commit 02a37c4
Showing
27 changed files
with
4,531 additions
and
2,125 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/ast-spec/src/expression/BinaryExpression/BinaryOperatorToText.ts
This file contains 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]: '!='; | ||
} |
This file contains 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 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'." | ||
}; |
This file contains 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 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 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 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 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 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 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 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 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 deletions
14
packages/eslint-plugin/src/rules/prefer-optional-chain-utils/PreferOptionalChainOptions.ts
This file contains 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.