Skip to content

Commit

Permalink
#287 Support getters and setters naming
Browse files Browse the repository at this point in the history
New configuration "regex_attribute_name" for naming attributes.

Closes #287
  • Loading branch information
florianschanda committed Sep 2, 2024
1 parent b6e5057 commit 353a3f4
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 20 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Not quite compatible with Octave yet. See #43 [octave support](https://github.co

### 0.9.44-dev


* New configuration item `regex_attribute_name` for the
`naming_functions` rule. This can be used to enforce naming for
class attributes and the associated getters and setters.

### 0.9.43

Expand Down
8 changes: 7 additions & 1 deletion docs/style_checker.html
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ <h4>Naming scheme for functions ("naming_functions")</h4>

<div>
This rule enforces a consistent naming for all user-defined
functions and methods.
functions, methods, getters, and setters.
</div>

<div>
Expand All @@ -971,6 +971,12 @@ <h4>Naming scheme for functions ("naming_functions")</h4>
<pre>[a-z]+(_[a-z]+)*</pre> This is all lower-case,
underscore separated names.
</li>
<li>
<b>regex_attribute_name</b>: A regular expression that
every class attribute and associated getter/setter must
match. The default is the same as it is for classes. (See
above.)
</li>
</ul>
</div>

Expand Down
7 changes: 5 additions & 2 deletions miss_hit_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools ##
## ##
## Copyright (C) 2020-2022, Florian Schanda ##
## Copyright (C) 2020-2024, Florian Schanda ##
## ##
## This file is part of MISS_HIT. ##
## ##
Expand Down Expand Up @@ -379,7 +379,10 @@ class Function_Metric(Code_Metric):
default = DEFAULT_NAMING_SCHEME),
"regex_method_name" : Regex_Style_Configuration(
"Regex for class method names",
default = DEFAULT_LC_NAMING_SCHEME)
default = DEFAULT_LC_NAMING_SCHEME),
"regex_attribute_name" : Regex_Style_Configuration(
"Regex for class attributes",
default = DEFAULT_NAMING_SCHEME)
}),

