Skip to content

Commit

Permalink
added more checks for automatic coding style detection (#414)
Browse files Browse the repository at this point in the history
* added more checks for automatic coding style detection
  • Loading branch information
AlexHaxe authored May 10, 2018
1 parent fdeb52a commit 7b3d12e
Show file tree
Hide file tree
Showing 25 changed files with 609 additions and 117 deletions.
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ trim_trailing_whitespace = true

[*.hx]
end_of_line = lf
charset = utf-8
charset = utf-8

[.travis.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
git:
depth: 1
depth: 1

language: haxe
haxe:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## dev branch / next version (2.x.x)

- New command line option `-show-parser-errors` to include parser errors into checkstyle results (default: off) [#413](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/413)
- Added more checks for automatic coding style detection [#414](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/414)
- Fixed handling of `Arrow` in type names in TokenTree [#413](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/413)
- Fixed handling of `Dot` after `KwdNew` in TokenTree [#413](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/413)
- Improved handling of file content and `class` parsing in TokenTree [#413](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/413)
Expand Down
6 changes: 0 additions & 6 deletions checkstyle.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,6 @@
},
"type": "LocalVariableName"
},
{
"props": {
"ignoreNumbers": [-1, 0, 1, 2, 3, 4.0, 5, 8, 10, 13, 21, 34, 100]
},
"type": "MagicNumber"
},
{
"props": {
"ignoreExtern": true,
Expand Down
10 changes: 5 additions & 5 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@
},
{
"props": {
"spaceIfCondition": 0,
"spaceIfCondition": "should",
"spaceAroundBinop": true,
"spaceForLoop": 0,
"spaceForLoop": "should",
"ignoreRangeOperator": true,
"spaceWhileLoop": 0,
"spaceCatch": 0,
"spaceSwitchCase": 0,
"spaceWhileLoop": "should",
"spaceCatch": "should",
"spaceSwitchCase": "should",
"noSpaceAroundUnop": true
},
"type": "Spacing"
Expand Down
33 changes: 0 additions & 33 deletions src/checkstyle/checks/Directive.hx

This file was deleted.

14 changes: 14 additions & 0 deletions src/checkstyle/checks/literal/StringLiteralCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ class StringLiteralCheck extends Check {
logPos('String "$s" uses single quotes instead of double quotes', pos);
}
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "policy",
values: [DOUBLE_AND_INTERPOLATION, ONLY_SINGLE, ONLY_DOUBLE]
},
{
propertyName: "allowException",
values: [false, true]
}]
}];
}
}

@:enum
Expand Down
31 changes: 28 additions & 3 deletions src/checkstyle/checks/metrics/CyclomaticComplexityCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CyclomaticComplexityCheck extends Check {
public function new() {
super(AST);
thresholds = [
{ severity : "WARNING", complexity : DEFAULT_COMPLEXITY_WARNING },
{ severity : "ERROR", complexity : DEFAULT_COMPLEXITY_ERROR }
{ severity : WARNING, complexity : DEFAULT_COMPLEXITY_WARNING },
{ severity : ERROR, complexity : DEFAULT_COMPLEXITY_ERROR }
];
categories = [Category.COMPLEXITY];
points = 13;
Expand Down Expand Up @@ -90,6 +90,31 @@ class CyclomaticComplexityCheck extends Check {
function notify(method:Target, complexity:Int, risk:Threshold) {
logPos('Method "${method.name}" is too complex (score: $complexity).', method.pos, risk.severity);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "thresholds",
values: [[
{ severity : WARNING, complexity : 6 },
{ severity : ERROR, complexity : 16 }
],
[
{ severity : WARNING, complexity : 11 },
{ severity : ERROR, complexity : 21 }
],
[
{ severity : WARNING, complexity : 16 },
{ severity : ERROR, complexity : 26 }
],
[
{ severity : WARNING, complexity : 21 },
{ severity : ERROR, complexity : 31 }
]]
}]
}];
}
}

