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 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20worker=20?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=20eslintInstance=20=E4=B8=BAundefin?= =?UTF-8?q?d=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); From ecf971b01303f3961f716c4cdba16d230907d9f9 Mon Sep 17 00:00:00 2001 From: fuhao <632951357@qq.com> Date: Wed, 25 Sep 2024 15:31:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/worker.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/core/src/worker.ts b/packages/core/src/worker.ts index 88b3648..9541885 100644 --- a/packages/core/src/worker.ts +++ b/packages/core/src/worker.ts @@ -22,19 +22,18 @@ let eslintInstance: ESLintInstance; let formatter: ESLintFormatter; let outputFixes: ESLintOutputFixes; -// this file needs to be compiled into cjs, which doesn't support top-level await -// so we use iife here -let resolve: () => void; -const p = new Promise((r) => { - resolve = r; -}); -const init = async () => { - debug("Initialize ESLint"); - const result = await initializeESLint(options); +const initPromise = initializeESLint(options).then((result) => { eslintInstance = result.eslintInstance; formatter = result.formatter; outputFixes = result.outputFixes; - resolve(); + return result; +}); + +// this file needs to be compiled into cjs, which doesn't support top-level await +// so we use iife here +(async () => { + debug("Initialize ESLint"); + const { eslintInstance, formatter, outputFixes } = await initPromise; if (options.lintOnStart) { debug("Lint on start"); lintFiles({ @@ -45,11 +44,10 @@ const init = async () => { options, }); // don't use context } -}; -init(); +})(); parentPort?.on("message", async (files) => { - await p; + if (!eslintInstance) await initPromise; debug("==== message event ===="); debug(`message: ${files}`); const shouldIgnore = await shouldIgnoreModule(files, filter, eslintInstance); From cdca14b30eeb375257812396592d158353560ef8 Mon Sep 17 00:00:00 2001 From: Rui Wu Date: Wed, 25 Sep 2024 17:58:48 +0800 Subject: [PATCH 3/3] chore: add comment --- packages/core/src/worker.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/worker.ts b/packages/core/src/worker.ts index 9541885..80cf919 100644 --- a/packages/core/src/worker.ts +++ b/packages/core/src/worker.ts @@ -33,6 +33,7 @@ const initPromise = initializeESLint(options).then((result) => { // so we use iife here (async () => { debug("Initialize ESLint"); + // remove this line will cause ts2454 const { eslintInstance, formatter, outputFixes } = await initPromise; if (options.lintOnStart) { debug("Lint on start"); @@ -47,6 +48,7 @@ const initPromise = initializeESLint(options).then((result) => { })(); parentPort?.on("message", async (files) => { + // make sure eslintInstance is initialized if (!eslintInstance) await initPromise; debug("==== message event ===="); debug(`message: ${files}`);