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

Add GoToLine Recipe + Utility Methods #3829

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JLLeitschuh
Copy link
Collaborator

Adds a recipe that will find the LST nodes in the tree at a given
line/column.

This is useful for matching the OpenRewrite LST against outputs from
other scanners, for example security scanners that generate SARIF files.

Signed-off-by: Jonathan Leitschuh Jonathan.Leitschuh@gmail.com

What's changed?

Added a GoToLine recipe.

What's your motivation?

Being able to find LST nodes by their line/column coordinates can be helpful when matcing up the LST against scanners.

Anything in particular you'd like reviewers to focus on?

Are there any unit tests that I'm missing that I should really make sure I include?

This strategy, unfortunately, doesn't inherintly support other languages. Is there any strategy that would make this multi-language?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@JLLeitschuh
Copy link
Collaborator Author

Supersedes openrewrite/rewrite-analysis#41

@knutwannheden
Copy link
Contributor

@JLLeitschuh I am surprised that we provide a recipe for this. What is the usefulness of that? Especially since the recipe doesn't have a path option for the file. Would it not make more sense to only have a visitor?

@JLLeitschuh
Copy link
Collaborator Author

Especially since the recipe doesn't have a path option for the file.

I figure if you want a path option that should, likely, be it's own recipe that flags the file as a precondition, then this is applied.

@JLLeitschuh
Copy link
Collaborator Author

Screenshot 2023-12-16 at 1 56 12 AM

I was implying that this should be a recipe from this comment. But I guess it was never explicitly stated by @jkschneider.

Idk. I thought it may be useful to demonstrate the capability, but I'm not attached to keeping the functionality in a recipe. The recipe needs to exist in some form for unit testing though.

If it's not a recipe, where would the API live? I had hoped on SourceFile but that's in core, which I wouldn't be able to do without services for other languages.

@knutwannheden
Copy link
Contributor

But when would you be using this recipe as is? I thought your use case would be programmatic use. How would you be using this?

Having line and column number options but no path option seems kind of weird.

@knutwannheden
Copy link
Contributor

For unit testing you can just use the toRecipe() utility which wraps a visitor into a recipe.

@knutwannheden
Copy link
Contributor

I think we should move this code into the core as soon as we have agreed on how this services API should look like. That has now also been incubating in rewrite-java for some time.

@JLLeitschuh
Copy link
Collaborator Author

JLLeitschuh commented Dec 16, 2023

But when would you be using this recipe as is?

Quick and dirty testing on the SaaS. But in general, not often. It doesn't need to be a recipe. I'd like a suggestion on where the API should live though.

@knutwannheden
Copy link
Contributor

I think the visitor might be interesting as a separate utility. If we additionally want a recipe, I think we should also have a path option.

Apart from that I think the code is in the right place. The service is something we would build on top.

@timtebeek timtebeek added the enhancement New feature or request label Dec 18, 2023

import java.util.*;

@Incubating(since = "8.11.3")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@Incubating(since = "8.11.3")
@Incubating(since = "8.12.1")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Resolved! Much appreciated!


@Incubating(since = "8.11.3")
public boolean isPrefix() {
return this.toString().endsWith("_PREFIX");
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we get a lot of false positives without this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The logic does not work correctly without a check to see if the Location is a prefix.

Without it, we end up findings AST elements on other lines

Adds a recipe that will find the LST nodes in the tree at a given
line/column.

This is useful for matching the OpenRewrite LST against outputs from
other scanners, for example security scanners that generate SARIF files.

Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>
@JLLeitschuh
Copy link
Collaborator Author

@knutwannheden PTAL at the changes I made yesterday and let me know if they are good for you to merge

@JLLeitschuh
Copy link
Collaborator Author

@timtebeek anything that can be done to get this over the finish line?

@timtebeek
Copy link
Contributor

Hi @JLLeitschuh ! Thanks for proposing to add this upstream; after some more discussion we feel this would fit better in rewrite-analysis or rewrite-java-security after all. Sorry about any rework; I know you started out there originally. Look forward to seeing it there, and what you'll build once it's available.

@JLLeitschuh
Copy link
Collaborator Author

Much appreciated. Any qualms on accepting the changes to Space for isPrefix' and isSuffix`?

@timtebeek
Copy link
Contributor

Any qualms on accepting the changes to Space for isPrefix' and isSuffix`?

Given the coordination required for any changes to tree packages, and our general lean towards a minimal public API it might be best to keep the pre/suffix checks within the GoToLine utility too, at least for now. Thanks again!

@JLLeitschuh
Copy link
Collaborator Author

Fair enough. Especially because, as of currently, this is likely fairly niche for now.

@knutwannheden do you have insights into how stable the [LANGUAGE]Printer's are for all the other languages? In order for this functionality to work for multiple languages, I'm going to have to build on top of those, so having them break out from under rewrite-analysis would be annoying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants