From a3f4f56eb4b5d9a89a36e2f5eedcbbf8cc8a26ad Mon Sep 17 00:00:00 2001 From: fuhao <632951357@qq.com> Date: Tue, 24 Sep 2024 16:08:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20worker=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8B=20eslintInstance=20=E4=B8=BAundefind?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复在worker模式下因异步问题导致经常报错 Cannot read properties of undefined (reading 'lintFiles') --- packages/core/src/worker.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/worker.ts b/packages/core/src/worker.ts index 94c8289..88b3648 100644 --- a/packages/core/src/worker.ts +++ b/packages/core/src/worker.ts @@ -24,12 +24,17 @@ let outputFixes: ESLintOutputFixes; // this file needs to be compiled into cjs, which doesn't support top-level await // so we use iife here -(async () => { +let resolve: () => void; +const p = new Promise((r) => { + resolve = r; +}); +const init = async () => { debug("Initialize ESLint"); const result = await initializeESLint(options); eslintInstance = result.eslintInstance; formatter = result.formatter; outputFixes = result.outputFixes; + resolve(); if (options.lintOnStart) { debug("Lint on start"); lintFiles({ @@ -40,9 +45,11 @@ let outputFixes: ESLintOutputFixes; options, }); // don't use context } -})(); +}; +init(); parentPort?.on("message", async (files) => { + await p; debug("==== message event ===="); debug(`message: ${files}`); const shouldIgnore = await shouldIgnoreModule(files, filter, eslintInstance);