mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

How to get rid of syntax errors for include_partial used by Wolverine gem? #163

Closed texpert closed 6 years ago

texpert commented 6 years ago

Hello!

It is a common practice to use shared Lua scripts in Wolverine Ruby gem with a command like:

<%= include_partial 'shared/_common.lua' %>

For which luacheck is reporting a syntax error:

(E011) expected statement near '<' 

Could these errors be somehow avoided?

mpeterv commented 6 years ago

That syntax is not standard Lua so luacheck doesn't support it. You could filter it out before passing file contents to luacheck, e.g. sed 's/<%=.*%>//' my_file_with_include_partial.lua | luacheck -.

texpert commented 6 years ago

Thanks for advise, @mpeterv , but I have no idea how to accomplish this running codeclimate-luacheck, if it is possible at all.

I could only completely disable 011 checks in Code Climate, but this is not an option.

mpeterv commented 6 years ago

I'm not sure that codeclimate-luacheck will decide to support that syntax extension either. If you just want to avoid breaking build with this, you could configure codeclimate to run luacheck only on files that you know don't containt that syntax (assuming it has a way to select what files should be linted). Alternatively, luacheck could have an option to filter out syntax errors, but normally that's a very strange thing to want.

texpert commented 6 years ago

Unfortunately, there are lot of such files in the project...

mikz commented 6 years ago

@texpert Hi, I'm the maintainer of codeclimate-luacheck.

So the issue is the wolverine library: https://github.com/Shopify/wolverine#nested-lua-scripts suggests you to write invalid Lua.

IMHO the .lua files should have .lua.erb extension, because they are indeed ERB files, not plain Lua files.

You could also make those lua files valid by:

-- <%= include_partial "foo" %>
local foo = "foo"

And change https://github.com/Shopify/wolverine/blob/d917d57b79b223389ad62a936581ecf4e084b349/lib/wolverine/script.rb#L98 to output a \n (maybe even with a file name) before rendering the template.

This is not really luachecks problem.