Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve No Space Check #125

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 47 additions & 6 deletions lib/Checks/NoSpaceCheck.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ public class ValaLint.Checks.NoSpaceCheck : Check {

}

public void check_list (Vala.List<Vala.CodeNode?> list,
ref Vala.ArrayList<FormatMistake?> mistake_list) {
public void check_space_before_paren (Vala.CodeNode node, ref Vala.ArrayList<FormatMistake?> 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<Vala.CodeNode?> list, ref Vala.ArrayList<FormatMistake?> mistake_list) {
if (state == Config.State.OFF) {
return;
}
Expand Down Expand Up @@ -67,23 +85,46 @@ public class ValaLint.Checks.NoSpaceCheck : Check {
}
}

public void check_binary_expression (Vala.BinaryExpression expr,
ref Vala.ArrayList<FormatMistake?> mistake_list) {
public void check_binary_expression (Vala.BinaryExpression expr, ref Vala.ArrayList<FormatMistake?> 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;
if (char_after[0] != ' ' && char_after[0] != '\n' && char_after[0] != '(') {
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<FormatMistake?> 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);
}
}
}
39 changes: 0 additions & 39 deletions lib/Checks/SpaceBeforeParenCheck.vala

This file was deleted.

1 change: 0 additions & 1 deletion lib/Config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand Down
1 change: 0 additions & 1 deletion lib/Linter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand Down
32 changes: 31 additions & 1 deletion lib/ValaVisitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 6 additions & 2 deletions test/FileTest.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileTestMistake?> ();
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 });
Expand Down
8 changes: 0 additions & 8 deletions test/UnitTest.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/files/pass.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/files/warnings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
1 change: 0 additions & 1 deletion vala-lint.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down