Skip to content

Commit 353a3f4

Browse files
#287 Support getters and setters naming
New configuration "regex_attribute_name" for naming attributes. Closes #287
1 parent b6e5057 commit 353a3f4

File tree

23 files changed

+290
-20
lines changed

23 files changed

+290
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ Not quite compatible with Octave yet. See #43 [octave support](https://github.co
2020

2121
### 0.9.44-dev
2222

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

2527
### 0.9.43
2628

docs/style_checker.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ <h4>Naming scheme for functions ("naming_functions")</h4>
949949

950950
<div>
951951
This rule enforces a consistent naming for all user-defined
952-
functions and methods.
952+
functions, methods, getters, and setters.
953953
</div>
954954

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

miss_hit_core/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## ##
44
## MATLAB Independent, Small & Safe, High Integrity Tools ##
55
## ##
6-
## Copyright (C) 2020-2022, Florian Schanda ##
6+
## Copyright (C) 2020-2024, Florian Schanda ##
77
## ##
88
## This file is part of MISS_HIT. ##
99
## ##
@@ -379,7 +379,10 @@ class Function_Metric(Code_Metric):
379379
default = DEFAULT_NAMING_SCHEME),
380380
"regex_method_name" : Regex_Style_Configuration(
381381
"Regex for class method names",
382-
default = DEFAULT_LC_NAMING_SCHEME)
382+
default = DEFAULT_LC_NAMING_SCHEME),
383+
"regex_attribute_name" : Regex_Style_Configuration(
384+
"Regex for class attributes",
385+
default = DEFAULT_NAMING_SCHEME)
383386
}),
384387

