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

ANNOTATION_NEW_LINE not applied to annotations on primary constructors #1887

Open
colesnodgrass opened this issue Dec 20, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@colesnodgrass
Copy link

Describe the bug

ANNOTATION_NEW_LINE doesn't apply to primary constructor annotations and/or WRONG_INDENTATION is incorrectly applied when a class only consists of a primary constructor.

Given the following class

open class ExampleExtension
  @Inject
  constructor(objects: ObjectFactory) {
    val name: Property<String> = objects.property()
    val mainClass: Property<String> = objects.property()
    val defaultJvmArgs: ListProperty<String> = objects.listProperty()
    val localEnvVars: MapProperty<String, String> = objects.mapProperty()
  }

and the configuration

- name: WRONG_INDENTATION
  enabled: true
  configuration:
    indentationSize: 2

Expected behavior

I would expect the above code to pass analysis as is.

Observed behavior

Currently fails with the WRONG_INDENTATION due to the @Inject and following lines being (correctly) indented

[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 2 but was 4 (diktat-ruleset:indentation)
[WRONG_INDENTATION] only spaces are allowed for indentation and each indentation should equal to 4 spaces (tabs are not allowed): expected 0 but was 2 (diktat-ruleset:indentation)

This can be bypassed by moving the annotated constructor to the same line as the class declaration

open class ExampleExtension @Inject constructor(objects: ObjectFactory) {
  val name: Property<String> = objects.property()
  val mainClass: Property<String> = objects.property()
  val defaultJvmArgs: ListProperty<String> = objects.listProperty()
  val localEnvVars: MapProperty<String, String> = objects.mapProperty()
}

However I would expect this to now fail the ANNOTATION_NEW_LINE rules as the @Inject annotation isn't on a new line, but it doesn't.

Steps to Reproduce

See above.

Environment information

  • diktat version: 2.0.0
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): ./gradlew diktatCheck
  • kotlin version: 1.9.21
@colesnodgrass colesnodgrass added the bug Something isn't working label Dec 20, 2023
@nulls
Copy link
Member

nulls commented Dec 21, 2023

Hi @colesnodgrass, thanks for raising the issue.

According to Diktat's code style, this behavior is expected and ANNOTATION_NEW_LINE should not be applied to a primary constructor.

We will investigate it to try to make it configurable to support your format

@nulls nulls added enhancement New feature or request and removed bug Something isn't working labels Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants