-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Issue #11604: allow 3rd party checks to print their own custom asts #12510
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of minors
callTreeStringPrinter(filesToProcess.get(0), | ||
Class.forName(options.customTreePrinterClass)); | ||
} | ||
catch (ClassNotFoundException | IOException | CheckstyleException ex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to catch CheckstyleException
here. It just wraps one CheckstyleException
with another:
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to work with custom tree printer
at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:308)
at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:197)
at com.puppycrawl.tools.checkstyle.Main.main(Main.java:132)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to work with custom tree printer
at com.puppycrawl.tools.checkstyle.Main.callTreeStringPrinter(Main.java:360)
at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:304)
... 2 more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the next push.
824310c
to
1706e7b
Compare
if (suppressionLineColumnNumber != null || configurationFile != null | ||
|| propertiesFile != null || outputPath != null | ||
|| parseResult.hasMatchedOption(OUTPUT_FORMAT_OPTION)) { | ||
result.add("Option '-t' cannot be used with other options."); | ||
result.add("Tree Options cannot be used with other options."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wording was identified as out of date and confusing for options where -t
is not used.
Examples:
PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -t .
Option '-t' cannot be used with other options.
PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -T .
Option '-t' cannot be used with other options.
PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -J .
Option '-t' cannot be used with other options.
1706e7b
to
c61064b
Compare
c61064b
to
8b48ac1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What alternatives have been considered to solve this problem? Extension of CLI/API like this deserves it's own issue/discussion IMO. Won't we need to add "alternative" options for others, such as -g
, -s
, and -b
?
See the end of my first post at #12510 (comment) . I was planning things in future PRs after this one.
I can create the issue if that is what is needed.
There is no alternative in Checkstyle as we don't allow any extension of the CLI. If this PR isn't acceptable, then there isn't a lot of good options. 3rd parties could create a hack of a check who's only function is to print the tree of the file it is given, which would require a configuration file for nothing more than specifying the tree printer which I am passing in as an argument in this PR. Other than that, 3rd parties will have to duplicate Checkstyle's CLI as they will need the same options (plus their own) if we were to allow them to use checkstyle/contribution#604 . |
Is allowing extension of CLI another option? Then people that want to extend CLI functionality can do whatever they want, and not be bothered by activities in main project. |
I am open to this option, but I am not fully seeing how to be honest. Any extension is based picocli allowing this as it is the main controller of the CLI and probably some overridable methods to allow such custom execution. https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L629 https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L113-L114 https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L188 If this is possible, I am seeing this require main repo make classes public and methods protected to be able to do an And even if we do this, things like |
If we want to inject dependencies, we could do something like:
|
* Interface for printing ASTs to String. | ||
*/ | ||
@FunctionalInterface | ||
public interface TreeStringPrinter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont like this design, to be honest.
method name like printFileAst
assumes that
- there is no return, similar to
System.out.println()
and similar methods. - Result is the AST, which is weird since return type is String, which means any text.
Plus, name of the interface is confusing, since even existing implementations dont do any printing of the AST, they just convert it into string. Printing is performed outside, in Main.java line 369.
Therefore I suggest to rename it to something like FileConverter (or something like this) with meaningful method name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nrmancuso @strkkk @pbludov
Is there any agreement we can make on design before I continue? @nrmancuso Was mentioning extension over something being built into the project. For @strkkk 's comment, everything was originally printFileAst
so thats why I went with this, but if we head down extension, then naming becomes not an issue as 3rd parties will be the ones to name it themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make
private static class CliOptions { |
I do not like this extension of our API, i think it should be done completely in thirdparty projects such custom support. |
no more ideas or points to reconcile disfigurements. |
can we use this property and print tree in format thatwe need at #14631 ? |
Issue #11604
Currently 3rd parties have no way to provide their own AST to do some of the basics that Checkstyle does like printing the ASTs for users to understand it.
This provides a new option in the CLI so custom printers can be used. Other printers were very slightly reworked to this new method to show its usage.
It is hoped to use this method for other CLI functionality like
printXpathBranch
andprintSuppressions
.