Skip to content

refactor(ui5Types/utils): Enhance detection of return value usage #700

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/linter/ui5Types/fix/CallExpressionBaseFix.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ts from "typescript";
import {PositionInfo} from "../../LinterContext.js";
import {
countChildNodesRecursive, findNodeRecursive, isExpectedValueExpression, isSideEffectFree,
countChildNodesRecursive, findNodeRecursive, isReturnValueUsed, isSideEffectFree,
} from "../utils/utils.js";
import BaseFix, {BaseFixParams} from "./BaseFix.js";

Expand All @@ -27,7 +27,7 @@ export default abstract class CallExpressionBaseFix extends BaseFix {
}
// If requested, check whether the return value of the call expression is assigned to a variable,
// passed to another function or used elsewhere.
if (this.params.mustNotUseReturnValue && isExpectedValueExpression(node)) {
if (this.params.mustNotUseReturnValue && isReturnValueUsed(node)) {
return false;
}
const containedCallExpression = findNodeRecursive<ts.CallExpression>(node.expression, this.nodeTypes);
Expand Down
42 changes: 34 additions & 8 deletions src/linter/ui5Types/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,46 @@ export function isAssignment(node: ts.AccessExpression): boolean {
return false;
}

export function isExpectedValueExpression(node: ts.Node): boolean {
let isExpectedValue = false;
while (node && !isExpectedValue) {
// Whether the function returns a value
if (!ts.isReturnStatement(node) && node.parent && ts.isBlock(node.parent)) {
export function isReturnValueUsed(node: ts.Node): boolean {
let isReturnValueUsed = false;
while (node && !isReturnValueUsed) {
if (!ts.isReturnStatement(node) && !ts.isThrowStatement(node) && node.parent && ts.isBlock(node.parent)) {
// If the node is not a return statement, but is part of a block, we can assume that it is not used
break;
}
if (node.parent && ts.isConditionalExpression(node.parent) && node === node.parent.condition) {
// If the node is part of a conditional expression, and is the condition, the return value is used
// Other positions in the conditional expression depend on how the whole expression is used
// Example: `method() ? 1 : 2;`
isReturnValueUsed = true;
break;
}

if (node.parent && (ts.isIfStatement(node.parent) || ts.isWhileStatement(node.parent) ||
ts.isDoStatement(node.parent) || ts.isSwitchStatement(node.parent)) && node !== node.parent.expression) {
// If the node is part of an if or while statement, but not the condition we can assume
// that the return value is not used
// Example: `while(1 === 1) method();` does not use the return value of `method()`
break;
}
if (node.parent && ts.isForStatement(node.parent) && node === node.parent.statement) {
// If the node is part of a for statement but part of the statement, we can assume
// that the return value is not used
// Example: `for (let i = 0; i < 10; i++) method();` does not use the return value of `method()`
break;
}

if (ts.isVariableDeclaration(node) ||
ts.isBinaryExpression(node) ||
ts.isIfStatement(node) ||
ts.isWhileStatement(node) ||
ts.isDoStatement(node) ||
ts.isForStatement(node) ||
ts.isSwitchStatement(node) ||
ts.isVariableStatement(node) ||
ts.isConditionalExpression(node) ||
ts.isParenthesizedExpression(node) ||
ts.isReturnStatement(node) ||
ts.isThrowStatement(node) ||
// () => jQuery.sap.log.error("FOO"); Explicit return in an arrow function
(ts.isArrowFunction(node) && !ts.isBlock(node.body)) ||
// Argument of a function call
Expand All @@ -257,12 +283,12 @@ export function isExpectedValueExpression(node: ts.Node): boolean {
ts.isPropertyAccessExpression(node.expression.parent) &&
ts.isCallExpression(node.expression.parent.expression))
) {
isExpectedValue = true;
isReturnValueUsed = true;
}
node = node.parent;
}

return isExpectedValue;
return isReturnValueUsed;
}

export function isConditionalAccess(node: ts.Node): boolean {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/autofix/jQuerySapLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ sap.ui.define([], async function () {
baseLog.debug("This is a debug log message");
const level = jQuery.sap.log.getLevel();

if (jQuery.sap.log.debug("This is a debug log message")) {}
while(jQuery.sap.log.debug("This is a debug log message")) {}
for (let i = 0; jQuery.sap.log.debug("This is a debug log message"); i++) {}

if (level === jQuery.sap.log.LogLevel.DEBUG) {
jQuery.sap.log.debug(`This is a debug (${jQuery.sap.Level.DEBUG}) log message`);
}
Expand Down
50 changes: 48 additions & 2 deletions test/lib/autofix/snapshots/autofix.fixtures.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5243,6 +5243,10 @@ Generated by [AVA](https://avajs.dev).
const baseLog = BaseLog.getLogger();␊
baseLog.debug("This is a debug log message");␊
const level = BaseLog.getLevel();␊
if (jQuery.sap.log.debug("This is a debug log message")) {}␊
while(jQuery.sap.log.debug("This is a debug log message")) {}␊
for (let i = 0; jQuery.sap.log.debug("This is a debug log message"); i++) {}␊
if (level === BaseLog.Level.DEBUG) {␊
BaseLog.debug(\`This is a debug (${jQuery.sap.Level.DEBUG}) log message\`);␊
Expand Down Expand Up @@ -5295,8 +5299,26 @@ Generated by [AVA](https://avajs.dev).
line: 17,
message: 'Unable to analyze this method call because the type of identifier "fatal" in "jQuery.sap.log.fatal("This is a fatal log message")" could not be determined',
},
{
category: 1,
column: 6,
line: 27,
message: 'Unable to analyze this method call because the type of identifier "debug" in "jQuery.sap.log.debug("This is a debug log message")" could not be determined',
},
{
category: 1,
column: 8,
line: 28,
message: 'Unable to analyze this method call because the type of identifier "debug" in "jQuery.sap.log.debug("This is a debug log message")" could not be determined',
},
{
category: 1,
column: 18,
line: 29,
message: 'Unable to analyze this method call because the type of identifier "debug" in "jQuery.sap.log.debug("This is a debug log message")" could not be determined',
},
],
errorCount: 2,
errorCount: 5,
fatalErrorCount: 0,
filePath: 'jQuerySapLog.js',
messages: [
Expand All @@ -5309,8 +5331,32 @@ Generated by [AVA](https://avajs.dev).
severity: 2,
},
{
column: 37,
column: 6,
line: 27,
message: 'Use of deprecated API \'jQuery.sap.log.debug\'',
messageDetails: 'Deprecated test message',
ruleId: 'no-deprecated-api',
severity: 2,
},
{
column: 8,
line: 28,
message: 'Use of deprecated API \'jQuery.sap.log.debug\'',
messageDetails: 'Deprecated test message',
ruleId: 'no-deprecated-api',
severity: 2,
},
{
column: 18,
line: 29,
message: 'Use of deprecated API \'jQuery.sap.log.debug\'',
messageDetails: 'Deprecated test message',
ruleId: 'no-deprecated-api',
severity: 2,
},
{
column: 37,
line: 32,
message: 'Use of deprecated API \'jQuery.sap.Level.DEBUG\'',
messageDetails: 'Deprecated test message',
ruleId: 'no-deprecated-api',
Expand Down
Binary file modified test/lib/autofix/snapshots/autofix.fixtures.ts.snap
Binary file not shown.
Loading