"naming_parameters" : Style_Rule(
Expand Down
18 changes: 16 additions & 2 deletions miss_hit_core/m_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools ##
## ##
## Copyright (C) 2019-2022, Florian Schanda ##
## Copyright (C) 2019-2024, Florian Schanda ##
## ##
## This file is part of MISS_HIT. ##
## ##
Expand Down Expand Up @@ -753,6 +753,9 @@ def sty_check_naming(self, mh, cfg):
assert isinstance(cfg, Config)
if cfg.active("naming_classes"):
self.n_name.sty_check_naming(mh, cfg, "class", "naming_classes")
for n_block in self.l_properties:
for n_prop in n_block.l_items:
n_prop.sty_check_naming(mh, cfg)
for n_block in self.l_methods:
for n_function in n_block.l_items:
n_function.sty_check_naming(mh, cfg)
Expand Down Expand Up @@ -1181,7 +1184,7 @@ def sty_check_naming(self, mh, cfg):
raise ICE("class method with %s node as name" %
self.n_name.__class__.__name__)
else:
self.n_name.n_field.sty_check_naming(mh, cfg, "method",
self.n_name.n_field.sty_check_naming(mh, cfg, "attribute",
"naming_functions")

else:
Expand Down Expand Up @@ -1452,6 +1455,17 @@ def visit(self, parent, function, relation):
self.n_default_value.visit(self, function, "Default")
self._visit_end(parent, function, relation)

def sty_check_naming(self, mh, cfg):
assert isinstance(mh, Message_Handler)
assert isinstance(cfg, Config)

if self.n_parent.kind() != "properties":
raise ICE("called naming check on %s block" % self.n_parent.kind())

if cfg.active("naming_functions"):
self.n_name.sty_check_naming(mh, cfg, "attribute",
"naming_functions")


class Argument_Validation_Delegation(Node):
""" AST for a the .? special syntax found inside argument blocks.
Expand Down
12 changes: 12 additions & 0 deletions tests/style/bug_142_continues_properties/expected_out.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,45 @@
<h1>Issues identified</h1>
<section>
<h2>Potato.m</h2>
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 9)">Potato.m: line 6:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 12)">Potato.m: line 6:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 14)">Potato.m: line 6:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 9)">Potato.m: line 7:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 9)">Potato.m: line 7:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 19)">Potato.m: line 7:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 21)">Potato.m: line 7:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 9)">Potato.m: line 8:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 9)">Potato.m: line 8:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 16)">Potato.m: line 8:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 18)">Potato.m: line 8:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 9, 9)">Potato.m: line 9:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 9, 9)">Potato.m: line 9:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 9)">Potato.m: line 13:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 13)">Potato.m: line 13:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 13)">Potato.m: line 13:</a> style: comma cannot be preceeded by whitespace and must be followed by whitespace</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 14)">Potato.m: line 13:</a> style: unnecessary statement terminator</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 16)">Potato.m: line 13:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 9)">Potato.m: line 14:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 9)">Potato.m: line 14:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 20)">Potato.m: line 14:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 22)">Potato.m: line 14:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 15, 9)">Potato.m: line 15:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 9)">Potato.m: line 16:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 9)">Potato.m: line 16:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 17)">Potato.m: line 16:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 19)">Potato.m: line 16:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 17, 9)">Potato.m: line 17:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 18, 9)">Potato.m: line 18:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato.m', 18, 9)">Potato.m: line 18:</a> style: indentation not correct, should be 10 spaces, not 8</div>
<h2>Potato2.m</h2>
<div class="message"><a href="matlab:opentoline('Potato2.m', 6, 10)">Potato2.m: line 6:</a> style: violates naming scheme for class</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 9, 9)">Potato2.m: line 9:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 10, 9)">Potato2.m: line 10:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 11, 11)">Potato2.m: line 11:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 12, 9)">Potato2.m: line 12:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 13, 11)">Potato2.m: line 13:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 14, 9)">Potato2.m: line 14:</a> style: useless line continuation</div>
<div class="message"><a href="matlab:opentoline('Potato2.m', 15, 11)">Potato2.m: line 15:</a> style: violates naming scheme for attribute</div>
</section>
</main>
</body>
Expand Down
40 changes: 38 additions & 2 deletions tests/style/bug_142_continues_properties/expected_out.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
=== PLAIN MODE ===
In Potato.m, line 6
| foo, ... comment1
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 6
| foo, ... comment1
| ^ style: end this with just a newline [fixed] [end_of_statements]
In Potato.m, line 6
| foo, ... comment1
Expand All @@ -13,6 +16,9 @@ In Potato.m, line 7
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato.m, line 8
| bar (1, 1), ... comment2
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 8
| bar (1, 1), ... comment2
| ^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 8
| baz = 5; ... comment3
Expand All @@ -22,9 +28,15 @@ In Potato.m, line 8
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato.m, line 10
| baz = 5; ... comment3
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 10
| baz = 5; ... comment3
| ^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 12
| bork single
| ^^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 12
| bork single
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 13
| foo2,, ... comment1
Expand All @@ -38,6 +50,9 @@ In Potato.m, line 13
In Potato.m, line 13
| foo2,, ... comment1
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato.m, line 16
| foo2,, ... comment1
| ^^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 14
| bar2 (1, 1), ... comment2
| ^ style: end this with just a newline [fixed] [end_of_statements]
Expand All @@ -46,6 +61,9 @@ In Potato.m, line 14
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato.m, line 18
| bar2 (1, 1), ... comment2
| ^^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 18
| bar2 (1, 1), ... comment2
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 16
| baz2 = 5; ... comment4
Expand All @@ -55,6 +73,9 @@ In Potato.m, line 16
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato.m, line 21
| baz2 = 5; ... comment4
| ^^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 21
| baz2 = 5; ... comment4
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 20
| ... comment3
Expand All @@ -64,23 +85,38 @@ In Potato.m, line 23
| ^^^^^^^^^^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato.m, line 24
| bork2 single
| ^^^^^ style: violates naming scheme for attribute [naming_functions]
In Potato.m, line 24
| bork2 single
| ^^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
In Potato2.m, line 6
| classdef Potato2
| ^^^^^^^ style: violates naming scheme for class [naming_classes]
In Potato2.m, line 9
| foo
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato2.m, line 10
| ...
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato2.m, line 11
| bar (1, 1)
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato2.m, line 12
| ...
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
In Potato2.m, line 13
| baz = 5
| ^^^ style: violates naming scheme for attribute [naming_functions]
In Potato2.m, line 14
| ...
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
MISS_HIT Style Summary: 2 file(s) analysed, 26 style issue(s)
In Potato2.m, line 15
| bork single
| ^^^^ style: violates naming scheme for attribute [naming_functions]
MISS_HIT Style Summary: 2 file(s) analysed, 38 style issue(s)

=== HTML MODE ===
MISS_HIT Style Summary: 2 file(s) analysed, 26 style issue(s)
MISS_HIT Style Summary: 2 file(s) analysed, 38 style issue(s)

=== ! BROKEN FIXES ! ===
Fixing is not idempotent for ./Potato.m
3 changes: 3 additions & 0 deletions tests/style/bug_191_constructor_naming/expected_out.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<h1>Issues identified</h1>
<section>
<h2>Kitten.m</h2>
<div class="message"><a href="matlab:opentoline('Kitten.m', 6, 9)">Kitten.m: line 6:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('Kitten.m', 19, 24)">Kitten.m: line 19:</a> error: class can only have one constructor, previous declaration in line 11</div>
<div class="message"><a href="matlab:opentoline('Kitten.m', 19, 24)">Kitten.m: line 19:</a> style: violates naming scheme for method</div>
<h2>Potato.m</h2>
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 9)">Potato.m: line 6:</a> style: violates naming scheme for attribute</div>
</section>
</main>
</body>
Expand Down
10 changes: 8 additions & 2 deletions tests/style/bug_191_constructor_naming/expected_out.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
=== PLAIN MODE ===
In Kitten.m, line 6
| x (1, 1) uint32
| ^ style: violates naming scheme for attribute [naming_functions]
In Kitten.m, line 19
| function obj = Kitten(x)
| ^^^^^^ error: class can only have one constructor, previous declaration in line 11
In Kitten.m, line 19
| function obj = Kitten(x)
| ^^^^^^ style: violates naming scheme for method [naming_functions]
MISS_HIT Style Summary: 2 file(s) analysed, 1 style issue(s), 1 error(s)
In Potato.m, line 6
| x (1, 1) uint32
| ^ style: violates naming scheme for attribute [naming_functions]
MISS_HIT Style Summary: 2 file(s) analysed, 3 style issue(s), 1 error(s)

=== HTML MODE ===
MISS_HIT Style Summary: 2 file(s) analysed, 1 style issue(s), 1 error(s)
MISS_HIT Style Summary: 2 file(s) analysed, 3 style issue(s), 1 error(s)
4 changes: 4 additions & 0 deletions tests/style/bug_286/expected_out.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ <h2>test.m</h2>
<div class="message"><a href="matlab:opentoline('test.m', 1, 24)">test.m: line 1:</a> style: Could not find any copyright notice</div>
<div class="message"><a href="matlab:opentoline('test.m', 8, 9)">test.m: line 8:</a> style: violates naming scheme for enumeration</div>
<div class="message"><a href="matlab:opentoline('test.m', 10, 9)">test.m: line 10:</a> style: violates naming scheme for enumeration</div>
<div class="message"><a href="matlab:opentoline('test.m', 15, 9)">test.m: line 15:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('test.m', 17, 9)">test.m: line 17:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('test.m', 19, 9)">test.m: line 19:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('test.m', 21, 9)">test.m: line 21:</a> style: violates naming scheme for attribute</div>
</section>
</main>
</body>
Expand Down
16 changes: 14 additions & 2 deletions tests/style/bug_286/expected_out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ In test.m, line 8
In test.m, line 10
| R2023A('R2023A', 49, 9.14, 10.7)
| ^^^^^^ style: violates naming scheme for enumeration [naming_enumerations]
MISS_HIT Style Summary: 1 file(s) analysed, 3 style issue(s)
In test.m, line 15
| name
| ^^^^ style: violates naming scheme for attribute [naming_functions]
In test.m, line 17
| number
| ^^^^^^ style: violates naming scheme for attribute [naming_functions]
In test.m, line 19
| matlabversion
| ^^^^^^^^^^^^^ style: violates naming scheme for attribute [naming_functions]
In test.m, line 21
| simulinkversion
| ^^^^^^^^^^^^^^^ style: violates naming scheme for attribute [naming_functions]
MISS_HIT Style Summary: 1 file(s) analysed, 7 style issue(s)

=== HTML MODE ===
MISS_HIT Style Summary: 1 file(s) analysed, 3 style issue(s)
MISS_HIT Style Summary: 1 file(s) analysed, 7 style issue(s)
2 changes: 2 additions & 0 deletions tests/style/bug_91/expected_out.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ <h1>Issues identified</h1>
<section>
<h2>foo.m</h2>
<div class="message"><a href="matlab:opentoline('foo.m', 3, 10)">foo.m: line 3:</a> style: violates naming scheme for class</div>
<div class="message"><a href="matlab:opentoline('foo.m', 5, 9)">foo.m: line 5:</a> style: violates naming scheme for attribute</div>
<div class="message"><a href="matlab:opentoline('foo.m', 5, 10)">foo.m: line 5:</a> style: end this with just a newline</div>
<div class="message"><a href="matlab:opentoline('foo.m', 6, 9)">foo.m: line 6:</a> style: violates naming scheme for attribute</div>
</section>
</main>
</body>
Expand Down
10 changes: 8 additions & 2 deletions tests/style/bug_91/expected_out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ In foo.m, line 3
| ^^^ style: violates naming scheme for class [naming_classes]
In foo.m, line 5
| x; % potato
| ^ style: violates naming scheme for attribute [naming_functions]
In foo.m, line 5
| x; % potato
| ^ style: end this with just a newline [fixed] [end_of_statements]
MISS_HIT Style Summary: 1 file(s) analysed, 2 style issue(s)
In foo.m, line 6
| y % potato
| ^ style: violates naming scheme for attribute [naming_functions]
MISS_HIT Style Summary: 1 file(s) analysed, 4 style issue(s)

=== HTML MODE ===
MISS_HIT Style Summary: 1 file(s) analysed, 2 style issue(s)
MISS_HIT Style Summary: 1 file(s) analysed, 4 style issue(s)
Loading

0 comments on commit 353a3f4

Please sign in to comment.