-
Notifications
You must be signed in to change notification settings - Fork 720
SONARJAVA-5978 Support Compact Source Files #5397
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
Changes from all commits
b1cea93
dd09789
d3a936b
6f27a65
7d074ad
fda7385
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| void main() { | ||
| int a = 5; | ||
| int x = 10; // Noncompliant {{Rename "x" which hides the field declared at line 11.}} | ||
| // ^ | ||
| float b = 3.14f; | ||
| System.out.println(a); | ||
| System.out.println(x); | ||
| System.out.println(b); | ||
| } | ||
|
|
||
| int x = 20; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // SONARJAVA-6028: FPs ahead. Only the line with "Too much." should be noncompliant. | ||
|
|
||
| void main() { // Noncompliant | ||
| System.out.println("Just right."); // Noncompliant | ||
| if (true) { | ||
| System.out.println("Too much."); // Noncompliant | ||
| } | ||
| } | ||
|
|
||
| class MyClass { // Noncompliant | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| void main() { | ||
| int a = 5; | ||
| int x = 10; | ||
| float b = 3.14f; | ||
| System.out.println(a); | ||
| System.out.println(x); | ||
| System.out.println(b); | ||
| } | ||
|
|
||
| int x = 20; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,4 +44,14 @@ void resource_file_mapping() { | |
| "org/sonar/java/JavaFilesCacheTestFile$A$3"); | ||
| } | ||
|
|
||
| @Test | ||
| void compact_source() { | ||
| JavaFilesCache javaFilesCache = new JavaFilesCache(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. |
||
| JavaAstScanner.scanSingleFileForTests(TestUtils.inputFile("src/test/resources/JavaFilesCacheTestFileCompact.java"), new VisitorsBridge(javaFilesCache)); | ||
|
|
||
| Set<String> classNames = javaFilesCache.getClassNames(); | ||
| assertThat(classNames) | ||
| .hasSize(4) | ||
| .contains("$1$1", "$1", "$1$C", "$1$C$D"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2102,4 +2102,43 @@ public class Source { | |
| JavaTree.ImportTreeImpl importConst = (JavaTree.ImportTreeImpl) cu.imports().get(1); | ||
| assertThat(importConst.isModule()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void compactSource_simple() { | ||
| String source = """ | ||
| void main() { | ||
| } | ||
| """; | ||
| JavaTree.CompilationUnitTreeImpl cu = test(source); | ||
| assertThat(cu.types()).hasSize(1); | ||
| ClassTreeImpl clazz = (ClassTreeImpl) cu.types().get(0); | ||
| assertThat(clazz).isNotNull(); | ||
| assertThat(clazz.kind()).isEqualTo(Tree.Kind.IMPLICIT_CLASS); | ||
| assertThat(clazz.simpleName()).isNull(); | ||
| assertThat(clazz.openBraceToken()).isNull(); | ||
| assertThat(clazz.closeBraceToken()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void compactSource_complex() { | ||
| String source = """ | ||
| void main() { | ||
| System.out.println("Hello, World!"); | ||
| } | ||
| int i = 43; | ||
| class Helper { | ||
| void help() { | ||
| System.out.println("Helping..."); | ||
| } | ||
| } | ||
| """; | ||
| JavaTree.CompilationUnitTreeImpl cu = test(source); | ||
| ClassTreeImpl clazz = (ClassTreeImpl) cu.types().get(0); | ||
| assertThat(clazz).isNotNull(); | ||
| assertThat(clazz.kind()).isEqualTo(Tree.Kind.IMPLICIT_CLASS); | ||
| assertThat(clazz.members()).hasSize(3); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we explicitly test that the 3 members are a method, a variable and a type?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| assertThat(clazz.members().get(0).kind()).isEqualTo(Tree.Kind.METHOD); | ||
| assertThat(clazz.members().get(1).kind()).isEqualTo(Tree.Kind.VARIABLE); | ||
| assertThat(clazz.members().get(2).kind()).isEqualTo(Tree.Kind.CLASS); | ||
| } | ||
| } | ||
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 address this but I wonder in how many places we infer a class is anonymous from it
simpleNameis null rather than using a more explicitKindThere 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.
https://sonarsource.atlassian.net/browse/SONARJAVA-6074