diff --git a/README.md b/README.md index 26586ce..0881c4e 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,6 @@ line-length=warn naming-convention=error no-space=error note=warn -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..e7d0a00 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,15 +85,17 @@ 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) { + 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; @@ -83,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/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 198b036..a201c73 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..63f5c4e 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); } @@ -154,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); @@ -178,10 +183,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 +207,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 +261,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 +336,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); @@ -339,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); } @@ -391,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); } 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 0da7e18..5eb2558 100644 --- a/test/FileTest.vala +++ b/test/FileTest.vala @@ -56,10 +56,14 @@ 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 ({ "double-spaces", line += 2 }); + 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 ({ "double-spaces", line += 3 }); m_warnings.add ({ "double-spaces", line += 1 }); m_warnings.add ({ "double-spaces", line += 0 }); m_warnings.add ({ "double-spaces", line += 0 }); diff --git a/test/UnitTest.vala b/test/UnitTest.vala index c10737b..9f70a53 100644 --- a/test/UnitTest.vala +++ b/test/UnitTest.vala @@ -151,14 +151,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/test/files/pass.vala b/test/files/pass.vala index c644d6d..b4abacb 100644 --- a/test/files/pass.vala +++ b/test/files/pass.vala @@ -8,8 +8,14 @@ class FileTest : GLib.Object { string literal = "Lorem ipsum"; var string_template = @"$literal et al."; + valid (); + + int counter = 0; + counter += 1; var /* comment */ string_double_space = /* */ "lorem"; + var regex = /--pkg[= ](\S+)/; + return 0; } } diff --git a/test/files/warnings.vala b/test/files/warnings.vala index bbff8c6..6fda465 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(); + } + int double_space = 2; string double_space_string = _(""); test (); diff --git a/vala-lint.conf b/vala-lint.conf index 63a96e0..e1b11ce 100644 --- a/vala-lint.conf +++ b/vala-lint.conf @@ -7,7 +7,6 @@ line-length=warn naming-convention=error no-space=error note=warn -space-before-paren=error use-of-tabs=error trailing-newlines=error trailing-whitespace=error