-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathgroup.ts
More file actions
177 lines (171 loc) · 6.63 KB
/
Copy pathgroup.ts
File metadata and controls
177 lines (171 loc) · 6.63 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { createHash } from 'node:crypto'
import { finding } from '#design-diff/compare'
import type { SourceTree } from '#design-diff/source'
import type { Change, Finding, UsageCount } from '#design-diff/types'
function fileOf(change: Change) {
return (change.after ?? change.before)?.location.file ?? ''
}
function changedValue(change: Change) {
return (
JSON.stringify([change.before?.value, change.before?.conditions]) !==
JSON.stringify([change.after?.value, change.after?.conditions])
)
}
function compare(a: Change, b: Change) {
return (
Number(a.decision === 'exempt') - Number(b.decision === 'exempt') ||
Number(!changedValue(a)) - Number(!changedValue(b)) ||
a.limitations.length - b.limitations.length ||
fileOf(a).localeCompare(fileOf(b), 'en') ||
((a.after ?? a.before)?.location.line ?? 0) - ((b.after ?? b.before)?.location.line ?? 0) ||
a.id.localeCompare(b.id, 'en')
)
}
function usage(tree: SourceTree, file: string, symbols?: Set<string>): UsageCount {
const references = tree.graph.usages(file, symbols)
return {
coverage: 'partial',
referenceCount: references.length,
fileCount: new Set(references.map((reference) => reference.location.file)).size,
references,
}
}
/** Attribute only changed sources reached by this definition's own dependency evidence. */
export function causalSources(
change: Change,
causes: Map<string, Set<string>>,
renames: Map<string, string>
): string[] {
const currentPath = (file: string) => [...renames].find(([, old]) => old === file)?.[0] ?? file
const file = fileOf(change)
return [...new Set([...(causes.get(file) ?? new Set([file]))].map(currentPath))].filter(
(root) => root === currentPath(file) || change.dependencies.map(currentPath).includes(root)
)
}
/** One finding per changed source, with direct evidence and one representative consumer. */
export function groupFindings(
changes: Change[],
causes: Map<string, Set<string>>,
before: SourceTree,
after: SourceTree,
renames: Map<string, string>
): Finding[] {
const currentPath = (file: string) => [...renames].find(([, old]) => old === file)?.[0] ?? file
const groups = new Map<string, Change[]>()
for (const change of changes) {
for (const root of causalSources(change, causes, renames)) {
const entries = groups.get(root) ?? []
entries.push(change)
groups.set(root, entries)
}
}
const result: Finding[] = []
for (const [file, entries] of groups) {
const oldFile = renames.get(file) ?? file
const all = [...new Map(entries.map((entry) => [entry.id, entry])).values()].sort(compare)
const direct = all.filter((entry) => [file, oldFile].includes(fileOf(entry)))
const indirect = all.filter((entry) => ![file, oldFile].includes(fileOf(entry)))
const primary = direct[0] ?? indirect[0]
if (!primary) continue
let example = indirect[0]
const previous = before.entries.get(oldFile)
const next = after.entries.get(file)
const symbols = before.graph.changedSymbols(oldFile, after.graph, file)
const impact = {
basis: 'resolved-static-references' as const,
before: usage(before, oldFile, symbols),
after: usage(after, file, symbols),
}
if (!example && impact.after.references.length && direct.length) {
const reference = impact.after.references.find((reference) => reference.kind === 'jsx')
if (reference) {
const definition = (blob: string | undefined) => ({
key: 'reference',
kind: 'review' as const,
property: 'component-reference',
value: { source: file, blob: blob ?? null },
location: reference.location,
symbol: reference.symbol,
conditions: [],
dependencies: [file, reference.location.file],
unresolved: ['Resolved component reference; its runtime appearance is not established'],
})
example = finding(definition(previous?.oid), definition(next?.oid))
}
}
const limitations = new Set([
...primary.limitations,
...direct.flatMap((entry) => entry.limitations),
])
if (example) for (const limitation of example.limitations) limitations.add(limitation)
const ambiguousExample =
example && new Set([...(causes.get(fileOf(example)) ?? [])].map(currentPath)).size > 1
if (ambiguousExample)
limitations.add(
'The representative consumer depends on multiple changed sources; its visual effect cannot be attributed to this source alone'
)
for (const tree of [before, after]) {
for (const entry of all)
for (const limitation of tree.graph.limitations.get(fileOf(entry)) ?? [])
limitations.add(limitation)
}
if (impact.before.referenceCount || impact.after.referenceCount)
limitations.add(
'Usage counts are resolved source references, not confirmed visual changes; overrides, inactive variants and runtime conditions can prevent an effect'
)
const { id: _primaryId, ...representative } = primary
const value: Omit<Finding, 'id'> = {
...representative,
decision: all.some((entry) => entry.decision === 'flag') ? 'flag' : 'exempt',
reason:
primary.decision === 'exempt' && all.some((entry) => entry.decision === 'flag')
? 'Potential downstream visual effect; static evidence is incomplete'
: primary.reason,
source: {
before: previous ? { file: oldFile, blob: previous.oid } : null,
after: next ? { file, blob: next.oid } : null,
},
categories: [...new Set(all.map((entry) => entry.category))].sort(),
changes: direct,
example: example
? {
basis:
changedValue(example) &&
!ambiguousExample &&
example.after?.property !== 'component-reference'
? 'changed-definition'
: 'potential-consumer',
change: example,
}
: null,
impact,
consumers: [
...new Set(
[...impact.before.references, ...impact.after.references].map(
(reference) => reference.location.file
)
),
].sort(),
dependencies: [
...new Set([
file,
oldFile,
...direct.flatMap((entry) => entry.dependencies),
...(example?.dependencies ?? []),
]),
].sort(),
limitations: [...limitations].sort(),
}
result.push({
...value,
id: createHash('sha256').update(JSON.stringify(value)).digest('hex').slice(0, 24),
})
}
return result.sort(
(a, b) =>
(a.source.after ?? a.source.before)?.file.localeCompare(
(b.source.after ?? b.source.before)?.file ?? '',
'en'
) ?? 0
)
}