Repository navigation
Expand file tree
/
Copy pathastdiff.go
More file actions
652 lines (601 loc) · 20.9 KB
/
Copy pathastdiff.go
File metadata and controls
652 lines (601 loc) · 20.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package analyzer
import (
"goodchanges/internal/log"
"strings"
"goodchanges/internal/tsparse"
"goodchanges/tsgo-vendor/pkg/ast"
"goodchanges/tsgo-vendor/pkg/scanner"
)
// sideEffectTaint is a sentinel taint token marking that a file has an *import-time
// side effect* change (a changed top-level side-effect statement, or a bare
// `import "x"` side-effect import) — as opposed to an ordinary whole-file "*" taint
// (e.g. a new file). It propagates through import/re-export edges so a barrel that
// re-exports a side-effectful module becomes side-effectful itself. It is not a
// valid JS identifier, so it can never collide with or be matched as a real symbol.
const sideEffectTaint = "__side-effect__"
// findAffectedSymbolsByASTDiff compares OLD and NEW file ASTs to find which symbols changed.
// Returns symbol names that have runtime changes (or type-only changes if includeTypes is true).
//
// For each symbol in the NEW file:
// - If it didn't exist in the OLD file → new symbol, affected
// - If it existed → compare body text. If different, check if change is type-only.
//
// Type-only classification:
// - interface/type declarations → always type-only
// - function/class/variable/enum → extract runtime-only text (strip type annotations,
// as/satisfies expressions), compare. If runtime texts match → type-only change.
func findAffectedSymbolsByASTDiff(oldAnalysis *tsparse.FileAnalysis, newAnalysis *tsparse.FileAnalysis, oldContent string, includeTypes bool) []string {
if newAnalysis == nil || newAnalysis.SourceFile == nil {
return nil
}
newText := newAnalysis.SourceFile.Text()
// Build map of old symbol name → body text
oldSymbolTexts := make(map[string]string)
oldSymbolRuntimeTexts := make(map[string]string)
if oldAnalysis != nil && oldAnalysis.SourceFile != nil {
oldText := oldAnalysis.SourceFile.Text()
oldLineMap := oldAnalysis.SourceFile.ECMALineMap()
for _, sym := range oldAnalysis.Symbols {
body := tsparse.ExtractTextForLines(oldText, oldLineMap, sym.StartLine, sym.EndLine)
oldSymbolTexts[sym.Name] = normalizeWhitespace(body)
}
// Also extract runtime-only texts for old symbols using AST
oldStmtMap := buildStmtMap(oldAnalysis.SourceFile)
for _, sym := range oldAnalysis.Symbols {
if sym.IsTypeOnly {
continue
}
if stmt, ok := oldStmtMap[sym.Name]; ok {
oldSymbolRuntimeTexts[sym.Name] = extractRuntimeText(stmt, oldText)
}
}
}
// Compare each new symbol against old
newLineMap := newAnalysis.SourceFile.ECMALineMap()
newStmtMap := buildStmtMap(newAnalysis.SourceFile)
var affected []string
for _, sym := range newAnalysis.Symbols {
newBody := tsparse.ExtractTextForLines(newText, newLineMap, sym.StartLine, sym.EndLine)
newBodyNorm := normalizeWhitespace(newBody)
oldBodyNorm, existedBefore := oldSymbolTexts[sym.Name]
if !existedBefore {
// New symbol — it's affected
if sym.IsTypeOnly && !includeTypes {
log.Debugf(" %s: NEW type-only symbol (skipped, includeTypes=false)", sym.Name)
continue
}
log.Debugf(" %s: NEW symbol", sym.Name)
affected = append(affected, sym.Name)
continue
}
if newBodyNorm == oldBodyNorm {
// Identical — not affected
continue
}
// Symbol text changed. Determine if it's type-only or runtime.
if sym.IsTypeOnly {
// interface/type alias — always type-only
if includeTypes {
log.Debugf(" %s: type-only change (interface/type)", sym.Name)
affected = append(affected, sym.Name)
}
continue
}
// Runtime symbol changed — check if the change is type-only
// by comparing runtime-stripped texts
oldRuntime := oldSymbolRuntimeTexts[sym.Name]
newRuntime := ""
if stmt, ok := newStmtMap[sym.Name]; ok {
newRuntime = extractRuntimeText(stmt, newText)
}
if oldRuntime != "" && newRuntime != "" && oldRuntime == newRuntime {
// Only type annotations changed (e.g. `x = foo` → `x = foo as Bar`)
if includeTypes {
log.Debugf(" %s: type-only change (runtime text identical)", sym.Name)
affected = append(affected, sym.Name)
}
continue
}
// Runtime change
log.Debugf(" %s: RUNTIME change", sym.Name)
affected = append(affected, sym.Name)
}
// Deleted symbols (in old but not in new) are themselves a change: removing an
// export can break whoever imported it, so record the deleted names and let
// them propagate to importers. This keeps detection precise — a removed
// *unused* export taints nobody, instead of the empty-diff falling through to a
// whole-file taint downstream. Type-only deletions are ignored unless includeTypes.
var deleted []string
if oldAnalysis != nil {
newSymbolNames := make(map[string]bool)
for _, sym := range newAnalysis.Symbols {
newSymbolNames[sym.Name] = true
}
for _, sym := range oldAnalysis.Symbols {
if newSymbolNames[sym.Name] {
continue
}
if sym.IsTypeOnly && !includeTypes {
log.Debugf(" %s: DELETED type-only symbol (skipped, includeTypes=false)", sym.Name)
continue
}
log.Debugf(" %s: DELETED symbol", sym.Name)
deleted = append(deleted, sym.Name)
}
}
// Re-pointed value imports: a local binding keeps its name but now resolves to
// a different module/export (`import { x } from "./a"` → "./b", or
// `{ a as x }` → `{ b as x }`). The usages don't change textually, so the
// symbol diff above misses them — taint the symbols that use the re-pointed
// binding. (Type-only imports are excluded unless includeTypes.)
if repointed := repointedImportBindings(oldAnalysis, newAnalysis, includeTypes); len(repointed) > 0 {
log.Debugf(" re-pointed import bindings: %v", repointed)
affected = append(affected, findTaintedSymbolsByUsage(newAnalysis, repointed)...)
}
// Intra-file propagation: if symbol A changed and symbol B references A,
// then B is also affected. E.g. `UiPagedVirtualListNotWrapped` changed,
// `UiPagedVirtualList = memo(UiPagedVirtualListNotWrapped)` is also affected.
if len(affected) > 0 && newAnalysis.SourceFile != nil {
affectedSet := make(map[string]bool)
affectedTypeOnly := make(map[string]bool)
for _, name := range affected {
affectedSet[name] = true
// Look up if this symbol is type-only
for _, sym := range newAnalysis.Symbols {
if sym.Name == name {
affectedTypeOnly[name] = sym.IsTypeOnly
break
}
}
}
// Build intra-file reference graph
dependsOn := make(map[string]map[string]bool)
for _, sym := range newAnalysis.Symbols {
bodyText := tsparse.ExtractTextForLines(newText, newLineMap, sym.StartLine, sym.EndLine)
deps := make(map[string]bool)
for _, other := range newAnalysis.Symbols {
if other.Name != sym.Name && containsIdentifier(bodyText, other.Name) {
deps[other.Name] = true
}
}
dependsOn[sym.Name] = deps
}
// Propagate until stable
changed := true
for changed {
changed = false
for _, sym := range newAnalysis.Symbols {
if affectedSet[sym.Name] {
continue
}
for dep := range dependsOn[sym.Name] {
if !affectedSet[dep] {
continue
}
// Type-only changes don't propagate to runtime symbols
if affectedTypeOnly[dep] && !sym.IsTypeOnly {
continue
}
affectedSet[sym.Name] = true
affectedTypeOnly[sym.Name] = sym.IsTypeOnly
changed = true
log.Debugf(" %s: affected via intra-file dep on %s", sym.Name, dep)
break
}
}
}
// Rebuild affected list with propagated symbols
affected = nil
for _, sym := range newAnalysis.Symbols {
if !affectedSet[sym.Name] {
continue
}
if affectedTypeOnly[sym.Name] && !includeTypes {
continue
}
affected = append(affected, sym.Name)
}
}
// Removed symbols propagate to whoever imported them. Appended after the
// intra-file rebuild (which only walks NEW symbols and would drop them) and
// before the fallback below, so a change that is purely a deletion is carried
// by these names rather than misrouted into a whole-file side-effect taint.
affected = append(affected, deleted...)
// Import-time side effects are checked INDEPENDENTLY of symbol-level changes:
// one diff can both edit an exported symbol and add/remove a top-level
// side-effect statement (console.log, describe(...)) or a bare `import "x"`.
// Gating this behind len(affected) == 0 dropped the "*"/sideEffectTaint in the
// mixed case, cutting off importers of other symbols and the transitive
// side-effect propagation. The checks compare only top-level side-effect
// statement text / bare-import sets, so a pure declaration edit does not
// trigger them — comment / formatting / type-only / import-reordering changes
// still fall through untainted.
if oldAnalysis != nil {
if hasSideEffectStmtChanges(oldAnalysis.SourceFile, newAnalysis.SourceFile) ||
bareImportsChanged(oldAnalysis, newAnalysis) {
log.Debugf(" file changed with import-time side effects — tainting all symbols")
// Use "*" wildcard to mark all exports as affected, plus the
// sideEffectTaint sentinel so the *import-time* nature propagates
// through import/re-export edges (a barrel importing this becomes
// side-effectful too).
affected = append(affected, "*", sideEffectTaint)
for _, sym := range newAnalysis.Symbols {
if sym.IsTypeOnly && !includeTypes {
continue
}
affected = append(affected, sym.Name)
}
} else if len(affected) == 0 {
oldText := ""
if oldAnalysis.SourceFile != nil {
oldText = oldAnalysis.SourceFile.Text()
}
if normalizeWhitespace(oldText) != normalizeWhitespace(newText) {
log.Debugf(" file changed but no symbols affected (comments/imports only)")
}
}
}
return affected
}
// buildStmtMap maps symbol names to their AST statement nodes.
func buildStmtMap(sf *ast.SourceFile) map[string]*ast.Node {
result := make(map[string]*ast.Node)
for _, stmt := range sf.Statements.Nodes {
switch stmt.Kind {
case ast.KindFunctionDeclaration, ast.KindClassDeclaration,
ast.KindInterfaceDeclaration, ast.KindTypeAliasDeclaration,
ast.KindEnumDeclaration:
name := stmt.Name()
if name != nil && ast.IsIdentifier(name) {
result[name.Text()] = stmt
}
case ast.KindVariableStatement:
vs := stmt.AsVariableStatement()
if vs.DeclarationList != nil {
dl := vs.DeclarationList.AsVariableDeclarationList()
if dl.Declarations != nil {
for _, decl := range dl.Declarations.Nodes {
name := decl.Name()
if name != nil && ast.IsIdentifier(name) {
result[name.Text()] = stmt
}
}
}
}
}
}
return result
}
// extractRuntimeText walks an AST statement and produces a text representation
// with all type-only constructs removed. This allows comparing whether two versions
// of a symbol differ only in type annotations.
//
// Type constructs stripped:
// - Type annotations on variables (`: Type`)
// - Return type annotations on functions
// - Type parameters (`<T extends Foo>`)
// - `as Type` expressions (keep the expression, strip the cast)
// - `satisfies Type` expressions (keep the expression, strip the check)
// - `<Type>expr` type assertions (keep the expression)
func extractRuntimeText(stmt *ast.Node, sourceText string) string {
// Collect all type-only ranges within this statement
typeRanges := collectTypeOnlyRanges(stmt)
// Extract the statement's full text
stmtStart := stmt.Pos()
stmtEnd := stmt.End()
if stmtStart < 0 || stmtEnd > len(sourceText) {
return ""
}
fullText := sourceText[stmtStart:stmtEnd]
// Strip the type ranges (adjust positions relative to statement start)
return normalizeWhitespace(stripRanges(fullText, typeRanges, stmtStart))
}
// collectTypeOnlyRanges walks the AST node tree and collects [start, end) positions
// of all type-only constructs.
func collectTypeOnlyRanges(node *ast.Node) [][2]int {
var ranges [][2]int
var walk func(n *ast.Node)
walk = func(n *ast.Node) {
if n == nil {
return
}
switch n.Kind {
case ast.KindAsExpression:
// `expr as Type` — keep expr, strip ` as Type`
ae := n.AsAsExpression()
if ae.Expression != nil && ae.Type != nil {
ranges = append(ranges, [2]int{ae.Expression.End(), n.End()})
}
// Recurse into the expression (it might contain more type casts)
if ae.Expression != nil {
walk(ae.Expression)
}
return // don't recurse into Type
case ast.KindSatisfiesExpression:
// `expr satisfies Type` — keep expr, strip ` satisfies Type`
se := n.AsSatisfiesExpression()
if se.Expression != nil && se.Type != nil {
ranges = append(ranges, [2]int{se.Expression.End(), n.End()})
}
if se.Expression != nil {
walk(se.Expression)
}
return
case ast.KindTypeAssertionExpression:
// `<Type>expr` — strip `<Type>`, keep expr
tae := n.AsTypeAssertion()
if tae.Expression != nil {
ranges = append(ranges, [2]int{n.Pos(), tae.Expression.Pos()})
walk(tae.Expression)
}
return
}
// For declarations, strip type annotations
switch n.Kind {
case ast.KindVariableDeclaration:
vd := n.AsVariableDeclaration()
if vd.Type != nil {
// `: Type` — from colon to start of initializer or end of declaration
colonEnd := vd.Type.End()
ranges = append(ranges, [2]int{vd.Name().End(), colonEnd})
}
case ast.KindFunctionDeclaration:
fd := n.AsFunctionDeclaration()
// Strip type parameters
if fd.TypeParameters != nil && len(fd.TypeParameters.Nodes) > 0 {
first := fd.TypeParameters.Nodes[0]
last := fd.TypeParameters.Nodes[len(fd.TypeParameters.Nodes)-1]
// Include the angle brackets: one char before first, one char after last
ranges = append(ranges, [2]int{first.Pos() - 1, last.End() + 1})
}
// Strip return type
if fd.Type != nil {
ranges = append(ranges, [2]int{fd.Parameters.End(), fd.Type.End()})
}
case ast.KindParameter:
p := n.AsParameterDeclaration()
if p.Type != nil {
ranges = append(ranges, [2]int{p.Name().End(), p.Type.End()})
}
case ast.KindArrowFunction:
af := n.AsArrowFunction()
if af.TypeParameters != nil && len(af.TypeParameters.Nodes) > 0 {
first := af.TypeParameters.Nodes[0]
last := af.TypeParameters.Nodes[len(af.TypeParameters.Nodes)-1]
ranges = append(ranges, [2]int{first.Pos() - 1, last.End() + 1})
}
if af.Type != nil {
ranges = append(ranges, [2]int{af.Parameters.End(), af.Type.End()})
}
}
// Recurse into children
n.ForEachChild(func(child *ast.Node) bool {
walk(child)
return false
})
}
walk(node)
return ranges
}
// stripRanges removes the given [start, end) byte ranges from the text.
// Ranges are absolute positions; offset is subtracted to make them relative to text start.
func stripRanges(text string, ranges [][2]int, offset int) string {
if len(ranges) == 0 {
return text
}
// Sort ranges by start position
sorted := make([][2]int, len(ranges))
copy(sorted, ranges)
for i := 0; i < len(sorted); i++ {
for j := i + 1; j < len(sorted); j++ {
if sorted[j][0] < sorted[i][0] {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
}
var b strings.Builder
pos := 0
for _, r := range sorted {
start := r[0] - offset
end := r[1] - offset
if start < 0 {
start = 0
}
if end > len(text) {
end = len(text)
}
if start < pos {
start = pos
}
if start > pos {
b.WriteString(text[pos:start])
}
pos = end
}
if pos < len(text) {
b.WriteString(text[pos:])
}
return b.String()
}
// normalizeWhitespace collapses all whitespace runs to single spaces and trims.
func normalizeWhitespace(s string) string {
var b strings.Builder
inSpace := false
for _, ch := range s {
if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
if !inSpace {
b.WriteByte(' ')
inSpace = true
}
} else {
b.WriteRune(ch)
inSpace = false
}
}
return strings.TrimSpace(b.String())
}
// hasSideEffectStmtChanges checks whether the top-level side-effect statements
// (statements that are NOT declarations, imports, or exports) differ between
// old and new source files. A change in side-effect statements means the module
// has different runtime behavior at load time, affecting all importers.
func hasSideEffectStmtChanges(oldSF *ast.SourceFile, newSF *ast.SourceFile) bool {
oldText := collectSideEffectText(oldSF)
newText := collectSideEffectText(newSF)
return oldText != newText
}
// collectSideEffectText extracts and normalizes the text of all top-level
// side-effect statements from a source file. Side-effect statements are
// everything except declarations, imports, exports, and empty statements.
func collectSideEffectText(sf *ast.SourceFile) string {
if sf == nil {
return ""
}
sourceText := sf.Text()
var b strings.Builder
for _, stmt := range sf.Statements.Nodes {
if isSideEffectStatement(stmt) {
// Use SkipTrivia to exclude leading comments/whitespace so that
// comment-only changes before a side-effect statement don't
// cause false positives.
start := scanner.SkipTrivia(sourceText, stmt.Pos())
end := stmt.End()
if start >= 0 && end <= len(sourceText) && start < end {
b.WriteString(normalizeWhitespace(sourceText[start:end]))
b.WriteByte('\n')
}
}
}
return b.String()
}
// isSideEffectStatement returns true if a top-level statement is a runtime
// side effect (not a declaration, import, export, or empty statement).
// Examples: console.log(), Object.defineProperty(), bare function calls.
func isSideEffectStatement(stmt *ast.Node) bool {
switch stmt.Kind {
case ast.KindFunctionDeclaration,
ast.KindClassDeclaration,
ast.KindInterfaceDeclaration,
ast.KindTypeAliasDeclaration,
ast.KindEnumDeclaration,
ast.KindVariableStatement,
ast.KindModuleDeclaration,
ast.KindImportDeclaration,
ast.KindImportEqualsDeclaration,
ast.KindExportDeclaration,
ast.KindExportAssignment,
ast.KindEmptyStatement:
return false
default:
return true
}
}
// importBindingOrigins maps each local binding introduced by a value import to a
// stable "origin" key (source + source-side name). Bare side-effect imports (no
// bindings) and — unless includeTypes — `import type` statements are excluded.
func importBindingOrigins(a *tsparse.FileAnalysis, includeTypes bool) map[string]string {
origins := make(map[string]string)
if a == nil {
return origins
}
for _, imp := range a.Imports {
if imp.IsTypeOnly && !includeTypes {
continue
}
for i, local := range imp.LocalNames {
src := ""
if i < len(imp.Names) {
src = imp.Names[i]
}
origins[local] = imp.Source + "\x00" + src
}
}
return origins
}
// repointedImportBindings returns local binding names present both before and
// after the change that now resolve to a different module/export.
func repointedImportBindings(oldA, newA *tsparse.FileAnalysis, includeTypes bool) []string {
if oldA == nil || newA == nil {
return nil
}
oldOrigins := importBindingOrigins(oldA, includeTypes)
newOrigins := importBindingOrigins(newA, includeTypes)
var repointed []string
for local, newOrigin := range newOrigins {
if oldOrigin, ok := oldOrigins[local]; ok && oldOrigin != newOrigin {
repointed = append(repointed, local)
}
}
return repointed
}
// containsIdentifier reports whether name occurs in text as a whole identifier
// token — flanked by non-identifier characters — rather than as a substring of a
// larger identifier. This prevents false taint links like `chooseAction` matching
// inside `chooseActionByIndex`. Names that are not plain identifiers (e.g. the "*"
// wildcard or "*:ns" namespace markers) fall back to a raw substring test.
func containsIdentifier(text, name string) bool {
if name == "" {
return false
}
if !isPlainIdentifier(name) {
return strings.Contains(text, name)
}
for from := 0; ; {
i := strings.Index(text[from:], name)
if i < 0 {
return false
}
start := from + i
end := start + len(name)
beforeOK := start == 0 || !isIdentByte(text[start-1])
afterOK := end == len(text) || !isIdentByte(text[end])
if beforeOK && afterOK {
return true
}
from = start + 1
}
}
func isPlainIdentifier(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
if !isIdentByte(s[i]) {
return false
}
}
return true
}
func isIdentByte(b byte) bool {
return b == '_' || b == '$' ||
(b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
}
// bareImportSources returns the set of module specifiers imported purely for
// their side effects (`import "./x"` — no bindings). These execute at import time.
func bareImportSources(a *tsparse.FileAnalysis) map[string]bool {
sources := make(map[string]bool)
if a == nil {
return sources
}
for _, imp := range a.Imports {
if len(imp.Names) == 0 && len(imp.LocalNames) == 0 {
sources[imp.Source] = true
}
}
return sources
}
// bareImportsChanged reports whether the set of side-effect imports differs
// between old and new (added, removed, or re-pointed) — an import-time behavior
// change that warrants whole-file taint.
func bareImportsChanged(oldA, newA *tsparse.FileAnalysis) bool {
oldSet := bareImportSources(oldA)
newSet := bareImportSources(newA)
if len(oldSet) != len(newSet) {
return true
}
for s := range newSet {
if !oldSet[s] {
return true
}
}
return false
}