Skip to content

Commit 0503882

Browse files
paultyngradeksimko
andauthoredJul 16, 2020
Fix some issues observed via staticcheck (hashicorp#234)
* Fix some issues observed via staticcheck * remove unused MockCall's unmarshaler Co-authored-by: Radek Simko <[email protected]>
1 parent 5924806 commit 0503882

File tree

15 files changed

+27
-94
lines changed

15 files changed

+27
-94
lines changed
 

Diff for: ‎internal/filesystem/file.go

-6
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ import (
66

77
"github.com/hashicorp/hcl/v2"
88
"github.com/hashicorp/terraform-ls/internal/source"
9-
encunicode "golang.org/x/text/encoding/unicode"
109
)
1110

12-
var utf16encoding = encunicode.UTF16(encunicode.LittleEndian, encunicode.IgnoreBOM)
13-
var utf16encoder = utf16encoding.NewEncoder()
14-
var utf16decoder = utf16encoding.NewDecoder()
15-
1611
type file struct {
1712
fullPath string
1813
content []byte
1914
open bool
2015

2116
version int
2217
ls source.Lines
23-
errs bool
2418
}
2519

2620
func NewFile(fullPath string, content []byte) *file {

Diff for: ‎internal/hcl/hcl.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
"github.com/hashicorp/hcl/v2"
7-
hcllib "github.com/hashicorp/hcl/v2"
87
"github.com/hashicorp/hcl/v2/hclsyntax"
98
"github.com/hashicorp/terraform-ls/internal/filesystem"
109
)
@@ -16,7 +15,7 @@ type file struct {
1615
}
1716

1817
type parsedFile struct {
19-
Body hcllib.Body
18+
Body hcl.Body
2019
Tokens hclsyntax.Tokens
2120
}
2221

@@ -54,17 +53,17 @@ func NewTestFile(b []byte) TokenizedFile {
5453

5554
func NewTestBlock(b []byte) (TokenizedBlock, error) {
5655
f := NewTestFile(b)
57-
return f.BlockAtPosition(hcllib.InitialPos)
56+
return f.BlockAtPosition(hcl.InitialPos)
5857
}
5958

6059
func (f *file) parse() (*parsedFile, error) {
6160
if f.pf != nil {
6261
return f.pf, nil
6362
}
6463

65-
var parseDiags hcllib.Diagnostics
64+
var parseDiags hcl.Diagnostics
6665

67-
tokens, diags := hclsyntax.LexConfig(f.content, f.filename, hcllib.InitialPos)
66+
tokens, diags := hclsyntax.LexConfig(f.content, f.filename, hcl.InitialPos)
6867
if diags.HasErrors() {
6968
parseDiags = append(parseDiags, diags...)
7069
}
@@ -94,14 +93,14 @@ func (f *file) PosInBlock(pos hcl.Pos) bool {
9493
return true
9594
}
9695

97-
func (f *file) BlockAtPosition(pos hcllib.Pos) (TokenizedBlock, error) {
96+
func (f *file) BlockAtPosition(pos hcl.Pos) (TokenizedBlock, error) {
9897
pf, _ := f.parse()
9998

10099
body, ok := pf.Body.(*hclsyntax.Body)
101100
if !ok {
102101
return nil, fmt.Errorf("unexpected body type (%T)", body)
103102
}
104-
if body.SrcRange.Empty() && pos != hcllib.InitialPos {
103+
if body.SrcRange.Empty() && pos != hcl.InitialPos {
105104
return nil, &InvalidHclPosErr{pos, body.SrcRange}
106105
}
107106
if !body.SrcRange.Empty() {
@@ -123,7 +122,7 @@ func (f *file) BlockAtPosition(pos hcllib.Pos) (TokenizedBlock, error) {
123122
return nil, &NoBlockFoundErr{pos}
124123
}
125124

126-
func (f *file) TokenAtPosition(pos hcllib.Pos) (hclsyntax.Token, error) {
125+
func (f *file) TokenAtPosition(pos hcl.Pos) (hclsyntax.Token, error) {
127126
pf, _ := f.parse()
128127

129128
for _, t := range pf.Tokens {
@@ -135,7 +134,7 @@ func (f *file) TokenAtPosition(pos hcllib.Pos) (hclsyntax.Token, error) {
135134
return hclsyntax.Token{}, &NoTokenFoundErr{pos}
136135
}
137136

138-
func tokensInRange(tokens hclsyntax.Tokens, rng hcllib.Range) hclsyntax.Tokens {
137+
func tokensInRange(tokens hclsyntax.Tokens, rng hcl.Range) hclsyntax.Tokens {
139138
var ts hclsyntax.Tokens
140139

141140
for _, t := range tokens {
@@ -202,7 +201,7 @@ func definitionTokens(tokens hclsyntax.Tokens) hclsyntax.Tokens {
202201
return tokens
203202
}
204203

205-
func posIsEqual(a, b hcllib.Pos) bool {
204+
func posIsEqual(a, b hcl.Pos) bool {
206205
return a.Byte == b.Byte &&
207206
a.Column == b.Column &&
208207
a.Line == b.Line

Diff for: ‎internal/hcl/hcl_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/google/go-cmp/cmp/cmpopts"
99
"github.com/hashicorp/hcl/v2"
1010
"github.com/hashicorp/hcl/v2/hclsyntax"
11-
"github.com/hashicorp/terraform-ls/internal/filesystem"
1211
)
1312

1413
func TestFile_BlockAtPosition(t *testing.T) {
@@ -260,12 +259,3 @@ provider "aws" {
260259
})
261260
}
262261
}
263-
264-
type testPosition struct {
265-
filesystem.FileHandler
266-
pos hcl.Pos
267-
}
268-
269-
func (p *testPosition) Position() hcl.Pos {
270-
return p.pos
271-
}

Diff for: ‎internal/lsp/file_handler_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
)
88

99
var (
10-
validUnixPath = "file:///valid/path/to/file.tf"
11-
validWindowsPath = "file:///C:/Users/With%20Space/tf-test/file.tf"
10+
validUnixPath = "file:///valid/path/to/file.tf"
1211
)
1312

1413
func TestFileHandler_invalid(t *testing.T) {

Diff for: ‎internal/lsp/file_handler_windows_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"github.com/sourcegraph/go-lsp"
77
)
88

9+
var (
10+
validWindowsPath = "file:///C:/Users/With%20Space/tf-test/file.tf"
11+
)
12+
913
func TestFileHandler_valid_windows(t *testing.T) {
1014
path := "file:///C:/Users/With%20Space/tf-test/file.tf"
1115
fh := FileHandlerFromDocumentURI(lsp.DocumentURI(path))

Diff for: ‎internal/terraform/exec/exec.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ func (e *Executor) cmd(ctx context.Context, args ...string) (*command, error) {
109109
// so we don't need to ask checkpoint for upgrades.
110110
cmd.Env = append(cmd.Env, "CHECKPOINT_DISABLE=1")
111111

112-
for _, envVar := range passthroughEnvVars {
113-
cmd.Env = append(cmd.Env, envVar)
114-
}
112+
cmd.Env = append(cmd.Env, passthroughEnvVars...)
115113

116114
if e.execLogPath != "" {
117115
logPath, err := logging.ParseExecLogPath(cmd.Args, e.execLogPath)
@@ -268,7 +266,7 @@ func (e *Executor) Version(ctx context.Context) (string, error) {
268266
if len(lines) < 1 {
269267
return "", fmt.Errorf("unexpected version output: %q", outString)
270268
}
271-
version := strings.TrimLeft(lines[0], "Terraform v")
269+
version := strings.TrimPrefix(lines[0], "Terraform v")
272270

273271
return version, nil
274272
}

Diff for: ‎internal/terraform/exec/exec_mock.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ func (mc *MockCall) MarshalJSON() ([]byte, error) {
4949
return json.Marshal(q)
5050
}
5151

52-
func (mc *MockCall) UnmarshalJSON(b []byte) error {
53-
q := MockQueue{}
54-
err := json.Unmarshal(b, &q)
55-
if err != nil {
56-
return err
57-
}
58-
59-
mc = (*MockCall)(q.Q[0])
60-
return nil
61-
}
62-
6352
func (mc *MockCall) NextMockItem() *MockItem {
6453
return (*MockItem)(mc)
6554
}
@@ -78,7 +67,7 @@ func (mc *MockQueue) NextMockItem() *MockItem {
7867
}
7968

8069
func MockExecutor(md MockItemDispenser) ExecutorFactory {
81-
return func(path string) *Executor {
70+
return func(_ string) *Executor {
8271
if md == nil {
8372
md = &MockCall{
8473
MockError: "no mocks provided",

Diff for: ‎internal/terraform/lang/datasource_block.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ func (r *datasourceBlock) Labels() []*ParsedLabel {
8383
return r.labels
8484
}
8585

86-
labels := make([]*ParsedLabel, len(r.labelSchema))
87-
labels = ParseLabels(r.tBlock, r.labelSchema)
86+
labels := ParseLabels(r.tBlock, r.labelSchema)
8887
r.labels = labels
8988

9089
return r.labels

Diff for: ‎internal/terraform/lang/errors.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package lang
33
import (
44
"fmt"
55
"reflect"
6-
7-
"github.com/hashicorp/hcl/v2"
86
)
97

108
type emptyCfgErr struct {
119
}
1210

1311
func (e *emptyCfgErr) Error() string {
14-
return fmt.Sprintf("empty config")
12+
return "empty config"
1513
}
1614

1715
var EmptyConfigErr = &emptyCfgErr{}
@@ -28,14 +26,6 @@ func (e *unknownBlockTypeErr) Error() string {
2826
return fmt.Sprintf("unknown block type: %q", e.BlockType)
2927
}
3028

31-
type unsupportedConfigTypeErr struct {
32-
Body hcl.Body
33-
}
34-
35-
func (e *unsupportedConfigTypeErr) Error() string {
36-
return fmt.Sprintf("unsupported body type: %T", e.Body)
37-
}
38-
3929
type noSchemaReaderErr struct {
4030
BlockType string
4131
}

Diff for: ‎internal/terraform/rootmodule/module_manifest.go

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func ParseModuleManifestFromFile(path string) (*moduleManifest, error) {
105105
}
106106

107107
mm, err := parseModuleManifest(b)
108+
if err != nil {
109+
return nil, err
110+
}
108111
mm.rootDir = rootModuleDirFromFilePath(path)
109112

110113
return mm, nil

Diff for: ‎internal/terraform/rootmodule/root_module_manager.go

-27
Original file line numberDiff line numberDiff line change
@@ -234,33 +234,6 @@ func (rmm *rootModuleManager) IsTerraformLoaded(path string) (bool, error) {
234234
return rm.IsTerraformLoaded(), nil
235235
}
236236

237-
func (rmm *rootModuleManager) terraformExecutorForDir(ctx context.Context, dir string) (*exec.Executor, error) {
238-
tfPath := rmm.tfExecPath
239-
if tfPath == "" {
240-
var err error
241-
d := &discovery.Discovery{}
242-
tfPath, err = d.LookPath()
243-
if err != nil {
244-
return nil, err
245-
}
246-
}
247-
248-
tf := exec.NewExecutor(tfPath)
249-
250-
tf.SetWorkdir(dir)
251-
tf.SetLogger(rmm.logger)
252-
253-
if rmm.tfExecLogPath != "" {
254-
tf.SetExecLogPath(rmm.tfExecLogPath)
255-
}
256-
257-
if rmm.tfExecTimeout != 0 {
258-
tf.SetTimeout(rmm.tfExecTimeout)
259-
}
260-
261-
return tf, nil
262-
}
263-
264237
func (rmm *rootModuleManager) CancelLoading() {
265238
for _, rm := range rmm.rms {
266239
rmm.logger.Printf("cancelling loading for %s", rm.Path())

Diff for: ‎internal/terraform/schema/errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
type NoSchemaAvailableErr struct{}
88

99
func (e *NoSchemaAvailableErr) Error() string {
10-
return fmt.Sprintf("no schema available")
10+
return "no schema available"
1111
}
1212

1313
type SchemaUnavailableErr struct {

Diff for: ‎langserver/session/session.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *session) ConfirmInitialization(req *jrpc2.Request) error {
7676
return fmt.Errorf("session was already confirmed as initalized at %s via request %s",
7777
s.initializedReqTime, s.initializedReq.ID())
7878
}
79-
return fmt.Errorf("session is not ready to be confirmed as initialized (%s).",
79+
return fmt.Errorf("session is not ready to be confirmed as initialized (%s)",
8080
s.state)
8181
}
8282
s.initializedReq = req

Diff for: ‎logging/logging.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func parseExecLogPathTemplate(args []string, rawPath string) (*template.Template
103103
func escapeArguments(rawArgs []string) string {
104104
unsafeCharsRe := regexp.MustCompile(`[^a-z-_]+`)
105105

106-
safeArgs := make([]string, len(rawArgs), len(rawArgs))
106+
safeArgs := make([]string, len(rawArgs))
107107
for _, rawArg := range rawArgs {
108108
// Replace any unsafe character with a hyphen
109109
safeArg := unsafeCharsRe.ReplaceAllString(rawArg, "-")

Diff for: ‎version.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ var version = "0.0.0"
1414
// such as "dev" (in development), "beta", "rc1", etc.
1515
var prerelease = "dev"
1616

17-
// semVer is an instance of version.Version. This has the secondary
18-
// benefit of verifying during tests and init time that our version is a
19-
// proper semantic version, which should always be the case.
20-
var semVer *goversion.Version
21-
2217
func init() {
23-
var err error
24-
semVer, err = goversion.NewVersion(version)
18+
// Verify that the version is proper semantic version, which should always be the case.
19+
_, err := goversion.NewVersion(version)
2520
if err != nil {
2621
panic(err.Error())
2722
}

0 commit comments

Comments
 (0)
Please sign in to comment.