rokucommunity / brighterscript

A superset of Roku's BrightScript language
MIT License
160 stars 46 forks source link

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

Closed agohof closed 6 months ago

agohof commented 7 months ago

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 commented 7 months ago

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

TwitchBronBron commented 7 months ago

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

https://github.com/rokucommunity/brighterscript/blob/aa92cecad83ce852a6934b0397f6efde6ba941f0/src/parser/Expression.ts#L1381

https://github.com/rokucommunity/brighterscript/blob/aa92cecad83ce852a6934b0397f6efde6ba941f0/src/util.ts#L1189-L1214