Skip to content

Commit

Permalink
chore: convert parser and printer to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Nov 26, 2023
1 parent 904ce59 commit 11e859c
Show file tree
Hide file tree
Showing 142 changed files with 1,820 additions and 5,160 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ env:
es6: true
node: true
parserOptions:
ecmaVersion: 2018
ecmaVersion: 2020
sourceType: module
rules:
no-fallthrough: off
curly: error
Expand Down
56 changes: 0 additions & 56 deletions benchmark/java-parser/benchmark-demo.js

This file was deleted.

50 changes: 19 additions & 31 deletions benchmark/java-parser/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
/* eslint no-console: 0 */
"use strict";
const path = require("path");
const klawSync = require("klaw-sync");
const _ = require("lodash");
const fs = require("fs");
const benchmark = require("benchmark");
const cp = require("child_process");
const niv = require("npm-install-version");

const version = cp
.execSync("npm show java-parser version")
.toString()
.replace("\n", "");
niv.install(`java-parser@${version}`);

const npmparser = require(`java-parser@${version}`);
const currentparser = require("../../packages/java-parser/src/index");
import path from "path";
import klawSync from "klaw-sync";
import fs from "fs";
import * as npmparser from "java-parser-npm";
import { performance } from "perf_hooks";
import url from "url";
import * as currentparser from "../../packages/java-parser/src/index.js";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const samplesDir = path.resolve(
__dirname,
"../../packages/java-parser/samples/java-design-patterns/flux"
Expand All @@ -26,32 +17,29 @@ const javaSampleFiles = sampleFiles.filter(fileDesc =>
fileDesc.path.endsWith(".java")
);

const javaPathAndText = _.map(javaSampleFiles, fileDesc => {
const javaPathAndText = javaSampleFiles.map(fileDesc => {
const currJavaFileString = fs.readFileSync(fileDesc.path, "utf8");
const relativePath = path.relative(__dirname, fileDesc.path);

return { path: relativePath, text: currJavaFileString };
});

function benchmarkParser(parser) {
_.forEach(javaPathAndText, javaText => {
const start = performance.now();
javaPathAndText.forEach(javaText => {
try {
parser(javaText.text);
} catch (e) {
console.log(e);
}
});
const end = performance.now();
return `${end - start}ms`;
}

new benchmark.Suite("Java parser benchmark", {
onStart: () => console.log(`Java parser benchmark`),
onCycle: event => console.log(String(event.target)),
onComplete: function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
}
})
.add(`NPM Java Parser (v${version})`, () => benchmarkParser(npmparser.parse))
.add("Local Repository Java Parser", () =>
benchmarkParser(currentparser.parse)
)
.run();
for (let i = 0; i < 3; i++) {
console.log(`NPM Java Parser (${benchmarkParser(npmparser.parse)})`);
console.log(
`Local Repository Java Parser (${benchmarkParser(currentparser.parse)})`
);
}
5 changes: 3 additions & 2 deletions benchmark/java-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.0",
"private": true,
"description": "Benchmark for comparing Parsers",
"main": "benchmark.js",
"type": "module",
"exports": "./benchmark.js",
"scripts": {
"start": "node benchmark.js"
},
Expand All @@ -18,6 +19,6 @@
},
"homepage": "https://github.com/jhipster/prettier-java#readme",
"devDependencies": {
"npm-install-version": "^6.0.2"
"java-parser-npm": "npm:java-parser@2.0.5"
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
]
},
"devDependencies": {
"benchmark": "^2.1.4",
"chai": "^4.3.7",
"eslint": "8.28.0",
"eslint-config-google": "0.14.0",
Expand All @@ -42,8 +41,8 @@
"lint-staged": "13.0.4",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"prettier": "2.8.0",
"sinon": "^9.2.1"
"prettier": "3.0.3",
"sinon": "^17.0.1"
},
"dependencies": {}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/java-parser/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ docs
samples
scripts
test
.mocharc.js
.mocharc.cjs
diagrams.html
*.log
3 changes: 2 additions & 1 deletion packages/java-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "java-parser",
"version": "2.1.0",
"description": "Java Parser in JavaScript",
"main": "src/index.js",
"type": "module",
"exports": "./src/index.js",
"repository": "https://github.com/jhipster/prettier-java/tree/main/packages/java-parser",
"license": "Apache-2.0",
"types": "./api.d.ts",
Expand Down
9 changes: 5 additions & 4 deletions packages/java-parser/scripts/clone-samples.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable no-console */
"use strict";
const cp = require("child_process");
const path = require("path");
const fs = require("fs-extra");
import cp from "child_process";
import path from "path";
import fs from "fs-extra";
import url from "url";

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

const sampleRepos = [
Expand Down
12 changes: 6 additions & 6 deletions packages/java-parser/scripts/gen-diagrams.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

/**
* A template for generating syntax diagrams html file.
* See: https://github.com/SAP/chevrotain/tree/master/diagrams for more details
Expand All @@ -10,10 +8,11 @@
* - open the "generated_diagrams.html" that will be created in this folder using
* your favorite browser.
*/
const path = require("path");
const fs = require("fs");
const chevrotain = require("chevrotain");
const JavaParser = require("../src/parser");
import path from "path";
import fs from "fs";
import chevrotain from "chevrotain";
import url from "url";
import JavaParser from "../src/parser.js";

// extract the serialized grammar.
const parserInstance = new JavaParser([]);
Expand All @@ -23,5 +22,6 @@ const serializedGrammar = parserInstance.getSerializedGastProductions();
const htmlText = chevrotain.createSyntaxDiagramsCode(serializedGrammar);

// Write the HTML file to disk
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const outPath = path.resolve(__dirname, "./");
fs.writeFileSync(outPath + "/../diagrams.html", htmlText);
10 changes: 4 additions & 6 deletions packages/java-parser/scripts/generate-signature.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable no-console */
"use strict";

const _ = require("lodash");
const path = require("path");
const fs = require("fs");
const JavaParser = require("../src/parser");
import _ from "lodash";
import path from "path";
import fs from "fs";
import JavaParser from "../src/parser";

const parseRule = rule => {
const children = {};
Expand Down
11 changes: 5 additions & 6 deletions packages/java-parser/scripts/parse-samples.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable no-console */
"use strict";
const klawSync = require("klaw-sync");
const path = require("path");
const fs = require("fs");
const javaParser = require("../src/index");
const _ = require("lodash");
import klawSync from "klaw-sync";
import path from "path";
import fs from "fs";
import javaParser from "../src/index";
import _ from "lodash";

const options = {
failFast: false,
Expand Down
4 changes: 1 addition & 3 deletions packages/java-parser/scripts/single-sample-runner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* This Script is used to debug the parsing of **small** code snippets.
*/
"use strict";

const javaParserChev = require("../src/index");
import javaParserChev from "../src/index";

const input = `
@Anno byte @Nullable ... test
Expand Down
15 changes: 8 additions & 7 deletions packages/java-parser/scripts/unicode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";
const unicode = {};
const fs = require("fs");
const path = require("path");
import fs from "fs";
import path from "path";
import url from "url";
const args = process.argv.slice(2)[0];

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

let categories; // Variable that stores every category we are going to parse
let restIdentCharCategories; // Variable that stores categories used for javaIdentfierPart
// Variable that stores authorized characters for javaIdentifierPart but that is not in a general category
Expand Down Expand Up @@ -160,7 +162,6 @@ function generateFile() {
* https://github.com/jhipster/prettier-java/issues/116
* https://github.com/jhipster/prettier-java/pull/155
*/
"use strict"
const addRanges = (set, rangesArr) => {
for (let i = 0; i < rangesArr.length; i++) {
const range = rangesArr[i];
Expand Down Expand Up @@ -221,9 +222,9 @@ function generateFile() {

data += `const ric = new Set(function*() { yield* fic; yield* ricd; }());`;

data += `module.exports = {
firstIdentChar: fic,
restIdentChar: ric
data += `export {
fic as firstIdentChar,
ric as restIdentChar
}`;
fs.writeFileSync(
path.resolve(__dirname, "../src/unicodesets.js"),
Expand Down
16 changes: 4 additions & 12 deletions packages/java-parser/src/comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const findLast = require("lodash/findLast");
import findLast from "lodash/findLast.js";

/**
* Search where is the position of the comment in the token array by
Expand Down Expand Up @@ -165,7 +163,7 @@ function shouldAttachTrailingComments(
* @param {{[startOffset: number]: CSTNode}} mostEnclosiveCstNodeByStartOffset
* @param {{[endOffset: number]: CSTNode}} mostEnclosiveCstNodeByEndOffset
*/
function attachComments(
export function attachComments(
tokens,
comments,
mostEnclosiveCstNodeByStartOffset,
Expand Down Expand Up @@ -256,7 +254,7 @@ function attachComments(
* @param comments
* @returns pairs of formatter:off and formatter:on
*/
function matchFormatterOffOnPairs(comments) {
export function matchFormatterOffOnPairs(comments) {
const onOffComments = comments.filter(comment =>
isFormatterOffOnComment(comment)
);
Expand Down Expand Up @@ -295,7 +293,7 @@ function matchFormatterOffOnPairs(comments) {
* @param node
* @param commentPairs
*/
function shouldNotFormat(node, commentPairs) {
export function shouldNotFormat(node, commentPairs) {
const matchingPair = findLast(
commentPairs,
comment => comment.off.endOffset < node.location.startOffset
Expand All @@ -308,9 +306,3 @@ function shouldNotFormat(node, commentPairs) {
node.ignore = true;
}
}

module.exports = {
matchFormatterOffOnPairs,
shouldNotFormat,
attachComments
};
Loading

0 comments on commit 11e859c

Please sign in to comment.