-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathextract.go
2115 lines (2006 loc) · 67.7 KB
/
extract.go
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package golang
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/printer"
"go/token"
"go/types"
"slices"
"sort"
"strconv"
"strings"
"text/scanner"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/gopls/internal/cache"
"golang.org/x/tools/gopls/internal/cache/parsego"
goplsastutil "golang.org/x/tools/gopls/internal/util/astutil"
"golang.org/x/tools/gopls/internal/util/bug"
"golang.org/x/tools/gopls/internal/util/safetoken"
"golang.org/x/tools/internal/analysisinternal"
"golang.org/x/tools/internal/astutil/cursor"
"golang.org/x/tools/internal/typesinternal"
)
// extractVariable implements the refactor.extract.{variable,constant} CodeAction command.
func extractVariable(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) {
return extractExprs(pkg, pgf, start, end, false)
}
// extractVariableAll implements the refactor.extract.{variable,constant}-all CodeAction command.
func extractVariableAll(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) {
return extractExprs(pkg, pgf, start, end, true)
}
// extractExprs replaces occurrence(s) of a specified expression within the same function
// with newVar. If 'all' is true, it replaces all occurrences of the same expression;
// otherwise, it only replaces the selected expression.
//
// The new variable/constant is declared as close as possible to the first found expression
// within the deepest common scope accessible to all candidate occurrences.
func extractExprs(pkg *cache.Package, pgf *parsego.File, start, end token.Pos, all bool) (*token.FileSet, *analysis.SuggestedFix, error) {
var (
fset = pkg.FileSet()
info = pkg.TypesInfo()
file = pgf.File
)
// TODO(adonovan): simplify, using Cursor.
tokFile := fset.File(file.FileStart)
exprs, err := canExtractVariable(info, pgf.Cursor, start, end, all)
if err != nil {
return nil, nil, fmt.Errorf("cannot extract: %v", err)
}
// innermost scope enclosing ith expression
exprScopes := make([]*types.Scope, len(exprs))
for i, e := range exprs {
exprScopes[i] = info.Scopes[file].Innermost(e.Pos())
}
hasCollision := func(name string) bool {
for _, scope := range exprScopes {
if s, _ := scope.LookupParent(name, token.NoPos); s != nil {
return true
}
}
return false
}
constant := info.Types[exprs[0]].Value != nil
// Generate name(s) for new declaration.
baseName := cond(constant, "newConst", "newVar")
var lhsNames []string
switch expr := exprs[0].(type) {
case *ast.CallExpr:
tup, ok := info.TypeOf(expr).(*types.Tuple)
if !ok {
// conversion or single-valued call:
// treat it the same as our standard extract variable case.
name, _ := generateName(0, baseName, hasCollision)
lhsNames = append(lhsNames, name)
} else {
// call with multiple results
idx := 0
for range tup.Len() {
// Generate a unique variable for each result.
var name string
name, idx = generateName(idx, baseName, hasCollision)
lhsNames = append(lhsNames, name)
}
}
default:
// TODO: stricter rules for selectorExpr.
name, _ := generateName(0, baseName, hasCollision)
lhsNames = append(lhsNames, name)
}
// Where all the extractable positions can see variable being declared.
var commonScope *types.Scope
counter := make(map[*types.Scope]int)
Outer:
for _, scope := range exprScopes {
for s := scope; s != nil; s = s.Parent() {
counter[s]++
if counter[s] == len(exprScopes) {
// A scope whose count is len(scopes) is common to all ancestor paths.
// Stop at the first (innermost) one.
commonScope = s
break Outer
}
}
}
var visiblePath []ast.Node
if commonScope != exprScopes[0] {
// This means the first expr within function body is not the largest scope,
// we need to find the scope immediately follow the common
// scope where we will insert the statement before.
child := exprScopes[0]
for p := child; p != nil; p = p.Parent() {
if p == commonScope {
break
}
child = p
}
visiblePath, _ = astutil.PathEnclosingInterval(file, child.Pos(), child.End())
} else {
// Insert newVar inside commonScope before the first occurrence of the expression.
visiblePath, _ = astutil.PathEnclosingInterval(file, exprs[0].Pos(), exprs[0].End())
}
variables, err := collectFreeVars(info, file, exprs[0].Pos(), exprs[0].End(), exprs[0])
if err != nil {
return nil, nil, err
}
// TODO: There is a bug here: for a variable declared in a labeled
// switch/for statement it returns the for/switch statement itself
// which produces the below code which is a compiler error. e.g.
// label:
// switch r1 := r() { ... break label ... }
// On extracting "r()" to a variable
// label:
// x := r()
// switch r1 := x { ... break label ... } // compiler error
//
var (
insertPos token.Pos
indentation string
stmtOK bool // ok to use ":=" instead of var/const decl?
)
if funcDecl, ok := visiblePath[len(visiblePath)-2].(*ast.FuncDecl); ok && goplsastutil.NodeContains(funcDecl.Body, start) {
before, err := stmtToInsertVarBefore(visiblePath, variables)
if err != nil {
return nil, nil, fmt.Errorf("cannot find location to insert extraction: %v", err)
}
// Within function: compute appropriate statement indentation.
indent, err := calculateIndentation(pgf.Src, tokFile, before)
if err != nil {
return nil, nil, err
}
insertPos = before.Pos()
indentation = "\n" + indent
// Currently, we always extract a constant expression
// to a const declaration (and logic in CodeAction
// assumes that we do so); this is conservative because
// it preserves its constant-ness.
//
// In future, constant expressions used only in
// contexts where constant-ness isn't important could
// be profitably extracted to a var declaration or :=
// statement, especially if the latter is the Init of
// an {If,For,Switch}Stmt.
stmtOK = !constant
} else {
// Outside any statement: insert before the current
// declaration, without indentation.
currentDecl := visiblePath[len(visiblePath)-2]
insertPos = currentDecl.Pos()
indentation = "\n"
}
// Create statement to declare extracted var/const.
//
// TODO(adonovan): beware the const decls are not valid short
// statements, so if fixing #70563 causes
// StmtToInsertVarBefore to evolve to permit declarations in
// the "pre" part of an IfStmt, like so:
// Before:
// if cond {
// } else if «1 + 2» > 0 {
// }
// After:
// if x := 1 + 2; cond {
// } else if x > 0 {
// }
// then it will need to become aware that this is invalid
// for constants.
//
// Conversely, a short var decl stmt is not valid at top level,
// so when we fix #70665, we'll need to use a var decl.
var newNode ast.Node
if !stmtOK {
// var/const x1, ..., xn = expr
var names []*ast.Ident
for _, name := range lhsNames {
names = append(names, ast.NewIdent(name))
}
newNode = &ast.GenDecl{
Tok: cond(constant, token.CONST, token.VAR),
Specs: []ast.Spec{
&ast.ValueSpec{
Names: names,
Values: []ast.Expr{exprs[0]},
},
},
}
} else {
// var: x1, ... xn := expr
var lhs []ast.Expr
for _, name := range lhsNames {
lhs = append(lhs, ast.NewIdent(name))
}
newNode = &ast.AssignStmt{
Tok: token.DEFINE,
Lhs: lhs,
Rhs: []ast.Expr{exprs[0]},
}
}
// Format and indent the declaration.
var buf bytes.Buffer
if err := format.Node(&buf, fset, newNode); err != nil {
return nil, nil, err
}
// TODO(adonovan): not sound for `...` string literals containing newlines.
assignment := strings.ReplaceAll(buf.String(), "\n", indentation) + indentation
textEdits := []analysis.TextEdit{{
Pos: insertPos,
End: insertPos,
NewText: []byte(assignment),
}}
for _, e := range exprs {
textEdits = append(textEdits, analysis.TextEdit{
Pos: e.Pos(),
End: e.End(),
NewText: []byte(strings.Join(lhsNames, ", ")),
})
}
return fset, &analysis.SuggestedFix{
TextEdits: textEdits,
}, nil
}
// stmtToInsertVarBefore returns the ast.Stmt before which we can safely insert a new variable,
// and ensures that the new declaration is inserted at a point where all free variables are declared before.
// Some examples:
//
// Basic Example:
//
// z := 1
// y := z + x
//
// If x is undeclared, then this function would return `y := z + x`, so that we
// can insert `x := ` on the line before `y := z + x`.
//
// valid IfStmt example:
//
// if z == 1 {
// } else if z == y {}
//
// If y is undeclared, then this function would return `if z == 1 {`, because we cannot
// insert a statement between an if and an else if statement. As a result, we need to find
// the top of the if chain to insert `y := ` before.
//
// invalid IfStmt example:
//
// if x := 1; true {
// } else if y := x + 1; true { //apply refactor.extract.variable to x
// }
//
// `x` is a free variable defined in the IfStmt, we should not insert
// the extracted expression outside the IfStmt scope, instead, return an error.
func stmtToInsertVarBefore(path []ast.Node, variables []*variable) (ast.Stmt, error) {
enclosingIndex := -1 // index in path of enclosing stmt
for i, p := range path {
if _, ok := p.(ast.Stmt); ok {
enclosingIndex = i
break
}
}
if enclosingIndex == -1 {
return nil, fmt.Errorf("no enclosing statement")
}
enclosingStmt := path[enclosingIndex].(ast.Stmt)
// hasFreeVar reports if any free variables is defined inside stmt (which may be nil).
// If true, indicates that the insertion point will sit before the variable declaration.
hasFreeVar := func(stmt ast.Stmt) bool {
if stmt == nil {
return false
}
for _, v := range variables {
if goplsastutil.NodeContains(stmt, v.obj.Pos()) {
return true
}
}
return false
}
// baseIfStmt walks up the if/else-if chain until we get to
// the top of the current if chain.
baseIfStmt := func(index int) (ast.Stmt, error) {
stmt := path[index]
for _, node := range path[index+1:] {
ifStmt, ok := node.(*ast.IfStmt)
if !ok || ifStmt.Else != stmt {
break
}
if hasFreeVar(ifStmt.Init) {
return nil, fmt.Errorf("Else's init statement has free variable declaration")
}
stmt = ifStmt
}
return stmt.(ast.Stmt), nil
}
switch enclosingStmt := enclosingStmt.(type) {
case *ast.IfStmt:
if hasFreeVar(enclosingStmt.Init) {
return nil, fmt.Errorf("IfStmt's init statement has free variable declaration")
}
// The enclosingStmt is inside of the if declaration,
// We need to check if we are in an else-if stmt and
// get the base if statement.
return baseIfStmt(enclosingIndex)
case *ast.CaseClause:
// Get the enclosing switch stmt if the enclosingStmt is
// inside of the case statement.
for _, node := range path[enclosingIndex+1:] {
switch stmt := node.(type) {
case *ast.SwitchStmt:
if hasFreeVar(stmt.Init) {
return nil, fmt.Errorf("SwitchStmt's init statement has free variable declaration")
}
return stmt, nil
case *ast.TypeSwitchStmt:
if hasFreeVar(stmt.Init) {
return nil, fmt.Errorf("TypeSwitchStmt's init statement has free variable declaration")
}
return stmt, nil
}
}
}
// Check if the enclosing statement is inside another node.
switch parent := path[enclosingIndex+1].(type) {
case *ast.IfStmt:
if hasFreeVar(parent.Init) {
return nil, fmt.Errorf("IfStmt's init statement has free variable declaration")
}
return baseIfStmt(enclosingIndex + 1)
case *ast.ForStmt:
if parent.Init == enclosingStmt || parent.Post == enclosingStmt {
return parent, nil
}
case *ast.SwitchStmt:
if hasFreeVar(parent.Init) {
return nil, fmt.Errorf("SwitchStmt's init statement has free variable declaration")
}
return parent, nil
case *ast.TypeSwitchStmt:
if hasFreeVar(parent.Init) {
return nil, fmt.Errorf("TypeSwitchStmt's init statement has free variable declaration")
}
return parent, nil
}
return enclosingStmt, nil
}
// canExtractVariable reports whether the code in the given range can be
// extracted to a variable (or constant). It returns the selected expression or, if 'all',
// all structurally equivalent expressions within the same function body, in lexical order.
func canExtractVariable(info *types.Info, curFile cursor.Cursor, start, end token.Pos, all bool) ([]ast.Expr, error) {
if start == end {
return nil, fmt.Errorf("empty selection")
}
file := curFile.Node().(*ast.File)
// TODO(adonovan): simplify, using Cursor.
path, exact := astutil.PathEnclosingInterval(file, start, end)
if !exact {
return nil, fmt.Errorf("selection is not an expression")
}
if len(path) == 0 {
return nil, bug.Errorf("no path enclosing interval")
}
for _, n := range path {
if _, ok := n.(*ast.ImportSpec); ok {
return nil, fmt.Errorf("cannot extract variable or constant in an import block")
}
}
expr, ok := path[0].(ast.Expr)
if !ok {
return nil, fmt.Errorf("selection is not an expression") // e.g. statement
}
if tv, ok := info.Types[expr]; !ok || !tv.IsValue() || tv.Type == nil || tv.HasOk() {
// e.g. type, builtin, x.(type), 2-valued m[k], or ill-typed
return nil, fmt.Errorf("selection is not a single-valued expression")
}
var exprs []ast.Expr
if !all {
exprs = append(exprs, expr)
} else if funcDecl, ok := path[len(path)-2].(*ast.FuncDecl); ok {
// Find all expressions in the same function body that
// are equal to the selected expression.
ast.Inspect(funcDecl.Body, func(n ast.Node) bool {
if e, ok := n.(ast.Expr); ok {
if goplsastutil.Equal(e, expr, func(x, y *ast.Ident) bool {
xobj, yobj := info.ObjectOf(x), info.ObjectOf(y)
// The two identifiers must resolve to the same object,
// or to a declaration within the candidate expression.
// (This allows two copies of "func (x int) { print(x) }"
// to match.)
if xobj != nil && goplsastutil.NodeContains(e, xobj.Pos()) &&
yobj != nil && goplsastutil.NodeContains(expr, yobj.Pos()) {
return x.Name == y.Name
}
// Use info.Uses to avoid including declaration, for example,
// when extractnig x:
//
// x := 1 // should not include x
// y := x // include x
// z := x // include x
xuse := info.Uses[x]
return xuse != nil && xuse == info.Uses[y]
}) {
exprs = append(exprs, e)
}
}
return true
})
} else {
return nil, fmt.Errorf("node %T is not inside a function", expr)
}
// Disallow any expr that sits in lhs of an AssignStmt or ValueSpec for now.
//
// TODO(golang/go#70784): In such cases, exprs are operated in "variable" mode (L-value mode in C).
// In contrast, exprs in the RHS operate in "value" mode (R-value mode in C).
// L-value mode refers to exprs that represent storage locations,
// while R-value mode refers to exprs that represent values.
// There are a number of expressions that may have L-value mode, given by:
//
// lvalue = ident -- Ident such that info.Uses[id] is a *Var
// | '(' lvalue ') ' -- ParenExpr
// | lvalue '[' expr ']' -- IndexExpr
// | lvalue '.' ident -- SelectorExpr.
//
// For example:
//
// type foo struct {
// bar int
// }
// f := foo{bar: 1}
// x := f.bar + 1 // f.bar operates in "value" mode.
// f.bar = 2 // f.bar operates in "variable" mode.
//
// When extracting exprs in variable mode, we must be cautious. Any such extraction
// may require capturing the address of the expression and replacing its uses with dereferenced access.
// The type checker records this information in info.Types[id].{IsValue,Addressable}().
// The correct result should be:
//
// newVar := &f.bar
// x := *newVar + 1
// *newVar = 2
for _, e := range exprs {
path, _ := astutil.PathEnclosingInterval(file, e.Pos(), e.End())
for _, n := range path {
if assignment, ok := n.(*ast.AssignStmt); ok {
if slices.Contains(assignment.Lhs, e) {
return nil, fmt.Errorf("node %T is in LHS of an AssignStmt", expr)
}
break
}
if value, ok := n.(*ast.ValueSpec); ok {
for _, name := range value.Names {
if name == e {
return nil, fmt.Errorf("node %T is in LHS of a ValueSpec", expr)
}
}
break
}
}
}
return exprs, nil
}
// Calculate indentation for insertion.
// When inserting lines of code, we must ensure that the lines have consistent
// formatting (i.e. the proper indentation). To do so, we observe the indentation on the
// line of code on which the insertion occurs.
func calculateIndentation(content []byte, tok *token.File, insertBeforeStmt ast.Node) (string, error) {
line := safetoken.Line(tok, insertBeforeStmt.Pos())
lineOffset, stmtOffset, err := safetoken.Offsets(tok, tok.LineStart(line), insertBeforeStmt.Pos())
if err != nil {
return "", err
}
return string(content[lineOffset:stmtOffset]), nil
}
// freshName returns an identifier based on prefix (perhaps with a
// numeric suffix) that is not in scope at the specified position
// within the file. It returns the next numeric suffix to use.
func freshName(info *types.Info, file *ast.File, pos token.Pos, prefix string, idx int) (string, int) {
scope := info.Scopes[file].Innermost(pos)
return generateName(idx, prefix, func(name string) bool {
obj, _ := scope.LookupParent(name, pos)
return obj != nil
})
}
// freshNameOutsideRange is like [freshName], but ignores names
// declared between start and end for the purposes of detecting conflicts.
//
// This is used for function extraction, where [start, end) will be extracted
// to a new scope.
func freshNameOutsideRange(info *types.Info, file *ast.File, pos, start, end token.Pos, prefix string, idx int) (string, int) {
scope := info.Scopes[file].Innermost(pos)
return generateName(idx, prefix, func(name string) bool {
// Only report a collision if the object declaration
// was outside the extracted range.
for scope != nil {
obj, declScope := scope.LookupParent(name, pos)
if obj == nil {
return false // undeclared
}
if !(start <= obj.Pos() && obj.Pos() < end) {
return true // declared outside ignored range
}
scope = declScope.Parent()
}
return false
})
}
func generateName(idx int, prefix string, hasCollision func(string) bool) (string, int) {
name := prefix
if idx != 0 {
name += fmt.Sprintf("%d", idx)
}
for hasCollision(name) {
idx++
name = fmt.Sprintf("%v%d", prefix, idx)
}
return name, idx + 1
}
// returnVariable keeps track of the information we need to properly introduce a new variable
// that we will return in the extracted function.
type returnVariable struct {
// name is the identifier that is used on the left-hand side of the call to
// the extracted function.
name *ast.Ident
// decl is the declaration of the variable. It is used in the type signature of the
// extracted function and for variable declarations.
decl *ast.Field
// zeroVal is the "zero value" of the type of the variable. It is used in a return
// statement in the extracted function.
zeroVal ast.Expr
}
// extractMethod refactors the selected block of code into a new method.
func extractMethod(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) {
return extractFunctionMethod(pkg, pgf, start, end, true)
}
// extractFunction refactors the selected block of code into a new function.
func extractFunction(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) {
return extractFunctionMethod(pkg, pgf, start, end, false)
}
// extractFunctionMethod refactors the selected block of code into a new function/method.
// It also replaces the selected block of code with a call to the extracted
// function. First, we manually adjust the selection range. We remove trailing
// and leading whitespace characters to ensure the range is precisely bounded
// by AST nodes. Next, we determine the variables that will be the parameters
// and return values of the extracted function/method. Lastly, we construct the call
// of the function/method and insert this call as well as the extracted function/method into
// their proper locations.
func extractFunctionMethod(cpkg *cache.Package, pgf *parsego.File, start, end token.Pos, isMethod bool) (*token.FileSet, *analysis.SuggestedFix, error) {
var (
fset = cpkg.FileSet()
pkg = cpkg.Types()
info = cpkg.TypesInfo()
src = pgf.Src
)
errorPrefix := "extractFunction"
if isMethod {
errorPrefix = "extractMethod"
}
file := pgf.Cursor.Node().(*ast.File)
// TODO(adonovan): simplify, using Cursor.
tok := fset.File(file.FileStart)
if tok == nil {
return nil, nil, bug.Errorf("no file for position")
}
p, ok, methodOk, err := canExtractFunction(tok, start, end, src, pgf.Cursor)
if (!ok && !isMethod) || (!methodOk && isMethod) {
return nil, nil, fmt.Errorf("%s: cannot extract %s: %v", errorPrefix,
safetoken.StartPosition(fset, start), err)
}
tok, path, start, end, outer, node := p.tok, p.path, p.start, p.end, p.outer, p.node
// A return statement is non-nested if its parent node is equal to the parent node
// of the first node in the selection. These cases must be handled separately because
// non-nested return statements are guaranteed to execute.
var retStmts []*ast.ReturnStmt
var hasNonNestedReturn bool
startParent := findParent(outer, node)
ast.Inspect(outer, func(n ast.Node) bool {
if n == nil {
return false
}
if n.Pos() < start || n.End() > end {
return n.Pos() <= end
}
// exclude return statements in function literals because they don't affect the refactor.
if _, ok := n.(*ast.FuncLit); ok {
return false
}
ret, ok := n.(*ast.ReturnStmt)
if !ok {
return true
}
if findParent(outer, n) == startParent {
hasNonNestedReturn = true
}
retStmts = append(retStmts, ret)
return false
})
containsReturnStatement := len(retStmts) > 0
// Now that we have determined the correct range for the selection block,
// we must determine the signature of the extracted function. We will then replace
// the block with an assignment statement that calls the extracted function with
// the appropriate parameters and return values.
variables, err := collectFreeVars(info, file, start, end, path[0])
if err != nil {
return nil, nil, err
}
var (
receiverUsed bool
receiver *ast.Field
receiverName string
receiverObj types.Object
)
if isMethod {
if outer == nil || outer.Recv == nil || len(outer.Recv.List) == 0 {
return nil, nil, fmt.Errorf("%s: cannot extract need method receiver", errorPrefix)
}
receiver = outer.Recv.List[0]
if len(receiver.Names) == 0 || receiver.Names[0] == nil {
return nil, nil, fmt.Errorf("%s: cannot extract need method receiver name", errorPrefix)
}
recvName := receiver.Names[0]
receiverName = recvName.Name
receiverObj = info.ObjectOf(recvName)
}
var (
params, returns []ast.Expr // used when calling the extracted function
paramTypes, returnTypes []*ast.Field // used in the signature of the extracted function
uninitialized []types.Object // vars we will need to initialize before the call
)
// Avoid duplicates while traversing vars and uninitialized.
seenVars := make(map[types.Object]ast.Expr)
seenUninitialized := make(map[types.Object]struct{})
// Some variables on the left-hand side of our assignment statement may be free. If our
// selection begins in the same scope in which the free variable is defined, we can
// redefine it in our assignment statement. See the following example, where 'b' and
// 'err' (both free variables) can be redefined in the second funcCall() while maintaining
// correctness.
//
//
// Not Redefined:
//
// a, err := funcCall()
// var b int
// b, err = funcCall()
//
// Redefined:
//
// a, err := funcCall()
// b, err := funcCall()
//
// We track the number of free variables that can be redefined to maintain our preference
// of using "x, y, z := fn()" style assignment statements.
var canRedefineCount int
qual := typesinternal.FileQualifier(file, pkg)
// Each identifier in the selected block must become (1) a parameter to the
// extracted function, (2) a return value of the extracted function, or (3) a local
// variable in the extracted function. Determine the outcome(s) for each variable
// based on whether it is free, altered within the selected block, and used outside
// of the selected block.
for _, v := range variables {
if _, ok := seenVars[v.obj]; ok {
continue
}
if v.obj.Name() == "_" {
// The blank identifier is always a local variable
continue
}
typ := typesinternal.TypeExpr(v.obj.Type(), qual)
seenVars[v.obj] = typ
identifier := ast.NewIdent(v.obj.Name())
// An identifier must meet three conditions to become a return value of the
// extracted function. (1) its value must be defined or reassigned within
// the selection (isAssigned), (2) it must be used at least once after the
// selection (isUsed), and (3) its first use after the selection
// cannot be its own reassignment or redefinition (objOverriden).
vscope := v.obj.Parent()
if vscope == nil {
return nil, nil, fmt.Errorf("parent nil")
}
isUsed, firstUseAfter := objUsed(info, end, vscope.End(), v.obj)
if v.assigned && isUsed && !varOverridden(info, firstUseAfter, v.obj, v.free, outer) {
returnTypes = append(returnTypes, &ast.Field{Type: typ})
returns = append(returns, identifier)
if !v.free {
uninitialized = append(uninitialized, v.obj)
} else {
// In go1.22, Scope.Pos for function scopes changed (#60752):
// it used to start at the body ('{'), now it starts at "func".
//
// The second condition below handles the case when
// v's block is the FuncDecl.Body itself.
if vscope.Pos() == startParent.Pos() ||
startParent == outer.Body && vscope == info.Scopes[outer.Type] {
canRedefineCount++
}
}
}
// An identifier must meet two conditions to become a parameter of the
// extracted function. (1) it must be free (isFree), and (2) its first
// use within the selection cannot be its own definition (isDefined).
if v.free && !v.defined {
// Skip the selector for a method.
if isMethod && v.obj == receiverObj {
receiverUsed = true
continue
}
params = append(params, identifier)
paramTypes = append(paramTypes, &ast.Field{
Names: []*ast.Ident{identifier},
Type: typ,
})
}
}
reorderParams(params, paramTypes)
// Find the function literal that encloses the selection. The enclosing function literal
// may not be the enclosing function declaration (i.e. 'outer'). For example, in the
// following block:
//
// func main() {
// ast.Inspect(node, func(n ast.Node) bool {
// v := 1 // this line extracted
// return true
// })
// }
//
// 'outer' is main(). However, the extracted selection most directly belongs to
// the anonymous function literal, the second argument of ast.Inspect(). We use the
// enclosing function literal to determine the proper return types for return statements
// within the selection. We still need the enclosing function declaration because this is
// the top-level declaration. We inspect the top-level declaration to look for variables
// as well as for code replacement.
enclosing := outer.Type
for _, p := range path {
if p == enclosing {
break
}
if fl, ok := p.(*ast.FuncLit); ok {
enclosing = fl.Type
break
}
}
// We put the selection in a constructed file. We can then traverse and edit
// the extracted selection without modifying the original AST.
startOffset, endOffset, err := safetoken.Offsets(tok, start, end)
if err != nil {
return nil, nil, err
}
selection := src[startOffset:endOffset]
extractedBlock, extractedComments, err := parseStmts(fset, selection)
if err != nil {
return nil, nil, err
}
// We need to account for return statements in the selected block, as they will complicate
// the logical flow of the extracted function. See the following example, where ** denotes
// the range to be extracted.
//
// Before:
//
// func _() int {
// a := 1
// b := 2
// **if a == b {
// return a
// }**
// ...
// }
//
// After:
//
// func _() int {
// a := 1
// b := 2
// cond0, ret0 := x0(a, b)
// if cond0 {
// return ret0
// }
// ...
// }
//
// func x0(a int, b int) (bool, int) {
// if a == b {
// return true, a
// }
// return false, 0
// }
//
// We handle returns by adding an additional boolean return value to the extracted function.
// This bool reports whether the original function would have returned. Because the
// extracted selection contains a return statement, we must also add the types in the
// return signature of the enclosing function to the return signature of the
// extracted function. We then add an extra if statement checking this boolean value
// in the original function. If the condition is met, the original function should
// return a value, mimicking the functionality of the original return statement(s)
// in the selection.
//
// If there is a return that is guaranteed to execute (hasNonNestedReturns=true), then
// we don't need to include this additional condition check and can simply return.
//
// Before:
//
// func _() int {
// a := 1
// b := 2
// **if a == b {
// return a
// }
// return b**
// }
//
// After:
//
// func _() int {
// a := 1
// b := 2
// return x0(a, b)
// }
//
// func x0(a int, b int) int {
// if a == b {
// return a
// }
// return b
// }
var retVars []*returnVariable
var ifReturn *ast.IfStmt
if containsReturnStatement {
if !hasNonNestedReturn {
// The selected block contained return statements, so we have to modify the
// signature of the extracted function as described above. Adjust all of
// the return statements in the extracted function to reflect this change in
// signature.
if err := adjustReturnStatements(returnTypes, seenVars, extractedBlock, qual); err != nil {
return nil, nil, err
}
}
// Collect the additional return values and types needed to accommodate return
// statements in the selection. Update the type signature of the extracted
// function and construct the if statement that will be inserted in the enclosing
// function.
retVars, ifReturn, err = generateReturnInfo(enclosing, pkg, path, file, info, start, end, hasNonNestedReturn)
if err != nil {
return nil, nil, err
}
}
// Determine if the extracted block contains any free branch statements, for
// example: "continue label" where "label" is declared outside of the
// extracted block, or continue inside a "for" statement where the for
// statement is declared outside of the extracted block.
// If the extracted block contains free branch statements, we add another
// return value "ctrl" to the extracted function that will be used to
// determine the control flow. See the following example, where === denotes
// the range to be extracted.
//
// Before:
// func f(cond bool) {
// for range "abc" {
// ==============
// if cond {
// continue
// }
// ==============
// println(0)
// }
// }
// After:
// func f(cond bool) {
// for range "abc" {
// ctrl := newFunction(cond)
// switch ctrl {
// case 1:
// continue
// }
// println(0)
// }
// }
//
// func newFunction(cond bool) int {
// if cond {
// return 1
// }
// return 0
// }
//
curSel, _ := pgf.Cursor.FindByPos(start, end) // since canExtractFunction succeeded, this will always return a valid cursor
freeBranches := freeBranches(info, curSel, start, end)
// Generate an unused identifier for the control value.
ctrlVar, _ := freshName(info, file, start, "ctrl", 0)
if len(freeBranches) > 0 {
zeroValExpr := &ast.BasicLit{
Kind: token.INT,
Value: "0",
}
var branchStmts []*ast.BranchStmt
var stack []ast.Node
// Add the zero "ctrl" value to each return statement in the extracted block.
ast.Inspect(extractedBlock, func(n ast.Node) bool {
if n != nil {
stack = append(stack, n)
} else {
stack = stack[:len(stack)-1]
}
switch n := n.(type) {
case *ast.ReturnStmt:
n.Results = append(n.Results, zeroValExpr)
case *ast.BranchStmt:
// Collect a list of branch statements in the extracted block to examine later.
if isFreeBranchStmt(stack) {
branchStmts = append(branchStmts, n)
}
case *ast.FuncLit:
// Don't descend into nested functions. When we return false
// here, ast.Inspect does not give us a "pop" event when leaving
// the subtree, so we need to pop here. (golang/go#73319)
stack = stack[:len(stack)-1]
return false
}
return true
})
// Construct a return statement to replace each free branch statement in the extracted block. It should have
// zero values for all return parameters except one, "ctrl", which dictates which continuation to follow.
var freeCtrlStmtReturns []ast.Expr
// Create "zero values" for each type.
for _, returnType := range returnTypes {