From db631f9ed332ce4802313f49db2ffb5bb896c7ac Mon Sep 17 00:00:00 2001 From: zhangzhuang15 <2592004894@qq.com> Date: Sun, 13 Aug 2023 02:11:32 +0800 Subject: [PATCH 1/2] fix #27 --- src/extension.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/extension.js b/src/extension.js index f67fd9f..5bb13ec 100644 --- a/src/extension.js +++ b/src/extension.js @@ -12,6 +12,8 @@ import { isFilePathMatchedByEslintIgnore, isFilePathMatchedByPrettierIgnore } fr const path = require('path'); +const rawResolve = require.resolve; + let outputChannel; async function formatter(document) { @@ -33,15 +35,43 @@ async function formatter(document) { const text = document.getText(range); const extensionConfig = workspace?.getConfiguration('vs-code-prettier-eslint'); + + /** + * In some case, user cannot format his/her codes because of the error + * "cannot find module @typescript-eslint/parser". + * + * In order to solve this problem, firstly take a try to search + * "@typescript-eslint/parser" in node_modules of user's work directory. + * + * If find it successfully, intercept the `require.resolve` to redirect + * the resolved path. + */ + try { + const typescriptEslintParserPath = require.resolve('@typescript-eslint/parser', { + paths: [path.join(workspaceDir, 'temp.js')], + }); + + require.resolve = function resolve(...args) { + const [moduleName] = args; + if (moduleName && moduleName === '@typescript-eslint/parser') return typescriptEslintParserPath; + return rawResolve(...args); + }; + } catch (error) { + /** do nothing, just follow default action of require.resolve * */ + } + const formatted = await format({ text, filePath: document.fileName, extensionConfig, }); + return [TextEdit.replace(range, formatted)]; } } catch (err) { outputChannel.appendLine(`Error: ${err.message} \n${err.stack}`); + } finally { + require.resolve = rawResolve; } } From d5255f97432f5a1030fee3443b524e6d3261ac09 Mon Sep 17 00:00:00 2001 From: zhangzhuang15 <2592004894@qq.com> Date: Thu, 7 Dec 2023 18:54:00 +0800 Subject: [PATCH 2/2] improve intercept of require.resolve; --- src/extension.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/extension.js b/src/extension.js index 5bb13ec..4409328 100644 --- a/src/extension.js +++ b/src/extension.js @@ -37,28 +37,29 @@ async function formatter(document) { const extensionConfig = workspace?.getConfiguration('vs-code-prettier-eslint'); /** - * In some case, user cannot format his/her codes because of the error - * "cannot find module @typescript-eslint/parser". + * In some case, user cannot format his/her codes and the error + * is "cannot find module XXX". * - * In order to solve this problem, firstly take a try to search - * "@typescript-eslint/parser" in node_modules of user's work directory. + * The reason is easy.This project depends on `prettier-eslint` which + * uses require.resolve API to search a module path, unfortunately, it + * doesn't search a module path under "/node_modules" as + * expected. * - * If find it successfully, intercept the `require.resolve` to redirect - * the resolved path. + * To fix this bug, before we call `format`, we rewrite require.resolve, + * and take a try to search module path under "/node_modules" + * firstly.When `format` is finished, restore require.resolve. */ - try { - const typescriptEslintParserPath = require.resolve('@typescript-eslint/parser', { - paths: [path.join(workspaceDir, 'temp.js')], - }); - require.resolve = function resolve(...args) { - const [moduleName] = args; - if (moduleName && moduleName === '@typescript-eslint/parser') return typescriptEslintParserPath; - return rawResolve(...args); - }; - } catch (error) { - /** do nothing, just follow default action of require.resolve * */ - } + require.resolve = function resolve(...args) { + const [moduleName] = args; + let modulePath = ''; + try { + modulePath = rawResolve(moduleName, { paths: [workspaceDir] }); + } catch (err) { + modulePath = rawResolve(...args); + } + return modulePath; + }; const formatted = await format({ text,