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

Add Check for Using Directive #129

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ use-of-tabs=error
trailing-newlines=error
trailing-whitespace=error
unnecessary-string-template=error
using-directive=error

[Disabler]
disable-by-inline-comments=true
Expand Down
45 changes: 45 additions & 0 deletions lib/Checks/UsingDirectiveCheck.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 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.UsingDirectiveCheck : Check {
const string MESSAGE = _("Use explicit namespace instead");

public UsingDirectiveCheck () {
Object (
title: "using-directive",
description: _("Checks for undesirable using directives")
);

state = Config.get_state (title);
}

public override void check (Vala.ArrayList<ParseResult?> parse_result,
ref Vala.ArrayList<FormatMistake?> mistake_list) {

}

public void check_using_directive (Vala.UsingDirective ns, ref Vala.ArrayList<FormatMistake?> mistake_list) {
if (state == Config.State.OFF) {
return;
}

var pos = ns.source_reference;
add_mistake ({ this, pos.begin, pos.end, MESSAGE }, ref mistake_list);
}
}
1 change: 1 addition & 0 deletions lib/Config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ValaLint.Config {
default_config.set_string ("Checks", "trailing-newlines", State.ERROR.to_string ());
default_config.set_string ("Checks", "trailing-whitespace", State.ERROR.to_string ());
default_config.set_string ("Checks", "unnecessary-string-template", State.ERROR.to_string ());
default_config.set_string ("Checks", "using-directive", State.ERROR.to_string ());

default_config.set_boolean ("Disabler", "disable-by-inline-comments", true);

Expand Down
1 change: 1 addition & 0 deletions lib/Linter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ValaLint.Linter : Object {
visitor.naming_convention_check = new Checks.NamingConventionCheck ();
visitor.no_space_check = new Checks.NoSpaceCheck ();
visitor.unnecessary_string_template_check = new Checks.UnnecessaryStringTemplateCheck ();
visitor.using_directive_check = new Checks.UsingDirectiveCheck ();
}

public Vala.ArrayList<FormatMistake?> run_checks_for_file (File file) throws Error, IOError {
Expand Down
10 changes: 10 additions & 0 deletions lib/ValaVisitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ class ValaLint.Visitor : Vala.CodeVisitor {
public Checks.NamingConventionCheck naming_convention_check;
public Checks.UnnecessaryStringTemplateCheck unnecessary_string_template_check;
public Checks.NoSpaceCheck no_space_check;
public Checks.UsingDirectiveCheck using_directive_check;

public void set_mistake_list (Vala.ArrayList<FormatMistake?> mistake_list) {
this.mistake_list = mistake_list;
}

public override void visit_source_file (Vala.SourceFile sf) {
sf.accept_children (this);

/* using directives are not accepted by default */
foreach (var using_directives in sf.current_using_directives) {
if (using_directives.source_reference != null) {
using_directives.accept (this);
}
}
}

public override void visit_namespace (Vala.Namespace ns) {
Expand Down Expand Up @@ -139,6 +147,8 @@ class ValaLint.Visitor : Vala.CodeVisitor {
}

public override void visit_using_directive (Vala.UsingDirective ns) {
using_directive_check.check_using_directive (ns, ref mistake_list);

ns.accept_children (this);
}

Expand Down
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ vala_linter_files = files(
'Checks/TrailingNewlinesCheck.vala',
'Checks/TrailingWhitespaceCheck.vala',
'Checks/UnnecessaryStringTemplateCheck.vala',
'Checks/UsingDirectiveCheck.vala',
)

vala_linter_deps = [
Expand Down
3 changes: 2 additions & 1 deletion test/FileTest.vala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ 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 ({ "using-directive", line += 3 });
m_warnings.add ({ "naming-convention", line += 3 });
m_warnings.add ({ "space-before-paren", line += 2 });
m_warnings.add ({ "note", line += 1, "TODO" });
m_warnings.add ({ "note", line += 1, "TODO: Lorem ipsum" });
Expand Down
4 changes: 3 additions & 1 deletion test/files/warnings.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// vala-lint=skip-file

class FileTest : GLib.Object {
using GLib;

class FileTest : Object {
const int underscore_constant = 3;

public static int main(string[] args) {
Expand Down
1 change: 1 addition & 0 deletions vala-lint.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use-of-tabs=error
trailing-newlines=error
trailing-whitespace=error
unnecessary-string-template=error
using-directive=error

[Disabler]
disable-by-inline-comments=true
Expand Down