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

Naming rules should allow keywords to be wrapped between backticks #2352

Closed
dimasadryantos opened this issue Nov 13, 2023 · 8 comments · Fixed by #2405
Closed

Naming rules should allow keywords to be wrapped between backticks #2352

dimasadryantos opened this issue Nov 13, 2023 · 8 comments · Fixed by #2405

Comments

@dimasadryantos
Copy link

Expected Behavior

Function name with backticks and short name should not throwing an error when formatting.

Observed Behavior

Ktlint detect function name with backticks wrongly for example if I have method like in the following :

fun `when`(): String {
    return ""
}

when I format the code it would throw an error like the following :
[ktlint] /Users/dsetyawan/Documents/pAPI/spring-kotlin-demo/src/main/kotlin/com/example/springkotlindemo/SpringKotlinDemoApplication.kt:13:5: Function name should start with a lowercase letter (except factory methods) and use camel case (standard:function-naming)

Steps to Reproduce

Clone my example project : https://github.com/dimasadryantos/spring-kotlin-demo/tree/master
Run : mvn clean compile or ktlint --format

Your Environment

@paul-dingemans
Copy link
Collaborator

Duplicate of #2339

@paul-dingemans paul-dingemans marked this as a duplicate of #2339 Nov 13, 2023
@paul-dingemans paul-dingemans closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
@JakeWharton
Copy link
Contributor

This is not a duplicate of that issue. You cannot remove the backticks here as the name is otherwise a keyword. Backticks are required to use spaces but it is certainly not their only use.

@paul-dingemans
Copy link
Collaborator

This is not a duplicate of that issue. You cannot remove the backticks here as the name is otherwise a keyword. Backticks are required to use spaces but it is certainly not their only use.

Indeed, you can not remove the backticks here.

According to Kotlin coding conventions:

In tests (and only in tests), you can use method names with spaces enclosed in backticks.

So when you read above literally, it would allow the example code above as a kind of helper method inside a Test class. But, from the Kotlin coding conventions it is quite clear that it is meant for functions annotated with @Test.

@JakeWharton
Copy link
Contributor

Right. So this issue should be reopened as there are valid, non-test-based uses of backticks. If you are overriding a Java method whose name is a Kotlin keyword, or you need to provide binary compatibility with a previous version of a library which used a Kotlin keyword as a function or property name they are necessary and should not be flagged.

@dimasadryantos
Copy link
Author

dimasadryantos commented Nov 28, 2023

I'd agree with @JakeWharton , also this issue is only appear on this version.

@JakeWharton
Copy link
Contributor

In my specific case, I have a property named "package":

val `package` = type.names[0]

It represents a package, so that's what I named it.

The failing rule says

Property name should start with a lowercase letter and use camel case

It does start with a lowercase "p" and use camel case. The backticks are not part of the name. The backticks allow me to tell the compiler's parser that I am producing a variable name and not a hard keyword.

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Dec 2, 2023

From the view point of the Kotlin parser, the backticks are an integral part of the identifier as can be seen in screenshot below. On the right hand side you the the PSI (AST) structure of the parsed file. The highlighted IDENTIFIER field matches with the name of the variable including the backticks.
Screenshot 2023-12-02 at 16 29 34

But I do see the point that you're making that the keywords should be accepted, even in case they are included between backticks. This applies to rules property-naming, function-naming and class-naming.

@paul-dingemans paul-dingemans reopened this Dec 2, 2023
@paul-dingemans paul-dingemans changed the title Ktlint detect backticks function name as non camel case Naming rules should allow keywords to be wrapped between backticks Dec 2, 2023
paul-dingemans added a commit that referenced this issue Dec 2, 2023
paul-dingemans added a commit that referenced this issue Dec 3, 2023
… with backticks (#2405)

* Allow property, function and class name to be same as keyword wrapped with backticks

Closes #2352
@JakeWharton
Copy link
Contributor

Thank you!

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