1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:54:57 -07:00
go/astutil
Dmitri Shuralyov c0060eca2c go.tools/astutil: fix edge case in DeleteImport causing merging of import sections.
The issue occurs only when deleting an import that has a blank line immediately preceding,
and other imports before that.

Currently, DeleteImport assumes there's a blank line-sized hole left behind
where the import was, and always deletes it. That blank line-sized hole is there in all cases
except the above edge case.

This fix checks for that edge case, and does not remove the blank line-sized hole.

The CL also adds a previously failing test case that catches this scenario. After the change to
DeleteImport, the new test passes (along with all other tests).

Fixes golang/go#7679.

Note that there is no attempt to ensure the result *ast.File and *token.FileSet are perfectly
matching to what you would get if you printed the AST and parsed it back. This is how the
rest of the package and the current tests work (i.e., they only check that printing the AST gives
the correct output).
Changing that is very hard, if not impossible, at least not
without resorting to manipulating AST via printing, text manipulation and parsing.
This is okay for most usages, but it does create potential problems. For example,
astutil.Imports() currently only works correctly on freshly parsed AST. If that AST
is manipulated via astutil funcs, then Imports() may not always generate correct
output. However, thas is a separate issue and should be treated as such.

LGTM=bradfitz
R=golang-codereviews, gobot, adonovan, bradfitz
CC=golang-codereviews
https://golang.org/cl/92250045
2014-05-19 14:04:30 -07:00
..
enclosing_test.go go.tools/importer: move PathEnclosingInterval to package astutil. 2013-12-09 09:36:29 -05:00
enclosing.go go.tools: rename packages. 2014-01-16 09:33:58 -05:00
imports_test.go go.tools/astutil: fix edge case in DeleteImport causing merging of import sections. 2014-05-19 14:04:30 -07:00
imports.go go.tools/astutil: fix edge case in DeleteImport causing merging of import sections. 2014-05-19 14:04:30 -07:00