385388
"naming_parameters" : Style_Rule(

miss_hit_core/m_ast.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## ##
44
## MATLAB Independent, Small & Safe, High Integrity Tools ##
55
## ##
6-
## Copyright (C) 2019-2022, Florian Schanda ##
6+
## Copyright (C) 2019-2024, Florian Schanda ##
77
## ##
88
## This file is part of MISS_HIT. ##
99
## ##
@@ -753,6 +753,9 @@ def sty_check_naming(self, mh, cfg):
753753
assert isinstance(cfg, Config)
754754
if cfg.active("naming_classes"):
755755
self.n_name.sty_check_naming(mh, cfg, "class", "naming_classes")
756+
for n_block in self.l_properties:
757+
for n_prop in n_block.l_items:
758+
n_prop.sty_check_naming(mh, cfg)
756759
for n_block in self.l_methods:
757760
for n_function in n_block.l_items:
758761
n_function.sty_check_naming(mh, cfg)
@@ -1181,7 +1184,7 @@ def sty_check_naming(self, mh, cfg):
11811184
raise ICE("class method with %s node as name" %
11821185
self.n_name.__class__.__name__)
11831186
else:
1184-
self.n_name.n_field.sty_check_naming(mh, cfg, "method",
1187+
self.n_name.n_field.sty_check_naming(mh, cfg, "attribute",
11851188
"naming_functions")
11861189

11871190
else:
@@ -1452,6 +1455,17 @@ def visit(self, parent, function, relation):
14521455
self.n_default_value.visit(self, function, "Default")
14531456
self._visit_end(parent, function, relation)
14541457

1458+
def sty_check_naming(self, mh, cfg):
1459+
assert isinstance(mh, Message_Handler)
1460+
assert isinstance(cfg, Config)
1461+
1462+
if self.n_parent.kind() != "properties":
1463+
raise ICE("called naming check on %s block" % self.n_parent.kind())
1464+
1465+
if cfg.active("naming_functions"):
1466+
self.n_name.sty_check_naming(mh, cfg, "attribute",
1467+
"naming_functions")
1468+
14551469

14561470
class Argument_Validation_Delegation(Node):
14571471
""" AST for a the .? special syntax found inside argument blocks.

tests/style/bug_142_continues_properties/expected_out.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,45 @@
1212
<h1>Issues identified</h1>
1313
<section>
1414
<h2>Potato.m</h2>
15+
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 9)">Potato.m: line 6:</a> style: violates naming scheme for attribute</div>
1516
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 12)">Potato.m: line 6:</a> style: end this with just a newline</div>
1617
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 14)">Potato.m: line 6:</a> style: useless line continuation</div>
18+
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 9)">Potato.m: line 7:</a> style: violates naming scheme for attribute</div>
1719
<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>
1820
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 19)">Potato.m: line 7:</a> style: end this with just a newline</div>
1921
<div class="message"><a href="matlab:opentoline('Potato.m', 7, 21)">Potato.m: line 7:</a> style: useless line continuation</div>
22+
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 9)">Potato.m: line 8:</a> style: violates naming scheme for attribute</div>
2023
<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>
2124
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 16)">Potato.m: line 8:</a> style: end this with just a newline</div>
2225
<div class="message"><a href="matlab:opentoline('Potato.m', 8, 18)">Potato.m: line 8:</a> style: useless line continuation</div>
26+
<div class="message"><a href="matlab:opentoline('Potato.m', 9, 9)">Potato.m: line 9:</a> style: violates naming scheme for attribute</div>
2327
<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>
28+
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 9)">Potato.m: line 13:</a> style: violates naming scheme for attribute</div>
2429
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 13)">Potato.m: line 13:</a> style: end this with just a newline</div>
2530
<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>
2631
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 14)">Potato.m: line 13:</a> style: unnecessary statement terminator</div>
2732
<div class="message"><a href="matlab:opentoline('Potato.m', 13, 16)">Potato.m: line 13:</a> style: useless line continuation</div>
33+
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 9)">Potato.m: line 14:</a> style: violates naming scheme for attribute</div>
2834
<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>
2935
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 20)">Potato.m: line 14:</a> style: end this with just a newline</div>
3036
<div class="message"><a href="matlab:opentoline('Potato.m', 14, 22)">Potato.m: line 14:</a> style: useless line continuation</div>
3137
<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>
38+
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 9)">Potato.m: line 16:</a> style: violates naming scheme for attribute</div>
3239
<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>
3340
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 17)">Potato.m: line 16:</a> style: end this with just a newline</div>
3441
<div class="message"><a href="matlab:opentoline('Potato.m', 16, 19)">Potato.m: line 16:</a> style: useless line continuation</div>
3542
<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>
43+
<div class="message"><a href="matlab:opentoline('Potato.m', 18, 9)">Potato.m: line 18:</a> style: violates naming scheme for attribute</div>
3644
<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>
3745
<h2>Potato2.m</h2>
3846
<div class="message"><a href="matlab:opentoline('Potato2.m', 6, 10)">Potato2.m: line 6:</a> style: violates naming scheme for class</div>
47+
<div class="message"><a href="matlab:opentoline('Potato2.m', 9, 9)">Potato2.m: line 9:</a> style: violates naming scheme for attribute</div>
3948
<div class="message"><a href="matlab:opentoline('Potato2.m', 10, 9)">Potato2.m: line 10:</a> style: useless line continuation</div>
49+
<div class="message"><a href="matlab:opentoline('Potato2.m', 11, 11)">Potato2.m: line 11:</a> style: violates naming scheme for attribute</div>
4050
<div class="message"><a href="matlab:opentoline('Potato2.m', 12, 9)">Potato2.m: line 12:</a> style: useless line continuation</div>
51+
<div class="message"><a href="matlab:opentoline('Potato2.m', 13, 11)">Potato2.m: line 13:</a> style: violates naming scheme for attribute</div>
4152
<div class="message"><a href="matlab:opentoline('Potato2.m', 14, 9)">Potato2.m: line 14:</a> style: useless line continuation</div>
53+
<div class="message"><a href="matlab:opentoline('Potato2.m', 15, 11)">Potato2.m: line 15:</a> style: violates naming scheme for attribute</div>
4254
</section>
4355
</main>
4456
</body>

tests/style/bug_142_continues_properties/expected_out.txt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
=== PLAIN MODE ===
22
In Potato.m, line 6
33
| foo, ... comment1
4+
| ^^^ style: violates naming scheme for attribute [naming_functions]
5+
In Potato.m, line 6
6+
| foo, ... comment1
47
| ^ style: end this with just a newline [fixed] [end_of_statements]
58
In Potato.m, line 6
69
| foo, ... comment1
@@ -13,6 +16,9 @@ In Potato.m, line 7
1316
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
1417
In Potato.m, line 8
1518
| bar (1, 1), ... comment2
19+
| ^^^ style: violates naming scheme for attribute [naming_functions]
20+
In Potato.m, line 8
21+
| bar (1, 1), ... comment2
1622
| ^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
1723
In Potato.m, line 8
1824
| baz = 5; ... comment3
@@ -22,9 +28,15 @@ In Potato.m, line 8
2228
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
2329
In Potato.m, line 10
2430
| baz = 5; ... comment3
31+
| ^^^ style: violates naming scheme for attribute [naming_functions]
32+
In Potato.m, line 10
33+
| baz = 5; ... comment3
2534
| ^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
2635
In Potato.m, line 12
2736
| bork single
37+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
38+
In Potato.m, line 12
39+
| bork single
2840
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
2941
In Potato.m, line 13
3042
| foo2,, ... comment1
@@ -38,6 +50,9 @@ In Potato.m, line 13
3850
In Potato.m, line 13
3951
| foo2,, ... comment1
4052
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
53+
In Potato.m, line 16
54+
| foo2,, ... comment1
55+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
4156
In Potato.m, line 14
4257
| bar2 (1, 1), ... comment2
4358
| ^ style: end this with just a newline [fixed] [end_of_statements]
@@ -46,6 +61,9 @@ In Potato.m, line 14
4661
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
4762
In Potato.m, line 18
4863
| bar2 (1, 1), ... comment2
64+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
65+
In Potato.m, line 18
66+
| bar2 (1, 1), ... comment2
4967
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
5068
In Potato.m, line 16
5169
| baz2 = 5; ... comment4
@@ -55,6 +73,9 @@ In Potato.m, line 16
5573
| ^^^^^^^^^^^^^ style: useless line continuation [fixed] [useless_continuation]
5674
In Potato.m, line 21
5775
| baz2 = 5; ... comment4
76+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
77+
In Potato.m, line 21
78+
| baz2 = 5; ... comment4
5879
| ^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
5980
In Potato.m, line 20
6081
| ... comment3
@@ -64,23 +85,38 @@ In Potato.m, line 23
6485
| ^^^^^^^^^^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
6586
In Potato.m, line 24
6687
| bork2 single
88+
| ^^^^^ style: violates naming scheme for attribute [naming_functions]
89+
In Potato.m, line 24
90+
| bork2 single
6791
| ^^^^^ style: indentation not correct, should be 10 spaces, not 8 [fixed] [indentation]
6892
In Potato2.m, line 6
6993
| classdef Potato2
7094
| ^^^^^^^ style: violates naming scheme for class [naming_classes]
95+
In Potato2.m, line 9
96+
| foo
97+
| ^^^ style: violates naming scheme for attribute [naming_functions]
7198
In Potato2.m, line 10
7299
| ...
73100
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
101+
In Potato2.m, line 11
102+
| bar (1, 1)
103+
| ^^^ style: violates naming scheme for attribute [naming_functions]
74104
In Potato2.m, line 12
75105
| ...
76106
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
107+
In Potato2.m, line 13
108+
| baz = 5
109+
| ^^^ style: violates naming scheme for attribute [naming_functions]
77110
In Potato2.m, line 14
78111
| ...
79112
| ^^^^ style: useless line continuation [fixed] [useless_continuation]
80-
MISS_HIT Style Summary: 2 file(s) analysed, 26 style issue(s)
113+
In Potato2.m, line 15
114+
| bork single
115+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
116+
MISS_HIT Style Summary: 2 file(s) analysed, 38 style issue(s)
81117

82118
=== HTML MODE ===
83-
MISS_HIT Style Summary: 2 file(s) analysed, 26 style issue(s)
119+
MISS_HIT Style Summary: 2 file(s) analysed, 38 style issue(s)
84120

85121
=== ! BROKEN FIXES ! ===
86122
Fixing is not idempotent for ./Potato.m

tests/style/bug_191_constructor_naming/expected_out.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
<h1>Issues identified</h1>
1313
<section>
1414
<h2>Kitten.m</h2>
15+
<div class="message"><a href="matlab:opentoline('Kitten.m', 6, 9)">Kitten.m: line 6:</a> style: violates naming scheme for attribute</div>
1516
<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>
1617
<div class="message"><a href="matlab:opentoline('Kitten.m', 19, 24)">Kitten.m: line 19:</a> style: violates naming scheme for method</div>
18+
<h2>Potato.m</h2>
19+
<div class="message"><a href="matlab:opentoline('Potato.m', 6, 9)">Potato.m: line 6:</a> style: violates naming scheme for attribute</div>
1720
</section>
1821
</main>
1922
</body>
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
=== PLAIN MODE ===
2+
In Kitten.m, line 6
3+
| x (1, 1) uint32
4+
| ^ style: violates naming scheme for attribute [naming_functions]
25
In Kitten.m, line 19
36
| function obj = Kitten(x)
47
| ^^^^^^ error: class can only have one constructor, previous declaration in line 11
58
In Kitten.m, line 19
69
| function obj = Kitten(x)
710
| ^^^^^^ style: violates naming scheme for method [naming_functions]
8-
MISS_HIT Style Summary: 2 file(s) analysed, 1 style issue(s), 1 error(s)
11+
In Potato.m, line 6
12+
| x (1, 1) uint32
13+
| ^ style: violates naming scheme for attribute [naming_functions]
14+
MISS_HIT Style Summary: 2 file(s) analysed, 3 style issue(s), 1 error(s)
915

1016
=== HTML MODE ===
11-
MISS_HIT Style Summary: 2 file(s) analysed, 1 style issue(s), 1 error(s)
17+
MISS_HIT Style Summary: 2 file(s) analysed, 3 style issue(s), 1 error(s)

tests/style/bug_286/expected_out.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ <h2>test.m</h2>
1515
<div class="message"><a href="matlab:opentoline('test.m', 1, 24)">test.m: line 1:</a> style: Could not find any copyright notice</div>
1616
<div class="message"><a href="matlab:opentoline('test.m', 8, 9)">test.m: line 8:</a> style: violates naming scheme for enumeration</div>
1717
<div class="message"><a href="matlab:opentoline('test.m', 10, 9)">test.m: line 10:</a> style: violates naming scheme for enumeration</div>
18+
<div class="message"><a href="matlab:opentoline('test.m', 15, 9)">test.m: line 15:</a> style: violates naming scheme for attribute</div>
19+
<div class="message"><a href="matlab:opentoline('test.m', 17, 9)">test.m: line 17:</a> style: violates naming scheme for attribute</div>
20+
<div class="message"><a href="matlab:opentoline('test.m', 19, 9)">test.m: line 19:</a> style: violates naming scheme for attribute</div>
21+
<div class="message"><a href="matlab:opentoline('test.m', 21, 9)">test.m: line 21:</a> style: violates naming scheme for attribute</div>
1822
</section>
1923
</main>
2024
</body>

tests/style/bug_286/expected_out.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ In test.m, line 8
88
In test.m, line 10
99
| R2023A('R2023A', 49, 9.14, 10.7)
1010
| ^^^^^^ style: violates naming scheme for enumeration [naming_enumerations]
11-
MISS_HIT Style Summary: 1 file(s) analysed, 3 style issue(s)
11+
In test.m, line 15
12+
| name
13+
| ^^^^ style: violates naming scheme for attribute [naming_functions]
14+
In test.m, line 17
15+
| number
16+
| ^^^^^^ style: violates naming scheme for attribute [naming_functions]
17+
In test.m, line 19
18+
| matlabversion
19+
| ^^^^^^^^^^^^^ style: violates naming scheme for attribute [naming_functions]
20+
In test.m, line 21
21+
| simulinkversion
22+
| ^^^^^^^^^^^^^^^ style: violates naming scheme for attribute [naming_functions]
23+
MISS_HIT Style Summary: 1 file(s) analysed, 7 style issue(s)
1224

1325
=== HTML MODE ===
14-
MISS_HIT Style Summary: 1 file(s) analysed, 3 style issue(s)
26+
MISS_HIT Style Summary: 1 file(s) analysed, 7 style issue(s)

0 commit comments

Comments
 (0)