Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extension": ["ts"],
"node-option": ["loader=ts-node/esm", "no-warnings"]
"node-option": ["experimental-transform-types"]
}
7 changes: 3 additions & 4 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import fs from "node:fs";
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";
import { bench, do_not_optimize, run } from "mitata";
import prettier from "prettier";
import javaPlugin from "../dist/index.mjs";

const dir = "samples";
const files = fs
.readdirSync(dir, { recursive: true })
const files = readdirSync(dir, { recursive: true })
.filter(file => file.endsWith(".java"))
.map(file => path.join(dir, file), "utf8");

bench("prettier-plugin-java", async () => {
try {
for (const file of files) {
const out = await prettier.format(fs.readFileSync(file, "utf-8"), {
const out = await prettier.format(readFileSync(file, "utf-8"), {
parser: "java",
plugins: [javaPlugin]
});
Expand Down
16 changes: 8 additions & 8 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import js from "@eslint/js";
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import globals from "globals";
import tseslint from "typescript-eslint";
import { defineConfig } from "eslint/config";

export default defineConfig([
export default defineConfig(
{
files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: { ...globals.browser, ...globals.node } }
languageOptions: {
globals: globals.node
}
},
eslint.configs.recommended,
tseslint.configs.recommended
]);
);
30 changes: 12 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"postinstall": "cp node_modules/tree-sitter-java-orchard/tree-sitter-java_orchard.wasm src/ && node scripts/generate-node-types.ts && prettier --write src/node-types.ts",
"lerna:publish": "lerna publish from-git --yes",
"lerna:version": "lerna version --exact --no-private",
"lint": "eslint src/**/*.ts",
"lint": "eslint src test",
"prepare": "husky",
"test": "yarn run test:unit && yarn run test:e2e-core",
"test:all": "yarn run test && yarn run test:e2e-jhipster1 && yarn run test:e2e-jhipster2",
Expand All @@ -57,27 +57,21 @@
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/chai": "^5.0.1",
"@types/emscripten": "^1.41.5",
"@types/fs-extra": "^11.0.4",
"@types/klaw-sync": "^6.0.5",
"@types/chai": "^5.2.3",
"@types/mocha": "^10.0.10",
"@types/node": "^18.19.64",
"chai": "^5.1.2",
"eslint": "^10.0.3",
"fs-extra": "^11.2.0",
"@types/node": "^25.5.0",
"chai": "^6.2.2",
"eslint": "^10.1.0",
"globals": "^17.4.0",
"husky": "^9.1.6",
"klaw-sync": "^6.0.0",
"lerna": "^8.1.9",
"lint-staged": "^15.2.10",
"husky": "^9.1.7",
"lerna": "^9.0.7",
"lint-staged": "^16.4.0",
"mitata": "^1.0.34",
"mocha": "^10.8.2",
"prettier": "^3.0.0",
"ts-node": "^10.9.2",
"mocha": "^11.7.5",
"prettier": "^3.8.1",
"tsdown": "^0.21.4",
"typescript": "^5.6.3",
"typescript-eslint": "^8.57.0"
"typescript": "^5.9.3",
"typescript-eslint": "^8.57.1"
},
"peerDependencies": {
"prettier": "^3.0.0"
Expand Down
20 changes: 10 additions & 10 deletions scripts/clone-samples.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable no-console */
import cp from "child_process";
import path from "path";
import fs from "fs-extra";
import url from "url";
import { execSync } from "node:child_process";
import { mkdirSync, rmSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const samplesDir = path.resolve(__dirname, "../samples");

const core = [
Expand Down Expand Up @@ -86,23 +85,24 @@ if (process.argv.length === 3) {
}
}

fs.emptyDirSync(samplesDir);
rmSync(samplesDir, { force: true, recursive: true });
mkdirSync(samplesDir);

sampleRepos.forEach(cloneRepo);

function cloneRepo({ repoUrl, branch, commitHash }) {
console.log(`cloning ${repoUrl}`);
if (commitHash) {
cp.execSync(`git clone ${repoUrl} --branch ${branch}`, {
execSync(`git clone ${repoUrl} --branch ${branch}`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
cp.execSync(`git checkout ${commitHash}`, {
execSync(`git checkout ${commitHash}`, {
cwd: path.resolve(samplesDir, repoUrl.split("/").pop()),
stdio: [0, 1, 2]
});
} else {
cp.execSync(`git clone ${repoUrl} --branch ${branch} --depth 1`, {
execSync(`git clone ${repoUrl} --branch ${branch} --depth 1`, {
cwd: samplesDir,
stdio: [0, 1, 2]
});
Expand Down
10 changes: 6 additions & 4 deletions scripts/generate-node-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "node:fs";
import { writeFileSync } from "node:fs";
import nodeTypeInfo from "tree-sitter-java-orchard/src/node-types.json" with { type: "json" };

fs.writeFileSync(
writeFileSync(
"src/node-types.ts",
`interface Point {
index: number;
Expand Down Expand Up @@ -30,7 +30,7 @@ export interface UnnamedNode<T extends UnnamedType = UnnamedType> extends Syntax
}

export interface CommentNode extends SyntaxNodeBase {
type: SyntaxType.BlockComment | SyntaxType.LineComment;
type: CommentType;
leading: boolean;
trailing: boolean;
printed: boolean;
Expand All @@ -54,7 +54,9 @@ ${nodeTypeInfo
.join("\n")}
};

export type NamedType = Exclude<SyntaxType, SyntaxType.BlockComment | SyntaxType.LineComment>;
export type CommentType = SyntaxType.BlockComment | SyntaxType.LineComment;

export type NamedType = Exclude<SyntaxType, CommentType>;

export type UnnamedType = ${nodeTypeInfo
.filter(({ named }) => !named)
Expand Down
72 changes: 39 additions & 33 deletions scripts/update-test-output.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
/* eslint-disable no-console */
import klawSync from "klaw-sync";
import path from "path";
import fs from "fs-extra";
import {
cpSync,
existsSync,
readdirSync,
readFileSync,
rmSync,
writeFileSync
} from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import prettier from "prettier";
import url from "url";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const updateTestOutput = async () => {
let samplesDir = path.resolve(__dirname, "../test/unit-test");
let originalSamplesDir = samplesDir;
if (process.argv.indexOf("-single") > -1) {
samplesDir = path.resolve(__dirname, "./single-printer-run");
} else if (process.argv.indexOf("-repository") > -1) {
const testSamples = path.resolve(__dirname, "../test-samples");
originalSamplesDir = path.resolve(
const originalSamplesDir = path.resolve(
__dirname,
process.argv[process.argv.indexOf("-repository") + 1]
);
samplesDir = path.resolve(testSamples, path.basename(originalSamplesDir));
if (fs.existsSync(samplesDir)) {
fs.removeSync(samplesDir);
if (existsSync(samplesDir)) {
rmSync(samplesDir, { recursive: true });
}
console.log(`start copy ${originalSamplesDir} to ${samplesDir}`);
fs.copySync(originalSamplesDir, samplesDir);
cpSync(originalSamplesDir, samplesDir, { recursive: true });
console.log(`end copy ${originalSamplesDir} to ${samplesDir}`);
}

Expand All @@ -32,30 +36,35 @@ const updateTestOutput = async () => {
numberOfTime = process.argv[process.argv.indexOf("-times") + 1];
}

const sampleFiles = klawSync(samplesDir, { nodir: true });
const javaSampleFiles = sampleFiles.filter(fileDesc => {
if (fileDesc.path.includes("node_modules")) {
return false;
}
if (process.argv.indexOf("-repository") > -1) {
return fileDesc.path.endsWith(".java");
}
return fileDesc.path.endsWith("input.java");
const sampleFiles = readdirSync(samplesDir, {
encoding: "utf-8",
recursive: true
});
const javaSampleFiles = sampleFiles
.filter(filePath => {
if (filePath.includes("node_modules")) {
return false;
}
if (process.argv.indexOf("-repository") > -1) {
return filePath.endsWith(".java");
}
return filePath.endsWith("input.java");
})
.map(filePath => path.join(samplesDir, filePath));

let failures = 0;
await Promise.all(
javaSampleFiles.map(async fileDesc => {
const javaFileText = fs.readFileSync(fileDesc.path, "utf8");
javaSampleFiles.map(async filePath => {
const javaFileText = readFileSync(filePath, "utf8");

try {
console.log(`Reading <${fileDesc.path}>`);
console.log(`Reading <${filePath}>`);
let newExpectedText = javaFileText;

const testDir = path.dirname(fileDesc.path);
const testDir = path.dirname(filePath);
const optionsPath = path.join(testDir, ".prettierrc.json");
const testOptions = fs.existsSync(optionsPath)
? fs.readJsonSync(optionsPath)
const testOptions = existsSync(optionsPath)
? JSON.parse(readFileSync(optionsPath, "utf-8"))
: {};

for (let i = 0; i < numberOfTime; i++) {
Expand All @@ -78,18 +87,15 @@ const updateTestOutput = async () => {
...testOptions
});
}
let outputFilePath = fileDesc.path.replace(
/input.java$/,
"output.java"
);
let outputFilePath = filePath.replace(/input.java$/, "output.java");
if (process.argv.indexOf("-repository") > -1) {
outputFilePath = fileDesc.path;
outputFilePath = filePath;
}
console.log(`writing <${outputFilePath}>`);
fs.writeFileSync(outputFilePath, newExpectedText);
writeFileSync(outputFilePath, newExpectedText);
} catch (e) {
failures++;
console.log(`Failed parsing: <${fileDesc.path}>`);
console.log(`Failed parsing: <${filePath}>`);
console.log(e);
}
})
Expand Down
8 changes: 4 additions & 4 deletions src/comments.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { util, type AstPath, type Doc } from "prettier";
import { builders } from "prettier/doc";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.js";
import parser from "./parser.js";
import printer from "./printer.js";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";
import parser from "./parser.ts";
import printer from "./printer.ts";
import {
hasChild,
printComment,
type JavaParserOptions,
type NamedNodePath
} from "./printers/helpers.js";
} from "./printers/helpers.ts";

const { hasNewline, isPreviousLineEmpty, skipNewline, skipSpaces } = util;
const { breakParent, hardline, line, lineSuffix } = builders;
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Plugin } from "prettier";
import type { SyntaxNode } from "./node-types.js";
import options from "./options.js";
import parser from "./parser.js";
import printer from "./printer.js";
import type { SyntaxNode } from "./node-types.ts";
import options from "./options.ts";
import parser from "./parser.ts";
import printer from "./printer.ts";

export default {
languages: [
Expand Down
9 changes: 4 additions & 5 deletions src/node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface UnnamedNode<
}

export interface CommentNode extends SyntaxNodeBase {
type: SyntaxType.BlockComment | SyntaxType.LineComment;
type: CommentType;
leading: boolean;
trailing: boolean;
printed: boolean;
Expand Down Expand Up @@ -196,10 +196,9 @@ export const enum SyntaxType {
VoidType = "void_type"
}

export type NamedType = Exclude<
SyntaxType,
SyntaxType.BlockComment | SyntaxType.LineComment
>;
export type CommentType = SyntaxType.BlockComment | SyntaxType.LineComment;

export type NamedType = Exclude<SyntaxType, CommentType>;

export type UnnamedType =
| "!"
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type Prettier from "prettier";
import nodeTypeInfo from "tree-sitter-java-orchard/src/node-types.json" with { type: "json" };
import { Language, Parser, type Node } from "web-tree-sitter";
import { determinePrettierIgnoreRanges } from "./comments.js";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.js";
import { determinePrettierIgnoreRanges } from "./comments.ts";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";

export default {
async parse(text) {
Expand Down
8 changes: 4 additions & 4 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
isFullyBetweenPrettierIgnore,
isPrettierIgnore,
willPrintOwnComments
} from "./comments.js";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.js";
} from "./comments.ts";
import { SyntaxType, type CommentNode, type SyntaxNode } from "./node-types.ts";
import {
printComment,
printValue,
type NamedNodePath
} from "./printers/helpers.js";
import { printerForNodeType } from "./printers/index.js";
} from "./printers/helpers.ts";
import { printerForNodeType } from "./printers/index.ts";

export default {
print(path, options, print, args) {
Expand Down
2 changes: 1 addition & 1 deletion src/printers/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { printArrayInitializer, type NamedNodePrinters } from "./helpers.js";
import { printArrayInitializer, type NamedNodePrinters } from "./helpers.ts";

export default {
array_initializer: printArrayInitializer
Expand Down
Loading