Skip to content

Commit

Permalink
Handle chain expressions (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hluisson authored Dec 19, 2021
1 parent 103309f commit 5c8ca8d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/rules/strict-logical-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,29 @@ export default createRule<Options, MessageIds>({
}
}

// Return the core identifier or expression
function determineNode(originalNode: TSESTree.Expression) {
let nodeToEvaluate = originalNode;
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
nodeToEvaluate = nodeToEvaluate.expression;
}

if (
nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
nodeToEvaluate.property.type !==
TSESTree.AST_NODE_TYPES.PrivateIdentifier
) {
nodeToEvaluate = nodeToEvaluate.property;
}

return nodeToEvaluate;
}

function checkLogicalExpression(
expressionNode: TSESTree.LogicalExpression,
checkRightNode: boolean
) {
let leftNode = expressionNode.left;
if (
leftNode.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
leftNode.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier
) {
leftNode = leftNode.property;
}
const leftNode = determineNode(expressionNode.left);

if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
checkLogicalExpression(leftNode, true);
Expand All @@ -116,13 +128,7 @@ export default createRule<Options, MessageIds>({
}

if (checkRightNode) {
let rightNode = expressionNode.right;
if (
rightNode.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
rightNode.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier
) {
rightNode = rightNode.property;
}
const rightNode = determineNode(expressionNode.right);

if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
checkAndReportIdentifier(rightNode, expressionNode.right);
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/rules/strict-logical-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ ruleTester.run("strict-logical-expressions", rule, {
options: [{ allowString: true }],
filename: "react.tsx",
},
{
code: [
'type Bar = "bar"',
"const obj: { foo: Bar } = { foo: 'bar' };",
"<App>{obj?.foo && <Foo/>}</App>",
].join("\n"),
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
filename: "react.tsx",
},
],
invalid: [
{
Expand Down Expand Up @@ -248,5 +261,41 @@ ruleTester.run("strict-logical-expressions", rule, {
].join("\n"),
filename: "react.tsx",
},
{
code: [
"const first = 100;",
'const second = { foo: "bar" };',
"<App>{first && second?.foo && <Foo/>}</App>",
].join("\n"),
errors: [{ messageId: "conditionErrorFalseyString" }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
output: [
"const first = 100;",
'const second = { foo: "bar" };',
"<App>{first && !!second?.foo && <Foo/>}</App>",
].join("\n"),
filename: "react.tsx",
},
{
code: [
"const obj = { foo: 100 };",
"<App>{obj?.foo && <Foo/>}</App>",
].join("\n"),
errors: [{ messageId: "conditionErrorFalseyNumber" }],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
output: [
"const obj = { foo: 100 };",
"<App>{!!obj?.foo && <Foo/>}</App>",
].join("\n"),
filename: "react.tsx",
},
],
});

0 comments on commit 5c8ca8d

Please sign in to comment.