Skip to content

Commit

Permalink
Added support for require-pragma option (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-paz committed Mar 1, 2023
1 parent fbb3bc9 commit 3bb0653
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/prettier-plugin-java/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function locEnd(/* node */) {
return -1;
}

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

const parsers = {
Expand Down
23 changes: 19 additions & 4 deletions packages/prettier-plugin-java/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ import { format, doc } from "prettier";
const { printDocToString } = doc.printer;

const pluginPath = resolve(__dirname, "../dist/index.js");
export function testSample(testFolder: string, exclusive?: boolean) {
export function testSampleWithOptions({
testFolder,
exclusive,
prettierOptions = {}
}: {
testFolder: string;
exclusive?: boolean;
prettierOptions?: any;
}) {
const itOrItOnly = exclusive ? it.only : it;
const inputPath = resolve(testFolder, "_input.java");
const expectedPath = resolve(testFolder, "_output.java");
Expand All @@ -31,7 +39,8 @@ export function testSample(testFolder: string, exclusive?: boolean) {
itOrItOnly(`can format <${relativeInputPath}>`, async () => {
const actual = await format(inputContents, {
parser: "java",
plugins: [pluginPath]
plugins: [pluginPath],
...prettierOptions
});

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

const secondPass = await format(onePass, {
parser: "java",
plugins: [pluginPath]
plugins: [pluginPath],
...prettierOptions
});
expect(onePass).to.equal(secondPass);
});
}

export function testSample(testFolder: string, exclusive?: boolean) {
testSampleWithOptions({ testFolder, exclusive });
}

export function testRepositorySample(
testFolder: string,
command: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @format
*/
public enum Enum {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}

}

class CLassWithEnum {

public static enum VALID_THINGS {

FIRST, SECOND

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @format
*/
public enum Enum {
SOME_ENUM,
ANOTHER_ENUM,
LAST_ENUM,
}

public enum Enum {
THIS_IS_GOOD("abc"),
THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}
}

class CLassWithEnum {

public static enum VALID_THINGS {
FIRST,
SECOND,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @surely this is invalid
*/
public enum Enum {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}

}

class CLassWithEnum {

public static enum VALID_THINGS {

FIRST, SECOND

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @surely this is invalid
*/
public enum Enum {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}

}

class CLassWithEnum {

public static enum VALID_THINGS {

FIRST, SECOND

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @prettier
*/
public enum Enum {

SOME_ENUM, ANOTHER_ENUM, LAST_ENUM;

}

public enum Enum {

THIS_IS_GOOD("abc"), THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}

}

class CLassWithEnum {

public static enum VALID_THINGS {

FIRST, SECOND

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @prettier
*/
public enum Enum {
SOME_ENUM,
ANOTHER_ENUM,
LAST_ENUM,
}

public enum Enum {
THIS_IS_GOOD("abc"),
THIS_IS_FINE("abc");

public static final String thisWillBeDeleted = "DELETED";

private final String value;

public Enum(String value) {
this.value = value;
}

public String toString() {
return "STRING";
}
}

class CLassWithEnum {

public static enum VALID_THINGS {
FIRST,
SECOND,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as path from "path";
import { testSampleWithOptions } from "../../test-utils";

describe("prettier-java: require-pragma option", () => {
[
path.resolve(__dirname, "./format-pragma"),
path.resolve(__dirname, "./prettier-pragma"),
path.resolve(__dirname, "./invalid-pragma")
].forEach(testFolder =>
testSampleWithOptions({
testFolder,
prettierOptions: { requirePragma: true }
})
);
});

0 comments on commit 3bb0653

Please sign in to comment.