From 5d185c4ee713c0e3146ab0007665d7d78edeb18b Mon Sep 17 00:00:00 2001 From: rgehbt Date: Tue, 23 Jul 2024 02:38:41 +0800 Subject: [PATCH 1/4] feat: quickfix action add lock installed version and upgrade single package --- src/commands/keepInstalledVersion.ts | 13 ++++++ src/commands/runNpmScriptInTerminal.ts | 6 +++ src/diagnostic/index.ts | 62 ++++++++++++++++++++++++-- src/extension.ts | 4 +- src/utils/constants.ts | 1 + 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/commands/keepInstalledVersion.ts diff --git a/src/commands/keepInstalledVersion.ts b/src/commands/keepInstalledVersion.ts new file mode 100644 index 0000000..aea01da --- /dev/null +++ b/src/commands/keepInstalledVersion.ts @@ -0,0 +1,13 @@ +import vscode from 'vscode'; + +interface Args { + versionRange: vscode.Range; + installedVersion: string; +} +export async function keepInstalledVersion(arg: Args) { + const editor = vscode.window.activeTextEditor; + + editor?.edit((editBuilder) => { + editBuilder.replace(arg.versionRange, arg.installedVersion); + }); +} diff --git a/src/commands/runNpmScriptInTerminal.ts b/src/commands/runNpmScriptInTerminal.ts index b01f0a6..61c1add 100644 --- a/src/commands/runNpmScriptInTerminal.ts +++ b/src/commands/runNpmScriptInTerminal.ts @@ -12,6 +12,10 @@ type Args = | { command: string; cwd: string; + } + | { + packageNameWithVersion: string; + cwd: string; }; export async function runNpmScriptInTerminal(args: Args) { @@ -33,6 +37,8 @@ export async function runNpmScriptInTerminal(args: Args) { } if ('command' in args) { terminal.sendText(`${pm} ${args.command}`); + } else if ('packageNameWithVersion' in args) { + terminal.sendText(`${pm} install ${args.packageNameWithVersion}`); } else { terminal.sendText(`${pm} run ${args.scriptName}`); } diff --git a/src/diagnostic/index.ts b/src/diagnostic/index.ts index 148b0a7..9bbe0f7 100644 --- a/src/diagnostic/index.ts +++ b/src/diagnostic/index.ts @@ -130,8 +130,11 @@ export class DepsCheckCodeActionProvider implements CodeActionProvider { const pm = await detectPm(vscode.workspace.getWorkspaceFolder(document.uri)!.uri); - const action = new vscode.CodeAction(`Run ${pm} install`, vscode.CodeActionKind.QuickFix); - action.command = { + const runInstall = new vscode.CodeAction( + `Run ${pm} install`, + vscode.CodeActionKind.QuickFix, + ); + runInstall.command = { command: commands.runNpmScriptInTerminal, title: `Run ${pm} install`, arguments: [ @@ -141,7 +144,58 @@ export class DepsCheckCodeActionProvider implements CodeActionProvider { }, ], }; - action.diagnostics = diagnostics; - return [action]; + runInstall.diagnostics = diagnostics; + + const packageNameRegex = /"([\s\S]*?)"/g; + const packageData = document + .lineAt(_range.start.line) + .text.match(packageNameRegex)! + .map((p) => JSON.parse(p)) as [string, string]; + const packageName = packageData[0]; + const packageVersion = packageData[1]; + const runInstallSingle = new vscode.CodeAction( + `Upgrade package ${packageName} to ${packageVersion}`, + vscode.CodeActionKind.QuickFix, + ); + runInstallSingle.command = { + command: commands.runNpmScriptInTerminal, + title: `Upgrade package ${packageName} to ${packageVersion}`, + arguments: [ + { + packageNameWithVersion: `${packageName}@${packageVersion}`, + cwd: vscode.workspace.getWorkspaceFolder(document.uri)!.uri.fsPath, + }, + ], + }; + runInstallSingle.diagnostics = diagnostics; + + const packageInfo = await getPackageInfo(packageName, { + packageInstallDir: await findPkgInstallDir(packageName, document.uri.fsPath), + fetchBundleSize: false, + remoteFetch: false, + skipBuiltinModuleCheck: true, + }); + + const fallbackVersion: vscode.CodeAction[] = []; + if (!packageInfo?.isBuiltinModule) { + const _fallbackVersion = new vscode.CodeAction( + `package ${packageName} lock to ${packageInfo?.installedVersion}`, + vscode.CodeActionKind.QuickFix, + ); + _fallbackVersion.command = { + command: commands.keepInstalledVersion, + title: `package ${packageName} lock to ${packageInfo?.installedVersion}`, + arguments: [ + { + versionRange: _range, + installedVersion: `\"${packageInfo?.installedVersion}\"`, + }, + ], + }; + _fallbackVersion.diagnostics = diagnostics; + fallbackVersion.push(_fallbackVersion); + } + + return [runInstall, runInstallSingle, ...fallbackVersion]; } } diff --git a/src/extension.ts b/src/extension.ts index 2c017ae..7089bc8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -97,7 +97,9 @@ export function activate(context: vscode.ExtensionContext) { (mod.runNpmScriptInTerminal as any)(...args), ), ); - + registerCommand(commands.keepInstalledVersion, (arg) => + import('./commands/keepInstalledVersion').then((mod) => mod.keepInstalledVersion(arg)), + ); registerTextEditorCommand(commands.addMissingDeps, (editor) => import('./commands/addMissingDeps').then((mod) => mod.addMissingDeps(editor)), ); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index d7a2429..4754730 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -12,6 +12,7 @@ const commandsArray = [ 'upgradeVersion', 'findNpmPackage', 'findPathInNodeModules', + 'keepInstalledVersion', ] as const; type CommandsArrayUnion = (typeof commandsArray)[number]; From 41fd40bb49d11e8b0dbdd65af21ed7f5646e686e Mon Sep 17 00:00:00 2001 From: rgehbt Date: Wed, 24 Jul 2024 20:24:06 +0800 Subject: [PATCH 2/4] fix: amend by review --- src/commands/runNpmScriptInTerminal.ts | 10 ++-- src/diagnostic/index.ts | 73 ++++++++++++-------------- src/extension.ts | 2 + 3 files changed, 41 insertions(+), 44 deletions(-) diff --git a/src/commands/runNpmScriptInTerminal.ts b/src/commands/runNpmScriptInTerminal.ts index 61c1add..ce0148b 100644 --- a/src/commands/runNpmScriptInTerminal.ts +++ b/src/commands/runNpmScriptInTerminal.ts @@ -12,10 +12,6 @@ type Args = | { command: string; cwd: string; - } - | { - packageNameWithVersion: string; - cwd: string; }; export async function runNpmScriptInTerminal(args: Args) { @@ -23,6 +19,10 @@ export async function runNpmScriptInTerminal(args: Args) { Uri.file(resolve(args.cwd, 'package.json')), ); if (!workspaceFolder) return; + const document = vscode.window.activeTextEditor?.document; + if (document?.isDirty) { + await document.save(); + } const pm = await detectPm(workspaceFolder.uri); const terminalName = 'Run Script'; @@ -37,8 +37,6 @@ export async function runNpmScriptInTerminal(args: Args) { } if ('command' in args) { terminal.sendText(`${pm} ${args.command}`); - } else if ('packageNameWithVersion' in args) { - terminal.sendText(`${pm} install ${args.packageNameWithVersion}`); } else { terminal.sendText(`${pm} run ${args.scriptName}`); } diff --git a/src/diagnostic/index.ts b/src/diagnostic/index.ts index 9bbe0f7..8d9f3f1 100644 --- a/src/diagnostic/index.ts +++ b/src/diagnostic/index.ts @@ -93,6 +93,17 @@ export async function updateDiagnostic(document: vscode.TextDocument) { vscode.DiagnosticSeverity.Warning, ); diagnostic.code = 'package-manager-enhancer.unmetDependency'; + diagnostic.relatedInformation = [ + new vscode.DiagnosticRelatedInformation( + new vscode.Location(document.uri, range), + `${name}@${version}`, + ), + new vscode.DiagnosticRelatedInformation( + new vscode.Location(document.uri, range), + `${installedVersion}`, + ), + ]; + diagnostics.push(diagnostic); } }), @@ -131,7 +142,7 @@ export class DepsCheckCodeActionProvider implements CodeActionProvider { const pm = await detectPm(vscode.workspace.getWorkspaceFolder(document.uri)!.uri); const runInstall = new vscode.CodeAction( - `Run ${pm} install`, + `Run "${pm} install"`, vscode.CodeActionKind.QuickFix, ); runInstall.command = { @@ -146,56 +157,42 @@ export class DepsCheckCodeActionProvider implements CodeActionProvider { }; runInstall.diagnostics = diagnostics; - const packageNameRegex = /"([\s\S]*?)"/g; - const packageData = document - .lineAt(_range.start.line) - .text.match(packageNameRegex)! - .map((p) => JSON.parse(p)) as [string, string]; - const packageName = packageData[0]; - const packageVersion = packageData[1]; + const packageNameAndVersion = diagnostics[0].relatedInformation![0].message; + + const runInstallSingleTitle = `Run "${pm} install ${packageNameAndVersion}"`; const runInstallSingle = new vscode.CodeAction( - `Upgrade package ${packageName} to ${packageVersion}`, + runInstallSingleTitle, vscode.CodeActionKind.QuickFix, ); runInstallSingle.command = { command: commands.runNpmScriptInTerminal, - title: `Upgrade package ${packageName} to ${packageVersion}`, + title: runInstallSingleTitle, arguments: [ { - packageNameWithVersion: `${packageName}@${packageVersion}`, + command: `install ${packageNameAndVersion}`, cwd: vscode.workspace.getWorkspaceFolder(document.uri)!.uri.fsPath, }, ], }; runInstallSingle.diagnostics = diagnostics; - const packageInfo = await getPackageInfo(packageName, { - packageInstallDir: await findPkgInstallDir(packageName, document.uri.fsPath), - fetchBundleSize: false, - remoteFetch: false, - skipBuiltinModuleCheck: true, - }); + const installedVersion = diagnostics[0].relatedInformation![1].message; + const fallbackVersion = new vscode.CodeAction( + `Lock to ${installedVersion}`, + vscode.CodeActionKind.QuickFix, + ); + fallbackVersion.command = { + command: commands.upgradeVersion, + title: `Lock to ${installedVersion}`, + arguments: [ + { + versionRange: _range, + installedVersion: `\"${installedVersion}\"`, + }, + ], + }; + fallbackVersion.diagnostics = diagnostics; - const fallbackVersion: vscode.CodeAction[] = []; - if (!packageInfo?.isBuiltinModule) { - const _fallbackVersion = new vscode.CodeAction( - `package ${packageName} lock to ${packageInfo?.installedVersion}`, - vscode.CodeActionKind.QuickFix, - ); - _fallbackVersion.command = { - command: commands.keepInstalledVersion, - title: `package ${packageName} lock to ${packageInfo?.installedVersion}`, - arguments: [ - { - versionRange: _range, - installedVersion: `\"${packageInfo?.installedVersion}\"`, - }, - ], - }; - _fallbackVersion.diagnostics = diagnostics; - fallbackVersion.push(_fallbackVersion); - } - - return [runInstall, runInstallSingle, ...fallbackVersion]; + return [runInstall, runInstallSingle, fallbackVersion]; } } diff --git a/src/extension.ts b/src/extension.ts index 7089bc8..3cb3d85 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -97,9 +97,11 @@ export function activate(context: vscode.ExtensionContext) { (mod.runNpmScriptInTerminal as any)(...args), ), ); + registerCommand(commands.keepInstalledVersion, (arg) => import('./commands/keepInstalledVersion').then((mod) => mod.keepInstalledVersion(arg)), ); + registerTextEditorCommand(commands.addMissingDeps, (editor) => import('./commands/addMissingDeps').then((mod) => mod.addMissingDeps(editor)), ); From 33b3552c1c52f578903da87c9ea31c944b6f80a2 Mon Sep 17 00:00:00 2001 From: rgehbt Date: Wed, 24 Jul 2024 20:43:05 +0800 Subject: [PATCH 3/4] feat(diagnostic): fix the location of relatedInformation --- src/diagnostic/index.ts | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/diagnostic/index.ts b/src/diagnostic/index.ts index 8d9f3f1..e74d430 100644 --- a/src/diagnostic/index.ts +++ b/src/diagnostic/index.ts @@ -1,4 +1,5 @@ -import path from 'path'; +import fs from 'node:fs/promises'; +import path from 'node:path'; import type { ParsedJson } from 'jsonpos'; import semver from 'semver'; @@ -7,7 +8,7 @@ import vscode from 'vscode'; import { configuration } from '../configuration'; import { logger } from '../logger'; -import { commands } from '../utils/constants'; +import { commands, PACKAGE_JSON } from '../utils/constants'; import { findPkgInstallDir } from '../utils/pkg'; import { getPackageInfo } from '../utils/pkg-info'; import { detectPm } from '../utils/pm'; @@ -20,7 +21,7 @@ export async function updateDiagnostic(document: vscode.TextDocument) { if (!configuration.depsVersionCheck.enable) return; if ( - !path.basename(document.uri.fsPath).includes('package.json') || + !path.basename(document.uri.fsPath).includes(PACKAGE_JSON) || document.languageId !== 'json' ) return; @@ -64,9 +65,9 @@ export async function updateDiagnostic(document: vscode.TextDocument) { location.end.line - 1, location.end.column - 1, ); - + const packageInstallDir = await findPkgInstallDir(name, document.uri.fsPath); const packageInfo = await getPackageInfo(name, { - packageInstallDir: await findPkgInstallDir(name, document.uri.fsPath), + packageInstallDir, fetchBundleSize: false, remoteFetch: false, skipBuiltinModuleCheck: true, @@ -93,9 +94,34 @@ export async function updateDiagnostic(document: vscode.TextDocument) { vscode.DiagnosticSeverity.Warning, ); diagnostic.code = 'package-manager-enhancer.unmetDependency'; + + const targetUri = await fs + .realpath(path.resolve(packageInstallDir!, PACKAGE_JSON)) + .then((p) => vscode.Uri.file(p)); + let versionRange: vscode.Range | undefined; + try { + const installedParsed = getParsedByString( + await fs.readFile(targetUri.fsPath, 'utf8'), + ); + const versionLocation = getLocation(installedParsed, { + path: ['version'], + }); + if (!versionLocation.start || !versionLocation.end) return; + versionRange = new vscode.Range( + versionLocation.start.line - 1, + versionLocation.start.column - 1, + versionLocation.end.line - 1, + versionLocation.end.column - 1, + ); + } catch (error: any) { + logger.error(error); + } diagnostic.relatedInformation = [ new vscode.DiagnosticRelatedInformation( - new vscode.Location(document.uri, range), + new vscode.Location( + targetUri, + versionRange ?? new vscode.Range(0, 0, 0, 0), + ), `${name}@${version}`, ), new vscode.DiagnosticRelatedInformation( @@ -182,7 +208,7 @@ export class DepsCheckCodeActionProvider implements CodeActionProvider { vscode.CodeActionKind.QuickFix, ); fallbackVersion.command = { - command: commands.upgradeVersion, + command: commands.keepInstalledVersion, title: `Lock to ${installedVersion}`, arguments: [ { From 2e86316ddcb0d1df308d0df6ea0959c671f9a8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E8=85=BE=E9=9D=96?= Date: Thu, 25 Jul 2024 01:56:29 +0800 Subject: [PATCH 4/4] refactor: use custom data to store related infos --- src/commands/keepInstalledVersion.ts | 3 +- src/diagnostic/index.ts | 159 ++++++++++++++------------- src/typings/module.d.ts | 12 ++ 3 files changed, 98 insertions(+), 76 deletions(-) diff --git a/src/commands/keepInstalledVersion.ts b/src/commands/keepInstalledVersion.ts index aea01da..dae283f 100644 --- a/src/commands/keepInstalledVersion.ts +++ b/src/commands/keepInstalledVersion.ts @@ -4,10 +4,11 @@ interface Args { versionRange: vscode.Range; installedVersion: string; } + export async function keepInstalledVersion(arg: Args) { const editor = vscode.window.activeTextEditor; - editor?.edit((editBuilder) => { + return editor?.edit((editBuilder) => { editBuilder.replace(arg.versionRange, arg.installedVersion); }); } diff --git a/src/diagnostic/index.ts b/src/diagnostic/index.ts index e74d430..376e4e4 100644 --- a/src/diagnostic/index.ts +++ b/src/diagnostic/index.ts @@ -1,4 +1,3 @@ -import fs from 'node:fs/promises'; import path from 'node:path'; import type { ParsedJson } from 'jsonpos'; @@ -8,13 +7,18 @@ import vscode from 'vscode'; import { configuration } from '../configuration'; import { logger } from '../logger'; -import { commands, PACKAGE_JSON } from '../utils/constants'; +import { commands, EXT_NAME, PACKAGE_JSON } from '../utils/constants'; import { findPkgInstallDir } from '../utils/pkg'; import { getPackageInfo } from '../utils/pkg-info'; import { detectPm } from '../utils/pm'; +enum DepsCheckDiagnosticCode { + PACKAGE_NOT_FOUND = `${EXT_NAME}.packageNotFound`, + UNMET_DEPENDENCY = `${EXT_NAME}.unmetDependency`, +} + export const diagnosticCollection = vscode.languages.createDiagnosticCollection( - 'package-manager-enhancer:depsVersionCheck', + `${EXT_NAME}:depsVersionCheck`, ); export async function updateDiagnostic(document: vscode.TextDocument) { @@ -75,17 +79,23 @@ export async function updateDiagnostic(document: vscode.TextDocument) { if (packageInfo?.isBuiltinModule) return; + // not installed if (!packageInfo || !packageInfo.installedVersion) { const diagnostic = new vscode.Diagnostic( range, `Package "${name}" not installed`, vscode.DiagnosticSeverity.Warning, ); - diagnostic.code = 'package-manager-enhancer.packageNotFound'; + diagnostic.code = DepsCheckDiagnosticCode.PACKAGE_NOT_FOUND; + diagnostic.data = { + depName: name, + depDeclaredVersion: version, + }; diagnostics.push(diagnostic); return; } + // doesn't match declared version const { version: installedVersion } = packageInfo; if (semver.validRange(version) && !semver.satisfies(installedVersion, version)) { const diagnostic = new vscode.Diagnostic( @@ -93,42 +103,10 @@ export async function updateDiagnostic(document: vscode.TextDocument) { `Installed ${name} version "${installedVersion}" doesn't match declared version: "${version}"`, vscode.DiagnosticSeverity.Warning, ); - diagnostic.code = 'package-manager-enhancer.unmetDependency'; - - const targetUri = await fs - .realpath(path.resolve(packageInstallDir!, PACKAGE_JSON)) - .then((p) => vscode.Uri.file(p)); - let versionRange: vscode.Range | undefined; - try { - const installedParsed = getParsedByString( - await fs.readFile(targetUri.fsPath, 'utf8'), - ); - const versionLocation = getLocation(installedParsed, { - path: ['version'], - }); - if (!versionLocation.start || !versionLocation.end) return; - versionRange = new vscode.Range( - versionLocation.start.line - 1, - versionLocation.start.column - 1, - versionLocation.end.line - 1, - versionLocation.end.column - 1, - ); - } catch (error: any) { - logger.error(error); - } - diagnostic.relatedInformation = [ - new vscode.DiagnosticRelatedInformation( - new vscode.Location( - targetUri, - versionRange ?? new vscode.Range(0, 0, 0, 0), - ), - `${name}@${version}`, - ), - new vscode.DiagnosticRelatedInformation( - new vscode.Location(document.uri, range), - `${installedVersion}`, - ), - ]; + diagnostic.code = DepsCheckDiagnosticCode.UNMET_DEPENDENCY; + diagnostic.data = { + depInstalledVersion: installedVersion, + }; diagnostics.push(diagnostic); } @@ -150,75 +128,106 @@ export async function updateDiagnostic(document: vscode.TextDocument) { } export class DepsCheckCodeActionProvider implements CodeActionProvider { - async provideCodeActions( - document: vscode.TextDocument, - _range: vscode.Range | vscode.Selection, - _context: vscode.CodeActionContext, - _token: vscode.CancellationToken, - ): Promise { - const diagnostics = vscode.languages - .getDiagnostics(document.uri) - .filter( - (diagnostic) => - diagnostic.code === 'package-manager-enhancer.packageNotFound' || - diagnostic.code === 'package-manager-enhancer.unmetDependency', - ); - if (diagnostics.length === 0) return; - - const pm = await detectPm(vscode.workspace.getWorkspaceFolder(document.uri)!.uri); - - const runInstall = new vscode.CodeAction( - `Run "${pm} install"`, + private createNpmInstallAction(diagnostics: vscode.Diagnostic[], pm: string, cwd: string) { + const runInstallTitle = `Run "${pm} install"`; + const runInstallAction = new vscode.CodeAction( + runInstallTitle, vscode.CodeActionKind.QuickFix, ); - runInstall.command = { + runInstallAction.command = { command: commands.runNpmScriptInTerminal, - title: `Run ${pm} install`, + title: runInstallTitle, arguments: [ { command: 'install', - cwd: vscode.workspace.getWorkspaceFolder(document.uri)!.uri.fsPath, + cwd, }, ], }; - runInstall.diagnostics = diagnostics; - - const packageNameAndVersion = diagnostics[0].relatedInformation![0].message; + runInstallAction.diagnostics = diagnostics; + return runInstallAction; + } + private createNpmInstallSingleAction( + diagnostics: vscode.Diagnostic[], + pm: string, + cwd: string, + ) { + const { depName, depDeclaredVersion } = diagnostics[0].data!; + const packageNameAndVersion = `${depName}@${depDeclaredVersion}`; const runInstallSingleTitle = `Run "${pm} install ${packageNameAndVersion}"`; - const runInstallSingle = new vscode.CodeAction( + const runInstallSingleAction = new vscode.CodeAction( runInstallSingleTitle, vscode.CodeActionKind.QuickFix, ); - runInstallSingle.command = { + runInstallSingleAction.command = { command: commands.runNpmScriptInTerminal, title: runInstallSingleTitle, arguments: [ { command: `install ${packageNameAndVersion}`, - cwd: vscode.workspace.getWorkspaceFolder(document.uri)!.uri.fsPath, + cwd, }, ], }; - runInstallSingle.diagnostics = diagnostics; + runInstallSingleAction.diagnostics = diagnostics; + return runInstallSingleAction; + } - const installedVersion = diagnostics[0].relatedInformation![1].message; - const fallbackVersion = new vscode.CodeAction( + private createLockVersionAction(diagnostics: vscode.Diagnostic[], range: vscode.Range) { + const { depInstalledVersion: installedVersion } = diagnostics[0].data!; + const lockVersionActon = new vscode.CodeAction( `Lock to ${installedVersion}`, vscode.CodeActionKind.QuickFix, ); - fallbackVersion.command = { + lockVersionActon.command = { command: commands.keepInstalledVersion, title: `Lock to ${installedVersion}`, arguments: [ { - versionRange: _range, + versionRange: range, installedVersion: `\"${installedVersion}\"`, }, ], }; - fallbackVersion.diagnostics = diagnostics; + lockVersionActon.diagnostics = diagnostics; + return lockVersionActon; + } + + async provideCodeActions( + document: vscode.TextDocument, + range: vscode.Range | vscode.Selection, + _context: vscode.CodeActionContext, + _token: vscode.CancellationToken, + ): Promise { + const codeActions: vscode.CodeAction[] = []; + + const line = range.start.line; + const diagnostics = vscode.languages + .getDiagnostics(document.uri) + .filter( + (diagnostic) => + line === diagnostic.range.start.line && + (diagnostic.code === DepsCheckDiagnosticCode.PACKAGE_NOT_FOUND || + diagnostic.code === DepsCheckDiagnosticCode.UNMET_DEPENDENCY), + ); + if (diagnostics.length === 0) return; + + const pm = await detectPm(vscode.workspace.getWorkspaceFolder(document.uri)!.uri); + const cwd = vscode.workspace.getWorkspaceFolder(document.uri)!.uri.fsPath; + + codeActions.push( + this.createNpmInstallAction(diagnostics, pm, cwd), + this.createNpmInstallSingleAction(diagnostics, pm, cwd), + ); + + const unmetDepDiagnostics = diagnostics.filter( + (diagnostic) => diagnostic.code === DepsCheckDiagnosticCode.UNMET_DEPENDENCY, + ); + if (unmetDepDiagnostics.length > 0) { + codeActions.push(this.createLockVersionAction(unmetDepDiagnostics, range)); + } - return [runInstall, runInstallSingle, fallbackVersion]; + return codeActions; } } diff --git a/src/typings/module.d.ts b/src/typings/module.d.ts index 824e4e4..d633b31 100644 --- a/src/typings/module.d.ts +++ b/src/typings/module.d.ts @@ -1,3 +1,15 @@ declare module '@npmcli/config/lib/definitions' { export const definitions: Record; } + +import * as vscode from 'vscode'; + +declare module 'vscode' { + export interface Diagnostic { + data?: { + depName?: string; + depDeclaredVersion?: string; + depInstalledVersion?: string; + }; + } +}