From 9a951c6d84882d3c37b7e4b1ec8a4e263c0e91a1 Mon Sep 17 00:00:00 2001 From: Lars Berscheid Date: Thu, 26 Sep 2019 09:46:58 +0200 Subject: [PATCH 1/4] integrate space-before-parent into no-space --- README.md | 1 - lib/Checks/NoSpaceCheck.vala | 25 ++++++++++++++--- lib/Checks/SpaceBeforeParenCheck.vala | 39 --------------------------- lib/Config.vala | 1 - lib/Linter.vala | 1 - lib/ValaVisitor.vala | 25 ++++++++++++++++- lib/meson.build | 1 - test/FileTest.vala | 2 +- test/UnitTest.vala | 8 ------ vala-lint.conf | 1 - 10 files changed, 46 insertions(+), 58 deletions(-) delete mode 100644 lib/Checks/SpaceBeforeParenCheck.vala diff --git a/README.md b/README.md index f0fd001..dc8fafc 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ line-length=error naming-convention=error no-space=error note=error -space-before-paren=error use-of-tabs=error trailing-newlines=error trailing-whitespace=error diff --git a/lib/Checks/NoSpaceCheck.vala b/lib/Checks/NoSpaceCheck.vala index 803a936..b10d1d1 100644 --- a/lib/Checks/NoSpaceCheck.vala +++ b/lib/Checks/NoSpaceCheck.vala @@ -32,8 +32,26 @@ public class ValaLint.Checks.NoSpaceCheck : Check { } - public void check_list (Vala.List list, - ref Vala.ArrayList mistake_list) { + public void check_space_before_paren (Vala.CodeNode node, ref Vala.ArrayList mistake_list) { + if (state == Config.State.OFF) { + return; + } + + char* pos = node.source_reference.begin.pos; + while (pos[0] != '(' && pos[0] != '\0') { + pos += 1; + } + + if (!(pos[-1].isspace () || pos[-1] == '_')) { + int offset = (int)(pos - node.source_reference.begin.pos); + var begin = Utils.shift_location (node.source_reference.begin, offset); + var end = Utils.shift_location (begin, 1); + + add_mistake ({ this, begin, end, _("Expected a whitespace before paren") }, ref mistake_list); + } + } + + public void check_list (Vala.List list, ref Vala.ArrayList mistake_list) { if (state == Config.State.OFF) { return; } @@ -67,8 +85,7 @@ public class ValaLint.Checks.NoSpaceCheck : Check { } } - public void check_binary_expression (Vala.BinaryExpression expr, - ref Vala.ArrayList mistake_list) { + public void check_binary_expression (Vala.BinaryExpression expr, ref Vala.ArrayList mistake_list) { char* char_before = expr.left.source_reference.end.pos; if (char_before[0] != ' ' && char_before[0] != '\n' && char_before[0] != ')') { diff --git a/lib/Checks/SpaceBeforeParenCheck.vala b/lib/Checks/SpaceBeforeParenCheck.vala deleted file mode 100644 index f44757f..0000000 --- a/lib/Checks/SpaceBeforeParenCheck.vala +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2018-2019 elementary LLC. (https://github.com/elementary/vala-lint) - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - */ - -public class ValaLint.Checks.SpaceBeforeParenCheck : Check { - public SpaceBeforeParenCheck () { - Object ( - title: _("space-before-paren"), - description: _("Checks for a space before parenthesis.") - ); - - state = Config.get_state (title); - } - - public override void check (Vala.ArrayList parse_result, - ref Vala.ArrayList mistake_list) { - foreach (ParseResult r in parse_result) { - if (r.type == ParseType.DEFAULT) { - add_regex_mistake ("""[^_\s{\[\(\)!~]\(""", _("Expected space before paren"), r, - ref mistake_list, 1, 1); - } - } - } -} diff --git a/lib/Config.vala b/lib/Config.vala index 45f47a8..f18b2e1 100644 --- a/lib/Config.vala +++ b/lib/Config.vala @@ -52,7 +52,6 @@ public class ValaLint.Config { default_config.set_string ("Checks", "naming-convention", State.ERROR.to_string ()); default_config.set_string ("Checks", "no-space", State.ERROR.to_string ()); default_config.set_string ("Checks", "note", State.WARN.to_string ()); - default_config.set_string ("Checks", "space-before-paren", State.ERROR.to_string ()); default_config.set_string ("Checks", "use-of-tabs", State.ERROR.to_string ()); default_config.set_string ("Checks", "trailing-newlines", State.ERROR.to_string ()); default_config.set_string ("Checks", "trailing-whitespace", State.ERROR.to_string ()); diff --git a/lib/Linter.vala b/lib/Linter.vala index f11e1f7..3ba52b7 100644 --- a/lib/Linter.vala +++ b/lib/Linter.vala @@ -38,7 +38,6 @@ public class ValaLint.Linter : Object { global_checks.add (new Checks.DoubleSpacesCheck ()); global_checks.add (new Checks.LineLengthCheck ()); global_checks.add (new Checks.NoteCheck ()); - global_checks.add (new Checks.SpaceBeforeParenCheck ()); global_checks.add (new Checks.TabCheck ()); global_checks.add (new Checks.TrailingNewlinesCheck ()); global_checks.add (new Checks.TrailingWhitespaceCheck ()); diff --git a/lib/ValaVisitor.vala b/lib/ValaVisitor.vala index a1ceff1..2025bb9 100644 --- a/lib/ValaVisitor.vala +++ b/lib/ValaVisitor.vala @@ -93,7 +93,7 @@ class ValaLint.Visitor : Vala.CodeVisitor { public override void visit_method (Vala.Method m) { /* method name may be null */ naming_convention_check.check_underscore (m, ref mistake_list); - + no_space_check.check_space_before_paren (m, ref mistake_list); no_space_check.check_list (m.get_parameters (), ref mistake_list); /* Error types depend on the vala version. */ @@ -127,10 +127,14 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_constructor (Vala.Constructor c) { + no_space_check.check_space_before_paren (c, ref mistake_list); + c.accept_children (this); } public override void visit_destructor (Vala.Destructor d) { + no_space_check.check_space_before_paren (d, ref mistake_list); + d.accept_children (this); } @@ -178,10 +182,14 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_if_statement (Vala.IfStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } public override void visit_switch_statement (Vala.SwitchStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } @@ -198,18 +206,26 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_while_statement (Vala.WhileStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } public override void visit_do_statement (Vala.DoStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } public override void visit_for_statement (Vala.ForStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } public override void visit_foreach_statement (Vala.ForeachStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } @@ -244,14 +260,20 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_catch_clause (Vala.CatchClause clause) { + no_space_check.check_space_before_paren (clause, ref mistake_list); + clause.accept_children (this); } public override void visit_lock_statement (Vala.LockStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } public override void visit_unlock_statement (Vala.UnlockStatement stmt) { + no_space_check.check_space_before_paren (stmt, ref mistake_list); + stmt.accept_children (this); } @@ -313,6 +335,7 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_method_call (Vala.MethodCall expr) { + no_space_check.check_space_before_paren (expr, ref mistake_list); no_space_check.check_list (expr.get_argument_list (), ref mistake_list); expr.accept_children (this); diff --git a/lib/meson.build b/lib/meson.build index 7aa747b..0765a45 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -17,7 +17,6 @@ vala_linter_files = files( 'Checks/NamingConventionCheck.vala', 'Checks/NoSpaceCheck.vala', 'Checks/NoteCheck.vala', - 'Checks/SpaceBeforeParenCheck.vala', 'Checks/TabCheck.vala', 'Checks/TrailingNewlinesCheck.vala', 'Checks/TrailingWhitespaceCheck.vala', diff --git a/test/FileTest.vala b/test/FileTest.vala index 61ca698..1181ee5 100644 --- a/test/FileTest.vala +++ b/test/FileTest.vala @@ -56,7 +56,7 @@ class FileTest : GLib.Object { int line = 0; // So that new tests can be added without changing every number... var m_warnings = new Vala.ArrayList (); m_warnings.add ({ "naming-convention", line += 4 }); - m_warnings.add ({ "space-before-paren", line += 2 }); + m_warnings.add ({ "no-space", line += 2 }); m_warnings.add ({ "note", line += 1, "TODO" }); m_warnings.add ({ "note", line += 1, "TODO: Lorem ipsum" }); m_warnings.add ({ "no-space", line += 5 }); diff --git a/test/UnitTest.vala b/test/UnitTest.vala index 68af6c8..705d54f 100644 --- a/test/UnitTest.vala +++ b/test/UnitTest.vala @@ -148,14 +148,6 @@ class UnitTest : GLib.Object { assert_warning (note_check, "lorem // TODO: nothing to do", 10, 29); assert_warning (note_check, "lorem // FIXME: nothing to do", 10, 30); - var space_before_paren_check = new ValaLint.Checks.SpaceBeforeParenCheck (); - assert_pass (space_before_paren_check, "void test ()"); - assert_pass (space_before_paren_check, "var test = 2 * (3 + 1);"); - assert_pass (space_before_paren_check, "a = !(true && false);"); - assert_pass (space_before_paren_check, "actions &= ~(Gdk.DragAction.COPY | Gdk.DragAction.LINK)"); - assert_warning (space_before_paren_check, "void test()", 10, 11); - assert_warning (space_before_paren_check, "void = 2*(2+2)", 10, 11); - var tab_check = new ValaLint.Checks.TabCheck (); assert_pass (tab_check, "lorem ipsum"); assert_warning (tab_check, "lorem ipsum", 6, 7); diff --git a/vala-lint.conf b/vala-lint.conf index e0d0666..68a2e56 100644 --- a/vala-lint.conf +++ b/vala-lint.conf @@ -7,7 +7,6 @@ line-length=error naming-convention=error no-space=error note=error -space-before-paren=error use-of-tabs=error trailing-newlines=error trailing-whitespace=error From 4fa4c384332a7cca34d87c85214fb3936f83c1e3 Mon Sep 17 00:00:00 2001 From: Lars Berscheid Date: Thu, 26 Sep 2019 12:49:23 +0200 Subject: [PATCH 2/4] add assignments --- lib/Checks/NoSpaceCheck.vala | 28 ++++++++++++++++++++++++++-- lib/ValaVisitor.vala | 7 +++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/Checks/NoSpaceCheck.vala b/lib/Checks/NoSpaceCheck.vala index b10d1d1..e7d0a00 100644 --- a/lib/Checks/NoSpaceCheck.vala +++ b/lib/Checks/NoSpaceCheck.vala @@ -86,13 +86,16 @@ public class ValaLint.Checks.NoSpaceCheck : Check { } public void check_binary_expression (Vala.BinaryExpression expr, ref Vala.ArrayList mistake_list) { + if (state == Config.State.OFF) { + return; + } char* char_before = expr.left.source_reference.end.pos; if (char_before[0] != ' ' && char_before[0] != '\n' && char_before[0] != ')') { var begin = Utils.shift_location (expr.left.source_reference.end, 1); var end = Utils.shift_location (begin, 1); - add_mistake ({ this, begin, end, _("Expected spaces around operators") }, ref mistake_list); + add_mistake ({ this, begin, end, _("Expected spaces around operator") }, ref mistake_list); } char* char_after = expr.right.source_reference.begin.pos - 1; @@ -100,7 +103,28 @@ public class ValaLint.Checks.NoSpaceCheck : Check { var begin = expr.right.source_reference.begin; var end = Utils.shift_location (begin, 1); - add_mistake ({ this, begin, end, _("Expected spaces around operators") }, ref mistake_list); + add_mistake ({ this, begin, end, _("Expected spaces around operator") }, ref mistake_list); + } + } + + public void check_assignment (Vala.CodeNode node, ref Vala.ArrayList mistake_list) { + if (state == Config.State.OFF) { + return; + } + + string symbol = (node is Vala.Assignment) ? ((Vala.Assignment)node).operator.to_string () : "="; + + char* pos = node.source_reference.begin.pos; + while (pos[0] != symbol[0] && pos[0] != '\0') { + pos += 1; + } + + if (!pos[-1].isspace () || !pos[symbol.length].isspace ()) { + int offset = (int)(pos - node.source_reference.begin.pos); + var begin = Utils.shift_location (node.source_reference.begin, offset); + var end = Utils.shift_location (begin, 1); + + add_mistake ({ this, begin, end, _("Expected whitespaces around assignment") }, ref mistake_list); } } } diff --git a/lib/ValaVisitor.vala b/lib/ValaVisitor.vala index 2025bb9..63f5c4e 100644 --- a/lib/ValaVisitor.vala +++ b/lib/ValaVisitor.vala @@ -158,6 +158,7 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_declaration_statement (Vala.DeclarationStatement stmt) { + no_space_check.check_assignment (stmt, ref mistake_list); double_semicolon_check.check_statement (stmt, ref mistake_list); stmt.accept_children (this); @@ -362,10 +363,14 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_sizeof_expression (Vala.SizeofExpression expr) { + no_space_check.check_space_before_paren (expr, ref mistake_list); + expr.accept_children (this); } public override void visit_typeof_expression (Vala.TypeofExpression expr) { + no_space_check.check_space_before_paren (expr, ref mistake_list); + expr.accept_children (this); } @@ -414,6 +419,8 @@ class ValaLint.Visitor : Vala.CodeVisitor { } public override void visit_assignment (Vala.Assignment a) { + no_space_check.check_assignment (a, ref mistake_list); + a.accept_children (this); } From ea007ba567b1b15014e793f1254858444f2ca9d8 Mon Sep 17 00:00:00 2001 From: Lars Berscheid Date: Thu, 26 Sep 2019 14:16:07 +0200 Subject: [PATCH 3/4] add tests --- test/FileTest.vala | 6 +++++- test/files/pass.vala | 5 +++++ test/files/warnings.vala | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/test/FileTest.vala b/test/FileTest.vala index 1181ee5..b164670 100644 --- a/test/FileTest.vala +++ b/test/FileTest.vala @@ -59,7 +59,11 @@ class FileTest : GLib.Object { m_warnings.add ({ "no-space", line += 2 }); m_warnings.add ({ "note", line += 1, "TODO" }); m_warnings.add ({ "note", line += 1, "TODO: Lorem ipsum" }); - m_warnings.add ({ "no-space", line += 5 }); + m_warnings.add ({ "no-space", line += 2 }); + m_warnings.add ({ "no-space", line += 1 }); + m_warnings.add ({ "no-space", line += 1 }); + m_warnings.add ({ "no-space", line += 1 }); + m_warnings.add ({ "no-space", line += 6 }); m_warnings.add ({ "double-semicolon", line += 1 }); m_warnings.add ({ "naming-convention", line += 1 }); m_warnings.add ({ "ellipsis", line += 2 }); diff --git a/test/files/pass.vala b/test/files/pass.vala index 3ad9a8c..5e7945c 100644 --- a/test/files/pass.vala +++ b/test/files/pass.vala @@ -8,6 +8,11 @@ class FileTest : GLib.Object { string literal = "Lorem ipsum"; var string_template = @"$literal et al."; + valid (); + + int counter = 0; + counter += 1; + return 0; } } diff --git a/test/files/warnings.vala b/test/files/warnings.vala index 088a974..cb1ebcf 100644 --- a/test/files/warnings.vala +++ b/test/files/warnings.vala @@ -7,6 +7,12 @@ class FileTest : GLib.Object { test (); // TODO test (); // TODO: Lorem ipsum + int counter= 0; + counter +=1; + if(counter == 0) { + test(); + } + return 0; } From e9c69a89931c9371cd2e710d04cb94c196110c4e Mon Sep 17 00:00:00 2001 From: Lars Berscheid Date: Mon, 7 Oct 2019 08:53:35 +0200 Subject: [PATCH 4/4] add regex test --- test/files/pass.vala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/files/pass.vala b/test/files/pass.vala index 677238c..b4abacb 100644 --- a/test/files/pass.vala +++ b/test/files/pass.vala @@ -14,6 +14,8 @@ class FileTest : GLib.Object { counter += 1; var /* comment */ string_double_space = /* */ "lorem"; + var regex = /--pkg[= ](\S+)/; + return 0; } }