Skip to content

Commit 18731b0

Browse files
committed
Fix some quality flaws
1 parent 7e5a157 commit 18731b0

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

python-checks/src/main/java/org/sonar/python/checks/LineLengthCheck.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
@Rule(
3434
key = LineLengthCheck.CHECK_KEY,
35-
priority = Priority.MINOR,
3635
name = "Lines should not be too long",
36+
priority = Priority.MINOR,
3737
tags = Tags.CONVENTION
3838
)
3939
@SqaleConstantRemediation("1min")
@@ -42,6 +42,8 @@ public class LineLengthCheck extends SquidCheck<Grammar> implements AstAndTokenV
4242
public static final String CHECK_KEY = "LineLength";
4343
private static final int DEFAULT_MAXIMUM_LINE_LENGTH = 120;
4444

45+
private Token previousToken;
46+
4547
@RuleProperty(
4648
key = "maximumLineLength",
4749
defaultValue = "" + DEFAULT_MAXIMUM_LINE_LENGTH)
@@ -51,8 +53,6 @@ public int getMaximumLineLength() {
5153
return maximumLineLength;
5254
}
5355

54-
private Token previousToken;
55-
5656
@Override
5757
public void visitFile(AstNode astNode) {
5858
previousToken = null;
@@ -65,26 +65,28 @@ public void leaveFile(AstNode astNode) {
6565

6666
@Override
6767
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());
8485
}
86+
length = 0;
8587
}
86-
previousToken = token;
8788
}
89+
previousToken = token;
8890
}
8991

9092
}

python-squid/src/main/java/org/sonar/python/PythonLinesOfCodeVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Visitor that computes the number of lines of code of a file.
3434
*/
35-
public class PythonLinesOfCodeVisitor<GRAMMAR extends Grammar> extends SquidAstVisitor<GRAMMAR> implements AstAndTokenVisitor {
35+
public class PythonLinesOfCodeVisitor<G extends Grammar> extends SquidAstVisitor<G> implements AstAndTokenVisitor {
3636

3737
private final MetricDef metric;
3838
private int lastTokenLine;

python-squid/src/main/java/org/sonar/python/lexer/PythonLexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636

3737
public final class PythonLexer {
3838

39-
private PythonLexer() {
40-
}
41-
4239
private static final String EXP = "([Ee][+-]?+[0-9_]++)";
4340
private static final String BYTES_PREFIX = "([bB][Rr]?|[rR][bB]?)";
4441
private static final String IMAGINARY_SUFFIX = "(j|J)";
4542
private static final String LONG_INTEGER_SUFFIX = "(l|L)";
4643
private static final String FORMATTED_STRING_PREFIX = "([fF][rR]?|[rR][fF]?)";
4744

45+
private PythonLexer() {
46+
}
47+
4848
public static Lexer create(PythonConfiguration conf) {
4949
LexerState lexerState = new LexerState();
5050

0 commit comments

Comments
 (0)