-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathinputs.ts
More file actions
59 lines (58 loc) · 2.36 KB
/
Copy pathinputs.ts
File metadata and controls
59 lines (58 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import path from 'node:path'
import { finding } from '#design-diff/compare'
import { Resolver } from '#design-diff/resolve'
import type { SourceTree } from '#design-diff/source'
import type { Change, Config } from '#design-diff/types'
/** Content never qualifies; validate current file-loaded documentation only for coverage notes. */
export function fileLoadedInputDiagnostics(tree: SourceTree, config: Config): Change[] {
const diagnostics = new Map<string, Change>()
for (const input of config.fileInputs ?? []) {
if (!tree.entries.has(input.list) && !tree.entries.has(input.renderer)) continue
const emit = (file: string, reason: string) => {
if (diagnostics.has(file)) return
diagnostics.set(
file,
finding(undefined, {
key: file,
kind: 'review',
property: 'file-loaded-documentation',
value: tree.entries.get(file)?.oid ?? 'missing',
location: { file, line: 1, column: 1 },
symbol: input.export,
conditions: [{ renderer: input.renderer, list: input.list }],
dependencies: [file, input.list, input.renderer],
unresolved: [reason],
})
)
}
try {
const resolver = new Resolver(tree)
const exported = resolver.module(input.list).exports.get(input.export)
if (!exported || !tree.entries.has(input.renderer))
throw new Error('Configured renderer or export unavailable')
const evidence = resolver.evaluate(exported, input.list)
if (
evidence.unresolved.length ||
!Array.isArray(evidence.value) ||
!evidence.value.every((file) => typeof file === 'string')
)
throw new Error('Configured input list is not a static string array')
for (const name of evidence.value as string[]) {
const file = path.posix.normalize(path.posix.join(input.root, name))
if (!file.startsWith(`${input.root}/`) || !file.endsWith('.json'))
throw new Error('Unsupported configured input path')
try {
JSON.parse(tree.texts.get(file) ?? '')
} catch {
emit(
file,
'Configured documentation JSON is missing, malformed or exceeds the source budget'
)
}
}
} catch {
emit(input.list, 'Configured documentation list or renderer could not be resolved')
}
}
return [...diagnostics.values()]
}