-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for ktlint (#27), MISSING: readme, KotlinCodeAssertTest, …
…configurable ruleset
- Loading branch information
Showing
11 changed files
with
425 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
code-assert/src/main/java/guru/nidi/codeassert/junit/kotlin/KotlinCodeAssertMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.junit.kotlin; | ||
|
||
import guru.nidi.codeassert.ktlint.KtlintMatcher; | ||
import guru.nidi.codeassert.ktlint.KtlintResult; | ||
import org.hamcrest.Matcher; | ||
|
||
public final class KotlinCodeAssertMatchers { | ||
private KotlinCodeAssertMatchers() { | ||
} | ||
|
||
public static Matcher<KtlintResult> hasNoKtlintIssues() { | ||
return new KtlintMatcher(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.ktlint; | ||
|
||
import com.github.shyiko.ktlint.core.KtLint; | ||
import com.github.shyiko.ktlint.core.LintError; | ||
import com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider; | ||
import guru.nidi.codeassert.Analyzer; | ||
import guru.nidi.codeassert.config.AnalyzerConfig; | ||
import guru.nidi.codeassert.config.UsageCounter; | ||
import kotlin.Unit; | ||
import kotlin.jvm.functions.Function1; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static guru.nidi.codeassert.config.Language.KOTLIN; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Collections.singletonList; | ||
|
||
public class KtlintAnalyzer implements Analyzer<List<LocatedLintError>> { | ||
private static final Logger LOG = LoggerFactory.getLogger(KtlintAnalyzer.class); | ||
|
||
private final AnalyzerConfig config; | ||
private final KtlintCollector collector; | ||
|
||
public KtlintAnalyzer(AnalyzerConfig config, KtlintCollector collector) { | ||
this.config = config; | ||
this.collector = collector; | ||
} | ||
|
||
public KtlintResult analyze() { | ||
final ErrorListener listener = new ErrorListener(); | ||
for (final File src : config.getSources(KOTLIN)) { | ||
try { | ||
listener.currentFile = src; | ||
KtLint.INSTANCE.lint(readFile(src), singletonList(new StandardRuleSetProvider().get()), listener); | ||
} catch (IOException e) { | ||
LOG.error("Could not read file {}", src, e); | ||
} | ||
} | ||
return createResult(listener); | ||
} | ||
|
||
private String readFile(File f) throws IOException { | ||
final StringBuilder sb = new StringBuilder(); | ||
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f), UTF_8))) { | ||
String line; | ||
while ((line = in.readLine()) != null) { | ||
sb.append(line).append("\n"); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private KtlintResult createResult(ErrorListener listener) { | ||
final List<LocatedLintError> filtered = new ArrayList<>(); | ||
final UsageCounter counter = new UsageCounter(); | ||
for (final LocatedLintError error : listener.errors) { | ||
if (counter.accept(collector.accept(error))) { | ||
filtered.add(error); | ||
} | ||
} | ||
collector.printUnusedWarning(counter); | ||
return new KtlintResult(this, filtered, collector.unusedActions(counter)); | ||
} | ||
|
||
private static class ErrorListener implements Function1<LintError, Unit> { | ||
private final List<LocatedLintError> errors = new ArrayList<>(); | ||
private File currentFile; | ||
|
||
@Override | ||
public Unit invoke(LintError e) { | ||
errors.add(new LocatedLintError(currentFile, e.getLine(), e.getCol(), e.getRuleId(), e.getDetail())); | ||
return null; | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.ktlint; | ||
|
||
import guru.nidi.codeassert.config.*; | ||
import guru.nidi.codeassert.util.ListUtils; | ||
|
||
import java.util.List; | ||
|
||
public class KtlintCollector extends BaseCollector<LocatedLintError, Ignore, KtlintCollector> { | ||
@Override | ||
public KtlintCollector config(final CollectorConfig<Ignore>... configs) { | ||
return new KtlintCollector() { | ||
@Override | ||
public ActionResult accept(LocatedLintError issue) { | ||
return accept(issue, KtlintCollector.this, configs); | ||
} | ||
|
||
public List<Ignore> unused(UsageCounter counter) { | ||
return unused(counter, KtlintCollector.this, configs); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return KtlintCollector.this.toString() + "\n" + ListUtils.join("\n", configs); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public ActionResult accept(LocatedLintError issue) { | ||
return ActionResult.accept(null, 1); | ||
} | ||
|
||
@Override | ||
protected ActionResult doAccept(LocatedLintError issue, Ignore action) { | ||
final String className = guessClassFromFile(issue.file.getAbsolutePath(), Language.KOTLIN); | ||
return action.accept(new NamedLocation(issue.ruleId, className, null, true)); | ||
} | ||
|
||
@Override | ||
public List<Ignore> unused(UsageCounter counter) { | ||
return unusedNullAction(counter, true); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ""; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.ktlint; | ||
|
||
import guru.nidi.codeassert.util.ResultMatcher; | ||
import org.hamcrest.Description; | ||
|
||
public class KtlintMatcher extends ResultMatcher<KtlintResult, LocatedLintError> { | ||
public void describeTo(Description description) { | ||
description.appendText("Has no ktlint issues"); | ||
} | ||
|
||
@Override | ||
protected void describeMismatchSafely(KtlintResult item, Description description) { | ||
for (final LocatedLintError error : item.findings()) { | ||
description.appendText("\n").appendText(printError(error)); | ||
} | ||
} | ||
|
||
private String printError(LocatedLintError error) { | ||
return String.format("%-35s %s:%d %s", | ||
error.ruleId, error.file.getAbsolutePath(), error.line, error.detail); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
code-assert/src/main/java/guru/nidi/codeassert/ktlint/KtlintResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.ktlint; | ||
|
||
import guru.nidi.codeassert.Analyzer; | ||
import guru.nidi.codeassert.AnalyzerResult; | ||
|
||
import java.util.List; | ||
|
||
public class KtlintResult extends AnalyzerResult<List<LocatedLintError>> { | ||
public KtlintResult(Analyzer<List<LocatedLintError>> analyzer, | ||
List<LocatedLintError> findings, List<String> unusedActions) { | ||
super(analyzer, findings, unusedActions); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
code-assert/src/main/java/guru/nidi/codeassert/ktlint/LocatedLintError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright © 2015 Stefan Niederhauser ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package guru.nidi.codeassert.ktlint; | ||
|
||
import java.io.File; | ||
|
||
class LocatedLintError { | ||
final File file; | ||
final int line; | ||
final int col; | ||
final String ruleId; | ||
final String detail; | ||
|
||
LocatedLintError(File file, int line, int col, String ruleId, String detail) { | ||
this.file = file; | ||
this.line = line; | ||
this.col = col; | ||
this.ruleId = ruleId; | ||
this.detail = detail; | ||
} | ||
} |
Oops, something went wrong.