diff --git a/src/eslint.ts b/src/eslint.ts index 65a13f5..52f5510 100644 --- a/src/eslint.ts +++ b/src/eslint.ts @@ -4,6 +4,7 @@ import { workspacePath } from './global' import { exec } from 'node:child_process' import { ESLint, Linter } from 'eslint' import path from 'node:path' +import { type ExtensionContext } from 'vscode' type PKG_MANAGERS = { agent: 'pnpm' | 'npm' | 'yarn', path: string } const resolveESLintPath = () => Files.resolve('eslint', workspacePath, workspacePath, message => { /**/ }) @@ -37,21 +38,25 @@ const resolveESLintPath = () => Files.resolve('eslint', workspacePath, workspace return Promise.reject('Fail to resolve global ESLint. Please install ESLint first.') }) -export const getESLintInstance = async (options?: ESLint.Options) => { - const eslintPath = 'E:\\Project\\@lvjiaxuan\\release\\node_modules\\.pnpm\\eslint@8.34.0\\node_modules\\eslint\\lib\\api.js'// await resolveESLintPath() - const eslintModule = await import(path.join(eslintPath)) as { - ESLint: typeof ESLint +export const getESLintInstance = async (options: ESLint.Options = {}, context: ExtensionContext) => { + + let eslintPath = context.workspaceState.get('eslintPath') + if (!eslintPath) { + eslintPath = await resolveESLintPath() + void context.workspaceState.update('eslintPath', eslintPath) + } else { + log('ESLint path found from storage.') } + const eslintModule = await import(path.join(eslintPath)) as { ESLint: typeof ESLint } + log(`ESLint library loaded from: ${ eslintPath }`) return new eslintModule.ESLint(options) } export const getESLintLinterInstance = async (options?: ESLint.Options) => { const eslintPath = await resolveESLintPath() - const eslintModule = await import(path.join(eslintPath)) as { - Linter: typeof Linter - } + const eslintModule = await import(path.join(eslintPath)) as { Linter: typeof Linter } log(`ESLint library loaded from: ${ eslintPath }`) return new eslintModule.Linter() diff --git a/src/extension.ts b/src/extension.ts index 0c30dcb..6f695b6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,14 +2,13 @@ import { type ExtensionContext, Position, Range, type Selection, SnippetString, import type { ESLint, Linter } from 'eslint' import { existFile, getTextBylines } from './utils' import { workspacePath } from './global' -import { getESLintInstance, getESLintLinterInstance } from './eslint' +import { getESLintInstance } from './eslint' import statusBarItem, { showStatusBarItem } from './statusBarItem' import log from './log' import config from './configuration' import path from 'node:path' let eslint: ESLint -// let linter: Linter type FileName = string type LineNumber = number @@ -18,10 +17,14 @@ const lintingCache = new Map>() let reLintingTimer: NodeJS.Timeout +let extensionContext: ExtensionContext + export async function activate(context: ExtensionContext) { const _startTime = Date.now() log('eslint-disable is activating!') + extensionContext = context + if (config.disable) { log('eslint-disable is disable.') return @@ -41,9 +44,7 @@ export async function activate(context: ExtensionContext) { }, ], }, - }) - - // linter = await getESLintLinterInstance() + }, context) context.subscriptions.push(...disposes, statusBarItem.value) @@ -87,8 +88,7 @@ const disposes = [ // Disable for lines commands.registerCommand('eslint-disable.disable', (silent = false) => { - clearTimeout(reLintingTimer) - + // keep void disable(silent as boolean, ({ text, activeTextEditor, selection, lineRulesMap }) => { let insertIndex = 0 while (text.charAt(insertIndex) == ' ') { @@ -194,6 +194,10 @@ const disposes = [ commands.registerCommand('eslint-disable.reload', async () => { log('Reloading eslint-disable.') showStatusBarItem('$(loading~spin) Reloading eslint-disable.', 0) + // 1. Clear storage + const storage = extensionContext.workspaceState + storage.keys().forEach(key => storage.update(key, null)) + // 2. Re-get ESLint eslint = await getESLintInstance({ overrideConfig: { overrides: [ @@ -203,7 +207,8 @@ const disposes = [ }, ], }, - }) + }, extensionContext) + // 3. lintingCache.clear() log('Reloading finished.') showStatusBarItem('$(check) Reloading finished.') @@ -217,6 +222,8 @@ async function disable(silent: boolean, insert: (opts: { selection: Selection, lineRulesMap: NonNullable> }) => void) { + clearTimeout(reLintingTimer) + const activeTextEditor = window.activeTextEditor! const fileName = activeTextEditor.document.fileName diff --git a/src/global.ts b/src/global.ts index a99b37d..07e98d1 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,4 +1,3 @@ import { workspace } from 'vscode' export const workspacePath = workspace.workspaceFolders?.[0].uri.fsPath -