internal/imports: make ApplyFixes work despite syntax errors

ApplyFixes is only used by gopls, which cares a lot about files with syntax
errors, and not at all about files that aren't structurally valid. Use
the standard ParseFile function instead of goimports' weird one.

Adding a test is impractical because it seems to break type checking of
whatever package it's in.

Fixes golang/go#35915.

Change-Id: Iaf0e331978415428a422d942a1e0c5f6e66dc8a1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/209579
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/imports/imports.go b/internal/imports/imports.go
index ed3867b..7c1b475 100644
--- a/internal/imports/imports.go
+++ b/internal/imports/imports.go
@@ -90,16 +90,25 @@
 		return nil, err
 	}
 
+	// Don't use parse() -- we don't care about fragments or statement lists
+	// here, and we need to work with unparseable files.
 	fileSet := token.NewFileSet()
-	file, adjust, err := parse(fileSet, filename, src, opt)
-	if err != nil {
+	parserMode := parser.Mode(0)
+	if opt.Comments {
+		parserMode |= parser.ParseComments
+	}
+	if opt.AllErrors {
+		parserMode |= parser.AllErrors
+	}
+	file, err := parser.ParseFile(fileSet, filename, src, parserMode)
+	if file == nil {
 		return nil, err
 	}
 
 	// Apply the fixes to the file.
 	apply(fileSet, file, fixes)
 
-	return formatFile(fileSet, file, src, adjust, opt)
+	return formatFile(fileSet, file, src, nil, opt)
 }
 
 // GetAllCandidates gets all of the standard library candidate packages to import in