32
32
33
33
@ Rule (
34
34
key = LineLengthCheck .CHECK_KEY ,
35
- priority = Priority .MINOR ,
36
35
name = "Lines should not be too long" ,
36
+ priority = Priority .MINOR ,
37
37
tags = Tags .CONVENTION
38
38
)
39
39
@ SqaleConstantRemediation ("1min" )
@@ -42,6 +42,8 @@ public class LineLengthCheck extends SquidCheck<Grammar> implements AstAndTokenV
42
42
public static final String CHECK_KEY = "LineLength" ;
43
43
private static final int DEFAULT_MAXIMUM_LINE_LENGTH = 120 ;
44
44
45
+ private Token previousToken ;
46
+
45
47
@ RuleProperty (
46
48
key = "maximumLineLength" ,
47
49
defaultValue = "" + DEFAULT_MAXIMUM_LINE_LENGTH )
@@ -51,8 +53,6 @@ public int getMaximumLineLength() {
51
53
return maximumLineLength ;
52
54
}
53
55
54
- private Token previousToken ;
55
-
56
56
@ Override
57
57
public void visitFile (AstNode astNode ) {
58
58
previousToken = null ;
@@ -65,26 +65,28 @@ public void leaveFile(AstNode astNode) {
65
65
66
66
@ Override
67
67
public void visitToken (Token token ) {
68
- if (!token .isGeneratedCode ()) {
69
- if (previousToken != null && previousToken .getLine () != token .getLine ()) {
70
- // Note that AbstractLineLengthCheck doesn't support tokens which span multiple lines - see SONARPLUGINS-2025
71
- String [] lines = previousToken .getValue ().split ("\r ?\n |\r " , -1 );
72
- int length = previousToken .getColumn ();
73
- for (int line = 0 ; line < lines .length ; line ++) {
74
- length += lines [line ].length ();
75
- if (length > getMaximumLineLength ()) {
76
- // Note that method from AbstractLineLengthCheck generates other message - see SONARPLUGINS-1809
77
- getContext ().createLineViolation (this ,
78
- "The line contains {0,number,integer} characters which is greater than {1,number,integer} authorized." ,
79
- previousToken .getLine (),
80
- length ,
81
- getMaximumLineLength ());
82
- }
83
- length = 0 ;
68
+ if (token .isGeneratedCode ()) {
69
+ return ;
70
+ }
71
+
72
+ if (previousToken != null && previousToken .getLine () != token .getLine ()) {
73
+ // Note that AbstractLineLengthCheck doesn't support tokens which span multiple lines - see SONARPLUGINS-2025
74
+ String [] lines = previousToken .getValue ().split ("\r ?\n |\r " , -1 );
75
+ int length = previousToken .getColumn ();
76
+ for (String line : lines ) {
77
+ length += line .length ();
78
+ if (length > getMaximumLineLength ()) {
79
+ // Note that method from AbstractLineLengthCheck generates other message - see SONARPLUGINS-1809
80
+ getContext ().createLineViolation (this ,
81
+ "The line contains {0,number,integer} characters which is greater than {1,number,integer} authorized." ,
82
+ previousToken .getLine (),
83
+ length ,
84
+ getMaximumLineLength ());
84
85
}
86
+ length = 0 ;
85
87
}
86
- previousToken = token ;
87
88
}
89
+ previousToken = token ;
88
90
}
89
91
90
92
}
0 commit comments