typedef Target = {
Expand All @@ -99,6 +124,6 @@ typedef Target = {
}

typedef Threshold = {
var severity:String;
var severity:SeverityLevel;
var complexity:Int;
}
10 changes: 10 additions & 0 deletions src/checkstyle/checks/size/FileLengthCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ class FileLengthCheck extends Check {
}
if (checker.lines.length > max) log('File length is ${checker.lines.length} lines (max allowed is ${max})', checker.lines.length, 0);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "max",
values: [for (i in 0...7) 8 * 100 + i * 100 * 2]
}]
}];
}
}
10 changes: 10 additions & 0 deletions src/checkstyle/checks/size/LineLengthCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ class LineLengthCheck extends Check {
}
}
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "max",
values: [for (i in 0...7) 8 * 10 + i * 10 * 2]
}]
}];
}
}
18 changes: 18 additions & 0 deletions src/checkstyle/checks/size/MethodCountCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ class MethodCountCheck extends Check {
return;
}
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "maxTotal",
values: [for (i in 0...21) (2 + i) * 5]
},
{
propertyName: "maxPrivate",
values: [for (i in 0...21) (2 + i) * 5]
},
{
propertyName: "maxPublic",
values: [for (i in 0...21) (2 + i) * 5]
}]
}];
}
}
14 changes: 14 additions & 0 deletions src/checkstyle/checks/size/MethodLengthCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ class MethodLengthCheck extends Check {
function warnFunctionLength(len:Int, name:String, pos:Position) {
logPos('Method `${name}` length is ${len} lines (max allowed is ${max})', pos);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "max",
values: [for (i in 0...17) 2 * 10 + i * 5]
},
{
propertyName: "countEmpty",
values: [true, false]
}]
}];
}
}
14 changes: 14 additions & 0 deletions src/checkstyle/checks/size/ParameterNumberCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ class ParameterNumberCheck extends Check {
function warnMaxParameter(name:String, pos:Position) {
logPos('Too many parameters for function: ${name} (> ${max})', pos);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "max",
values: [for (i in 4...15) i]
},
{
propertyName: "ignoreOverriddenMethods",
values: [true, false]
}]
}];
}
}
10 changes: 10 additions & 0 deletions src/checkstyle/checks/type/AnonymousCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ class AnonymousCheck extends Check {
function error(name:String, pos:Position) {
logPos('Anonymous structure "${name}" found, use "typedef"', pos);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "severity",
values: ["INFO"]
}]
}];
}
}
10 changes: 10 additions & 0 deletions src/checkstyle/checks/type/DynamicCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ class DynamicCheck extends Check {
function error(name:String, pos:Position) {
logPos('"${name}" type is "Dynamic"', pos);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "severity",
values: ["INFO"]
}]
}];
}
}
18 changes: 18 additions & 0 deletions src/checkstyle/checks/type/ReturnCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,22 @@ class ReturnCheck extends Check {
}
else logPos('Return type not specified for method "${name}"', pos);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "allowEmptyReturn",
values: [true, false]
},
{
propertyName: "enforceReturnTypeForAnonymous",
values: [true, false]
},
{
propertyName: "enforceReturnType",
values: [true, false]
}]
}];
}
}
14 changes: 14 additions & 0 deletions src/checkstyle/checks/whitespace/ArrayAccessCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ class ArrayAccessCheck extends Check {
lastExpr = e;
});
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [],
properties: [{
propertyName: "spaceBefore",
values: [true, false]
},
{
propertyName: "spaceInside",
values: [true, false]
}]
}];
}
}
33 changes: 33 additions & 0 deletions src/checkstyle/checks/whitespace/EmptyLinesCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,37 @@ class EmptyLinesCheck extends LineCheckBase {
function logInfo(pos) {
log('Too many consecutive empty lines (> ${max})', pos, 0);
}

override public function detectableInstances():DetectableInstances {
return [{
fixed: [{
propertyName: "max",
value: 1
}],
properties: [{
propertyName: "allowEmptyLineAfterSingleLineComment",
values: [false, true]
},
{
propertyName: "allowEmptyLineAfterMultiLineComment",
values: [false, true]
},
{
propertyName: "requireEmptyLineAfterPackage",
values: [true, false]
},
{
propertyName: "requireEmptyLineAfterClass",
values: [true, false]
},
{
propertyName: "requireEmptyLineAfterInterface",
values: [true, false]
},
{
propertyName: "requireEmptyLineAfterAbstract",
values: [true, false]
}]
}];
}
}
4 changes: 2 additions & 2 deletions src/checkstyle/checks/whitespace/IndentationCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class IndentationCheck extends Check {
function calcLineIndentationBkOpen(token:TokenTree, lineIndentation:Array<Int>) {
var child:TokenTree = token.getFirstChild();
if (child == null) return;
if (child.is(BrOpen)) {
if ((child.is(BrOpen)) || (child.is(BkOpen))) {
// only indent once, if directly next to each other `[{`
if (token.pos.min + 1 == child.pos.min) return;
}
Expand Down Expand Up @@ -354,7 +354,7 @@ class IndentationCheck extends Check {
},
{
propertyName: "wrapPolicy",
values: [NONE, LARGER, EXACT]
values: [NONE, EXACT, LARGER]
}]
}];
}
Expand Down
Loading

0 comments on commit 7b3d12e

Please sign in to comment.