Skip to content

Commit

Permalink
node-syntax-tree and synckit
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Aug 10, 2022
1 parent 15619d5 commit f1d1937
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
},
"homepage": "https://github.com/prettier/plugin-ruby#readme",
"dependencies": {
"prettier": ">=2.3.0"
"node-syntax-tree": "^0.2.0",
"prettier": ">=2.3.0",
"synckit": "^0.8.1"
},
"devDependencies": {
"eslint": "^8.15.0",
Expand Down
8 changes: 7 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const { parseSync } = require("./parseSync");
const { createSyncFn } = require("synckit");
const parseSyncFn = createSyncFn(require.resolve("./worker"));

function parseSync(kind, source, options) {
const { locStart, locEnd, plugins, preprocess, printer, ...cloneableOptions } = options;
return parseSyncFn(kind, source, cloneableOptions);
}

/*
* metadata mostly pulled from linguist and rubocop:
Expand Down
21 changes: 21 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { runAsWorker } = require("synckit/lib/index.cjs");
const { default: createSyntaxTree } = require("node-syntax-tree");

let cachedSyntaxTree = null;

function fetchSyntaxTree() {
if (cachedSyntaxTree == null) {
return createSyntaxTree().then((syntaxTree) => {
cachedSyntaxTree = syntaxTree;
return cachedSyntaxTree;
});
} else {
return Promise.resolve(cachedSyntaxTree);
}
}

runAsWorker((kind, source, options) => {
return fetchSyntaxTree().then((syntaxTree) => {
return syntaxTree.handlers[kind].format(source);
});
});

0 comments on commit f1d1937

Please sign in to comment.