Skip to content

Commit

Permalink
Fix kotlinGradle throwing ParseException (#133)
Browse files Browse the repository at this point in the history
Closes #132
  • Loading branch information
JLLeitschuh authored and nedtwigg committed Aug 14, 2017
1 parent 6fbc287 commit 8772ea6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
18 changes: 15 additions & 3 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public static FormatterStep create(Provisioner provisioner) {
}

public static FormatterStep create(String version, Provisioner provisioner) {
return create(version, provisioner, false);
}

public static FormatterStep createForScript(String version, Provisioner provisioner) {
return create(version, provisioner, true);
}

private static FormatterStep create(String version, Provisioner provisioner, boolean isScript) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(version, provisioner),
() -> new State(version, provisioner, isScript),
State::createFormat);
}

Expand All @@ -57,11 +65,14 @@ public static String defaultVersion() {
static final class State implements Serializable {
private static final long serialVersionUID = 1L;

/** Are the files being linted Kotlin script files. */
private final boolean isScript;
/** The jar that contains the eclipse formatter. */
final JarState jarState;

State(String version, Provisioner provisioner) throws IOException {
State(String version, Provisioner provisioner, boolean isScript) throws IOException {
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
this.isScript = isScript;
}

FormatterFunc createFormat() throws Exception {
Expand Down Expand Up @@ -93,7 +104,8 @@ FormatterFunc createFormat() throws Exception {
Class<?> ktlintClass = classLoader.loadClass("com.github.shyiko.ktlint.core.KtLint");
Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null);
// and its format method
Method formatterMethod = ktlintClass.getMethod("format", String.class, Iterable.class, function2Interface);
String formatterMethodName = isScript ? "formatScript" : "format";
Method formatterMethod = ktlintClass.getMethod(formatterMethodName, String.class, Iterable.class, function2Interface);

return input -> {
try {
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# spotless-plugin-gradle releases

### Version 3.6.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
* Fixed `kotlinGradle` linting [Gradle Kotlin DSL](https://github.com/gradle/kotlin-dsl) files throwing `ParseException` ([#132](https://github.com/diffplug/spotless/issues/132)).

### Version 3.5.0 - August 13th 2017 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.5.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.5.0))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public KotlinGradleExtension(SpotlessExtension rootExtension) {
/** Adds the specified version of [ktlint](https://github.com/shyiko/ktlint). */
public void ktlint(String version) {
Objects.requireNonNull(version, "version");
addStep(KtLintStep.create(version, GradleProvisioner.fromProject(getProject())));
addStep(KtLintStep.createForScript(version, GradleProvisioner.fromProject(getProject())));
}

public void ktlint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,24 @@ public void integration_default() throws IOException {
String formatted = getTestResource("kotlin/ktlint/basic.clean");
Assert.assertEquals(formatted, result);
}

@Test
public void integration_lint_script_files_without_top_level_declaration() throws IOException {
write("build.gradle",
"plugins {",
" id 'nebula.kotlin' version '1.0.6'",
" id 'com.diffplug.gradle.spotless'",
"}",
"repositories { mavenCentral() }",
"spotless {",
" kotlinGradle {",
" ktlint()",
" }",
"}");
write("configuration.gradle.kts", "buildscript {}");
gradleRunner().withArguments("spotlessApply").build();
String result = read("configuration.gradle.kts");
String formatted = "buildscript {}\n";
Assert.assertEquals(formatted, result);
}
}

0 comments on commit 8772ea6

Please sign in to comment.