diff --git a/README.md b/README.md index 26586ce..2276d5e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/Checks/UsingDirectiveCheck.vala b/lib/Checks/UsingDirectiveCheck.vala new file mode 100644 index 0000000..b7e607c --- /dev/null +++ b/lib/Checks/UsingDirectiveCheck.vala @@ -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 parse_result, + ref Vala.ArrayList mistake_list) { + + } + + public void check_using_directive (Vala.UsingDirective ns, ref Vala.ArrayList mistake_list) { + if (state == Config.State.OFF) { + return; + } + + var pos = ns.source_reference; + add_mistake ({ this, pos.begin, pos.end, MESSAGE }, ref mistake_list); + } +} diff --git a/lib/Config.vala b/lib/Config.vala index 198b036..45d1c0b 100644 --- a/lib/Config.vala +++ b/lib/Config.vala @@ -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); diff --git a/lib/Linter.vala b/lib/Linter.vala index f11e1f7..77ccee6 100644 --- a/lib/Linter.vala +++ b/lib/Linter.vala @@ -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 run_checks_for_file (File file) throws Error, IOError { diff --git a/lib/ValaVisitor.vala b/lib/ValaVisitor.vala index a1ceff1..76bef41 100644 --- a/lib/ValaVisitor.vala +++ b/lib/ValaVisitor.vala @@ -25,6 +25,7 @@ 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 mistake_list) { this.mistake_list = mistake_list; @@ -32,6 +33,13 @@ class ValaLint.Visitor : Vala.CodeVisitor { 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) { @@ -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); } diff --git a/lib/meson.build b/lib/meson.build index 7aa747b..92adcfc 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -22,6 +22,7 @@ vala_linter_files = files( 'Checks/TrailingNewlinesCheck.vala', 'Checks/TrailingWhitespaceCheck.vala', 'Checks/UnnecessaryStringTemplateCheck.vala', + 'Checks/UsingDirectiveCheck.vala', ) vala_linter_deps = [ diff --git a/test/FileTest.vala b/test/FileTest.vala index 0da7e18..e58b5e3 100644 --- a/test/FileTest.vala +++ b/test/FileTest.vala @@ -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 (); - 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" }); diff --git a/test/files/warnings.vala b/test/files/warnings.vala index bbff8c6..b8243d5 100644 --- a/test/files/warnings.vala +++ b/test/files/warnings.vala @@ -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) { diff --git a/vala-lint.conf b/vala-lint.conf index 63a96e0..119ee52 100644 --- a/vala-lint.conf +++ b/vala-lint.conf @@ -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