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

Using enums in ternary if statements has unused variable warnings when compiled #1091

Closed
agohof opened this issue Mar 7, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@agohof
Copy link
Contributor

agohof commented Mar 7, 2024

When using enums to get values in a ternary if statement, the transpiled code will set the enums as params but it won't use them to get the results. Instead, it inserts the actual values directly in the return statements.

Example

Using the following enums:

enum ThumbButtonSize
    Width = 40
    Height = 37
end enum

enum ClosedCaptionsButtonSize
    Width = 38
    Height = 30
end enum

This ternary operation:

buttonBaseWidth = (nodeId = "closedCaptions") ? ClosedCaptionsButtonSize.Width : ThumbButtonSize.Width

Will be transpiled into the following code:

buttonBaseWidth = (function(__bsCondition, ClosedCaptionsButtonSize, ThumbButtonSize)
            if __bsCondition then
                return 38
            else
                return 40
            end if
        end function)((nodeId = "closedCaptions"), ClosedCaptionsButtonSize, ThumbButtonSize)

When compiled, there will be unused variable warnings because of the ClosedCaptionsButtonSize and ThumbButtonSize params.

@TwitchBronBron
Copy link
Member

Yeah, we need to improve the transpiled output to exclude those enums. Good catch.

@TwitchBronBron TwitchBronBron added the bug Something isn't working label Mar 7, 2024
@TwitchBronBron
Copy link
Member

Any chance you'd be interested in trying to fix this? I think util.getExpressionInfo() needs updated to exclude enums, constants, and interfaces.

let consequentInfo = util.getExpressionInfo(this.consequent!);

brighterscript/src/util.ts

Lines 1189 to 1214 in aa92cec

public getExpressionInfo(expression: Expression): ExpressionInfo {
const expressions = [expression];
const variableExpressions = [] as VariableExpression[];
const uniqueVarNames = new Set<string>();
function expressionWalker(expression) {
if (isExpression(expression)) {
expressions.push(expression);
}
if (isVariableExpression(expression)) {
variableExpressions.push(expression);
uniqueVarNames.add(expression.name.text);
}
}
// Collect all expressions. Most of these expressions are fairly small so this should be quick!
// This should only be called during transpile time and only when we actually need it.
expression?.walk(expressionWalker, {
walkMode: WalkMode.visitExpressions
});
//handle the expression itself (for situations when expression is a VariableExpression)
expressionWalker(expression);
return { expressions: expressions, varExpressions: variableExpressions, uniqueVarNames: [...uniqueVarNames] };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants