From afc7bee0f04a7eb467dab49351bbb25e94bbd2f4 Mon Sep 17 00:00:00 2001 From: fuxichen <632951357@qq.com> Date: Wed, 25 Sep 2024 18:01:29 +0800 Subject: [PATCH] fix: eslintInstance may not be initialized when calling lintFiles in the worker (#40) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复 worker 模式下 eslintInstance 为undefind的问题 修复在worker模式下因异步问题导致经常报错 Cannot read properties of undefined (reading 'lintFiles') * fix: 调整实现方案 * chore: add comment --------- Co-authored-by: Rui Wu --- packages/core/src/worker.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/core/src/worker.ts b/packages/core/src/worker.ts index 94c8289..80cf919 100644 --- a/packages/core/src/worker.ts +++ b/packages/core/src/worker.ts @@ -22,14 +22,19 @@ let eslintInstance: ESLintInstance; let formatter: ESLintFormatter; let outputFixes: ESLintOutputFixes; +const initPromise = initializeESLint(options).then((result) => { + eslintInstance = result.eslintInstance; + formatter = result.formatter; + outputFixes = result.outputFixes; + 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 result = await initializeESLint(options); - eslintInstance = result.eslintInstance; - formatter = result.formatter; - outputFixes = result.outputFixes; + // remove this line will cause ts2454 + const { eslintInstance, formatter, outputFixes } = await initPromise; if (options.lintOnStart) { debug("Lint on start"); lintFiles({ @@ -43,6 +48,8 @@ let outputFixes: ESLintOutputFixes; })(); parentPort?.on("message", async (files) => { + // make sure eslintInstance is initialized + if (!eslintInstance) await initPromise; debug("==== message event ===="); debug(`message: ${files}`); const shouldIgnore = await shouldIgnoreModule(files, filter, eslintInstance);