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 bug with incorrect package name fixing #877

Merged
merged 6 commits into from
May 12, 2021

Conversation

kgevorkyan
Copy link
Member

@kgevorkyan kgevorkyan commented May 11, 2021

What's done:

  • Fix bug with incorrect package name fixing when there is exist file annotation
  • Add tests

Closes #875

### What's done:
* Fix bug with incorrect package name fixing when there is exist file annotation
* Add tests
@codecov
Copy link

codecov bot commented May 11, 2021

Codecov Report

Merging #877 (1e14fda) into master (2b17b31) will increase coverage by 0.00%.
The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #877   +/-   ##
=========================================
  Coverage     83.52%   83.53%           
- Complexity     2211     2215    +4     
=========================================
  Files           101      101           
  Lines          5681     5690    +9     
  Branches       1624     1628    +4     
=========================================
+ Hits           4745     4753    +8     
  Misses          256      256           
- Partials        680      681    +1     
Flag Coverage Δ Complexity Δ
unittests 83.53% <91.66%> (+<0.01%) 2215.00 <0.00> (+4.00)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
...qfn/diktat/ruleset/rules/chapter1/PackageNaming.kt 91.91% <90.90%> (-0.31%) 38.00 <0.00> (+4.00) ⬇️
.../ruleset/rules/chapter3/files/FileStructureRule.kt 83.87% <100.00%> (ø) 77.00 <0.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2b17b31...1e14fda. Read the comment docs.

@kgevorkyan kgevorkyan requested review from petertrr and orchestr7 May 11, 2021 12:45
Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

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

lgtm

@petertrr petertrr added this to the 0.5.3 milestone May 11, 2021
### What's done:
* Add recommendations according review
val insertPackageDirectiveBefore = if (newPackageDirective != newPackageDirective.treeParent.firstChildNode) {
newPackageDirective
} else {
newPackageDirective.treeNext
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if there will be EOL_COMMENT or another type of comment? I suppose, comment will be in wrong line.

Copy link
Member

@orchestr7 orchestr7 May 11, 2021

Choose a reason for hiding this comment

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

very good note, Denis, thanks

newPackageDirective.treeParent.addChild(PsiWhiteSpaceImpl("\n\n"), newPackageDirective.treeNext)
val packageDirectiveParent = packageDirectiveNode.treeParent
packageDirectiveParent.replaceChild(packageDirectiveNode, newPackageDirective)
val insertWhitespaceBefore = if (newPackageDirective != packageDirectiveParent.firstChildNode) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe, check whether there is a white space node before and after package directive node? Actually, these \n\n are not responsibility of this rule, so here we should ensure that the tree is correct, and FileStructureRule will do the rest.

Copy link
Collaborator

@kentr0w kentr0w May 12, 2021

Choose a reason for hiding this comment

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

But shouldn't each rule work independently? What if user disables FileStructureRule ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, rules should work independently. My main suggestion was to check, whether there are white spaces both before and after the package directive node; I think we can make two newlines on both sides.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it. Yes, I agree

### What's done:
* Improve algorithm of insertNewPackageName func, add more tests
* Discovered bug in FileStructure rule
@kgevorkyan kgevorkyan force-pushed the bugfix/incorrect_package_name_fix_with_annotations branch from 914c59d to 9331884 Compare May 12, 2021 11:35
if (packageDirectiveNode != packageDirectiveParent.firstChildNode) {
// We will insert new package directive node before first node, which is not in the following list
val possibleTypesBeforePackageDirective = listOf(WHITE_SPACE, EOL_COMMENT, BLOCK_COMMENT, KDOC, PACKAGE_DIRECTIVE, FILE_ANNOTATION_LIST)
val addBefore = packageDirectiveParent.children().filter { it.elementType !in possibleTypesBeforePackageDirective }.first()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
val addBefore = packageDirectiveParent.children().filter { it.elementType !in possibleTypesBeforePackageDirective }.first()
val addBefore = packageDirectiveParent.children().first { it.elementType !in possibleTypesBeforePackageDirective }

Copy link
Member Author

@kgevorkyan kgevorkyan May 12, 2021

Choose a reason for hiding this comment

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

Thanks, changed

@@ -19,4 +20,12 @@ class TopLevelOrderRuleFixTest : FixTestBase("test/paragraph3/top_level", ::TopL
fun `should fix top level order with comment`() {
fixAndCompare("TopLevelWithCommentExpected.kt", "TopLevelWithCommentTest.kt")
}

// FixMe:
@Disabled
Copy link
Member

Choose a reason for hiding this comment

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

Please add reason (@Disabled("reason")

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

### What's done:
Fix bug in smoke test
### What's done:
* Add recommendations
@kgevorkyan kgevorkyan requested a review from kentr0w May 12, 2021 14:37
Copy link
Collaborator

@kentr0w kentr0w left a comment

Choose a reason for hiding this comment

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

lgtm

@kgevorkyan kgevorkyan merged commit cf126df into master May 12, 2021
@kgevorkyan kgevorkyan deleted the bugfix/incorrect_package_name_fix_with_annotations branch June 10, 2021 10:01
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.

Package name fixing incorrect with file-level directives
4 participants