Skip to content

Commit

Permalink
Merge pull request #1481 from paul-dingemans/promote-to-standard-rules
Browse files Browse the repository at this point in the history
Promote experimental rules older than 12 months to standard rules
  • Loading branch information
paul-dingemans authored Jun 1, 2022
2 parents cada15f + f0ddae2 commit 90f8dde
Show file tree
Hide file tree
Showing 34 changed files with 346 additions and 100 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ This project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased

### Promoting experimental rules to standard

The rules below are promoted from the `experimental` ruleset to the `standard` ruleset.
* `annotation`
* `annotation-spacing`
* `argument-list-wrapping`
* `double-colon-spacing`
* `enum-entry-name-case`
* `multiline-if-else`
* `no-empty-first-line-in-method-block`
* `package-name`
* `traling-comma`
* `spacing-around-angle-brackets`
* `spacing-between-declarations-with-annotations`
* `spacing-between-declarations-with-comments`
* `unary-op-spacing`

Note that as a result of moving the rules that the prefix `experimental:` has to be removed from all references to this rule. Check references in:
* The `.editorconfig` setting `disabled_rules`.
* KtLint disable and enable directives.
* The `VisitorModifier.RunAfterRule`.

If your project did not run with the `experimental` ruleset enabled before, you might expect new lint violations to be reported. Please note that rules can be disabled via the the `.editorconfig` in case you do not want the rules to be applied on your project.

### API Changes & RuleSet providers

