Skip to content

Commit 9212aa4

Browse files
pantorLars Berscheidcolinkiama
authored
Add Check for Using Directive (#129)
* add using-directive check * using check * don't translate check title * Fix offset value for naming-convention mistake in general warnings test --------- Co-authored-by: Lars Berscheid <lars.berscheid@kit.edu> Co-authored-by: Colin Kiama <colinkiama@gmail.com>
1 parent 6c2bb68 commit 9212aa4

10 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ use-of-tabs=error
9090
trailing-newlines=error
9191
trailing-whitespace=error
9292
unnecessary-string-template=error
93+
using-directive=error
9394

9495
[Disabler]
9596
disable-by-inline-comments=true
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2019 elementary LLC. (https://github.com/elementary/vala-lint)
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public
15+
* License along with this program; if not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301 USA.
18+
*/
19+
20+
public class ValaLint.Checks.UsingDirectiveCheck : Check {
21+
const string MESSAGE = _("Use explicit namespace instead");
22+
23+
public UsingDirectiveCheck () {
24+
Object (
25+
title: "using-directive",
26+
description: _("Checks for undesirable using directives")
27+
);
28+
29+
state = Config.get_state (title);
30+
}
31+
32+
public override void check (Vala.ArrayList<ParseResult?> parse_result,
33+
ref Vala.ArrayList<FormatMistake?> mistake_list) {
34+
35+
}
36+
37+
public void check_using_directive (Vala.UsingDirective ns, ref Vala.ArrayList<FormatMistake?> mistake_list) {
38+
if (state == Config.State.OFF) {
39+
return;
40+
}
41+
42+
var pos = ns.source_reference;
43+
add_mistake ({ this, pos.begin, pos.end, MESSAGE }, ref mistake_list);
44+
}
45+
}

‎lib/Config.vala‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class ValaLint.Config {
5757
default_config.set_string ("Checks", "trailing-newlines", State.ERROR.to_string ());
5858
default_config.set_string ("Checks", "trailing-whitespace", State.ERROR.to_string ());
5959
default_config.set_string ("Checks", "unnecessary-string-template", State.ERROR.to_string ());
60+
default_config.set_string ("Checks", "using-directive", State.ERROR.to_string ());
6061

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

‎lib/Linter.vala‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class ValaLint.Linter : Object {
5151
visitor.naming_convention_check = new Checks.NamingConventionCheck ();
5252
visitor.no_space_check = new Checks.NoSpaceCheck ();
5353
visitor.unnecessary_string_template_check = new Checks.UnnecessaryStringTemplateCheck ();
54+
visitor.using_directive_check = new Checks.UsingDirectiveCheck ();
5455
}
5556

5657
public Vala.ArrayList<FormatMistake?> run_checks_for_file (File file) throws Error, IOError {

‎lib/ValaVisitor.vala‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ class ValaLint.Visitor : Vala.CodeVisitor {
2525
public Checks.NamingConventionCheck naming_convention_check;
2626
public Checks.UnnecessaryStringTemplateCheck unnecessary_string_template_check;
2727
public Checks.NoSpaceCheck no_space_check;
28+
public Checks.UsingDirectiveCheck using_directive_check;
2829

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

3334
public override void visit_source_file (Vala.SourceFile sf) {
3435
sf.accept_children (this);
36+
37+
/* using directives are not accepted by default */
38+
foreach (var using_directives in sf.current_using_directives) {
39+
if (using_directives.source_reference != null) {
40+
using_directives.accept (this);
41+
}
42+
}
3543
}
3644

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

141149
public override void visit_using_directive (Vala.UsingDirective ns) {
150+
using_directive_check.check_using_directive (ns, ref mistake_list);
151+
142152
ns.accept_children (this);
143153
}
144154

‎lib/meson.build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ vala_linter_files = files(
2525
'Checks/TrailingNewlinesCheck.vala',
2626
'Checks/TrailingWhitespaceCheck.vala',
2727
'Checks/UnnecessaryStringTemplateCheck.vala',
28+
'Checks/UsingDirectiveCheck.vala',
2829
)
2930

3031
vala_linter_deps = [

‎test/ContentTest.vala‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class ContentTest : GLib.Object {
5757
check_contents_for_mistake (CheckTest.get_test_file ("general-pass.vala"), empty_list);
5858

5959
var general_warnings = CheckTest.FileTestMistakeList ();
60-
general_warnings.add ("naming-convention", 4);
60+
general_warnings.add ("using-directive", 3);
61+
general_warnings.add ("naming-convention", 3);
6162
general_warnings.add ("space-before-paren", 2, 26, 27);
6263
general_warnings.add ("note", 1, 21, 25, "TODO");
6364
general_warnings.add ("note", 1, 21, 38, "TODO: Lorem ipsum");

‎test/FileTest.vala‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class FileTest : GLib.Object {
5252
check_file_for_mistake (CheckTest.get_test_file ("general-pass.vala"), empty_list);
5353

5454
var general_warnings = CheckTest.FileTestMistakeList ();
55-
general_warnings.add ("naming-convention", 4);
55+
general_warnings.add ("using-directive", 3);
56+
general_warnings.add ("naming-convention", 3);
5657
general_warnings.add ("space-before-paren", 2, 26, 27);
5758
general_warnings.add ("note", 1, 21, 25, "TODO");
5859
general_warnings.add ("note", 1, 21, 38, "TODO: Lorem ipsum");

‎test/files/general-warnings.vala‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// vala-lint=skip-file
22

3-
class FileTest : GLib.Object {
3+
using GLib;
4+
5+
class FileTest : Object {
46
const int underscore_constant = 3;
57

68
public static int main(string[] args) {

‎vala-lint.conf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use-of-tabs=error
1212
trailing-newlines=error
1313
trailing-whitespace=error
1414
unnecessary-string-template=error
15+
using-directive=error
1516

1617
[Disabler]
1718
disable-by-inline-comments=true

0 commit comments

Comments
 (0)