From 1cf683d4824aed307be80e03624dc9af7cd5ba62 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 16 May 2022 10:29:38 +0300 Subject: [PATCH] checker: add a suggestion for misspelled mod.const_name + a test --- vlib/v/checker/checker.v | 16 +++++++++++++++- ...sspelled_mod_const_should_have_suggestion.out | 6 ++++++ ...isspelled_mod_const_should_have_suggestion.vv | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out create mode 100644 vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 837e30d878..a7dbbb1775 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3396,7 +3396,21 @@ pub fn (mut c Checker) ident(mut node ast.Ident) ast.Type { c.note('`[if $node.name]` is deprecated. Use `[if $node.name?]` instead', node.pos) } else { - c.error('undefined ident: `$node.name`', node.pos) + cname_mod := node.name.all_before('.') + if cname_mod.len != node.name.len { + mut const_names_in_mod := []string{} + for _, so in c.table.global_scope.objects { + if so is ast.ConstField { + if so.mod == cname_mod { + const_names_in_mod << so.name + } + } + } + c.error(util.new_suggestion(node.name, const_names_in_mod).say('undefined ident: `$node.name`'), + node.pos) + } else { + c.error('undefined ident: `$node.name`', node.pos) + } } } if c.table.known_type(node.name) { diff --git a/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out new file mode 100644 index 0000000000..ccd510a636 --- /dev/null +++ b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv:3:21: error: undefined ident: `time.secondz`. +Did you mean `time.second`? + 1 | import time + 2 | + 3 | time.sleep(1 * time.secondz) + | ~~~~~~~ diff --git a/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv new file mode 100644 index 0000000000..8eb7a568b2 --- /dev/null +++ b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv @@ -0,0 +1,3 @@ +import time + +time.sleep(1 * time.secondz)