question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type checkers are a great tool to improve robustness of code written. It would be great to add some kind of type-checking capabilities to EmmyLua where the types of arguments are matched with the expected types as defined in their definition. This would also include checking if returned values match the specified return types.

This could be a plugin setting that could be enabled or not, and when enabled shows either an error or a warning whenever a type missmatch occurs, and would require the types of each function to be declared. I envision this working similar to TypeScript’s type system.

For example:

--- @type myEnum
ENUM_VAL = 1

--- @param enum myEnum
--- @return number
function myFunction(enum)
    return "2" -- ERROR: Return type (string) does not match specified type (number)
end

myFunction(3) -- ERROR: Parameter 1' type (int) does not match expected type (myEnum)

I don’t know if this is something that is on the agenda or how hard this would be to include in the plugin, but I think including this functionality would be great.

PS: I am more than willing to share my knowledge on the subject if needed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
DoctorGestercommented, Sep 26, 2017

@tangzx

Has a ton of issues rn such as some things evaluating as TyLazyClass and nil evaluating as any. Also argument number checking is a bad idea since a lot of functions even in standard lib have optional args 😦

Argument number could still be enforced with some function flag

The code is simple right now:

override fun visitCallExpr(o: LuaCallExpr) {
            super.visitCallExpr(o)

            val searchContext = SearchContext(o.project)
            val type = o.expr.guessType(searchContext)

            if (type is TyPsiFunction) {
                val signatureArguments = type.mainSignature.params

                val givenArguments = o.args.exprList?.exprList ?: listOf()
                val givenArgumentsCount = givenArguments.size

                if (givenArgumentsCount != signatureArguments.size) {
                    val errorMessage = "Incorrect number of arguments, expected %d, got %d".format(signatureArguments.size, givenArgumentsCount)
                    myHolder!!.createErrorAnnotation(o, errorMessage)
                } else {
                    for (i in 0..signatureArguments.size - 1) {
                        val signatureParameter = type.mainSignature.params[i]
                        val givenArgument = givenArguments[i]

                        val guessedArgumentType = givenArgument.guessType(searchContext)

                        if (signatureParameter.ty != guessedArgumentType) {
                            val warningMessage = "Incorrect argument type, expected %s, got %s".format(signatureParameter.ty, guessedArgumentType)

                            myHolder!!.createWarningAnnotation(givenArgument, warningMessage)
                        }
                    }
                }
            }
        }
0reactions
Perryvwcommented, Oct 1, 2017

See #59

Read more comments on GitHub >

github_iconTop Results From Across the Web

gvvaughan/typecheck: Gradual type checking for Lua functions
A Luaish run-time gradual type checking system, for argument and return types at function boundaries with simple annotations that can be disabled in...
Read more >
Lua Type Checking - lua-users wiki
Lua Type Checking ... Many programming languages provide some form of static (compile-time) or dynamic (run-time) type checking, with each form ...
Read more >
Type checking - Luau
A fast, small, safe, gradually typed embeddable scripting language derived from Lua.
Read more >
typecheck - LuaRocks
A Luaish run-time gradual type checking system, for argument and return types at function boundaries with simple annotations that can
Read more >
Typed Lua: An Optional Type System for Lua - Lua.org
lua :1331: in function 'typecheck' ./tlc:119: in main chunk. [C]: in ? 03/27. Page 4. Why not combine both? 04 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found