Repository navigation
lua-language-server type rules, a strict mode, flow narrowing and FiveM data updates - #20
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
This brings the type checks of qbx-lua-ls close to lua-language-server 3.19.1. It also adds an opt-in strict mode that reports what TypeScript's strict mode would. And it fixes the indexing and inference gaps that showed up while comparing the two on real FiveM resources.
It is 155 commits. Each is one change with its own tests and docs, so it can be reviewed commit by commit, and the release notes read from the commit messages.
New rules
Most new rules default to
warning, so projects will see new findings after upgrading. The exceptions aretrailing-space,redundant-returnandunused-vararg, which default tohint, and the missing-doc rules, which areoff. Rules that need types run in qbx-lua-ls, and throughqbx-lua-ls --checkoutside an editor.param-type-mismatch,need-check-nil,cast-type-mismatch,cast-local-typeinject-field,redundant-return-value,discard-returns,invisibleclose-non-object,circle-doc-class,unknown-operator,unknown-cast-variableredundant-parameter,count-down-loop,duplicate-set-fieldnewline-call,newfield-call,ambiguity-1,global-in-nil-envredundant-return,unused-vararg,trailing-spaceundefined-doc-param,duplicate-doc-alias,duplicate-doc-field,duplicate-doc-param,doc-field-no-class,unknown-diag-codemissing-global-doc,missing-local-export-docandincomplete-signature-docExisting rules
missing-fields: checks tables typed as a union of classes and shapes, or as a shape. It also checks the tables that arrays, maps and tuples hold. A(partial)class requires only its own fields, and---@metafiles are skipped.undefined-fieldandinject-field: report fields read from, or set on, values whose type lacks them.assign-type-mismatchandreturn-type-mismatch: check each type of a union, the entries stored in declared tables, whattable.insertadds, and typed locals that nothing assigns.unreachable-code,deprecated,unused-local,unused-function,redefined-local,missing-parameter: cover the cases lua-language-server reports and these missed.native-wrong-side: reports names of one side used in shared scripts, and server-only Lua libraries used on the client.no-unknown: reports values of unknown type stored where a type is declared.fivem/event-missing-arguments: leaves out the handler parameters that its doc comment marks optional.lowercase-global,implicit-globalandunused-argument: leave---@metastubs alone.Strict mode
strict = trueinqbxlint.tomlturns it on. It also works per folder through[[overrides]], or through the editor'sdiagnostics.strict. The type rules then also report what TypeScript's strict mode does, such asn + 1for anumber?, or astring?passed for astring.It is off by default. Without it, the rules aim to report what lua-language-server 3.19.1 reports on the same code. The index, hover and completion don't depend on it.
Inference
type()andmath.type()checks, and comparisons with literals and with other locals;elseifchains, and locals that are assigned again;---@castlines;if okafterlocal ok = x and f(x);and/orsides whose type is never false.setmetatableand__index;@operatorlines;{ [K]: V }binding generics astable<K, V>does.---@extend Name fun(...)adds signatures from other files.@return,or/andside or statement declares.assert,tonumberand...Treturns are typed, and so are calls of functions that return nothing.Language server
---|values in completion and hover.qbx-lua-ls --check [<dir>...]reports what the editor reports, from the command line.sqltype and theqbx/sqlFunctionsrequest, so editors can highlight queries.FiveM data
feat(natives): update the natives and return their pointers as the Lua wrappers do, so that data diff can be reviewed apart. Natives return their pointers after the result, as the Lua wrappers do. The functions they call back and the tables they return are typed.cfx_events.luadeclares the events FiveM itself triggers. They're used for completion, hover, handler parameter types andfivem/event-wrong-side.CreateThread,json.decode,msgpack.unpack,table.*,RegisterNetEvent,load,string.findandos.date('*t'). The server-only Lua libraries are split into their own stub.LuaCATS
---@extend;async fun(...);---|lines that continue@param,@return,@typeand@field;[K]?: V.selfin doc types resolves to the class of the function's table.docs/rules.md,docs/reference.mdand the language server's README and protocol docs describe each change.The editor side is in the qbx-editor PR Qbox-project/qbx-editor#7: the strict setting, hover levels, SQL highlighting and
---@extendhighlighting. Its CI builds this repository'smain, so this PR should merge first.Checks
cargo fmt --all --checkcargo clippy --workspace --all-targets --locked -- -D warningscargo test --workspace --lockedcargo test --locked -p qbx_fivem_data --features docscargo build --release --locked -p qbx_lua_lsandcargo build --profile lint-release --locked -p qbx_lint.crates/qbx_lua_ls/testsandcrates/qbx_lua_analysis/tests.🤖 Generated with Claude Code