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

Code Style: Initialize Objects with Properties #143

Open
wants to merge 17 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 @@ -80,6 +80,7 @@ block-opening-brace-space-before=error
double-semicolon=error
double-spaces=error
ellipsis=error
initialize-objects-with-properties=warn
line-length=warn
naming-convention=error
no-space=error
Expand Down
75 changes: 75 additions & 0 deletions lib/Checks/InitializeObjectsWithPropertiesCheck.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2020 elementary Inc. (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.
*
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class ValaLint.Checks.InitializeObjectsWithPropertiesCheck : Check {
private Regex? regex;

public InitializeObjectsWithPropertiesCheck () {
Object (
title: "initialize-objects-with-properties",
description: _("Checks if objects are initialized with properties")
);

state = Config.get_state (title);

try {
regex = new Regex (
"(?P<init>[\\w,.]+ (?P<name>\\w+) = new [\\w,.]+ \\([^;]+);\n" +
"(?:[ \t]*(?P=name).(?P<props>\\w+\\s?=\\s?[^;]+);[\n])*" +
"(?:[ \t]*(?P=name).(\\w+\\s?=\\s?[^;]+);)");
} catch (RegexError e) {
stderr.printf (e.message);
}
}

public override void check (Vala.ArrayList<ParseResult?> parse_result,
ref Vala.ArrayList<FormatMistake?> mistake_list) {
string content = "";
foreach (ParseResult r in parse_result) {
content += r.text;
}

if (parse_result.size > 0 && regex != null) {
ParseResult r = parse_result[0];
int index = 0;
GLib.MatchInfo mi;
try {
while (regex.match_full (content, -1, index, 0, out mi)) {
int name_start_pos, start_pos, end_pos;
if ((mi.fetch_named_pos ("name", out name_start_pos, out end_pos)) &&
(mi.fetch_pos (0, out start_pos, out end_pos))) {
var begin = Utils.get_absolute_location (r.begin, content, name_start_pos);
var end = Utils.get_absolute_location (r.begin, content, end_pos);

var name = mi.fetch_named ("name");
var message = "\"%s\" should be initialized with properties".printf (name);

mistake_list.add ({ this, begin, end, message });

index = end_pos;
}
}
} catch (RegexError e) {
stderr.printf (e.message);
}
}
}
}
1 change: 1 addition & 0 deletions lib/Config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ValaLint.Config {
default_config.set_string ("Checks", "double-semicolon", State.ERROR.to_string ());
default_config.set_string ("Checks", "double-spaces", State.ERROR.to_string ());
default_config.set_string ("Checks", "ellipsis", State.ERROR.to_string ());
default_config.set_string ("Checks", "initialize-objects-with-properties", State.WARN.to_string ());
default_config.set_string ("Checks", "line-length", State.WARN.to_string ());
default_config.set_string ("Checks", "naming-convention", State.ERROR.to_string ());
default_config.set_string ("Checks", "no-space", State.ERROR.to_string ());
Expand Down
1 change: 1 addition & 0 deletions lib/Linter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ValaLint.Linter : Object {
global_checks = new Vala.ArrayList<Check> ();
global_checks.add (new Checks.BlockOpeningBraceSpaceBeforeCheck ());
global_checks.add (new Checks.DoubleSpacesCheck ());
global_checks.add (new Checks.InitializeObjectsWithPropertiesCheck ());
global_checks.add (new Checks.LineLengthCheck ());
global_checks.add (new Checks.NoteCheck ());
global_checks.add (new Checks.SpaceBeforeParenCheck ());
Expand Down
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vala_linter_files = files(
'Checks/DoubleSemicolonCheck.vala',
'Checks/DoubleSpacesCheck.vala',
'Checks/EllipsisCheck.vala',
'Checks/InitializeObjectsWithPropertiesCheck.vala',
'Checks/LineLengthCheck.vala',
'Checks/NamingConventionCheck.vala',
'Checks/NoSpaceCheck.vala',
Expand Down
10 changes: 10 additions & 0 deletions test/FileTest.vala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ class FileTest : GLib.Object {
general_warnings.add ("trailing-newlines", 2);
check_file_for_mistake (get_test_file ("general-warnings.vala"), general_warnings);

var initialize_objects_with_properties_warnings = FileTestMistakeList ();
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 4, 8, 25);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 3, 8, 60);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 5, 8, 70);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 5, 8, 42);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 5, 8, 22);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 4, 8, 37);
initialize_objects_with_properties_warnings.add ("initialize-objects-with-properties", 8, 8, 35);
check_file_for_mistake (get_test_file ("initialize-objects-with-properties.vala"), initialize_objects_with_properties_warnings);

var line_length_warnings = FileTestMistakeList ();
line_length_warnings.add ("line-length", 8, 120, 141);
check_file_for_mistake (get_test_file ("line-length-check.vala"), line_length_warnings);
Expand Down
38 changes: 38 additions & 0 deletions test/files/initialize-objects-with-properties.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// vala-lint=skip-file

int main (string[] args) {
var entry = new Clutter.TransitionGroup ();
entry.duration = 400;

var transition = new Clutter.KeyframeTransition ("opacity");
transition.duration = 200;
transition.remove_on_complete = true;
transition.progress_mode = Clutter.AnimationMode.LINEAR;

var shadow_transition = new PropertyTransition ("shadow-opacity");
shadow_transition.duration = MultitaskingView.ANIMATION_DURATION;
shadow_transition.remove_on_complete = true;
shadow_transition.progress_mode = MultitaskingView.ANIMATION_MODE;

var container = new WindowCloneContainer (true);
container.padding_top = TOP_GAP;
container.padding_left = container.padding_right = BORDER;
container.padding_bottom = BOTTOM_GAP;

var clone = new SafeWindowClone (window, true);
clone.x = actor.x;
clone.y = actor.y;

var icon = new WindowIcon (window, dock_settings.IconSize, ui_scale_factor, true);
icon.reactive = true;
icon.opacity = 100;
icon.x_expand = true;
icon.y_expand = true;
icon.x_align = ActorAlign.CENTER;
icon.y_align = ActorAlign.CENTER;

var effect = new ShadowEffect (40, 5);
effect.css_class = "workspace";

return 0;
}
1 change: 1 addition & 0 deletions vala-lint.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ block-opening-brace-space-before=error
double-semicolon=error
double-spaces=error
ellipsis=error
initialize-objects-with-properties=warn
line-length=warn
naming-convention=error
no-space=error
Expand Down