Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude #142

Merged
merged 20 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
-x TestMain

--next
-cmd neko run -s src -p resources/static-analysis.txt -c resources/checkstyle.json
-cmd neko run -s src -s test -p resources/static-analysis.txt -c resources/checkstyle.json -e resources/checkstyle-exclude.json
-cmd neko run --default-config resources/default-config.json
-cmd neko run -c resources/default-config.json
62 changes: 62 additions & 0 deletions resources/checkstyle-exclude.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice to have these in a separated file now! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really enjoyed removing all the @SuppressWarnings in code 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're all class-wide now though (for now at least?).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.. for now.. need to find a way to implement method level exclude.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean field level? (for variables and properties it should be possible as well)

"all": [],
"Dynamic": [
"checkstyle.Main",
"checkstyle.Checker",
"checkstyle.ChecksInfo"
],
"EmptyLines": [
"checks"
],
"MultipleStringLiterals": [
"checks",
"token"
],
"MultipleVariableDeclarations": [
"checkstyle.checks.coding.MultipleVariableDeclarationsCheck",
"checks.coding.MultipleVariableDeclarationsCheckTest"
],
"TrailingWhitespace": [
"checks.whitespace.SpacingCheckTest",
"checks.whitespace.TrailingWhitespaceCheckTest",
"checks.naming.MemberNameCheckTest"
],
"TabForAligning": [
"checks.whitespace.IndentationCharacterCheckTest",
"checks.whitespace.TabForAligningCheckTest"
],
"LineLength": [
"checks.size.LineLengthCheckTest"
],
"IndentationCharacter": [
"checks.whitespace.IndentationCharacterCheckTest"
],
"CyclomaticComplexity": [
"checkstyle.checks.Check",
"checkstyle.utils.ExprUtils",
"checkstyle.utils.ComplexTypeUtils",
"checkstyle.checks.CyclomaticComplexityCheck",
"checkstyle.checks.block.LeftCurlyCheck",
"checkstyle.checks.block.RightCurlyCheck",
"checkstyle.checks.naming.MethodNameCheck",
"checkstyle.checks.type.ReturnCheck",
"checkstyle.checks.whitespace.OperatorWrapCheck",
"checkstyle.checks.whitespace.WhitespaceAfterCheck",
"checkstyle.checks.whitespace.WhitespaceAroundCheck",
"checkstyle.token.TokenTreeBuilder"
],
"LeftCurly": [
"checkstyle.checks.CyclomaticComplexityCheck"
],
"RightCurly": [
"checkstyle.checks.CyclomaticComplexityCheck"
],
"MethodLength": [
"checkstyle.checks.whitespace.WhitespaceAfterCheck",
"checkstyle.checks.whitespace.WhitespaceAroundCheck",
"checkstyle.token.TokenTreeBuilder"
],
"MagicNumber": [
"checkstyle.reporter.ProgressReporter"
]
}
28 changes: 24 additions & 4 deletions src/checkstyle/Checker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import sys.io.File;
import checkstyle.token.TokenTree;
import checkstyle.token.TokenTreeBuilder;

using checkstyle.utils.ArrayUtils;
using StringTools;

