Skip to content

Commit

Permalink
Lint Kotlin examples (#3923)
Browse files Browse the repository at this point in the history
* [x] Needs #3919 as this builds on it
* [x] Update this to use #3961, switching to `KtlintModule`
* [x] Needs #3966 to disable ktlint for some files

Last change I think needed for #3829

Overview:
* Changes `KtfmtModule` to set the error code when there's changes like
is true of the other formatters.
* Some simplifications were made to the `example/package.mill` based on
similarities with Java/Kotlin.
* Files were formatted
* CI was updated

---------

Co-authored-by: Li Haoyi <[email protected]>
  • Loading branch information
myyk and lihaoyi authored Nov 15, 2024
1 parent 122d2e6 commit 4cddacd
Show file tree
Hide file tree
Showing 72 changed files with 955 additions and 808 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2

[*.{kt,kts}]
ktlint_code_style = intellij_idea
ktlint_standard_no-wildcard-imports = disabled

[example/kotlinlib/linting/**/*]
ktlint = disabled

[kotlinlib/test/resources/contrib/ktfmt/**/*]
ktlint = disabled

[kotlinlib/test/resources/kotlin-js/foo/test/src/foo/**/*]
ktlint = disabled
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ jobs:
uses: ./.github/workflows/run-mill-action.yml
with:
java-version: '17'
buildcmd: ./mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources && ./mill -i __.mimaReportBinaryIssues && ./mill -i __.fix --check && ./mill -i mill.javalib.palantirformat.PalantirFormatModule/ --check
buildcmd: ./mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll __.sources + __.mimaReportBinaryIssues + __.fix --check + mill.javalib.palantirformat.PalantirFormatModule/ --check + mill.kotlinlib.ktlint.KtlintModule/checkFormatAll --check
12 changes: 5 additions & 7 deletions example/kotlinlib/basic/1-simple/foo/src/foo/Foo.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package foo

import kotlinx.html.h1
import kotlinx.html.stream.createHTML
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import kotlinx.html.h1
import kotlinx.html.stream.createHTML

class Foo: CliktCommand() {
val text by option("-t", "--text", help="text to insert").required()
class Foo : CliktCommand() {
val text by option("-t", "--text", help = "text to insert").required()

override fun run() {
echo(generateHtml(text))
}
}

fun generateHtml(text: String): String {
return createHTML(prettyPrint = false).h1 { text(text) }.toString()
}
fun generateHtml(text: String): String = createHTML(prettyPrint = false).h1 { text(text) }.toString()

fun main(args: Array<String>) = Foo().main(args)
18 changes: 9 additions & 9 deletions example/kotlinlib/basic/1-simple/foo/test/src/foo/FooTest.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package foo

import foo.generateHtml
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class FooTest : FunSpec({
test("testSimple") {
generateHtml("hello") shouldBe "<h1>hello</h1>"
}
class FooTest :
FunSpec({
test("testSimple") {
generateHtml("hello") shouldBe "<h1>hello</h1>"
}

test("testEscaping") {
generateHtml("<hello>") shouldBe "<h1>&lt;hello&gt;</h1>"
}
})
test("testEscaping") {
generateHtml("<hello>") shouldBe "<h1>&lt;hello&gt;</h1>"
}
})
22 changes: 11 additions & 11 deletions example/kotlinlib/basic/2-custom-build-logic/foo/src/foo/Foo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package foo

import java.io.IOException

fun getLineCount(): String? {
return try {
::main.javaClass.classLoader
.getResourceAsStream("line-count.txt")
.readAllBytes()
.toString(Charsets.UTF_8)
} catch (e: IOException) {
null
}
fun getLineCount(): String? = try {
::main.javaClass.classLoader
.getResourceAsStream("line-count.txt")
.readAllBytes()
.toString(Charsets.UTF_8)
} catch (e: IOException) {
null
}

fun main() = println("Line Count: " + getLineCount())

fun main() {
var msg = "Line Count: " + getLineCount()
println(msg)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package foo

import foo.getLineCount
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class FooTests : FunSpec({
class FooTests :
FunSpec({

test("testSimple") {
val expectedLineCount = 12
val actualLineCount = getLineCount()?.trim().let { Integer.parseInt(it) }
actualLineCount shouldBe expectedLineCount
}
})
test("testSimple") {
val expectedLineCount = 12
val actualLineCount = getLineCount()?.trim().let { Integer.parseInt(it) }
actualLineCount shouldBe expectedLineCount
}
})
4 changes: 1 addition & 3 deletions example/kotlinlib/basic/3-multi-module/bar/src/bar/Bar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package bar
import kotlinx.html.h1
import kotlinx.html.stream.createHTML

fun generateHtml(text: String): String {
return createHTML(prettyPrint = false).h1 { text(text) }.toString()
}
fun generateHtml(text: String): String = createHTML(prettyPrint = false).h1 { text(text) }.toString()

fun main(args: Array<String>) {
println("Bar.value: " + generateHtml(args[0]))
Expand Down
22 changes: 11 additions & 11 deletions example/kotlinlib/basic/3-multi-module/bar/test/src/bar/BarTests.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package bar

import bar.generateHtml
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class BarTests : FunSpec({
class BarTests :
FunSpec({

test("simple") {
val result = generateHtml("hello")
result shouldBe "<h1>hello</h1>"
}
test("simple") {
val result = generateHtml("hello")
result shouldBe "<h1>hello</h1>"
}

test("escaping") {
val result = generateHtml("<hello>")
result shouldBe "<h1>&lt;hello&gt;</h1>"
}
})
test("escaping") {
val result = generateHtml("<hello>")
result shouldBe "<h1>&lt;hello&gt;</h1>"
}
})
7 changes: 5 additions & 2 deletions example/kotlinlib/basic/3-multi-module/foo/src/foo/Foo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.ajalt.clikt.parameters.options.required

const val VALUE: String = "hello"

class Foo: CliktCommand() {
class Foo : CliktCommand() {
val fooText by option("--foo-text").required()
val barText by option("--bar-text").required()

Expand All @@ -15,7 +15,10 @@ class Foo: CliktCommand() {
}
}

fun mainFunction(fooText: String, barText: String) {
fun mainFunction(
fooText: String,
barText: String,
) {
println("Foo.value: " + VALUE)
println("Bar.value: " + bar.generateHtml(barText))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package foo
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class FooTests : FunSpec({
test("hello") {
val result = hello()
result shouldBe "Hello World, Earth"
}
})
class FooTests :
FunSpec({
test("hello") {
val result = hello()
result shouldBe "Hello World, Earth"
}
})
2 changes: 1 addition & 1 deletion example/kotlinlib/basic/6-realistic/bar/src/bar/Bar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import kotlinx.html.p
import kotlinx.html.stream.createHTML

object Bar {
fun value() = createHTML(prettyPrint = false).p { text("world") }.toString()
fun value() = createHTML(prettyPrint = false).p { text("world") }.toString()
}
11 changes: 6 additions & 5 deletions example/kotlinlib/basic/6-realistic/bar/test/src/bar/BarTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package bar
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class BarTests : FunSpec({
class BarTests :
FunSpec({

test("world") {
Bar.value() shouldBe "<p>world</p>"
}
})
test("world") {
Bar.value() shouldBe "<p>world</p>"
}
})
11 changes: 6 additions & 5 deletions example/kotlinlib/basic/6-realistic/foo/test/src/foo/FooTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package foo
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class FooTests : FunSpec({
test("hello") {
Foo.VALUE shouldBe "<h1>hello</h1>"
}
})
class FooTests :
FunSpec({
test("hello") {
Foo.VALUE shouldBe "<h1>hello</h1>"
}
})
1 change: 0 additions & 1 deletion example/kotlinlib/dependencies/1-ivy-deps/src/foo/Foo.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package foo

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature

fun main(args: Array<String>) {
val value = ObjectMapper().writeValueAsString(args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package bar

import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.io.IOException
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class BarServlet : HttpServlet() {
protected override fun doGet(request: HttpServletRequest, response: HttpServletResponse) {
protected override fun doGet(
request: HttpServletRequest,
response: HttpServletResponse,
) {
response.setContentType("text/html")
response.setStatus(HttpServletResponse.SC_OK)
response.getWriter().println("<html><body>Hello World!</body></html>")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package foo

import com.grack.nanojson.JsonParser
import com.grack.nanojson.JsonObject

fun main(args: Array<String>) {
val jsonString = args[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package foo

import com.williamfiset.fastjavaio.InputReader

import java.io.FileInputStream
import java.io.IOException

fun main(args: Array<String>) {
val filePath = args[0]
val fi = try {
InputReader(FileInputStream(filePath))
} catch (e: IOException) {
e.printStackTrace()
null
}
val fi =
try {
InputReader(FileInputStream(filePath))
} catch (e: IOException) {
e.printStackTrace()
null
}

var line: String? = fi?.nextLine()
while (line != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package foo
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import foo.main as fooMain
import kotlinx.html.h1
import kotlinx.html.stream.createHTML

Expand Down
2 changes: 1 addition & 1 deletion example/kotlinlib/linting/2-ktlint/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object `package` extends RootModule with KotlinModule with KtlintModule {

/** Usage

> ./mill ktlint # run ktlint to produce a report, defaults to warning without error
> ./mill ktlint --check true # run ktlint to produce a report, defaults to warning without error
error: ...src/example/FooWrong.kt:6:28: Missing newline before ")" (standard:parameter-list-wrapping)...
...src/example/FooWrong.kt:6:28: Newline expected before closing parenthesis (standard:function-signature)...
...src/example/FooWrong.kt:6:28: Missing trailing comma before ")" (standard:trailing-comma-on-declaration-site)...
Expand Down
2 changes: 1 addition & 1 deletion example/kotlinlib/linting/3-ktfmt/build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object `package` extends RootModule with KotlinModule with KtfmtModule {
/** Usage

> ./mill ktfmt --format=false # run ktfmt to produce a list of files which should be formatter
...src/example/FooWrong.kt...
error: ...src/example/FooWrong.kt...

> ./mill ktfmt # running without arguments will format all files
Done formatting ...src/example/FooWrong.kt
Expand Down
24 changes: 9 additions & 15 deletions example/kotlinlib/module/1-common-config/custom-src/Foo2.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package foo

import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.util.Properties
import kotlinx.html.h1
import kotlinx.html.stream.createHTML
import java.io.IOException

fun main(args: Array<String>) {
println("Foo2.value: ${Foo2.VALUE}")
Expand All @@ -29,16 +25,14 @@ fun main(args: Array<String>) {
}

object Foo2 {
val VALUE = createHTML(prettyPrint = false).h1 { text("hello2") }.toString()
val VALUE = createHTML(prettyPrint = false).h1 { text("hello2") }.toString()
}

private fun readResource(resourceName: String): String? {
return try {
::main.javaClass.classLoader
.getResourceAsStream(resourceName)
.readAllBytes()
.toString(Charsets.UTF_8)
} catch (e: IOException) {
null
}
private fun readResource(resourceName: String): String? = try {
::main.javaClass.classLoader
.getResourceAsStream(resourceName)
.readAllBytes()
.toString(Charsets.UTF_8)
} catch (e: IOException) {
null
}
2 changes: 1 addition & 1 deletion example/kotlinlib/module/1-common-config/src/foo/Foo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import kotlinx.html.h1
import kotlinx.html.stream.createHTML

object Foo {
val VALUE = createHTML(prettyPrint = false).h1 { text("hello") }.toString()
val VALUE = createHTML(prettyPrint = false).h1 { text("hello") }.toString()
}
1 change: 0 additions & 1 deletion example/kotlinlib/module/11-main-class/src/foo/Bar.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package foo

class Bar {

fun main() = println("Hello Bar")
}
1 change: 0 additions & 1 deletion example/kotlinlib/module/11-main-class/src/foo/Foo.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package foo

class Foo {

fun main() = println("Hello Foo")
}
Loading

0 comments on commit 4cddacd

Please sign in to comment.