-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/lsp/analysis: embed directive analyzer check following decl
Extend the embed directive analyzer. Verify that embed directives are followed by a single variable declaration. Updates golang/go#50262 Change-Id: Ib69bbfb3aab586a349360d501ceb714a5434e8bd Reviewed-on: https://go-review.googlesource.com/c/tools/+/504295 Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]>
- Loading branch information
Showing
6 changed files
with
196 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
gopls/internal/lsp/analysis/embeddirective/testdata/src/a/a.go
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
gopls/internal/lsp/analysis/embeddirective/testdata/src/a/b.go
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
gopls/internal/lsp/analysis/embeddirective/testdata/src/a/import_missing.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package a | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
//go:embed embedtext // want "must import \"embed\" when using go:embed directives" | ||
var s string | ||
|
||
// This is main function | ||
func main() { | ||
fmt.Println(s) | ||
} |
73 changes: 73 additions & 0 deletions
73
gopls/internal/lsp/analysis/embeddirective/testdata/src/a/import_present.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package a | ||
|
||
// Misplaced, above imports. | ||
//go:embed embedText // want "go:embed directives must precede a \"var\" declaration" | ||
|
||
import ( | ||
"fmt" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed embedText // ok | ||
var s string | ||
|
||
// The analyzer does not check for many directives using the same var. | ||
// | ||
//go:embed embedText // ok | ||
//go:embed embedText // ok | ||
var s string | ||
|
||
// Comments and blank lines between are OK. | ||
// | ||
//go:embed embedText // ok | ||
// | ||
// foo | ||
|
||
var s string | ||
|
||
// Followed by wrong kind of decl. | ||
// | ||
//go:embed embedText // want "go:embed directives must precede a \"var\" declaration" | ||
func foo() | ||
|
||
// Multiple variable specs. | ||
// | ||
//go:embed embedText // want "declarations following go:embed directives must define a single variable" | ||
var foo, bar []byte | ||
|
||
// Specifying a value is not allowed. | ||
// | ||
//go:embed embedText // want "declarations following go:embed directives must not specify a value" | ||
var s string = "foo" | ||
|
||
// TODO: This should not be OK, misplaced according to compiler. | ||
// | ||
//go:embed embedText // ok | ||
var ( | ||
s string | ||
x string | ||
) | ||
|
||
// var blocks are OK as long as the variable following the directive is OK. | ||
var ( | ||
x, y, z string | ||
//go:embed embedText // ok | ||
s string | ||
q, r, t string | ||
) | ||
|
||
//go:embed embedText // want "go:embed directives must precede a \"var\" declaration" | ||
var () | ||
|
||
// This is main function | ||
func main() { | ||
fmt.Println(s) | ||
} | ||
|
||
// No declaration following. | ||
//go:embed embedText // want "go:embed directives must precede a \"var\" declaration" |
26 changes: 26 additions & 0 deletions
26
gopls/internal/lsp/analysis/embeddirective/testdata/src/a/import_present_go120.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package a | ||
|
||
var ( | ||
// Okay directive wise but the compiler will complain that | ||
// imports must appear before other declarations. | ||
//go:embed embedText // ok | ||
"foo" | ||
) | ||
|
||
import ( | ||
"fmt" | ||
|
||
_ "embed" | ||
) | ||
|
||
// This is main function | ||
func main() { | ||
fmt.Println(s) | ||
} |