Question

I'm trying to create some design and style rules for my team using PMD Designer. The goal is to add these rules to our teams SonarQube. It would be useful in some cases to identify when a line break is used. For example:

MyResult result = new Builder().doSomething().run();

versus

MyResult result = new Builder()
    .doSomething()
    .run();

However the AST for each is identical:

AST Comparison of each technique

Is there anyway to identify the difference in line breaks within a PMD rule? If not, is there a different analysis engine that would?

Was it helpful?

Solution

While the structure of the AST is the same, there is more detail than that in the actual XML file. In particular, there are attributes for the line number and column of tags. Which means you can write an XPath or Java rule against those attributes.

You can see an example here.

<TypeDeclaration BeginColumn="1" BeginLine="1" EndColumn="1" EndLine="10"

You can also generate this for your own file for testing using the PMD Eclipse plugin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top