Skip to content
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

fix(eslint-plugin): [prefer-optional-chain] only look at left operand for requireNullish #8559

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5c5a4c2
ignore right
abrahamguo Feb 27, 2024
d74cc6c
invert check
abrahamguo Feb 27, 2024
2e3eec3
invert
abrahamguo Feb 27, 2024
3e22435
add failing case
abrahamguo Feb 27, 2024
08681ed
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Feb 27, 2024
14d3aeb
move operator check
abrahamguo Feb 27, 2024
cece182
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 1, 2024
0e4fcf4
WIP
abrahamguo Mar 4, 2024
fe06a96
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 5, 2024
3969122
WIP
abrahamguo Mar 7, 2024
703b3c7
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 11, 2024
d6ff24c
pull check out
abrahamguo Mar 11, 2024
2833796
lint
abrahamguo Mar 11, 2024
52f9448
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 12, 2024
386d63c
add tests
abrahamguo Mar 12, 2024
2738405
add output
abrahamguo Mar 12, 2024
5e04ce9
quotes
abrahamguo Mar 12, 2024
c9f0c62
finish tests
abrahamguo Mar 12, 2024
4747282
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 13, 2024
ed4aaa7
remove last child
abrahamguo Mar 13, 2024
8b88d03
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 13, 2024
606e1f6
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Mar 16, 2024
fd2b38f
add a few more tests
abrahamguo Mar 16, 2024
b9fc141
Merge branch 'main' of github.com:abrahamguo/typescript-eslint into p…
abrahamguo Apr 7, 2024
2bba0ac
remove skipvalidation
abrahamguo Apr 7, 2024
15d7dbc
update snapshots
abrahamguo Apr 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ type Operand = ValidOperand | InvalidOperand;
const NULLISH_FLAGS = ts.TypeFlags.Null | ts.TypeFlags.Undefined;
function isValidFalseBooleanCheckType(
node: TSESTree.Node,
operator: TSESTree.LogicalExpression['operator'],
operatorNode: TSESTree.LogicalExpression,
checkType: 'true' | 'false',
parserServices: ParserServicesWithTypeInformation,
options: PreferOptionalChainOptions,
): boolean {
const type = parserServices.getTypeAtLocation(node);
const types = unionTypeParts(type);

const { operator } = operatorNode;
const disallowFalseyLiteral =
(operator === '||' && checkType === 'false') ||
(operator === '&&' && checkType === 'true');
Expand All @@ -94,7 +95,7 @@ function isValidFalseBooleanCheckType(
}
}

if (options.requireNullish === true) {
if (options.requireNullish === true && operatorNode.left === node) {
abrahamguo marked this conversation as resolved.
Show resolved Hide resolved
return types.some(t => isTypeFlagSet(t, NULLISH_FLAGS));
}

Expand Down Expand Up @@ -258,7 +259,7 @@ export function gatherLogicalOperands(
operand.operator === '!' &&
isValidFalseBooleanCheckType(
operand.argument,
node.operator,
node,
'false',
parserServices,
options,
Expand All @@ -285,7 +286,7 @@ export function gatherLogicalOperands(
if (
isValidFalseBooleanCheckType(
operand,
node.operator,
node,
'true',
parserServices,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,16 @@ describe('hand-crafted cases', () => {
],
},

// requireNullish
{
code: `
declare const thing1: string | null;
thing1 && thing1.toString();
`,
options: [{ requireNullish: false }],
errors: [{ messageId: `preferOptionalChain` }],
},

abrahamguo marked this conversation as resolved.
Show resolved Hide resolved
// allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing
{
code: `
Expand Down