Fix: sort members treats record declarations as types#2942
Fix: sort members treats record declarations as types#2942jamesepatrick wants to merge 1 commit into
Conversation
ASTNode.RECORD_DECLARATION was missing from the switch statement in DefaultJavaElementComparator.category(), causing nested record declarations to fall through to the `return 0` fallback instead of returning TYPE_INDEX. This placed records in the wrong sort category (near methods) rather than with other type declarations (T). The same node type was also missing from the secondary sort switch in compare(), which handles name-based ordering within a category. I did not add any tests for this are there were not tests for the Eclipse formatter.
|
Please hold. I believe my gotcha may be incorrect. Verifying results. |
|
My theory of this bug was incorrect. I had assumed that because this was hitting The TL;DR is the As a quick validation I hard-coded support for java 17. diff --git i/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java w/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
index a50c41a45..f40f096ae 100644
--- i/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
+++ w/lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java
@@ -28,6 +28,7 @@ import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IBufferChangedListener;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
@@ -220,7 +221,9 @@ public final class EclipseJdtSortMembers {
@Override
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
- return Map.of();
+ return Map.of(
+ JavaCore.COMPILER_SOURCE, JavaCore.VERSION_17,
+ JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_17);
}
}Results in the correct sort order public class SortMembersRecordBug {
public record ConversionType(String convertType, String extension) {
}
public void aMethod() {
}
public void bMethod() {
}
public void zMethod() {
}
}So there are a couple of different potential options. 1. Hard code in support for latest supported versionThe simplest answer would be to use 2. Let the user specify itWe could let user define the it in the configuration file. For example <plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<baseDir>${spotless.baseDir}</baseDir>
<java>
<includes>
<include>**/*.java</include>
</includes>
<eclipse>
<compilerSourceVersion>17</compilerSourceVersion> <!-- added -->
<compilerComplianceVersion>17</compilerComplianceVersion> <!-- added -->
<sortMembersEnabled>true</sortMembersEnabled>
<sortMembersOrder>T,SF,SI,SM,F,I,C,M</sortMembersOrder>
<sortMembersDoNotSortFields>true</sortMembersDoNotSortFields>
<sortMembersVisibilityOrderEnabled>true</sortMembersVisibilityOrderEnabled>
<sortMembersVisibilityOrder>B,V,R,D</sortMembersVisibilityOrder>
</eclipse>
</java>
</configuration>
</plugin>3. Use the version of java that the formatter is being run withI think it would be a reasonable assumption that the version number used to run spotless is a good version to run the I think the best course of action is to realistically is an all of the above.
I should have time to add this tomorrow. I'm not really sure how the properties get passed around so I may forego implementing that part. |
See #2941
ASTNode.RECORD_DECLARATIONwas missing from the switch statement inDefaultJavaElementComparator.category(), causing nested record declarations to fall through to thereturn 0fallback instead of returningTYPE_INDEX. This placed records in the wrong sort category (near methods) rather than with other type declarations (T).The same node type was also missing from the secondary sort switch in
compare(), which handles name-based ordering within a category.I did not add any tests for this are there were not tests for the Eclipse formatter.