Add max_line_length config
This commit is contained in:
parent
308bdfd1c1
commit
7c54e445c6
|
@ -57,8 +57,10 @@ redundant_parens_check="enabled"
|
|||
mismatched_args_check="enabled"
|
||||
; Checks for labels with the same name as variables
|
||||
label_var_same_name_check="enabled"
|
||||
; Checks for lines longer than 120 characters
|
||||
; Checks for lines longer than `max_line_length` characters
|
||||
long_line_check="disabled"
|
||||
; Maximum line length in `long_line_check`.
|
||||
max_line_length="120"
|
||||
; Checks for assignment to auto-ref function parameters
|
||||
auto_ref_assignment_check="enabled"
|
||||
; Checks for incorrect infinite range definitions
|
||||
|
|
|
@ -146,7 +146,7 @@ Note that the "--skipTests" option is the equivalent of changing each
|
|||
* Variables that could have been declared const or immutable (experimental)
|
||||
* Redundant parenthesis.
|
||||
* Unused labels.
|
||||
* Lines longer than 120 characters.
|
||||
* Lines longer than `max_line_length` characters.
|
||||
* Incorrect infinite range definitions.
|
||||
* Some assertions that check conditions that will always be true.
|
||||
* Auto functions without return statement. The compiler doesn't see an omission and it infers 'void' as return type.
|
||||
|
|
|
@ -140,9 +140,12 @@ struct StaticAnalysisConfig
|
|||
@INI("Checks for labels with the same name as variables")
|
||||
string label_var_same_name_check = Check.enabled;
|
||||
|
||||
@INI("Checks for lines longer than 120 characters")
|
||||
@INI("Checks for lines longer than `max_line_length` characters")
|
||||
string long_line_check = Check.enabled;
|
||||
|
||||
@INI("The maximum line length used in `long_line_check`.")
|
||||
int max_line_length = 120;
|
||||
|
||||
@INI("Checks for assignment to auto-ref function parameters")
|
||||
string auto_ref_assignment_check = Check.enabled;
|
||||
|
||||
|
|
|
@ -13,17 +13,18 @@ import dparse.lexer;
|
|||
import std.typecons : tuple, Tuple;
|
||||
|
||||
/**
|
||||
* Checks for lines longer than 120 characters
|
||||
* Checks for lines longer than `max_line_length` characters
|
||||
*/
|
||||
final class LineLengthCheck : BaseAnalyzer
|
||||
{
|
||||
mixin AnalyzerInfo!"long_line_check";
|
||||
|
||||
///
|
||||
this(string fileName, const(Token)[] tokens, bool skipTests = false)
|
||||
this(string fileName, const(Token)[] tokens, int maxLineLength, bool skipTests = false)
|
||||
{
|
||||
super(fileName, null, skipTests);
|
||||
this.tokens = tokens;
|
||||
this.maxLineLength = maxLineLength;
|
||||
}
|
||||
|
||||
override void visit(const Module)
|
||||
|
@ -44,7 +45,7 @@ final class LineLengthCheck : BaseAnalyzer
|
|||
: 0;
|
||||
endColumn += wsChange + info.length;
|
||||
}
|
||||
if (endColumn > MAX_LINE_LENGTH)
|
||||
if (endColumn > maxLineLength)
|
||||
triggerError(token);
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +60,7 @@ private:
|
|||
{
|
||||
if (tok.line != lastErrorLine)
|
||||
{
|
||||
addErrorMessage(tok.line, tok.column, KEY, MESSAGE);
|
||||
addErrorMessage(tok.line, tok.column, KEY, message);
|
||||
lastErrorLine = tok.line;
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +80,7 @@ private:
|
|||
{
|
||||
if (isLineSeparator(c))
|
||||
{
|
||||
if (col > MAX_LINE_LENGTH)
|
||||
if (col > maxLineLength)
|
||||
triggerError(tok);
|
||||
col = 1;
|
||||
}
|
||||
|
@ -91,9 +92,9 @@ private:
|
|||
|
||||
unittest
|
||||
{
|
||||
assert(new LineLengthCheck(null, null).checkMultiLineToken(Token(tok!"stringLiteral", " ", 0, 0, 0)) == 8);
|
||||
assert(new LineLengthCheck(null, null).checkMultiLineToken(Token(tok!"stringLiteral", " \na", 0, 0, 0)) == 2);
|
||||
assert(new LineLengthCheck(null, null).checkMultiLineToken(Token(tok!"stringLiteral", " \n ", 0, 0, 0)) == 5);
|
||||
assert(new LineLengthCheck(null, null, 120).checkMultiLineToken(Token(tok!"stringLiteral", " ", 0, 0, 0)) == 8);
|
||||
assert(new LineLengthCheck(null, null, 120).checkMultiLineToken(Token(tok!"stringLiteral", " \na", 0, 0, 0)) == 2);
|
||||
assert(new LineLengthCheck(null, null, 120).checkMultiLineToken(Token(tok!"stringLiteral", " \n ", 0, 0, 0)) == 5);
|
||||
}
|
||||
|
||||
static size_t tokenByteLength()(auto ref const Token tok)
|
||||
|
@ -155,9 +156,13 @@ private:
|
|||
|
||||
import std.conv : to;
|
||||
|
||||
string message() const
|
||||
{
|
||||
return "Line is longer than " ~ to!string(maxLineLength) ~ " characters";
|
||||
}
|
||||
|
||||
enum string KEY = "dscanner.style.long_line";
|
||||
enum string MESSAGE = "Line is longer than " ~ to!string(MAX_LINE_LENGTH) ~ " characters";
|
||||
enum MAX_LINE_LENGTH = 120;
|
||||
const int maxLineLength;
|
||||
const(Token)[] tokens;
|
||||
}
|
||||
|
||||
|
@ -201,5 +206,20 @@ assert("foo" == "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
|
|||
`);
|
||||
}c, sac);
|
||||
|
||||
// Test customizing max_line_length.
|
||||
sac.max_line_length = 115;
|
||||
assertAnalyzerWarnings(q{
|
||||
Window window = Platform.instance.createWindow("Дистанционное управлсварочным оборудованием ", null);
|
||||
Window window = Platform.instance.createWindow("Дистанционное управлсварочным оборудованием ", null); // [warn]: Line is longer than 115 characters
|
||||
unittest {
|
||||
// with tabs
|
||||
assert("foo" == "fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo1");
|
||||
assert("foo" == "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo2"); // [warn]: Line is longer than 115 characters
|
||||
// with whitespace (don't overwrite)
|
||||
assert("foo" == "booooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo3");
|
||||
assert("foo" == "boooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo4"); // [warn]: Line is longer than 115 characters
|
||||
}
|
||||
}c, sac);
|
||||
|
||||
stderr.writeln("Unittest for LineLengthCheck passed.");
|
||||
}
|
||||
|
|
|
@ -502,6 +502,7 @@ MessageSet analyze(string fileName, const Module m, const StaticAnalysisConfig a
|
|||
|
||||
if (moduleName.shouldRun!LineLengthCheck(analysisConfig))
|
||||
checks ~= new LineLengthCheck(fileName, tokens,
|
||||
analysisConfig.max_line_length,
|
||||
analysisConfig.long_line_check == Check.skipTests && !ut);
|
||||
|
||||
if (moduleName.shouldRun!AutoRefAssignmentCheck(analysisConfig))
|
||||
|
|
Loading…
Reference in New Issue