rokucommunity / bslint

A linter for BrightScript and BrighterScript.
MIT License
27 stars 14 forks source link

Rule for enforcing use of a common set of Helper Utils #90

Open charlie-abbott-deltatre opened 1 year ago

charlie-abbott-deltatre commented 1 year ago

On Roku a lot of projects make use of custom Helper methods to validate and set data. For example:

function isBoolean(obj as dynamic) as boolean
    return obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
end function

These are used to enforce safety (prevent Channel crashes), for readability and for less typing of code.

If would be good to define a list of Helper methods for that project, that should be used in place of the Roku native checking. For example:

Use: getCount(NODE) or getCount(AA) Replaces: NODE.getChildCount() and AA.count() and ARRAY.count()

This rule would also need to define a single file and/or list of exceptions (for Task Node loops etc.) that are exempt from using the Helper Util methods.

TwitchBronBron commented 1 year ago

How would you envision detecting spots where these helpers should have been used? Would you look for any place where a GetInterface(whateverVar, "ifBoolean") is used and then warn about needing to use a helper instead? Or does the line also need to include an invalid check before you show the warning?

How would you let the user define the name of a helper function in the bsconfig? I'm guessing you'd need to carve out specific primitive types in bslint because each type might have slightly different type checking syntax that would require specific logic in bslint to detect. Something like this?

bsconfig.json


{
    "helperFuncs": {
        "enabled": true,
        "names": {
            "boolean": "isBoolean",
            "float": "isFloat"
            ...
        }
    }
}