class Checker {

public var file:LintFile;
Expand All @@ -27,6 +30,7 @@ class Checker {
var lineSeparator:String;
var tokenTree:TokenTree;
var asts:Array<Ast>;
var excludes:Map<String, Array<String>>;

public function new() {
checks = [];
Expand Down Expand Up @@ -135,7 +139,8 @@ class Checker {
return parser.parse();
}

public function process(files:Array<LintFile>) {
public function process(files:Array<LintFile>, excludesMap:Map<String, Array<String>>) {
excludes = excludesMap;
var advanceFrame = function() {};
#if hxtelemetry
var hxt = new hxtelemetry.HxTelemetry();
Expand Down Expand Up @@ -166,7 +171,6 @@ class Checker {
lintFile.content = null;
}

@SuppressWarnings("checkstyle:Dynamic")
function createContext(lintFile:LintFile):Bool {
this.file = lintFile;
for (reporter in reporters) reporter.fileStart(file);
Expand Down Expand Up @@ -235,9 +239,9 @@ class Checker {
message1.moduleName == message2.moduleName;
}

@SuppressWarnings("checkstyle:Dynamic")
function runCheck(check:Check):Array<LintMessage> {
try {
if (checkForExclude(check.getModuleName())) return [];
return check.run(this);
}
catch (e:Dynamic) {
Expand All @@ -246,7 +250,23 @@ class Checker {
}
}

@SuppressWarnings("checkstyle:Dynamic")
function checkForExclude(moduleName:String):Bool {
if (excludes == null) return false;
var excludesForCheck:Array<String> = excludes.get(moduleName);
if (excludesForCheck == null || excludesForCheck.length == 0) return false;

var cls = file.name.substring(0, file.name.indexOf(".hx"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the indexOf(".hx") is a good idea here. We may need to add support for other extensions in the future, maybe with a file filter regex argument. For example, OpenFL uses .hxp for project files (much less popular than the .xml format though): http://www.openfl.org/documentation/projects/project-files/hxp-format/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.hx is used earlier as well to push all the files into array
https://github.com/adireddy/haxe-checkstyle/blob/dev/src/checkstyle/Main.hx#L270

We can move the extensions into config and default it to .hx

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it should be specific to extensions, it might be more flexible to apply a regex to the entire path. OpenFL generates .hx file in the export directory, which makes it difficult for me to use checkstyle in say, flixel-demos, which has 75 projects.

For generating API docs, I have excluded these specific file names / packages for dox with a regex: https://github.com/HaxeFlixel/flixel-docs/blob/master/api/dox-gen/gendocs.bat

--exclude "(__ASSET__|ApplicationMain|DocumentClass|DefaultAssetLibrary|Main|NMEPreloader|zpp_nape)"

if (excludesForCheck.contains(cls)) return true;

cls = cls.replace("/", ":");
for (exclude in excludesForCheck) {
var regStr:String = exclude + ":.*?" + cls.substring(cls.lastIndexOf(":") + 1, cls.length) + "$";
var r = new EReg(regStr.replace("/", ":"), "i");
if (r.match(cls)) return true;
}
return false;
}

function getErrorMessage(e:Dynamic, fileName:String, step:String):LintMessage {
return {
fileName:fileName,
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/ChecksInfo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package checkstyle;

import checkstyle.checks.Check;

@SuppressWarnings("checkstyle:Dynamic")
class ChecksInfo {

var checkInfos:Map<String, CheckInfo>;
Expand Down
133 changes: 91 additions & 42 deletions src/checkstyle/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,12 @@ import sys.FileSystem;
import sys.io.File;

using checkstyle.utils.ArrayUtils;
using checkstyle.utils.StringUtils;

class Main {

@SuppressWarnings('checkstyle:Dynamic')
public static function main() {
var args;
var cwd;
var oldCwd = null;

try {
args = Sys.args();
cwd = Sys.getCwd();
if (Sys.getEnv("HAXELIB_RUN") != null) {
cwd = args.pop();
oldCwd = Sys.getCwd();
}
if (oldCwd != null) Sys.setCwd(cwd);

new Main().run(args);
}
catch (e:Dynamic) {
Sys.stderr().writeString(e + "\n");
Sys.stderr().writeString(CallStack.toString(CallStack.exceptionStack()) + "\n");
}
if (oldCwd != null) Sys.setCwd(oldCwd);
Sys.exit(exitCode);
}

var info:ChecksInfo;
var checker:Checker;

static var DEFAULT_CONFIG:String = "checkstyle.json";
static var DEFAULT_EXCLUDE_CONFIG:String = "checkstyle-exclude.json";
static var REPORT_TYPE:String = "text";
static var XML_PATH:String = "check-style-report.xml";
static var JSON_PATH:String = "check-style-report.json";
Expand All @@ -58,19 +32,29 @@ class Main {
static var EXIT_CODE:Bool = false;
static var exitCode:Int;

var info:ChecksInfo;
var checker:Checker;
var paths:Array<String>;
var allExcludes:Array<String>;
var excludesMap:Map<String, Array<String>>;

function new() {
info = new ChecksInfo();
checker = new Checker();
paths = [];
allExcludes = [];
excludesMap = new Map();
exitCode = 0;
}

function run(args:Array<String>) {
var files:Array<String> = [];
var configPath:String = null;
var excludePath:String = null;

var argHandler = Args.generate([
@doc("Set source folder to process (multiple allowed)") ["-s", "--source"] => function(path:String) traverse(path, files),
@doc("Set source folder to process (multiple allowed)") ["-s", "--source"] => function(path:String) paths.push(path),
@doc("Set config file (default: checkstyle.json)") ["-c", "--config"] => function(path:String) configPath = path,
@doc("Set exclude config file (default: checkstyle-exclude.json)") ["-e", "--exclude"] => function(path:String) excludePath = path,
@doc("Set reporter (xml, json or text, default: text)") ["-r", "--reporter"] => function(name:String) REPORT_TYPE = name,
@doc("Set reporter output path") ["-p", "--path"] => function(path:String) {
XML_PATH = path;
Expand All @@ -93,19 +77,19 @@ class Main {
}
argHandler.parse(args);

var i:Int = 0;
var toProcess:Array<LintFile> = [for (file in files) {name:file, content:null, index:i++}];

if (configPath == null && FileSystem.exists(DEFAULT_CONFIG) && !FileSystem.isDirectory(DEFAULT_CONFIG)) {
configPath = DEFAULT_CONFIG;
}

if (excludePath == null && FileSystem.exists(DEFAULT_EXCLUDE_CONFIG) && !FileSystem.isDirectory(DEFAULT_EXCLUDE_CONFIG)) {
excludePath = DEFAULT_EXCLUDE_CONFIG;
}

if (configPath == null) addAllChecks();
else loadConfig(configPath);
checker.addReporter(createReporter(files.length));
if (SHOW_PROGRESS) checker.addReporter(new ProgressReporter(files.length));
if (EXIT_CODE) checker.addReporter(new ExitCodeReporter());
checker.process(toProcess);

if (excludePath != null) loadExcludeConfig(excludePath);
else start();
}

function loadConfig(configPath:String) {
Expand All @@ -127,6 +111,32 @@ class Main {
}
}

function loadExcludeConfig(excludeConfigPath:String) {
var config:Config = Json.parse(File.getContent(excludeConfigPath));
var excludes = Reflect.fields(config);
for (e in excludes) {
createExcludeMapElement(e);
var excludeValues:Array<String> = Reflect.field(config, e);
if (excludeValues != null && excludeValues.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could invert this if and use continue for less deep nesting.

for (val in excludeValues) {
for (p in paths) {
var path = p + "/" + val.split(".").join("/");
if (e == "all") allExcludes.push(path);
else {
if (!p.contains(":")) excludesMap.get(e).push(path);
}
}
}
}
}

start();
}

function createExcludeMapElement(name:String) {
if (excludesMap.get(name) == null) excludesMap.set(name, []);
}

function createCheck(checkConf:CheckConfig):Check {
var check:Check = info.build(checkConf.type);
if (check == null) failWith('Unknown check \'${checkConf.type}\'');
Expand Down Expand Up @@ -239,12 +249,27 @@ class Main {
return s + "/" + t;
}

function traverse(node:String, files:Array<String>) {
if (FileSystem.isDirectory(node)) {
var nodes = FileSystem.readDirectory(node);
for (child in nodes) traverse(pathJoin(node, child), files);
function start() {
var files:Array<String> = [];
for (path in paths) traverse(path, files);

var i:Int = 0;
var toProcess:Array<LintFile> = [for (file in files) {name:file, content:null, index:i++}];

checker.addReporter(createReporter(files.length));
if (SHOW_PROGRESS) checker.addReporter(new ProgressReporter(files.length));
if (EXIT_CODE) checker.addReporter(new ExitCodeReporter());
checker.process(toProcess, excludesMap);
}

function traverse(path:String, files:Array<String>) {
if (FileSystem.isDirectory(path) && !allExcludes.contains(path)) {
var nodes = FileSystem.readDirectory(path);
for (child in nodes) traverse(pathJoin(path, child), files);
}
else if (~/(.hx)$/i.match(path) && !allExcludes.contains(path.substring(0, path.indexOf(".hx")))) {
files.push(path);
}
else if (~/(.hx)$/i.match(node)) files.push(node);
}

function failWith(message:String) {
Expand All @@ -255,4 +280,28 @@ class Main {
public static function setExitCode(newExitCode:Int) {
exitCode = newExitCode;
}

public static function main() {
var args;
var cwd;
var oldCwd = null;

try {
args = Sys.args();
cwd = Sys.getCwd();
if (Sys.getEnv("HAXELIB_RUN") != null) {
cwd = args.pop();
oldCwd = Sys.getCwd();
}
if (oldCwd != null) Sys.setCwd(cwd);

new Main().run(args);
}
catch (e:Dynamic) {
Sys.stderr().writeString(e + "\n");
Sys.stderr().writeString(CallStack.toString(CallStack.exceptionStack()) + "\n");
}
if (oldCwd != null) Sys.setCwd(oldCwd);
Sys.exit(exitCode);
}
}
1 change: 0 additions & 1 deletion src/checkstyle/checks/Check.hx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class Check {
return isCharPosSuppressed(pos.min);
}

@SuppressWarnings('checkstyle:CyclomaticComplexity')
function isCharPosSuppressed(pos:Int):Bool {
for (td in checker.ast.decls) {
switch (td.decl){
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/CyclomaticComplexityCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Lambda;

@name("CyclomaticComplexity")
@desc("McCabe simplified cyclomatic complexity check")
@SuppressWarnings(['checkstyle:CyclomaticComplexity', 'checkstyle:LeftCurly', 'checkstyle:RightCurly'])
class CyclomaticComplexityCheck extends Check {

static var DEFAULT_COMPLEXITY_WARNING:Int = 20;
Expand Down
2 changes: 0 additions & 2 deletions src/checkstyle/checks/block/LeftCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class LeftCurlyCheck extends Check {
/**
* find effective parent token and check against configured tokens
*/

@SuppressWarnings("checkstyle:CyclomaticComplexity")
function findParentToken(token:TokenTree):ParentToken {
if (token == null) return {token:token, hasToken: false};
switch (token.tok) {
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/block/RightCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class RightCurlyCheck extends Check {
}
}

@SuppressWarnings("checkstyle:CyclomaticComplexity")
function filterParentToken(token:TokenTree):Bool {
if (token == null) return false;
switch (token.tok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class MultipleVariableDeclarationsCheck extends Check {
super(TOKEN);
}

@SuppressWarnings('checkstyle:MultipleVariableDeclarations')
override function actualRun() {
var root:TokenTree = checker.getTokenTree();
var acceptableTokens:Array<TokenTree> = root.filter([Kwd(KwdVar)], ALL);
Expand Down
1 change: 0 additions & 1 deletion src/checkstyle/checks/naming/MethodNameCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class MethodNameCheck extends NameCheckBase<MethodNameCheckToken> {
}
}

@SuppressWarnings('checkstyle:CyclomaticComplexity')
function checkField(f:Field, p:ParentType) {
if (f.isGetter() || f.isSetter()) return;
if (hasToken(NOTINLINE) && !hasToken(INLINE) && f.isInline(p)) return;
Expand Down
Loading