Skip to content

Commit

Permalink
feat: cache ESLint path. close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Feb 27, 2023
1 parent 8877234 commit 0f611cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
19 changes: 12 additions & 7 deletions src/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => { /**/ })
Expand Down Expand Up @@ -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<string>('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()
Expand Down
23 changes: 15 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,10 +17,14 @@ const lintingCache = new Map<FileName, Map<LineNumber, LineRules>>()

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
Expand All @@ -41,9 +44,7 @@ export async function activate(context: ExtensionContext) {
},
],
},
})

// linter = await getESLintLinterInstance()
}, context)

context.subscriptions.push(...disposes, statusBarItem.value)

Expand Down Expand Up @@ -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) == ' ') {
Expand Down Expand Up @@ -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: [
Expand All @@ -203,7 +207,8 @@ const disposes = [
},
],
},
})
}, extensionContext)
// 3.
lintingCache.clear()
log('Reloading finished.')
showStatusBarItem('$(check) Reloading finished.')
Expand All @@ -217,6 +222,8 @@ async function disable(silent: boolean, insert: (opts: {
selection: Selection,
lineRulesMap: NonNullable<ReturnType<(typeof lintingCache)['get']>>
}) => void) {
clearTimeout(reLintingTimer)

const activeTextEditor = window.activeTextEditor!

const fileName = activeTextEditor.document.fileName
Expand Down
1 change: 0 additions & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { workspace } from 'vscode'

export const workspacePath = workspace.workspaceFolders?.[0].uri.fsPath

0 comments on commit 0f611cb

Please sign in to comment.