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
Refactor life cycle hooks on Rule #1547
Merged
paul-dingemans
merged 58 commits into
pinterest:master
from
paul-dingemans:visitor-provider
Jul 17, 2022
Merged
Refactor life cycle hooks on Rule #1547
paul-dingemans
merged 58 commits into
pinterest:master
from
paul-dingemans:visitor-provider
Jul 17, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Despite the name, the concurrent visitor is not faster than the sequential visitor as each file is processed on a single thread. There seem to be no other benefits for having two different visitor providers.
This eliminates some inconsistencies and improves readability.
…from VisitorProvider to Ktlint The visitor provider is only responsible for running the relevant rules in the correct order. On which node is to be executed is not relevant.
…y default, the root node (e.g. the node with ElementType FILE) can not be suppressed. A '@file:Suppress' annotation on top of the file is already contained in a non-root node.
…isitChildNodes and afterLastNode and deprecating visit. Rules no longer should implement their custom AST traversal in case processing is required after child nodes have been processed.
…Rule as this is the responsibility of the IndentationRule
…gRule as this is the responsibility of the IndentationRule
Replace generic visit method from "package.kt" with private as the visit should not be used in rules as it breaks the possibility to suppress rules on certain location. But this visit method does not have this side effect when used in the SuppressionLocator itself.
This visitor modifier was typically used to run a custom node visit algorithm using the root node and the generic "package.visit" methods. The "package.visit" method however did not support rule suppression. With the new lifecycle hooks on the Rule class the visit modifier has become obsolete.
…thod instead of "visit"
Prepare to remove ".editorconfig" properties from the UserData in ktlint 0.48. Those properties are only required on the root node but the "getUserData" method can be called on any node. The new Rule lifecycle hooks "beforeFirstNode" provides the ".editorconfig" properties directly to the rule. This contract is cleaner and more explicit.
…perties.getEditorConfigValue" by using the Rule "beforeFirstNode" lifecycle hook
Despite the name, the concurrent visitor is not faster than the sequential visitor as each file is processed on a single thread. There seem to be no other benefits for having two different visitor providers.
This eliminates some inconsistencies and improves readability.
…from VisitorProvider to Ktlint The visitor provider is only responsible for running the relevant rules in the correct order. On which node is to be executed is not relevant.
…y default, the root node (e.g. the node with ElementType FILE) can not be suppressed. A '@file:Suppress' annotation on top of the file is already contained in a non-root node.
…isitChildNodes and afterLastNode and deprecating visit. Rules no longer should implement their custom AST traversal in case processing is required after child nodes have been processed.
…Rule as this is the responsibility of the IndentationRule
…gRule as this is the responsibility of the IndentationRule
Replace generic visit method from "package.kt" with private as the visit should not be used in rules as it breaks the possibility to suppress rules on certain location. But this visit method does not have this side effect when used in the SuppressionLocator itself.
This visitor modifier was typically used to run a custom node visit algorithm using the root node and the generic "package.visit" methods. The "package.visit" method however did not support rule suppression. With the new lifecycle hooks on the Rule class the visit modifier has become obsolete.
…thod instead of "visit"
Prepare to remove ".editorconfig" properties from the UserData in ktlint 0.48. Those properties are only required on the root node but the "getUserData" method can be called on any node. The new Rule lifecycle hooks "beforeFirstNode" provides the ".editorconfig" properties directly to the rule. This contract is cleaner and more explicit.
…perties.getEditorConfigValue" by using the Rule "beforeFirstNode" lifecycle hook
…rovider # Conflicts: # ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt # ktlint-core/src/test/kotlin/com/pinterest/ktlint/core/KtLintTest.kt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Up until ktlint 0.46 the Rule class provided only one life cycle hook. This "visit" hook was called in a depth-first-approach on all nodes in the file. A rule like the IndentationRule used the RunOnRootOnly visitor modifier to call this lifecycle hook for the root node only in combination with an alternative way of traversing the ASTNodes. Downside of this approach was that suppression of the rule on blocks inside a file was not possible (#631). More generically, this applied to all rules, applying alternative traversals of the AST.
The Rule class now offers new life cycle hooks:
The "visit" life cycle hook will be removed in Ktlint 0.48. In KtLint 0.47 the "visit" life cycle hook will be called only when hook "beforeVisitChildNodes" is not overridden. It is recommended to migrate to the new lifecycle hooks in KtLint 0.47. Please create an issue, in case you need additional assistence to implement the new life cycle hooks in your rules.
Closes #631
Checklist
CHANGELOG.md
is updatedIn case of adding a new rule:
README.md
is updated