Skip to content

Commit

Permalink
We dont have to restart the worker if we have code errors that do not…
Browse files Browse the repository at this point in the history
… allow to format.
  • Loading branch information
0x2aff committed Dec 4, 2023
1 parent 7b77cff commit 88a0cc0
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 80 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-prettier-eslint",
"displayName": "VSCode Prettier ESLint",
"description": "VSCode extension to format your JavaScript and TypeScript using prettier-eslint",
"version": "1.1.4",
"version": "1.1.5",
"publisher": "exceptionptr",
"author": "Stanislaw Schlosser <[email protected]>",
"icon": "icon.png",
Expand Down
10 changes: 0 additions & 10 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,12 @@ function fileNotInWorkspace(filePath, workspaceDirectory) {
*/
async function formatText(text, filePath) {
try {
const name = await prettierEslint.import(filePath);

if (!name) {
outputChannel.appendLine('No prettier-eslint found.');
throw new Error('No prettier-eslint found.');
}

const result = await prettierEslint
.callMethod(text, filePath);

return result;
} catch (/** @type { any } */ err) {
prettierEslint.reload();

outputChannel.appendLine(`Error: ${err.message} \n ${err.stack}`);
outputChannel.show();
}

return text;
Expand Down
35 changes: 0 additions & 35 deletions src/prettier-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import * as path from 'path';
import { Worker } from "worker_threads";

export class PrettierEslintVSCode {
/**
* @private
* @type {{ resolve: (name: string) => void; reject: (err: Error) => void; } | null}
*/
_importResolver = null;

/**
* @private
* @type {{ id: number; resolve: (result: unknown) => void; reject: (err: Error) => void; }[]}}
Expand Down Expand Up @@ -37,14 +31,6 @@ export class PrettierEslintVSCode {

this._worker.on('message', ({ type, payload }) => {
switch (type) {
case 'import':
if (payload.error)
this._importResolver?.reject(new Error(payload.error));
else
this._importResolver?.resolve(payload.name);

break;

case 'callMethod':
const callMethodResolver = this._callMethodResolvers.find(({ id }) => id === payload.id);
if (!callMethodResolver) break;
Expand All @@ -62,23 +48,6 @@ export class PrettierEslintVSCode {
});
}

/**
* @param {string} filePath
* @returns {Promise<string>}
*/
async import(filePath) {
const promise = new Promise((resolve, reject) => {
this._importResolver = { resolve, reject };
});

this._worker.postMessage({
type: 'import',
payload: { filePath, modulePath: this._modulePath },
});

return promise;
}

/**
* @param {string} text
* @param {string} filePath
Expand All @@ -98,8 +67,4 @@ export class PrettierEslintVSCode {

return promise;
}

reload() {
this._worker = new Worker(path.resolve(__dirname, 'worker.js'));
}
}
35 changes: 3 additions & 32 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ parentPort?.on('message', onParentPortMessage);
*/
function onParentPortMessage({ type, payload }) {
switch (type) {
case 'import':
onImport(payload);
break;

case 'callMethod':
onCallMethod(payload);
break;
Expand All @@ -25,8 +21,9 @@ function onParentPortMessage({ type, payload }) {
*
* @param {*} payload
*/
function onImport(payload) {
const { filePath, modulePath } = payload;
function onCallMethod(payload) {
const { modulePath, text, filePath, id } = payload;

try {
let instance = moduleCache.get(modulePath);

Expand All @@ -39,32 +36,6 @@ function onImport(payload) {
moduleCache.set(modulePath, instance);
}

parentPort?.postMessage({
type: 'import',
payload: { name: instance.name },
});
} catch (/** @type { any } */ err) {
parentPort?.postMessage({
type: 'import',
payload: { error: err.message },
});
}
}

/**
*
* @param {*} payload
*/
function onCallMethod(payload) {
const { modulePath, text, filePath, id } = payload;

try {
const instance = moduleCache.get(modulePath);

if (!instance) {
throw new Error(`No instance found for module path: ${modulePath}`);
}

const arg = { text, filePath };
const result = instance(arg);

Expand Down

0 comments on commit 88a0cc0

Please sign in to comment.