this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/mod/modimports: use cue/parser.ImportsOnly

Invalid syntax after import declarations should be handled consistently
no matter whether a file started with a package clause or not.
If for any reason cueimports.Read thinks there is invalid syntax
and reads the entire source, such as when a package clause is missing,
stop cue/parser from parsing the entire file as well.

Not only is parsing the entire file in that case wasteful,
but it also leads to errors which aren't particularly useful
when all we care about here is listing imported packages.

While here, remove the use of ParseComments; none of the modimports
users need to parse comments at the top of the file.
This is a remnant from Go, where comments at the top of the file
are important as they may contain build tag directives.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I35eeade6481b74da7f04240f9ef85da338491f7e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194961
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+4 -6
+1 -1
internal/mod/modimports/modimports.go
··· 154 154 if err != nil { 155 155 return yield(pf, err) 156 156 } 157 - syntax, err := parser.ParseFile(fpath, data, parser.ParseComments) 157 + syntax, err := parser.ParseFile(fpath, data, parser.ImportsOnly) 158 158 if err != nil { 159 159 return yield(pf, err) 160 160 }
+3 -5
internal/mod/modimports/testdata/parseerror.txtar
··· 1 1 -- want -- 2 - bad_after_import.cue: error: expected ')', found newline 2 + bad_after_import.cue "dep3" 3 3 bad_after_package.cue 4 4 bad_after_package_import.cue "dep4" 5 5 bad_imports.cue: error: string literal not terminated 6 - bad_lone.cue: error: expected ')', found newline 6 + bad_lone.cue 7 7 bad_package_imports.cue: error: string literal not terminated 8 8 -- want-imports -- 9 - error: cannot read "bad_after_import.cue": expected ')', found newline 9 + error: cannot read "bad_imports.cue": string literal not terminated 10 10 -- bad_imports.cue -- 11 11 // Invalid import syntax without a package clause. 12 12 ··· 21 21 22 22 -- bad_lone.cue -- 23 23 // Invalid syntax without a package clause, assumed to be after imports. 24 - // TODO(mvdan): just like bad_after_package.cue, this shouldn't fail. 25 24 26 25 (unterminated 27 26 ··· 34 33 35 34 -- bad_after_import.cue -- 36 35 // Invalid syntax after some imports without a package clause. 37 - // TODO(mvdan): just like bad_after_package_import.cue, this shouldn't fail. 38 36 39 37 import "dep3" 40 38