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

doc: a bit of polishing #224

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions authoring-recipes/multiple-visitors.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,14 @@ Using this printer, in combination with robust and thorough tests, can help ensu

### VariableDeclarations Visitor

Now that we have some ideas of which LSTs we care about, it's time to get started with making the first visitor in our recipe. This visitor will be the one responsible for finding, and potentially modifying, variables.
Now that we have some ideas of which LSTs we care about, it's time to get started with making the first visitor in our recipe. This visitor will be the one responsible for finding and potentially modifying variables.

Generally, you want to start by restricting the scope of the visitor and returning quickly if the recipe should not make any changes.

Let's outline what that would look like:

{% hint style="note" %}
The `VariableDeclarations` LST contains all the variables defined on one line of code. In most cases, this will just be a single variable, but it is possible for many to be defined. Our visitor will need to handle both cases.
The `VariableDeclarations` LST contains all the variables defined in one line of code. In most cases, this will just be a single variable, but it is possible for many to be defined. Our visitor will need to handle both cases.
{% endhint %}

```java
Expand All @@ -389,11 +389,11 @@ public class FinalizeLocalVariables extends Recipe {

// TODO: Return if any variables in the varDec are uninitialized

// TODO: Return if the varDec is in a for loop control
// TODO: Return if the varDec is in a for-loop control

// TODO: Return if the varDec is an instance or class variable

// TODO: If none of the above apply, find all use cases for the variables and determine whether or not they're reassigned. If none have been, add the final modifier. If one ore more has, do nothing.
// TODO: If none of the above apply, find all use cases for the variables and determine whether or not they're reassigned. If none have been, add the final modifier. If one or more has, do nothing.
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting typo. Sadly ore is a word, but ^ore$ could be added to https://github.com/openrewrite/rewrite-docs/blob/master/.github/actions/spelling/reject.txt if it's unlikely to be appropriate in this repository :)


return variableDeclarations;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ public JavaIsoVisitor<ExecutionContext> getVisitor() {
return variableDeclarations;
}

// TODO: Return if the varDec is in a for loop control
// TODO: Return if the varDec is in a for-loop control

// TODO: Return if the varDec is an instance or class variable

Expand All @@ -453,7 +453,7 @@ public class FinalizeLocalVariables extends Recipe {
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations varDec, ExecutionContext executionContext) {
// ...

// Variables that are in a for loop control should not be changed
// Variables that are in a for-loop control should not be changed
if (isDeclaredInForLoopControl(getCursor())) {
return variableDeclarations;
}
Expand Down Expand Up @@ -495,7 +495,7 @@ We need a visitor that:
* Takes in and saves all of the necessary information to identify whether or not a variable it is visiting matches the one provided by the other visitor.
* Takes in a tree to visit that is scoped to only the areas where the variable can be reassigned (to increase speed and to ensure we can accurately detect changes to a variable).
* Keeps track of whether or not said variable has been reassigned. This state needs to be accessible and modifiable in all of the visit functions.
* Provides a way for our other visitor to access the above state.
* Provides a way for our other visitors to access the above state.

To address these needs, let's create a `static` visitor that extends `JavaIsoVisitor` and uses an `AtomicBoolean` as its type. We can then add a final `J.VariableDeclarations.NamedVariable` object to this visitor. Lastly, we can create a `find` function that takes in the necessary information to set this up and returns a new instance of this visitor:

Expand Down Expand Up @@ -811,4 +811,4 @@ public class FinalizeLocalVariables extends Recipe {

Congratulations on making it through this guide! You now know how to approach and create a complex recipe with multiple visitors.

If you have further questions or need more help, please ask the team in [Slack](https://join.slack.com/t/rewriteoss/shared\_invite/zt-nj42n3ea-b\~62rIHzb3Vo0E1APKCXEA) or in [Discord](https://discord.gg/xk3ZKrhWAb).
If you have further questions or need more help, please ask the team in [Slack](https://join.slack.com/t/rewriteoss/shared\_invite/zt-nj42n3ea-b\~62rIHzb3Vo0E1APKCXEA) or in [Discord](https://discord.gg/xk3ZKrhWAb).