github pinterest/ktlint 0.46.0

latest releases: 1.4.1, 1.4.0, 1.3.1...
2 years ago

[0.46.0] - 2022-06-18

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
  • trailing-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.

Lint and formatting functions

The lint and formatting changes no longer accept parameters of type Params but only ExperimentalParams. Also, the VisitorProvider parameter has been removed. Because of this, your integration with KtLint breaks. Based on feedback with ktlint 0.45.x, we now prefer to break at compile time instead of trying to keep the interface backwards compatible. Please raise an issue, in case you help to convert to the new API.

Use of ".editorconfig" properties & userData

The interface UsesEditorConfigProperties provides method getEditorConfigValue to retrieve a named .editorconfig property for a given ASTNode. When implementing this interface, the value editorConfigProperties needs to be overridden. Previously it was not checked whether a retrieved property was actually recorded in this list. Now, retrieval of unregistered properties results in an exception.

Property Ktlint.DISABLED has been removed. The property value can now be retrieved as follows:

astNode
    .getEditorConfigValue(DefaultEditorConfigProperties.disabledRulesProperty)
    .split(",")

and be supplied via the ExperimentalParams as follows:

ExperimentalParams(
    ...
    editorConfigOverride =  EditorConfigOverride.from(
      DefaultEditorConfigProperties.disabledRulesProperty to "some-rule-id,experimental:some-other-rule-id"
    )
    ...
)

Property Ktlint.ANDROID_USER_DATA_KEY has been removed. The property value can now be retrieved as follows:

astNode
    .getEditorConfigValue(DefaultEditorConfigProperties.codeStyleProperty)

and be supplied via the ExperimentalParams as follows:

ExperimentalParams(
    ...
    editorConfigOverride =  EditorConfigOverride.from(
      DefaultEditorConfigProperties.codeStyleProperty to "android" 
    )
    ...
)

This property defaults to the official Kotlin code style when not set.

Testing KtLint rules

An AssertJ style API for testing KtLint rules (#1444) has been added. Usage of this API is encouraged in favor of using the old RuleExtension API. For more information, see KtLintAssertThat API

Added

  • Add experimental rule for unexpected spacing between function name and opening parenthesis (spacing-between-function-name-and-opening-parenthesis) (#1341)
  • Add experimental rule for unexpected spacing in the parameter list (parameter-list-spacing) (#1341)
  • Add experimental rule for incorrect spacing around the function return type (function-return-type-spacing) (#1341)
  • Add experimental rule for unexpected spaces in a nullable type (nullable-type-spacing) (#1341)
  • Do not add a space after the typealias name (type-parameter-list-spacing) (#1435)
  • Add experimental rule for consistent spacing before the start of the function body (function-start-of-body-spacing) (#1341)
  • Suppress ktlint rules using @Suppress (more information) (#765)
  • Add experimental rule for rewriting the function signature (function-signature) (#1341)

Fixed

  • Move disallowing blank lines in chained method calls from no-consecutive-blank-lines to new rule (no-blank-lines-in-chained-method-calls) (#1248)
  • Fix check of spacing in the receiver type of an anonymous function (#1440)
  • Allow comment on same line as super class in class declaration wrapping (#1457)
  • Respect git hooksPath setting (#1465)
  • Fix formatting of a property delegate with a dot-qualified-expression indent (#1340)
  • Keep formatting of for-loop in sync with default IntelliJ formatter (indent) and a newline in the expression in a for-statement should not force to wrap it wrapping (#1350)
  • Fix indentation of property getter/setter when the property has an initializer on a separate line indent (#1335)
  • When .editorconfig setting indentSize is set to value tab then return the default tab width as value for indentSize (#1485)
  • Allow suppressing all rules or a list of specific rules in the entire file with @file:Suppress(...) (#1029)

Changed

  • Update Kotlin development version to 1.7.0 and Kotlin version to 1.7.0.
  • Update shadow plugin to 7.1.2 release
  • Update picocli to 4.6.3 release
  • A file containing only one (non private) top level declaration (class, interface, object, type alias or function) must be named after that declaration. The name also must comply with the Pascal Case convention. The same applies to a file containing one single top level class declaration and one ore more extension functions for that class. filename (#1004)
  • 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)
  • The CLI parameter --android can be omitted when the .editorconfig property ktlint_code_style = android is defined

Don't miss a new ktlint release

NewReleases is sending notifications on new releases.