LuaLS / LuaLS/lua-language-server
Returning module type from require wrapper
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
I cannot figure out a way to make a function return the type information of a module.
Basically, we have a wrapper function for require, which takes 2 params, the first being the repository, the second being the module name.
It's a weird legacy thing we have to deal with in our code.
I was just wondering how I can annotate that function to return the module type of the second parameter.
It can basically ignore the first parameter. I've added paths to the library & paths etc... and the typing does work when specifying using normal require, just not the custom wrapper.
essentially the code of the wrapping function (not exact, but like I said, the first param can be ignored):
```
function custom_require(repo, modname)
return require(modname)
end
```
I was hoping it could do something similar to the following annotation, but I have not been able to do so.
```
---@param repo string
---@param modname string
---@return module
```
The only way I've got it to work functionally is to use the plugin feature to replace the text, but this screws up the colouring / highlighting of the entire file.
plugin matcher: `"()local([^\n]+)custom_require%([^\n,]+,([^\n%)]+)%)()"`
with `"---@module " .. mod .. "\nlocal" .. varDec .. "require(" .. mod .. ")"`. (where varDec is the second group match, and mod is the third, the first being the position)
And the "specials" don't help in this case, as that tries to use the first param, not second.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the custom_require wrapper and the existing plugin matcher described in the issue, then trace how module types and "specials" resolve parameters. Done means the wrapper infers the returned module type from its second argument without relying on text replacement that breaks syntax highlighting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100