Skip to content

Commit 3bb0653

Browse files
authored
Added support for require-pragma option (#574)
1 parent fbb3bc9 commit 3bb0653

9 files changed

Lines changed: 246 additions & 6 deletions

File tree

packages/prettier-plugin-java/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ function locEnd(/* node */) {
2727
return -1;
2828
}
2929

30-
function hasPragma(/* text */) {
31-
return false;
30+
function hasPragma(text) {
31+
return /^\/\*\*[\n][\t\s]+\*\s@(prettier|format)[\n][\t\s]+\*\//.test(text);
3232
}
3333

3434
const parsers = {

packages/prettier-plugin-java/test/test-utils.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import { format, doc } from "prettier";
1313
const { printDocToString } = doc.printer;
1414

1515
const pluginPath = resolve(__dirname, "../dist/index.js");
16-
export function testSample(testFolder: string, exclusive?: boolean) {
16+
export function testSampleWithOptions({
17+
testFolder,
18+
exclusive,
19+
prettierOptions = {}
20+
}: {
21+
testFolder: string;
22+
exclusive?: boolean;
23+
prettierOptions?: any;
24+
}) {
1725
const itOrItOnly = exclusive ? it.only : it;
1826
const inputPath = resolve(testFolder, "_input.java");
1927
const expectedPath = resolve(testFolder, "_output.java");
@@ -31,7 +39,8 @@ export function testSample(testFolder: string, exclusive?: boolean) {
3139
itOrItOnly(`can format <${relativeInputPath}>`, async () => {
3240
const actual = await format(inputContents, {
3341
parser: "java",
34-
plugins: [pluginPath]
42+
plugins: [pluginPath],
43+
...prettierOptions
3544
});
3645

3746
expect(actual).to.equal(expectedContents);
@@ -40,17 +49,23 @@ export function testSample(testFolder: string, exclusive?: boolean) {
4049
it(`Performs a stable formatting for <${relativeInputPath}>`, async () => {
4150
const onePass = await format(inputContents, {
4251
parser: "java",
43-
plugins: [pluginPath]
52+
plugins: [pluginPath],
53+
...prettierOptions
4454
});
4555

4656
const secondPass = await format(onePass, {
4757
parser: "java",
48-
plugins: [pluginPath]
58+
plugins: [pluginPath],
59+
...prettierOptions
4960
});
5061
expect(onePass).to.equal(secondPass);
5162
});
5263
}
5364

65+
export function testSample(testFolder: string, exclusive?: boolean) {
66+
testSampleWithOptions({ testFolder, exclusive });
67+
}
68+
5469
export function testRepositorySample(
5570
testFolder: string,
5671
command: string,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @format
3+
*/
4+
public enum Enum {
5+
6+
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;
7+
8+
}
9+
10+
public enum Enum {
11+
12+
THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
26+
}
27+
28+
class CLassWithEnum {
29+
30+
public static enum VALID_THINGS {
31+
32+
FIRST, SECOND
33+
34+
}
35+
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @format
3+
*/
4+
public enum Enum {
5+
SOME_ENUM,
6+
ANOTHER_ENUM,
7+
LAST_ENUM,
8+
}
9+
10+
public enum Enum {
11+
THIS_IS_GOOD("abc"),
12+
THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
}
26+
27+
class CLassWithEnum {
28+
29+
public static enum VALID_THINGS {
30+
FIRST,
31+
SECOND,
32+
}
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @surely this is invalid
3+
*/
4+
public enum Enum {
5+
6+
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;
7+
8+
}
9+
10+
public enum Enum {
11+
12+
THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
26+
}
27+
28+
class CLassWithEnum {
29+
30+
public static enum VALID_THINGS {
31+
32+
FIRST, SECOND
33+
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @surely this is invalid
3+
*/
4+
public enum Enum {
5+
6+
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;
7+
8+
}
9+
10+
public enum Enum {
11+
12+
THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
26+
}
27+
28+
class CLassWithEnum {
29+
30+
public static enum VALID_THINGS {
31+
32+
FIRST, SECOND
33+
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @prettier
3+
*/
4+
public enum Enum {
5+
6+
SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;
7+
8+
}
9+
10+
public enum Enum {
11+
12+
THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
26+
}
27+
28+
class CLassWithEnum {
29+
30+
public static enum VALID_THINGS {
31+
32+
FIRST, SECOND
33+
34+
}
35+
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @prettier
3+
*/
4+
public enum Enum {
5+
SOME_ENUM,
6+
ANOTHER_ENUM,
7+
LAST_ENUM,
8+
}
9+
10+
public enum Enum {
11+
THIS_IS_GOOD("abc"),
12+
THIS_IS_FINE("abc");
13+
14+
public static final String thisWillBeDeleted = "DELETED";
15+
16+
private final String value;
17+
18+
public Enum(String value) {
19+
this.value = value;
20+
}
21+
22+
public String toString() {
23+
return "STRING";
24+
}
25+
}
26+
27+
class CLassWithEnum {
28+
29+
public static enum VALID_THINGS {
30+
FIRST,
31+
SECOND,
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as path from "path";
2+
import { testSampleWithOptions } from "../../test-utils";
3+
4+
describe("prettier-java: require-pragma option", () => {
5+
[
6+
path.resolve(__dirname, "./format-pragma"),
7+
path.resolve(__dirname, "./prettier-pragma"),
8+
path.resolve(__dirname, "./invalid-pragma")
9+
].forEach(testFolder =>
10+
testSampleWithOptions({
11+
testFolder,
12+
prettierOptions: { requirePragma: true }
13+
})
14+
);
15+
});

0 commit comments

Comments
 (0)