Skip to content

Commit a875486

Browse files
authored
refactor: remove utils.FindInArray and replace with slices.Contains (#292)
1 parent c5f5c0b commit a875486

File tree

11 files changed

+25
-38
lines changed

11 files changed

+25
-38
lines changed

pkg/parser/context.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package parser
22

33
import (
4+
"slices"
5+
46
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
5-
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/utils"
67
)
78

89
func (doc *YamlDocument) assignContexts() {
@@ -29,13 +30,13 @@ func (doc *YamlDocument) assignContexts() {
2930
}
3031

3132
func (doc *YamlDocument) addContextToJob(job ast.Job, context string) {
32-
if utils.FindInArray(*job.Contexts, context) < 0 {
33+
if !slices.Contains(*job.Contexts, context) {
3334
*job.Contexts = append(*job.Contexts, context)
3435
}
3536
}
3637

3738
func (doc *YamlDocument) addContextToCommand(command ast.Command, context string) {
38-
if utils.FindInArray(*command.Contexts, context) < 0 {
39+
if !slices.Contains(*command.Contexts, context) {
3940
*command.Contexts = append(*command.Contexts, context)
4041
}
4142
}

pkg/parser/jsonschema.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ func (validator *JSONSchemaValidator) ValidateWithJSONSchema(rootNode *sitter.No
153153
yamlLoader := gojsonschema.NewGoLoader(file)
154154

155155
result, err := validator.schema.Validate(yamlLoader)
156-
157156
if err != nil {
158157
// Should never happen
159158
return []protocol.Diagnostic{utils.CreateErrorDiagnosticFromNode(rootNode, err.Error())}
@@ -198,10 +197,6 @@ func (validator *JSONSchemaValidator) ValidateWithJSONSchema(rootNode *sitter.No
198197
//
199198
// But in the JSON Schema, the `when` key is defined as an object, so the validation
200199
// will fail if we don't ignore it.
201-
var PARAMS_KEYS = []string{
202-
"when",
203-
}
204-
205200
func (validator *JSONSchemaValidator) doesNodeUseParameter(node *sitter.Node) bool {
206201
if node.Type() == "block_mapping_pair" {
207202
keyNode, valueNode := validator.Doc.GetKeyValueNodes(node)
@@ -211,7 +206,7 @@ func (validator *JSONSchemaValidator) doesNodeUseParameter(node *sitter.Node) bo
211206
key := validator.Doc.GetNodeText(keyNode)
212207
value := validator.Doc.GetNodeText(valueNode)
213208

214-
if isInArray := utils.FindInArray(PARAMS_KEYS, key); utils.CheckIfOnlyParamUsed(value) && isInArray > 0 {
209+
if key == "when" && utils.CheckIfOnlyParamUsed(value) {
215210
return true
216211
}
217212
}

pkg/parser/parameters.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parser
22

33
import (
44
"fmt"
5+
"slices"
56
"strconv"
67

78
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -210,7 +211,7 @@ func (doc *YamlDocument) parseEnumParameter(paramName string, paramNode *sitter.
210211
}
211212
})
212213

213-
if enumParam.HasDefault && utils.FindInArray(enumParam.Enum, enumParam.Default) == -1 {
214+
if enumParam.HasDefault && !slices.Contains(enumParam.Enum, enumParam.Default) {
214215
doc.addDiagnostic(utils.CreateErrorDiagnosticFromRange(enumParam.DefaultRange, "Default value is not in enum"))
215216
}
216217

@@ -370,7 +371,6 @@ func (doc *YamlDocument) parseParameterValue(child *sitter.Node) (ast.ParameterV
370371

371372
key := doc.GetNodeText(keyNode)
372373
paramValue, err := doc.parseParameterValue(child)
373-
374374
if err != nil {
375375
return
376376
}
@@ -500,7 +500,6 @@ func (doc *YamlDocument) parseSimpleParameterValue(paramName string, simpleParam
500500
case "integer_scalar":
501501
rawValue := doc.GetNodeText(simpleParamNode)
502502
intValue, err := strconv.Atoi(rawValue)
503-
504503
if err != nil {
505504
return ast.ParameterValue{}, fmt.Errorf("invalid integer value: %s", rawValue)
506505
}

pkg/parser/validate/enum.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validate
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -10,7 +11,7 @@ import (
1011

1112
func (val Validate) checkEnumTypeDefinition(definedParam ast.EnumParameter) {
1213
if definedParam.HasDefault {
13-
if utils.FindInArray(definedParam.Enum, definedParam.Default) == -1 {
14+
if !slices.Contains(definedParam.Enum, definedParam.Default) {
1415
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
1516
definedParam.Range,
1617
fmt.Sprintf("Default value %s is not in enum '%s'", definedParam.Default, strings.Join(definedParam.Enum, ", "))))

pkg/parser/validate/executor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validate
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -59,7 +60,7 @@ var ValidMacOSResourceClasses = []string{
5960
}
6061

6162
func (val Validate) validateMacOSExecutor(executor ast.MacOSExecutor) {
62-
if utils.FindInArray(ValidXCodeVersions, executor.Xcode) == -1 {
63+
if !slices.Contains(ValidXCodeVersions, executor.Xcode) {
6364
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
6465
executor.XcodeRange,
6566
fmt.Sprintf("Invalid Xcode version %s", executor.Xcode),
@@ -135,7 +136,7 @@ func (val Validate) validateLinuxMachineExecutor(executor ast.MachineExecutor) {
135136
}
136137

137138
func (val Validate) validateImage(img string, imgRange protocol.Range) {
138-
if utils.FindInArray(utils.ValidARMOrMachineImages, img) == -1 {
139+
if !slices.Contains(utils.ValidARMOrMachineImages, img) {
139140
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
140141
imgRange,
141142
"Invalid or deprecated image",
@@ -238,7 +239,7 @@ func (val Validate) validateWindowsExecutor(executor ast.WindowsExecutor) {
238239

239240
func (val Validate) checkIfValidResourceClass(resourceClass string, validResourceClasses []string, resourceClassRange protocol.Range) {
240241
if !utils.CheckIfOnlyParamUsed(resourceClass) && resourceClass != "" &&
241-
utils.FindInArray(validResourceClasses, resourceClass) == -1 &&
242+
!slices.Contains(validResourceClasses, resourceClass) &&
242243
!val.Doc.IsSelfHostedRunner(resourceClass) {
243244

244245
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
@@ -275,7 +276,6 @@ func (val Validate) validateExecutorNamespace(resourceClass string, resourceClas
275276

276277
var response RegistryNamespace
277278
err := client.Run(request, &response)
278-
279279
if err != nil {
280280
return
281281
}

pkg/parser/validate/parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validate
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -45,7 +46,7 @@ func (val Validate) checkParamSimpleType(param ast.ParameterValue, stepName stri
4546
}
4647

4748
value := param.Value.(string)
48-
if utils.FindInArray(definedParam.(ast.EnumParameter).Enum, value) == -1 {
49+
if !slices.Contains(definedParam.(ast.EnumParameter).Enum, value) {
4950
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
5051
param.Range,
5152
fmt.Sprintf("Parameter %s is not a valid value for %s", value, definedParam.GetName()),
@@ -128,7 +129,6 @@ func (val Validate) CheckIfParamsExist() {
128129
node := capture.Node
129130
content := val.Doc.GetRawNodeText(node)
130131
params, err := utils.GetParamsInString(content)
131-
132132
if err != nil {
133133
return
134134
}

pkg/parser/validate/steps.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validate
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -79,7 +80,7 @@ func (val Validate) validateRunCommand(step ast.Run, jobOrCommandParameters map[
7980
value = step.When
8081
}
8182

82-
if utils.FindInArray(WHEN_KEYWORDS, value) < 0 {
83+
if !slices.Contains(WHEN_KEYWORDS, value) {
8384
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
8485
step.WhenRange,
8586
fmt.Sprintf("Invalid when condition: expected `%s`; got `%s`", strings.Join(WHEN_KEYWORDS, "`, `"), value)))

pkg/parser/yamlparser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"slices"
78
"strconv"
89
"strings"
910

@@ -234,7 +235,7 @@ func (doc *YamlDocument) IsBuiltIn(commandName string) bool {
234235
"unless", // Has nothing to do here, tech debt to resolve
235236
}
236237

237-
return utils.FindInArray(builtInCommands, commandName) != -1
238+
return slices.Contains(builtInCommands, commandName)
238239
}
239240

240241
func (doc *YamlDocument) IsOrbReference(orbReference string) bool {

pkg/services/semantics.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package languageservice
22

33
import (
44
"regexp"
5+
"slices"
56
"sort"
67
"strings"
78

@@ -30,7 +31,6 @@ var PARAM_REGEX, _ = regexp.Compile(`<<\s*(parameters|pipeline.parameters)\.([A-
3031

3132
func SemanticTokens(params protocol.SemanticTokensParams, cache *utils.Cache, context *utils.LsContext) protocol.SemanticTokens {
3233
doc, err := parser.ParseFromUriWithCache(params.TextDocument.URI, cache, context)
33-
3434
if err != nil {
3535
return protocol.SemanticTokens{}
3636
}
@@ -96,7 +96,7 @@ var ROOT_KEYWORDS = []string{
9696
}
9797

9898
func (sem SemanticTokenStruct) highlightBuiltInKeywords(keyNode *sitter.Node) {
99-
if keyName := sem.doc.GetNodeText(keyNode); keyNode.Type() == "flow_node" && utils.FindInArray(KEYWORDS, keyName) != -1 {
99+
if keyName := sem.doc.GetNodeText(keyNode); keyNode.Type() == "flow_node" && slices.Contains(KEYWORDS, keyName) {
100100
length := keyNode.EndPoint().Column - keyNode.StartPoint().Column
101101
sem.addToken(protocol.Position{Line: keyNode.StartPoint().Row, Character: keyNode.StartPoint().Column}, length, 0, 0)
102102
}
@@ -119,7 +119,7 @@ func (sem SemanticTokenStruct) highlightBuiltInKeywords(keyNode *sitter.Node) {
119119
return
120120
}
121121

122-
if keyName := sem.doc.GetNodeText(keyNode); document.Type() == "document" && keyNode.Type() == "flow_node" && utils.FindInArray(ROOT_KEYWORDS, keyName) != -1 {
122+
if keyName := sem.doc.GetNodeText(keyNode); document.Type() == "document" && keyNode.Type() == "flow_node" && slices.Contains(ROOT_KEYWORDS, keyName) {
123123
length := keyNode.EndPoint().Column - keyNode.StartPoint().Column
124124
sem.addToken(protocol.Position{Line: keyNode.StartPoint().Row, Character: keyNode.StartPoint().Column}, length, 0, 0)
125125
}
@@ -131,7 +131,6 @@ func (sem SemanticTokenStruct) highlightParameters(valueNode *sitter.Node) {
131131

132132
func (sem SemanticTokenStruct) highlightCacheKeys(valueNode *sitter.Node) {
133133
reg, err := regexp.Compile(`{{ ?(.Branch|.BuildNum|.Revision|.CheckoutKey|.Environment.variableName|checksum .*|epoch|arch) ?}}`)
134-
135134
if err != nil {
136135
return
137136
}

pkg/utils/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path"
7+
"slices"
78
"sync"
89

910
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
@@ -121,7 +122,7 @@ func (c *FileCache) AddEnvVariableToProjectLinkedToFile(uri protocol.URI, envVar
121122
defer c.cacheMutex.Unlock()
122123
project := c.fileCache[uri]
123124

124-
if FindInArray(project.EnvVariables, envVariable) < 0 {
125+
if !slices.Contains(project.EnvVariables, envVariable) {
125126
project.EnvVariables = append(project.EnvVariables, envVariable)
126127
}
127128
c.fileCache[uri] = project
@@ -263,7 +264,6 @@ func CreateCache() *Cache {
263264
func GetOrbCacheFSPath(orbYaml string) string {
264265
file := path.Join("cci", "orbs", ".circleci", orbYaml+".yml")
265266
filePath, err := xdg.CacheFile(file)
266-
267267
if err != nil {
268268
filePath = path.Join(xdg.Home, ".cache", file)
269269
}
@@ -306,7 +306,7 @@ func (c *ContextCache) AddEnvVariableToOrganizationContext(organizationId string
306306
defer c.cacheMutex.Unlock()
307307
ctx := c.contextCache[organizationId][name]
308308

309-
if FindInArray(ctx.envVariables, envVariable) < 0 {
309+
if !slices.Contains(ctx.envVariables, envVariable) {
310310
ctx.envVariables = append(ctx.envVariables, envVariable)
311311
}
312312
c.contextCache[organizationId][name] = ctx

0 commit comments

Comments
 (0)