If you are not an API user nor a RuleSet provider, then you can safely skip this section. Otherwise, please read below carefully and upgrade your usage of ktlint. In this and coming releases, we are changing and adapting important parts of our API in order to increase maintainability and flexibility for future changes. Please avoid skipping a releases as that will make it harder to migrate.
Expand Down Expand Up @@ -40,6 +64,7 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter
- Update shadow plugin to `7.1.2` release
- Update picocli to `4.6.3` release
- Simplified rule `filename`. Only when the file contains a single class (including data class, enum class and sealed class) or a single interface, the file name should be identical to that class/interface. In all other cases the file name should be a descriptive name compliant with the PascalCase convention ([#1004](https://github.com/pinterest/ktlint/pull/1117))
- Promote experimental rules to standard rules set: `annotation`, `annotation-spacing`, `argument-list-wrapping`, `double-colon-spacing`, `enum-entry-name-case`, `multiline-if-else`, `no-empty-first-line-in-method-block`, `package-name`, `traling-comma`, `spacing-around-angle-brackets`, `spacing-between-declarations-with-annotations`, `spacing-between-declarations-with-comments`, `unary-op-spacing` ([#1481](https://github.com/pinterest/ktlint/pull/1481))

### Removed

Expand Down Expand Up @@ -92,6 +117,7 @@ This section is applicable when providing rules that depend on one or more value

### Changed
- Print the rule id always in the PlainReporter ([#1121](https://github.com/pinterest/ktlint/issues/1121))
- All wrapping logic is moved from the `indent` rule to the new rule `wrapping` (as part of the `standard` ruleset). In case you currently have disabled the `indent` rule, you may want to reconsider whether this is still necessary or that you also want to disable the new `wrapping` rule to keep the status quo. Both rules can be run independent of each other. ([#835](https://github.com/pinterest/ktlint/issues/835))

## [0.44.0] - 2022-02-15

Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@ It's also [easy to create your own](#creating-a-reporter).

## Standard rules

- `annotation`: Annotation formatting - multiple annotations should be on a separate line than the annotated declaration; annotations with parameters should each be on separate lines; annotations should be followed by a space
- `argument-list-wrapping`: Argument list wrapping
- `chain-wrapping`: When wrapping chained calls `.`, `?.` and `?:` should be placed on the next line
- `comment-spacing`: The end of line comment sign `//` should be preceded and followed by exactly a space
- `enum-entry-name-case`: Enum entry names should be uppercase underscore-separated names
- `filename`: Files containing only one toplevel domain should be named according to that element.
- `final-newline`: Newline at the end of each file (enabled by default)
(set `insert_final_newline=false` in .editorconfig to disable (see [EditorConfig](#editorconfig) section for more)).
- `import-ordering`: Imports ordered consistently (see [Custom ktlint EditorConfig properties](#custom-ktlint-specific-editorconfig-properties) for more)
- `indent`: Indentation formatting - respects `.editorconfig` `indent_size` with no continuation indent (see [EditorConfig](#editorconfig) section for more)
- `max-line-length`: Ensures that lines do not exceed the given length of `.editorconfig` property `max_line_length` (see [EditorConfig](#editorconfig) section for more). This rule does not apply in a number of situations. For example, in the case a line exceeds the maximum line length due to and comment that disables ktlint rules than that comment is being ignored when validating the length of the line. The `.editorconfig` property `ktlint_ignore_back_ticked_identifier` can be set to ignore identifiers which are enclosed in backticks, which for example is very useful when you want to allow longer names for unit tests.
- `modifier-order`: Consistent order of modifiers
- `multiline-if-else`: Braces required for multiline if/else statements
- `no-blank-line-before-rbrace`: No blank lines before `}`
- `no-blank-lines-in-chained-method-calls`: No blank lines in chained method expressions
- `no-consecutive-blank-lines`: No consecutive blank lines
- `no-empty-class-body`: No empty (`{}`) class bodies
- `no-empty-first-line-in-method-block`: No leading empty lines in method blocks
- `no-line-break-after-else`: Disallows line breaks after the else keyword if that could lead to confusion, for example:
```kotlin
if (conditionA()) {
Expand All @@ -59,52 +64,47 @@ It's also [easy to create your own](#creating-a-reporter).
- `no-unit-return`: No `Unit` returns (`fun fn {}` instead of `fun fn: Unit {}`)
- `no-unused-imports`: No unused `import`s
- `no-wildcard-imports`: No wildcard `import`s expect imports listed in `.editorconfig` property `ij_kotlin_packages_to_use_import_on_demand`
- `package-name`: No underscores in package names
- `parameter-list-wrapping`: When class/function signature doesn't fit on a single line, each parameter must be on a separate line
- `string-template`: Consistent string templates (`$v` instead of `${v}`, `${p.v}` instead of `${p.v.toString()}`)

### Spacing
- `annotation-spacing`: Annotations should be separated by a single line break
- `colon-spacing`: Consistent spacing around colon
- `comma-spacing`: Consistent spacing around comma
- `curly-spacing`: Consistent spacing around curly braces
- `dot-spacing`: Consistent spacing around dots
- `double-colon-spacing`: No spaces around `::`
- `keyword-spacing`: Consistent spacing around keywords
- `op-spacing`: Consistent spacing around operators
- `paren-spacing`: Consistent spacing around parenthesis
- `range-spacing`: Consistent spacing around range operators
- `spacing-around-angle-brackets`: No spaces around angle brackets
- `spacing-between-declarations-with-annotations`: Declarations with annotations should be separated by a blank line
- `spacing-between-declarations-with-comments`: Declarations with comments should be separated by a blank line
- `unary-op-spacing`: No spaces around unary operators

## Experimental rules
New rules will be added into the [experimental ruleset](https://github.com/pinterest/ktlint/tree/master/ktlint-ruleset-experimental), which can be enabled
by passing the `--experimental` flag to `ktlint`.

- `experimental:annotation`: Annotation formatting - multiple annotations should be on a separate line than the annotated declaration; annotations with parameters should each be on separate lines; annotations should be followed by a space
- `experimental:block-comment-initial-star-alignment`: Lines in a block comment which (exclusive the indentation) start with a `*` should have this `*` aligned with the `*` in the opening of the block comment.
- `experimental:discouraged-comment-location`: Detect discouraged comment locations (no autocorrect)
- `experimental:enum-entry-name-case`: Enum entry names should be uppercase underscore-separated names
- `experimental:multiline-if-else`: Braces required for multiline if/else statements
- `experimental:no-empty-first-line-in-method-block`: No leading empty lines in method blocks
- `experimental:package-name`: No underscores in package names
- `experimental:parameter-list-spacing`: Consistent spacing inside the parameter list
- `experimental:unnecessary-parentheses-before-trailing-lambda`: An empty parentheses block before a lambda is redundant. For example `some-string".count() { it == '-' }`
- `function-signature`: rewrites the function signature to a single line when possible (e.g. when not exceeding the `max_line_length` property) or a multiline signature otherwise. In case of function with a body expression, the body expression is placed on the same line as the function signature when not exceeding the `max_line_length` property. Optionally the function signature can be forced to be written as a multiline signature in case the function has more than a specified number of parameters (`.editorconfig' property `ktlint_function_signature_wrapping_rule_always_with_minimum_parameters`)
### Spacing
- `experimental:annotation-spacing`: Annotations should be separated by the annotated declaration by a single line break
- `experimental:double-colon-spacing`: No spaces around `::`
- `experimental:fun-keyword-spacing`: Consistent spacing after the fun keyword
- `experimental:function-return-type-spacing`: Consistent spacing around the function return type
- `experimental:function-start-of-body-spacing`: Consistent spacing before start of function body
- `experimental:function-type-reference-spacing`: Consistent spacing in the type reference before a function
- `experimental:modifier-list-spacing`: Consistent spacing between modifiers in and after the last modifier in a modifier list
- `experimental:nullable-type-spacing`: No spaces in a nullable type
- `experimental:spacing-around-angle-brackets`: No spaces around angle brackets
- `experimental:spacing-between-declarations-with-annotations`: Declarations with annotations should be separated by a blank line
- `experimental:spacing-between-declarations-with-comments`: Declarations with comments should be separated by a blank line
- `experimental:parameter-list-spacing`: Consistent spacing inside the parameter list
- `experimental:spacing-between-function-name-and-opening-parenthesis`: Consistent spacing between function name and opening parenthesis
- `experimental:type-parameter-list-spacing`: Spacing after a type parameter list in function and class declarations
- `experimental:unary-op-spacing`: No spaces around unary operators
### Wrapping
- `experimental:argument-list-wrapping`: Argument list wrapping
- `experimental:comment-wrapping`: A block comment should start and end on a line that does not contain any other element. A block comment should not be used as end of line comment.
- `experimental:kdoc-wrapping`: A KDoc comment should start and end on a line that does not contain any other element.
Expand Down Expand Up @@ -136,7 +136,7 @@ Such behaviour violates `.editorconfig` [specification](https://github.com/edito
# Comma-separated list of rules to disable (Since 0.34.0)
# Note that rules in any ruleset other than the standard ruleset will need to be prefixed
# by the ruleset identifier.
disabled_rules=no-wildcard-imports,experimental:annotation,my-custom-ruleset:my-custom-rule
disabled_rules=some-standard-rule,experimental:some-experimental-rule,my-custom-ruleset:my-custom-rule
# Defines the imports layout. The layout can be composed by the following symbols:
# "*" - wildcard. There must be at least one entry of a single wildcard to match all other imports. Matches anything after a specified symbol/import as well.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.ruleset.experimental.trailingcomma.TrailingCommaRule

public const val experimentalRulesetId = "experimental"

public class ExperimentalRuleSetProvider : RuleSetProvider {
override fun get(): RuleSet = RuleSet(
experimentalRulesetId,
AnnotationRule(),
ArgumentListWrappingRule(),
MultiLineIfElseRule(),
NoEmptyFirstLineInMethodBlockRule(),
TrailingCommaRule(),
PackageNameRule(),
EnumEntryNameCaseRule(),
SpacingAroundDoubleColonRule(),
SpacingBetweenDeclarationsWithCommentsRule(),
SpacingBetweenDeclarationsWithAnnotationsRule(),
SpacingAroundAngleBracketsRule(),
SpacingAroundUnaryOperatorRule(),
AnnotationSpacingRule(),
UnnecessaryParenthesesBeforeTrailingLambdaRule(),
TypeParameterListSpacingRule(),
TypeArgumentListSpacingRule(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pinterest.ktlint.ruleset.experimental
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.ruleset.experimental.FunctionSignatureRule.Companion.functionSignatureWrappingMinimumParametersProperty
import com.pinterest.ktlint.ruleset.standard.NoMultipleSpacesRule
import com.pinterest.ktlint.ruleset.standard.SpacingAroundAngleBracketsRule
import com.pinterest.ktlint.ruleset.standard.SpacingAroundColonRule
import com.pinterest.ktlint.ruleset.standard.SpacingAroundCommaRule
import com.pinterest.ktlint.ruleset.standard.SpacingAroundDotRule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.ruleset.standard.SpacingAroundUnaryOperatorRule
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.LintViolation
import org.assertj.core.api.Assertions.assertThat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.FILE_ANNOTATION_LIST
Expand Down Expand Up @@ -34,7 +34,7 @@ import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
*
* @see [AnnotationSpacingRule] for white space rules. Moved since
*/
class AnnotationRule : Rule("$experimentalRulesetId:annotation") {
class AnnotationRule : Rule("annotation") {

companion object {
const val multipleAnnotationsOnSameLineAsAnnotatedConstructErrorMessage =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
Expand All @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.psi.psiUtil.leaves
*
* https://kotlinlang.org/docs/reference/coding-conventions.html#annotation-formatting
*/
class AnnotationSpacingRule : Rule("$experimentalRulesetId:annotation-spacing") {
class AnnotationSpacingRule : Rule("annotation-spacing") {

companion object {
const val ERROR_MESSAGE = "Annotations should occur immediately before the annotated construct"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.IndentConfig
import com.pinterest.ktlint.core.Rule
Expand Down Expand Up @@ -41,7 +41,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
*/
@OptIn(FeatureInAlphaState::class)
class ArgumentListWrappingRule :
Rule("$experimentalRulesetId:argument-list-wrapping"),
Rule("argument-list-wrapping"),
UsesEditorConfigProperties {
private var editorConfigIndent = IndentConfig.DEFAULT_INDENT_CONFIG
private var maxLineLength = -1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement
import org.jetbrains.kotlin.psi.KtEnumEntry

public class EnumEntryNameCaseRule : Rule("$experimentalRulesetId:enum-entry-name-case") {
/**
* https://kotlinlang.org/docs/coding-conventions.html#property-names
*/
public class EnumEntryNameCaseRule : Rule("enum-entry-name-case") {
internal companion object {
val regex = Regex("[A-Z]([A-Za-z\\d]*|[A-Z_\\d]*)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MaxLineLengthRule :
// This rule should run after all other rules. Each time a rule visitor is modified with
// RunAsLateAsPossible, it needs to be checked that this rule still runs after that new rule or that it
// won't be affected by that rule.
ruleId = "experimental:trailing-comma",
ruleId = "trailing-comma",
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.ELSE
Expand All @@ -19,11 +19,8 @@ import org.jetbrains.kotlin.psi.psiUtil.leaves

/**
* https://kotlinlang.org/docs/reference/coding-conventions.html#formatting-control-flow-statements
*
* TODO: if, for, when branch, do, while
*/
class MultiLineIfElseRule : Rule("$experimentalRulesetId:multiline-if-else") {

class MultiLineIfElseRule : Rule("multiline-if-else") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
Expand All @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement

class NoEmptyFirstLineInMethodBlockRule : Rule("$experimentalRulesetId:no-empty-first-line-in-method-block") {
class NoEmptyFirstLineInMethodBlockRule : Rule("no-empty-first-line-in-method-block") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.psi.KtPackageDirective

class PackageNameRule : Rule("$experimentalRulesetId:package-name") {
/**
* https://kotlinlang.org/docs/coding-conventions.html#naming-rules
*/
class PackageNameRule : Rule("package-name") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pinterest.ktlint.ruleset.experimental
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.FUN_KEYWORD
Expand All @@ -13,7 +13,7 @@ import com.pinterest.ktlint.core.ast.prevLeaf
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement

class SpacingAroundAngleBracketsRule : Rule("$experimentalRulesetId:spacing-around-angle-brackets") {
class SpacingAroundAngleBracketsRule : Rule("spacing-around-angle-brackets") {
private fun String.trimBeforeLastLine() = this.substring(this.lastIndexOf('\n'))

override fun visit(
Expand Down
Loading

0 comments on commit 90f8dde

Please sign in to comment.