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

Fix string-template for dot-expression from which the toString() is removed #1026

Merged
merged 3 commits into from
Dec 21, 2020

Conversation

paul-dingemans
Copy link
Collaborator

@paul-dingemans paul-dingemans commented Dec 20, 2020

Given code example below:

                fun getDrafts(val draftsIds: List<Long>) {
                    println("draftIds=[${'$'}{draftsIds.toString()}]")
                }

Formatting with autocorrect of this code fails on the no-unused-imports rule as the dot-expression node still exists but the toString invocation was already removed.

Resolves #996

…g is removed (pinterest#996)

The no-unused-imports rule fails on the existence of the dot-expression node but for which the actual call to the toString method was removed.
@paul-dingemans
Copy link
Collaborator Author

This might fix #1019 although this is not sure as the issue could not be reproduced.

(node.lastChildNode as LeafPsiElement).rawReplaceWithText("") // entry end
val leftCurlyBraceNode = node.findChildByType(LONG_TEMPLATE_ENTRY_START)
val rightCurlyBraceNode = node.findChildByType(LONG_TEMPLATE_ENTRY_END)
if (node.children().count() == 3 && leftCurlyBraceNode != null && rightCurlyBraceNode != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on why count is 3 exactly? I guess I am missing a bit of context :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit of defensive programma. The block above is surrounded by the following if-statement:

            if (node.text.startsWith("${'$'}{") &&
                node.text.let { it.substring(2, it.length - 1) }.all { it.isPartOfIdentifier() } &&
                (entryExpression is KtNameReferenceExpression || entryExpression is KtThisExpression) &&
                node.treeNext.let { nextSibling ->
                    nextSibling.elementType == CLOSING_QUOTE ||
                        (
                            nextSibling.elementType == LITERAL_STRING_TEMPLATE_ENTRY &&
                                !nextSibling.text[0].isPartOfIdentifier()
                            )
                }
            ) {

I was not 100% sure that this statement really covers the situation of a prefix-node, content-node and a suffix-node. In case that also prefix-node, content-node-1, ..., content-node-n, suffix-node can match this statement then I do think it is not a valid to prefix only the first node with the '$'. So line 74 guards the autocorrect to patterns which can be corrected safely.

@romtsn romtsn merged commit 820c432 into pinterest:master Dec 21, 2020
@paul-dingemans paul-dingemans deleted the 996-fix-string-template-rule branch January 10, 2021 10:41
romtsn added a commit to paul-dingemans/ktlint that referenced this pull request Mar 7, 2021
…emoved (pinterest#1026)

* Fix string-template for dot-expression of which the redundant toString is removed (pinterest#996)

The no-unused-imports rule fails on the existence of the dot-expression node but for which the actual call to the toString method was removed.

* Update changelog

* Fix code style violation

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internal error no-unused-imports
2 participants