internal/lsp: check the file's parse mode before formatting

Change-Id: I9ac3c273f305c41aed065cfcfdf96e59a828d71f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/196317
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Rebecca Stambler 2019-09-18 19:27:47 -04:00
parent f68e2b6f23
commit 928b73f71f

View File

@ -42,6 +42,11 @@ func Format(ctx context.Context, view View, f File) ([]protocol.TextEdit, error)
if err != nil {
return nil, err
}
// Be extra careful that the file's ParseMode is correct,
// otherwise we might replace the user's code with a trimmed AST.
if ph.Mode() != ParseFull {
return nil, errors.Errorf("%s was parsed in the incorrect mode", ph.File().Identity().URI)
}
file, m, _, err := ph.Parse(ctx)
if err != nil {
return nil, err
@ -102,6 +107,11 @@ func Imports(ctx context.Context, view View, f GoFile, rng span.Range) ([]protoc
if err != nil {
return nil, err
}
// Be extra careful that the file's ParseMode is correct,
// otherwise we might replace the user's code with a trimmed AST.
if ph.Mode() != ParseFull {
return nil, errors.Errorf("%s was parsed in the incorrect mode", ph.File().Identity().URI)
}
options := &imports.Options{
// Defaults.
AllErrors: true,