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

Allow naming file with single extension method after the target of the extension #1521

Closed
dalewking opened this issue Jun 26, 2022 · 8 comments · Fixed by #1528
Closed

Allow naming file with single extension method after the target of the extension #1521

dalewking opened this issue Jun 26, 2022 · 8 comments · Fixed by #1528
Milestone

Comments

@dalewking
Copy link

When a file contains a single top-level extension function should allow naming the file after the fully qualified function, just like is allowed for extension properties

Steps to Reproduce

Given a class like this:

    class Foo {
        enum Status { A, B }
    }

If I create a file that only has a single extension method on Foo.Status like this:

    fun Foo.Status.image() {}

Observed Behavior

Ktlint demands that the file be named Image.kt which is not descriptive and may even conflict if I had multiple similar functions in the same directory

Expected Behavior

I should be able to name the file based on the target of the exension, so in this case FooStatusImage.kt.

Note that this is the behavior when it is an extension property, but not when it is an extension function.

Your Environment

ktlint 0.46.1

@paul-dingemans
Copy link
Collaborator

I like the suggestion for the expected behavior. So either the name of the function Image.kt or FooStatusImage.kt should be acceptable names.

For now, you can work around this by suppressing the rule for this file specifically:

@file:Suppress("ktlint:filename")

fun Foo.Status.image() {}

@paul-dingemans paul-dingemans added this to the 0.47.0 milestone Jun 26, 2022
@dalewking
Copy link
Author

In my particular case I worked around it by converting it from a function to a property since it probably should have been a property all along

paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Jun 27, 2022
…plus name of an extension function when it is the only top level declaration in a file

Closes pinterest#1521
@PaulWoitaschek
Copy link
Contributor

In my particular case I worked around it by converting it from a function to a property since it probably should have been a property all along

That works sometimes but in general it can't be a solution.

In my case, the rule wants to name my file Plus.kt

operator fun PaddingValues.plus(other: PaddingValues): PaddingValues {
  val self = this
  return object : PaddingValues {

    override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp {
      return self.calculateLeftPadding(layoutDirection) + other.calculateLeftPadding(layoutDirection)
    }

    override fun calculateTopPadding(): Dp {
      return self.calculateTopPadding() + other.calculateTopPadding()
    }

    override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp {
      return self.calculateRightPadding(layoutDirection) + other.calculateRightPadding(layoutDirection)
    }

    override fun calculateBottomPadding(): Dp {
      return self.calculateBottomPadding() + other.calculateBottomPadding()
    }
  }
}

@mattelacchiato
Copy link

I would question if this rule should be applied to file which does not contain any class at all. In earlier versions, this rule wasn't applied in this case and I would really like to keep it this way (for the already provided reasons here).

@PaulWoitaschek
Copy link
Contributor

Yeah I'd prefer the rules to be split. Currently it's all or nothing but I'd like to apply some rules and others not.

@paul-dingemans
Copy link
Collaborator

The Kotlin Coding Conventions have recently been updated based on a question I asked when the rule was changed in 0.46.0. The wording is now more clear:

If a Kotlin file contains a single class or interface (potentially with related top-level declarations), its name should be the same as the name of the class, with the .kt extension appended. It applies to all types of classes and interfaces. If a file contains multiple classes, or only top-level declarations, choose a name describing what the file contains, and name the file accordingly. Use upper camel case with an uppercase first letter (also known as Pascal case), for example, ProcessDeclarations.kt.

From above, it is clear that a single top level declaration should not dictate the name of the file. So that needs to be changed.

@simjohan
Copy link

simjohan commented Aug 2, 2022

When can we expect a new tag including this fix?

@paul-dingemans
Copy link
Collaborator

When can we expect a new tag including this fix?

I don't know. You can track the progress of what I planned to do in this release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants