Open miorel opened 4 months ago
@miorel, Do we need to build our own java linter or just want to integrate an external linter plugin/extension and configure it for our project?
Also, do we want live linting (that is real-time feedback during development) like sonarlint
Building our own Java linter would be a very interesting project and I'm sure we'd learn a lot from it! But for now the scope of this issue is to integrate an already existing linter.
I'm not familiar with what's the latest and greatest in Java linting, so I think the first step would be to research some of the options, and then try one. The minimum requirement is being able to run it from the command line to report issues. Editor integration would be a bonus but I don't think we should necessarily assume what editor people are using.
Building our own Java linter would be a very interesting project and I'm sure we'd learn a lot from it! But for now the scope of this issue is to integrate an already existing linter.
I'm not familiar with what's the latest and greatest in Java linting, so I think the first step would be to research some of the options, and then try one. The minimum requirement is being able to run it from the command line to report issues. Editor integration would be a bonus but I don't think we should necessarily assume what editor people are using.
Got it. I'll start with the researching available options for java linters.
@miorel,
I've been looking into Java linters and explored a few options besides Spotless, such as Checkstyle, SpotBugs, and PMD.
I'm thinking we can combine Spotless with either PMD / SpotBugs or Checkstyle for our setup.
Spotless will primarily handle code formatting, as its static analysis capabilities are limited (e.g. Removing unused imports and couple of others). However, it's perfect for enforcing consistent formatting, like adhering to the Google Java Style Guide. We can use ./gradlew spotlessCheck
to check for any formatting violations and ./gradlew spotlessApply
to automatically format the code based on the configured rules.
For in-depth static analysis, we can rely on PMD. It has a comprehensive list of rules for linting Java code. We can customize this ruleset according to our needs (see Making rulesets). When running ./gradlew pmdMain
, a main.html report will be generated in the /build/reports/pmd folder, detailing any rule violations. Here's an example of what the main.html file might contain:
`
# | File | Line | Problem |
---|---|---|---|
1 | /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/digits_int/AP.java | 32 | Avoid using literals in if statements |
1 | /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/simple_iterator/SimpleIterator.java | 40 | Assigning an Object to null is a code smell. Consider refactoring. |
1 | /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/union_find/UnionFind.java | 10 | Field components has the same name as a method |
1 | /home/utsav/leetcode-curriculum/workspaces/adventure-pack/goodies/java/src/virtual_list/VirtualList.java | 8 | Field size has the same name as a method |
For example https://github.com/diffplug/spotless