From b15297e7d9a21e3ce05c31a38da20008c1bbe9b4 Mon Sep 17 00:00:00 2001 From: Christopher Chang <51393127+chriscerie@users.noreply.github.com> Date: Tue, 15 Aug 2023 07:46:39 -0700 Subject: [PATCH] Show missing required param message when given in standard library format --- CHANGELOG.md | 1 + selene-lib/src/lints/standard_library.rs | 14 +++++++++++++- selene-lib/tests/lints/standard_library/assert.lua | 1 + .../tests/lints/standard_library/assert.stderr | 8 ++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53f47f66..b120375f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `roblox_incorrect_roact_usage` now lints for illegal `Name` property - Added `ignore_pattern` config to `global_usage`, which will ignore any global variables with names that match the pattern - `roblox_incorrect_roact_usage` now checks for incorrect Roact17's `createElement` usage on variables named `React`. For Roact17 only, `key`, `children`, and `ref` are valid properties to Roblox instances. +- When given in standard library format, additional information now shows up in `incorrect_standard_library_use` missing required parameter errors. ### Fixed - `string.pack` and `string.unpack` now have proper function signatures in the Lua 5.3 standard library. diff --git a/selene-lib/src/lints/standard_library.rs b/selene-lib/src/lints/standard_library.rs index 8872238a..f5e0aa99 100644 --- a/selene-lib/src/lints/standard_library.rs +++ b/selene-lib/src/lints/standard_library.rs @@ -568,7 +568,17 @@ impl Visitor for StandardLibraryVisitor<'_> { if (arguments_length < expected_args && !maybe_more_arguments) || (!vararg && arguments_length > max_args) { - self.diagnostics.push(Diagnostic::new( + let required_param_message = function + .arguments + .get(arguments_length) + .into_iter() + .filter_map(|arg| match &arg.required { + Required::Required(Some(message)) => Some(message.clone()), + _ => None, + }) + .collect(); + + self.diagnostics.push(Diagnostic::new_complete( "incorrect_standard_library_use", format!( "standard library function `{}` requires {} parameters, {} passed", @@ -577,6 +587,8 @@ impl Visitor for StandardLibraryVisitor<'_> { argument_types.len(), ), Label::from_node(call, None), + required_param_message, + Vec::new(), )); } diff --git a/selene-lib/tests/lints/standard_library/assert.lua b/selene-lib/tests/lints/standard_library/assert.lua index 06473166..e6442caf 100644 --- a/selene-lib/tests/lints/standard_library/assert.lua +++ b/selene-lib/tests/lints/standard_library/assert.lua @@ -6,3 +6,4 @@ assert(...) assert(true) assert(true, "message", call()) assert(true, "message", ...) +assert() diff --git a/selene-lib/tests/lints/standard_library/assert.stderr b/selene-lib/tests/lints/standard_library/assert.stderr index 25fc666c..1974ef1f 100644 --- a/selene-lib/tests/lints/standard_library/assert.stderr +++ b/selene-lib/tests/lints/standard_library/assert.stderr @@ -3,6 +3,8 @@ error[incorrect_standard_library_use]: standard library function `assert` requir │ 6 │ assert(true) │ ^^^^^^^^^^^^ + │ + = A failed assertion without a message is unhelpful to users. error[incorrect_standard_library_use]: standard library function `assert` requires 2 parameters, 3 passed ┌─ assert.lua:7:1 @@ -16,3 +18,9 @@ error[incorrect_standard_library_use]: standard library function `assert` requir 8 │ assert(true, "message", ...) │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error[incorrect_standard_library_use]: standard library function `assert` requires 2 parameters, 0 passed + ┌─ assert.lua:9:1 + │ +9 │ assert() + │ ^^^^^^^^ +