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

Migrate to ktlint 0.47.0 #24

Merged
merged 3 commits into from
Aug 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
}

plugins {
id "com.diffplug.spotless" version "6.9.1"
id "com.diffplug.spotless" version "6.10.0"
}

allprojects {
Expand All @@ -26,7 +26,9 @@ subprojects {
spotless {
kotlin {
target "**/*.kt"
ktlint(libs.versions.ktlint.get())
// ktlint 0.47.0 currently not supported - https://github.com/diffplug/spotless/pull/1303
// ktlint(libs.versions.ktlint.get())
ktlint("0.46.1")
licenseHeaderFile rootProject.file('spotless/copyright.txt')
}
groovyGradle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast

abstract class TwitterKtlintRule(id: String) : Rule(id), KtElementVisitors {

final override fun visit(
final override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[versions]
kotlin = "1.7.0"
ktlint = "0.46.1"
junit = "5.8.2"
kotlin = "1.7.10"
ktlint = "0.47.0"
junit = "5.9.0"

[libraries]
ktlint-core = { module = "com.pinterest.ktlint:ktlint-core", version.ref = "ktlint" }
ktlint-test = { module = "com.pinterest.ktlint:ktlint-test", version.ref = "ktlint" }
ktlint-gradlePlugin = "org.jlleitschuh.gradle:ktlint-gradle:10.3.0"
ktlint-gradlePlugin = "org.jlleitschuh.gradle:ktlint-gradle:11.0.0"

kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-compiler = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" }

gradleMavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.20.0"
gradleMavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.21.0"

junit5 = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
junit5-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junit" }
assertj = "org.assertj:assertj-core:3.22.0"
assertj = "org.assertj:assertj-core:3.23.1"
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
@file:Suppress("DEPRECATION")

package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.core.RuleProvider
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.RuleSetProviderV2

class TwitterComposeRuleSetProvider :
RuleSetProviderV2(CustomRuleSetId, RuleSetAbout),
RuleSetProvider {

class TwitterComposeRuleSetProvider : RuleSetProvider {
// Pre-0.47.0 ruleset (will go away in 0.48.0)
@Suppress("OVERRIDE_DEPRECATION")
override fun get(): RuleSet = RuleSet(
"twitter-compose",
CustomRuleSetId,
ComposeModifierComposableCheck(),
ComposeModifierMissingCheck(),
ComposeModifierReusedCheck(),
Expand All @@ -19,4 +28,29 @@ class TwitterComposeRuleSetProvider : RuleSetProvider {
ComposeRememberMissingCheck(),
ComposeViewModelForwardingCheck()
)

// 0.47.0+ ruleset
override fun getRuleProviders(): Set<RuleProvider> = setOf(
RuleProvider { ComposeModifierComposableCheck() },
RuleProvider { ComposeModifierMissingCheck() },
RuleProvider { ComposeModifierReusedCheck() },
RuleProvider { ComposeModifierWithoutDefaultCheck() },
RuleProvider { ComposeMultipleContentEmittersCheck() },
RuleProvider { ComposeMutableParametersCheck() },
RuleProvider { ComposeNamingCheck() },
RuleProvider { ComposeParameterOrderCheck() },
RuleProvider { ComposeRememberMissingCheck() },
RuleProvider { ComposeViewModelForwardingCheck() }
)

private companion object {
private val RuleSetAbout = About(
maintainer = "Twitter, Inc",
description = "Static checks to aid with a healthy adoption of Jetpack Compose",
license = "Apache License, Version 2.0",
repositoryUrl = "https://github.com/twitter/compose-rules/",
issueTrackerUrl = "https://github.com/twitter/compose-rules/issues"
)
const val CustomRuleSetId = "twitter-compose"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.twitter.rules.ktlint.compose.TwitterComposeRuleSetProvider
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeModifierComposableCheckTest {

private val modifierRuleAssertThat = ComposeModifierComposableCheck().assertThat()
private val modifierRuleAssertThat = assertThatRule { ComposeModifierComposableCheck() }

@Test
fun `errors when a composable Modifier extension is detected`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeModifierMissingCheckTest {

private val modifierRuleAssertThat = ComposeModifierMissingCheck().assertThat()
private val modifierRuleAssertThat = assertThatRule { ComposeModifierMissingCheck() }

@Test
fun `errors when a Composable has a layout inside and it doesn't have a modifier`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeModifierReusedCheckTest {

private val modifierRuleAssertThat = ComposeModifierReusedCheck().assertThat()
private val modifierRuleAssertThat = assertThatRule { ComposeModifierReusedCheck() }

@Test
fun `errors when the modifier parameter of a Composable is used more than once by siblings or parent-children`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeModifierWithoutDefaultCheckTest {

private val modifierRuleAssertThat = ComposeModifierWithoutDefaultCheck().assertThat()
private val modifierRuleAssertThat = assertThatRule { ComposeModifierWithoutDefaultCheck() }

@Test
fun `errors when a Composable has modifiers but without default values, and is able to auto fix it`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeMultipleContentEmittersCheckTest {

private val emittersRuleAssertThat = ComposeMultipleContentEmittersCheck().assertThat()
private val emittersRuleAssertThat = assertThatRule { ComposeMultipleContentEmittersCheck() }

@Test
fun `passes when only one item emits up at the top level`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeMutableParametersCheckTest {

private val mutableParamRuleAssertThat = ComposeMutableParametersCheck().assertThat()
private val mutableParamRuleAssertThat = assertThatRule { ComposeMutableParametersCheck() }

@Test
fun `errors when a Composable has a mutable parameter`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeNamingCheckTest {

private val namingRuleAssertThat = ComposeNamingCheck().assertThat()
private val namingRuleAssertThat = assertThatRule { ComposeNamingCheck() }

@Test
fun `passes when a composable that returns values is lowercase`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import com.twitter.rules.ktlint.compose.ComposeParameterOrderCheck.Companion.createErrorMessage
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeParameterOrderCheckTest {

private val orderingRuleAssertThat = ComposeParameterOrderCheck().assertThat()
private val orderingRuleAssertThat = assertThatRule { ComposeParameterOrderCheck() }

@Test
fun `no errors when ordering is correct`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeRememberMissingCheckTest {

private val rememberRuleAssertThat = ComposeRememberMissingCheck().assertThat()
private val rememberRuleAssertThat = assertThatRule { ComposeRememberMissingCheck() }

@Test
fun `passes when a non-remembered mutableStateOf is used outside of a Composable`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test

class ComposeViewModelForwardingCheckTest {

private val forwardingRuleAssertThat = ComposeViewModelForwardingCheck().assertThat()
private val forwardingRuleAssertThat = assertThatRule { ComposeViewModelForwardingCheck() }

@Test
fun `allows the forwarding of ViewModels in overridden Composable functions`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
package com.twitter.rules.ktlint.compose

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.intellij.lang.annotations.Language
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class ComposeViewModelInjectionCheckTest {

private val injectionRuleAssertThat = ComposeViewModelInjectionCheck().assertThat()
private val injectionRuleAssertThat = assertThatRule { ComposeViewModelInjectionCheck() }

@ParameterizedTest
@ValueSource(strings = ["viewModel", "weaverViewModel"])
Expand Down