Skip to content

Commit 24e476b

Browse files
committed
Merge branch 'main' into fast-npm-meta
# Conflicts: # src/providers/diagnostics/rules/vulnerability.ts # src/providers/hover/npmx.ts
2 parents efd2196 + a9efcd0 commit 24e476b

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/extractors/package-json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const DEPENDENCY_SECTIONS = [
1414
]
1515

1616
export class PackageJsonExtractor implements Extractor<Node> {
17-
parse = createCachedParse(parseTree)
17+
parse = createCachedParse((text) => parseTree(text) ?? null)
1818

1919
getNodeRange(doc: TextDocument, node: Node) {
2020
const start = doc.positionAt(node.offset + 1)

src/providers/diagnostics/rules/vulnerability.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ export const checkVulnerability: DiagnosticRule = async (dep, pkg) => {
1818
if (!versionInfo)
1919
return
2020

21-
const { totalCounts } = await getVulnerability({ name: dep.name, version: exactVersion })
21+
const result = await getVulnerability({ name: dep.name, version: exactVersion })
22+
if (!result)
23+
return
2224

25+
const { totalCounts } = result
2326
const message: string[] = []
2427
let severity: DiagnosticSeverity | null = null
2528

src/utils/memoize.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ interface MemoizeEntry<V> {
1313
expiresAt?: number
1414
}
1515

16-
export function memoize<P, V>(fn: (params: P) => V, options: MemoizeOptions<P> = {}): (params: P) => V {
16+
type MemoizeReturn<R> = R extends Promise<infer V> ? Promise<V | undefined> : R | undefined
17+
18+
export function memoize<P, V>(fn: (params: P) => V, options: MemoizeOptions<P> = {}): (params: P) => MemoizeReturn<V> {
1719
const {
1820
getKey = String,
1921
ttl = CACHE_TTL_ONE_DAY,
2022
} = options
2123

2224
const cache = new Map<MemoizeKey, MemoizeEntry<V>>()
23-
const pending = new Map<MemoizeKey, V>()
25+
const pending = new Map<MemoizeKey, Promise<any>>()
2426

2527
function get(key: MemoizeKey): Awaited<V> | undefined {
2628
const entry = cache.get(key)
@@ -40,7 +42,7 @@ export function memoize<P, V>(fn: (params: P) => V, options: MemoizeOptions<P> =
4042
})
4143
}
4244

43-
return function cachedFn(params: P): V {
45+
return function cachedFn(params: P) {
4446
const key = getKey(params)
4547

4648
const hit = get(key)
@@ -62,10 +64,10 @@ export function memoize<P, V>(fn: (params: P) => V, options: MemoizeOptions<P> =
6264
.catch(() => cache.get(key)?.value)
6365
.finally(() => {
6466
pending.delete(key)
65-
}) as V
67+
}) as any
6668
pending.set(key, promise)
6769
return promise
68-
} else {
70+
} else if (result !== undefined) {
6971
set(key, result as Awaited<V>)
7072
return result
7173
}

src/utils/parse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Extractor, ValidNode } from '#types/extractor'
1+
import type { ValidNode } from '#types/extractor'
22
import type { TextDocument } from 'vscode'
33
import { createHash } from 'node:crypto'
44
import { memoize } from './memoize'
@@ -8,8 +8,8 @@ function computeHash(text: string) {
88
}
99

1010
export function createCachedParse<T extends ValidNode>(
11-
parse: (text: string) => ReturnType<Extractor<T>['parse']>,
12-
): Extractor<T>['parse'] {
11+
parse: (text: string) => T | null,
12+
) {
1313
return memoize(
1414
(doc: TextDocument) => parse(doc.getText()),
1515
{ getKey: (doc) => `${doc.uri}:${computeHash(doc.getText())}` },

0 commit comments

Comments
 (0)