Linting Is Definitely a Must for Corona SDK and Lua

I’ve been working with Corona SDK for a few years now. I have always written my code using Sublime Text with the Corona plugin. My challenge with writing code in Lua has always been organization. It’s so loosely typed and you really have to keep track of the scope and naming of your variables.

For Example:

Re-declaring variable in upper scope

local someVariable = 2

local someFunction = function()
  local myVariable = 1
  -- shadowing variable defined above
  local someVariable = 3
end

Unintentionally defining a global

local someVariable = 2

local someFunction = function()
  local myVariable = 1
  -- unintenionally creating a global because my variable name doesn't match up
  somevariable = 3
end

This seems easy enough to avoid, but when you have a large project you will constantly create unintentional bugs. In the past I’ve wasted so much of my time hunting things like these down.

So half way through my third Corona project I decided to ditch Sublime Text and start using VSCode instead. You do lose the autocomplete for Corona.. but I don’t find that to be a big deal because the autocomplete isn’t that good to begin with. Also, after a while you end up memorizing all the syntax anyway.

What you do gain is an awesome linter that will warn you about the types of things I demonstrated above. To get started do this:

Use homebrew to install luarocks:

brew install luarocks

Then use luarocks to install luacheck:

luarocks install luacheck

Once you do that then install the vscode-lua extension. It has some useful code formatting built in but it’s mostly useful for enabling the linter. In my case I had to set my luacheck path to: /usr/local/bin/luacheck

You also have to set lua.preferLuaCheckErrors: true which tells this extension we want to check for errors using luacheck instead.

Ok, so now if you restart VSCode you should see some linting happening.. The only problem is we’re over-linting. luacheck is confused by some of the globals built into Corona that it doesn’t see being defined anywhere. So to fix this we need to create a .luacheckrc file in the root of our project.

This is the one I use for all my projects.

local coronaAPI = {
  'audio',
  'display',
  'easing',
  'graphics',
  'lfs',
  'media',
  'native',
  'network',
  'Runtime',
  'system',
  'timer',
  'transition',
  'print',
  'require',
  'package',
  'table'
 }

max_line_length = false

stds.corona = {
   read_globals = coronaAPI   -- these globals can only be accessed.
}

ignore = {
  '212/%.%.%.',
  '212/self',
  '143/math.*'
}
std = 'lua51+corona'

Restart VSCode one more time and you should be good. Now when I open my old projects I notice so many unused variables, scope issues, etc etc. It would have saved me a lot of pain if I was using a linter to start. With a language like Lua you absolutely need all the help you can get keeping things organized. I would never write Lua code without a linter again.

comments powered by Disqus