-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint-plugin): [prefer-optional-chain] only look at left operand…
… for `requireNullish` (#8559) * ignore right * invert check * invert * add failing case * move operator check * WIP * WIP * pull check out * lint * add tests * add output * quotes * finish tests * remove last child * add a few more tests * remove skipvalidation * update snapshots
- Loading branch information
1 parent
9e0d9f5
commit eef257b
Showing
7 changed files
with
189 additions
and
30 deletions.
There are no files selected for viewing
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
38 changes: 38 additions & 0 deletions
38
packages/eslint-plugin/src/rules/prefer-optional-chain-utils/checkNullishAndReport.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,38 @@ | ||
import { isTypeFlagSet } from '@typescript-eslint/type-utils'; | ||
import type { | ||
ParserServicesWithTypeInformation, | ||
TSESTree, | ||
} from '@typescript-eslint/utils'; | ||
import type { | ||
ReportDescriptor, | ||
RuleContext, | ||
} from '@typescript-eslint/utils/ts-eslint'; | ||
import { unionTypeParts } from 'ts-api-utils'; | ||
import * as ts from 'typescript'; | ||
|
||
import type { | ||
PreferOptionalChainMessageIds, | ||
PreferOptionalChainOptions, | ||
} from './PreferOptionalChainOptions'; | ||
|
||
export function checkNullishAndReport( | ||
context: RuleContext< | ||
PreferOptionalChainMessageIds, | ||
[PreferOptionalChainOptions] | ||
>, | ||
parserServices: ParserServicesWithTypeInformation, | ||
{ requireNullish }: PreferOptionalChainOptions, | ||
maybeNullishNodes: TSESTree.Expression[], | ||
descriptor: ReportDescriptor<PreferOptionalChainMessageIds>, | ||
): void { | ||
if ( | ||
!requireNullish || | ||
maybeNullishNodes.some(node => | ||
unionTypeParts(parserServices.getTypeAtLocation(node)).some(t => | ||
isTypeFlagSet(t, ts.TypeFlags.Null | ts.TypeFlags.Undefined), | ||
), | ||
) | ||
) { | ||
context.report(descriptor); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/eslint-plugin/tests/docs-eslint-output-snapshots/prefer-optional-chain.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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