-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support unnamed class compilation unit (#615)
* feat: add UnnamedClassCompilationUnit declaration in parser * feat: support unnamed class compilation unit (with spec deviation) * test: add test for Unnamed Class Compilation Unit printing * chore: update signature * style: fix prettier issue
- Loading branch information
1 parent
87192b4
commit f489ce2
Showing
8 changed files
with
193 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/java-parser/src/productions/utils/class-body-types.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const classBodyTypes = { | ||
unknown: 0, | ||
fieldDeclaration: 1, | ||
methodDeclaration: 2, | ||
classDeclaration: 3, | ||
interfaceDeclaration: 4, | ||
semiColon: 5, | ||
instanceInitializer: 6, | ||
staticInitializer: 7, | ||
constructorDeclaration: 8 | ||
}; | ||
|
||
module.exports = { | ||
classBodyTypes | ||
}; |
96 changes: 96 additions & 0 deletions
96
packages/java-parser/test/compilation-unit/unnamed-class-compilation-unit-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict"; | ||
|
||
const { expect } = require("chai"); | ||
const javaParser = require("../../src/index"); | ||
|
||
describe("Unnamed Class Compilation Unit", () => { | ||
it("should handle UnnamedClassCompilationUnit", () => { | ||
const input = ` | ||
void main() { | ||
System.out.println("Hello, World!"); | ||
} | ||
`; | ||
javaParser.parse(input, "compilationUnit"); | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with only method", () => { | ||
const input = ` | ||
void main() { | ||
System.out.println("Hello, World!"); | ||
} | ||
`; | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with fields", () => { | ||
const input = ` | ||
String greeting = "Hello world!"; | ||
String hourra = "Hourra!"; | ||
void main() { | ||
System.out.println(greeting); | ||
System.out.println(hourra); | ||
} | ||
`; | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with class declaration", () => { | ||
const input = ` | ||
class Test { static String greetings() { return "Hello world!"; } } | ||
void main() { | ||
System.out.println(Test.greetings()); | ||
} | ||
`; | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with interface declaration", () => { | ||
const input = ` | ||
interface Test { default String greetings() { return "Hello world!"; } } | ||
void main() { | ||
System.out.println(Test.greetings()); | ||
} | ||
`; | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with semicolons", () => { | ||
const input = ` | ||
; | ||
void main() { | ||
System.out.println("Hello World!"); | ||
} | ||
`; | ||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with class member declarations", () => { | ||
const input = ` | ||
String greeting() { return "Hello, World!"; } | ||
void main() { | ||
System.out.println(greeting()); | ||
} | ||
`; | ||
|
||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
|
||
it("should handle UnnamedClassCompilationUnit with imports", () => { | ||
const input = ` | ||
import com.toto.titi.Test; | ||
import com.toto.titi.Toast; | ||
void main() { | ||
System.out.println(Test.greeting()); | ||
} | ||
`; | ||
|
||
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw(); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/prettier-plugin-java/test/unit-test/unnamed-class-compilation-unit/_input.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import com.toto.titi.Test; | ||
import com.toto.titi.Toast; | ||
|
||
class TestClass { static String greetings() { return "Hello world!"; } } | ||
interface TestInterface { default String greetings() { return "Hello world!"; } } | ||
|
||
; | ||
String greeting() { return "Hello, World!"; } | ||
|
||
void main() { | ||
System.out.println(Test.greeting()); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/prettier-plugin-java/test/unit-test/unnamed-class-compilation-unit/_output.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import com.toto.titi.Test; | ||
import com.toto.titi.Toast; | ||
|
||
class TestClass { | ||
|
||
static String greetings() { | ||
return "Hello world!"; | ||
} | ||
} | ||
|
||
interface TestInterface { | ||
default String greetings() { | ||
return "Hello world!"; | ||
} | ||
} | ||
|
||
String greeting() { | ||
return "Hello, World!"; | ||
} | ||
|
||
void main() { | ||
System.out.println(Test.greeting()); | ||
} |
5 changes: 5 additions & 0 deletions
5
...java/test/unit-test/unnamed-class-compilation-unit/unnamed-class-compilation-unit-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { testSample } from "../../test-utils"; | ||
|
||
describe("prettier-java: Unnamed Class Compilation Unit", () => { | ||
testSample(__dirname); | ||
}); |