Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
"*": "eslint --fix"
},
"devDependencies": {
"@pnpm/catalogs.config": "catalog:inline",
"@pnpm/catalogs.resolver": "catalog:inline",
"@types/node": "catalog:dev",
"@types/vscode": "1.101.0",
"@vida0905/eslint-config": "catalog:dev",
Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ catalogs:
typescript: ^5.9.3
vscode-ext-gen: ^1.5.1
inline:
'@pnpm/catalogs.config': ^1000.0.5
'@pnpm/catalogs.resolver': ^1000.0.5
fast-npm-meta: ^1.2.1
jsonc-parser: ^3.3.1
module-replacements: ^2.11.0
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export const NPMX_DEV = 'https://npmx.dev'
export const NPMX_DEV_API = `${NPMX_DEV}/api`

export const SPACER = ' '

export const CATALOG_DIAGNOSTIC_RELATED_INFO_PREFIX = 'catalog:'
47 changes: 37 additions & 10 deletions src/providers/code-actions/quick-fix.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import type { CodeActionContext, CodeActionProvider, Diagnostic, Range, TextDocument } from 'vscode'
import type { CodeActionContext, CodeActionProvider, Diagnostic, Range, TextDocument, Uri } from 'vscode'
import { CATALOG_DIAGNOSTIC_RELATED_INFO_PREFIX } from '#constants'
import { CodeAction, CodeActionKind, WorkspaceEdit } from 'vscode'

interface QuickFixRule {
pattern: RegExp
title: (target: string) => string
titleSuffix?: string
isPreferred?: boolean
}

function createReplaceAction(title: string, diagnostic: Diagnostic, uri: Uri, range: Range, target: string, isPreferred = false): CodeAction {
const action = new CodeAction(title, CodeActionKind.QuickFix)
action.isPreferred = isPreferred
action.diagnostics = [diagnostic]
action.edit = new WorkspaceEdit()
action.edit.replace(uri, range, target)
return action
}

const quickFixRules: Record<string, QuickFixRule> = {
upgrade: {
pattern: /^New version available: (?<target>\S+)$/,
title: (target) => `Update to ${target}`,
},
vulnerability: {
pattern: / Upgrade to (?<target>\S+) to fix\.$/,
title: (target) => `Update to ${target} to fix vulnerabilities`,
titleSuffix: ' to fix vulnerability',
isPreferred: true,
},
}
Expand Down Expand Up @@ -42,12 +51,30 @@ export class QuickFixProvider implements CodeActionProvider {
if (!target)
return []

const action = new CodeAction(rule.title(target), CodeActionKind.QuickFix)
action.isPreferred = rule.isPreferred ?? false
action.diagnostics = [diagnostic]
action.edit = new WorkspaceEdit()
action.edit.replace(document.uri, diagnostic.range, target)
return [action]
const {
titleSuffix = '',
} = rule

const relatedCatalog = diagnostic.relatedInformation?.find((i) => i.message.startsWith(CATALOG_DIAGNOSTIC_RELATED_INFO_PREFIX))

if (relatedCatalog) {
const openFix = new CodeAction('Open catalog entry in pnpm-workspace.yaml', CodeActionKind.QuickFix)
openFix.command = {
title: openFix.title,
command: 'vscode.open',
arguments: [relatedCatalog.location.uri, { selection: relatedCatalog.location.range, preview: false }],
}
openFix.diagnostics = [diagnostic]

const updateFix = createReplaceAction(`Update catalog entry to ${target}${titleSuffix}`, diagnostic, relatedCatalog.location.uri, relatedCatalog.location.range, target)

return [
openFix,
updateFix,
]
} else {
return [createReplaceAction(`Update to ${target}${titleSuffix}`, diagnostic, document.uri, diagnostic.range, target, rule.isPreferred)]
}
})
}
}
39 changes: 38 additions & 1 deletion src/providers/diagnostics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { Diagnostic, TextDocument } from 'vscode'
import { useActiveExtractor } from '#composables/active-extractor'
import { config, logger } from '#state'
import { getPackageInfo } from '#utils/api/package'
import { resolveCatalogDependency } from '#utils/catalog'
import { parseVersion } from '#utils/version'
import { debounce } from 'perfect-debounce'
import { computed, useActiveTextEditor, useDisposable, useDocumentText, watch } from 'reactive-vscode'
import { languages } from 'vscode'
Expand All @@ -26,6 +28,12 @@ export function useDiagnostics() {
const activeDocumentText = useDocumentText(() => activeEditor.value?.document)
const activeExtractor = useActiveExtractor()

const versionRules = new Set<DiagnosticRule>([
checkUpgrade,
checkDeprecation,
checkVulnerability,
])

const enabledRules = computed<DiagnosticRule[]>(() => {
const rules: DiagnosticRule[] = []
if (config.diagnostics.upgrade)
Expand Down Expand Up @@ -77,14 +85,43 @@ export function useDiagnostics() {
return

try {
const rawParsed = parseVersion(dep.version)
let depForRules = dep
let shouldSkipVersionRules = false

if (rawParsed?.protocol === 'catalog') {
const resolution = await resolveCatalogDependency({
documentUri: document.uri,
alias: dep.name,
bareSpecifier: dep.version,
})

if (resolution) {
depForRules = {
...dep,
resolvedVersion: resolution.resolvedSpecifier,
catalogResolution: {
catalogName: resolution.catalogName,
workspaceUri: resolution.workspaceUri,
entryLocation: resolution.entryLocation,
},
}
} else {
shouldSkipVersionRules = true
}
}

const pkg = await getPackageInfo(dep.name)
if (isDocumentChanged(document, targetUri, targetVersion))
return
if (!pkg)
continue

for (const rule of rules) {
const diagnostic = await rule(dep, pkg)
if (shouldSkipVersionRules && versionRules.has(rule))
continue

const diagnostic = await rule(depForRules, pkg)
if (isDocumentChanged(document, targetUri, targetVersion))
return
if (!diagnostic)
Expand Down
16 changes: 14 additions & 2 deletions src/providers/diagnostics/rules/upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import type { DependencyInfo } from '#types/extractor'
import type { ParsedVersion } from '#utils/version'
import type { DiagnosticRule, NodeDiagnosticInfo } from '..'
import { CATALOG_DIAGNOSTIC_RELATED_INFO_PREFIX } from '#constants'
import { formatVersion, getPrereleaseId, isSupportedProtocol, lt, parseVersion } from '#utils/version'
import { DiagnosticSeverity } from 'vscode'
import { DiagnosticRelatedInformation, DiagnosticSeverity } from 'vscode'

function createUpgradeDiagnostic(dep: DependencyInfo, parsed: ParsedVersion, upgradeVersion: string): NodeDiagnosticInfo {
const target = formatVersion({ ...parsed, semver: upgradeVersion })

const relatedInformation = dep.catalogResolution
? [
new DiagnosticRelatedInformation(
dep.catalogResolution.entryLocation,
`${CATALOG_DIAGNOSTIC_RELATED_INFO_PREFIX}${dep.catalogResolution.catalogName}`,
),
]
: undefined

return {
node: dep.versionNode,
severity: DiagnosticSeverity.Hint,
message: `New version available: ${target}`,
code: 'upgrade',
relatedInformation,
}
}

export const checkUpgrade: DiagnosticRule = (dep, pkg) => {
const parsed = parseVersion(dep.version)
const parsed = parseVersion(dep.resolvedVersion ?? dep.version)
if (!parsed || !isSupportedProtocol(parsed.protocol))
return

Expand Down
10 changes: 9 additions & 1 deletion src/types/extractor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import type { Node as JsonNode } from 'jsonc-parser'
import type { Range, TextDocument } from 'vscode'
import type { Location, Range, TextDocument, Uri } from 'vscode'
import type { Node as YamlNode } from 'yaml'

export type ValidNode = JsonNode | YamlNode

export interface CatalogResolutionInfo {
catalogName: string
workspaceUri: Uri
entryLocation: Location
}

export interface DependencyInfo<T extends ValidNode = any> {
nameNode: T
versionNode: T
name: string
version: string
resolvedVersion?: string
catalogResolution?: CatalogResolutionInfo
}

export interface Extractor<T extends ValidNode = any> {
Expand Down
Loading