-
-
Notifications
You must be signed in to change notification settings - Fork 755
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
Bump ktlint to version 0.45.1 and implement new rules #4645
Changes from all commits
1e8bbc8
5752259
f71264e
854abc7
108f539
f79782b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package io.gitlab.arturbosch.detekt.formatting | ||
|
||
import com.pinterest.ktlint.core.EditorConfig.Companion.fromMap | ||
import com.pinterest.ktlint.core.KtLint | ||
import com.pinterest.ktlint.core.Rule.VisitorModifier.RunAsLateAsPossible | ||
import com.pinterest.ktlint.core.Rule.VisitorModifier.RunOnRootNodeOnly | ||
|
@@ -57,10 +56,7 @@ abstract class FormattingRule(config: Config) : Rule(config) { | |
root.node.putUserData(KtLint.ANDROID_USER_DATA_KEY, isAndroid) | ||
positionByOffset = KtLintLineColCalculator | ||
.calculateLineColByOffset(KtLintLineColCalculator.normalizeText(root.text)) | ||
overrideEditorConfig()?.let { overrides -> | ||
val oldEditorConfig = root.node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY) | ||
root.node.putUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY, oldEditorConfig.copy(overrides)) | ||
} | ||
|
||
val editorConfigProperties = overrideEditorConfigProperties() | ||
|
||
if (!editorConfigProperties.isNullOrEmpty()) { | ||
|
@@ -78,8 +74,6 @@ abstract class FormattingRule(config: Config) : Rule(config) { | |
root.node.putUserData(KtLint.FILE_PATH_USER_DATA_KEY, root.name) | ||
} | ||
|
||
open fun overrideEditorConfig(): Map<String, Any>? = null | ||
|
||
open fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String>? = null | ||
|
||
fun apply(node: ASTNode) { | ||
|
@@ -89,8 +83,14 @@ abstract class FormattingRule(config: Config) : Rule(config) { | |
|
||
// KtLint 0.44.0 is assuming that KtLint.EDITOR_CONFIG_USER_DATA_KEY is available on all the nodes. | ||
// If not, it crashes with a NPE. Here we're patching their behavior. | ||
// This block is deprecated and will be removed in KtLint 0.46. But we have to suppress the | ||
// deprecation warning because the ci runs with -Werror. | ||
@Suppress("DEPRECATION") | ||
if (node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY) == null) { | ||
node.putUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY, fromMap(emptyMap())) | ||
node.putUserData( | ||
KtLint.EDITOR_CONFIG_USER_DATA_KEY, | ||
com.pinterest.ktlint.core.EditorConfig.Companion.fromMap(emptyMap()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the import as it was before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then I have to suppress the warning over the entire file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup makes sense. Thanks for clarifying 👍 |
||
) | ||
} | ||
|
||
wrapping.visit(node, autoCorrect) { offset, message, _ -> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.ruleset.experimental.BlockCommentInitialStarAlignmentRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-spacing">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class BlockCommentInitialStarAlignment(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = BlockCommentInitialStarAlignmentRule() | ||
override val issue = issueFor("Detect the alignment of the initial star in a block comment.") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties | ||
import com.pinterest.ktlint.core.api.FeatureInAlphaState | ||
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties | ||
import com.pinterest.ktlint.ruleset.experimental.CommentWrappingRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.api.internal.Configuration | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-indentation">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class CommentWrapping(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = CommentWrappingRule() | ||
override val issue = issueFor("Reports mis-indented code") | ||
|
||
@Configuration("indentation size") | ||
private val indentSize by config(4) | ||
|
||
@OptIn(FeatureInAlphaState::class) | ||
override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> = | ||
mapOf( | ||
DefaultEditorConfigProperties.indentSizeProperty to indentSize.toString(), | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.ruleset.experimental.DiscouragedCommentLocationRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-spacing">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class DiscouragedCommentLocation(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = DiscouragedCommentLocationRule() | ||
override val issue = issueFor("Detect discouraged comment locations.") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.ruleset.experimental.FunKeywordSpacingRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-spacing">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class FunKeywordSpacing(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = FunKeywordSpacingRule() | ||
override val issue = issueFor("Checks the spacing after the fun keyword.") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.ruleset.experimental.FunctionTypeReferenceSpacingRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-spacing">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class FunctionTypeReferenceSpacing(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = FunctionTypeReferenceSpacingRule() | ||
override val issue = | ||
issueFor("Checks the spacing before and after the angle brackets of a type argument list.") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.gitlab.arturbosch.detekt.formatting.wrappers | ||
|
||
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties | ||
import com.pinterest.ktlint.core.api.FeatureInAlphaState | ||
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties | ||
import com.pinterest.ktlint.ruleset.experimental.KdocWrappingRule | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.config | ||
import io.gitlab.arturbosch.detekt.api.internal.AutoCorrectable | ||
import io.gitlab.arturbosch.detekt.api.internal.Configuration | ||
import io.gitlab.arturbosch.detekt.formatting.FormattingRule | ||
|
||
/** | ||
* See <a href="https://ktlint.github.io/#rule-indentation">ktlint-website</a> for documentation. | ||
*/ | ||
@AutoCorrectable(since = "1.20.0") | ||
class KdocWrapping(config: Config) : FormattingRule(config) { | ||
|
||
override val wrapping = KdocWrappingRule() | ||
override val issue = issueFor("Reports mis-indented code") | ||
|
||
@Configuration("indentation size") | ||
private val indentSize by config(4) | ||
|
||
@OptIn(FeatureInAlphaState::class) | ||
override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> = | ||
mapOf( | ||
DefaultEditorConfigProperties.indentSizeProperty to indentSize.toString(), | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I'm not really happy with this solution. Also I wanted to declare the ´overrideEditorConfig()` method as deprecated. But sadly each of deprecation will lead to a warning which is handled as error in the ci pipeline so I was forced to suppress this warning (without this code snippet the build will not succeed).