Skip to content

Commit

Permalink
internal/lsp/source: do not allow extraction of an import spec
Browse files Browse the repository at this point in the history
Fixes golang/go#40635

Change-Id: Iab8ca37d251a95334c19f32873f9ba9cc1bdd2f4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/251018
Run-TryBot: Rebecca Stambler <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
stamblerre committed Aug 27, 2020
1 parent df83f4e commit 989ebae
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/lsp/source/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ func extractVariable(fset *token.FileSet, rng span.Range, src []byte, file *ast.
var lhsNames []string
switch expr := expr.(type) {
// TODO: stricter rules for selectorExpr.
case *ast.BasicLit, *ast.CompositeLit, *ast.IndexExpr, *ast.SliceExpr, *ast.UnaryExpr,
*ast.BinaryExpr, *ast.SelectorExpr:
lhsNames = append(lhsNames,
generateAvailableIdentifier(expr.Pos(), file, path, info, "x", 0))
case *ast.BasicLit, *ast.CompositeLit, *ast.IndexExpr, *ast.SliceExpr,
*ast.UnaryExpr, *ast.BinaryExpr, *ast.SelectorExpr:
lhsNames = append(lhsNames, generateAvailableIdentifier(expr.Pos(), file, path, info, "x", 0))
case *ast.CallExpr:
tup, ok := info.TypeOf(expr).(*types.Tuple)
if !ok {
Expand Down Expand Up @@ -101,6 +100,11 @@ func canExtractVariable(rng span.Range, file *ast.File) (ast.Expr, []ast.Node, b
if len(path) == 0 {
return nil, nil, false, fmt.Errorf("no path enclosing interval")
}
for _, n := range path {
if _, ok := n.(*ast.ImportSpec); ok {
return nil, nil, false, fmt.Errorf("cannot extract variable in an import block")
}
}
node := path[0]
if rng.Start != node.Pos() || rng.End != node.End() {
return nil, nil, false, fmt.Errorf("range does not map to an AST node")
Expand Down

0 comments on commit 989ebae

Please sign in to comment.