How to deal with Lua warning setting non-standard global variable?

Viewed 837

When I tried to declare a global variable luachecks always showing the warning setting non-standard global variable .I wonder how can I fix this.(Not just ignore the warning,but setting a global variable in a standard way)

2 Answers

The warning isn't that you're setting a global variable in a non-standard way. It's that you're setting one at all. Once your user code starts running, all of the standard global variables are already done being set. You should use local variables instead as much as you can. If you really need to set your own global variables anyway, then pass their names with --globals to luacheck to ignore the warning for them.

You can solve this by creating a .luacheckrc in your root configuration for LunarVim and passing global variables for what you would need.

In this case for LunarVim, you will spend most of your time adjusting the lvim global variable.

Like Joseph said, it's throwing because you haven't explicitly defined it locally in the scope, you are simply extending the global variable that's been defined elsewhere via LunarVim configuration, so LuaCheck will throw warnings to let you know this.

Since you use LunarVim, you can target standard globals directly to NeoVim (LunarVim is just a NeoVim wrapper). Likewise, you can pass in additional things like vim.g which would be a Vim global set as well.

stds.nvim = {
  globals = {
     "lvim"
  },
}

Additional reference here: https://luacheck.readthedocs.io/en/stable/config.html

Related