Skip to content

Commit

Permalink
refactor: remove name suggestion in evaluation.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Sep 3, 2024
1 parent 0f47cf5 commit 45940ae
Show file tree
Hide file tree
Showing 11 changed files with 8 additions and 278 deletions.
27 changes: 0 additions & 27 deletions didyoumean.go

This file was deleted.

27 changes: 0 additions & 27 deletions hclsyntax/didyoumean.go

This file was deleted.

54 changes: 0 additions & 54 deletions hclsyntax/didyoumean_test.go

This file was deleted.

19 changes: 2 additions & 17 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,11 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
},
}
} else {
suggestion := nameSuggestion(name, avail)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %s%s?", namespace, suggestion)
}

return cty.DynamicVal, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Call to unknown function",
Detail: fmt.Sprintf("There is no function named %q in namespace %s.%s", name, namespace, suggestion),
Detail: fmt.Sprintf("There is no function named %q in namespace %s", name, namespace),
Subject: &e.NameRange,
Context: e.Range().Ptr(),
Expression: e,
Expand All @@ -321,21 +316,11 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
}
}
}

avail := make([]string, 0, len(ctx.Functions))
for name := range ctx.Functions {
avail = append(avail, name)
}
suggestion := nameSuggestion(e.Name, avail)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
}

return cty.DynamicVal, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Call to unknown function",
Detail: fmt.Sprintf("There is no function named %q.%s", e.Name, suggestion),
Detail: fmt.Sprintf("There is no function named %q", e.Name),
Subject: &e.NameRange,
Context: e.Range().Ptr(),
Expression: e,
Expand Down
8 changes: 1 addition & 7 deletions hclsyntax/parser_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,17 +597,11 @@ Token:

default:
if !p.recovery {
suggestions := []string{"if", "for", "else", "endif", "endfor"}
given := string(kw.Bytes)
suggestion := nameSuggestion(given, suggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
}

diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid template control keyword",
Detail: fmt.Sprintf("%q is not a valid template control keyword.%s", given, suggestion),
Detail: fmt.Sprintf("%q is not a valid template control keyword", given),
Subject: &kw.Range,
Context: hcl.RangeBetween(next.Range, kw.Range).Ptr(),
})
Expand Down
34 changes: 2 additions & 32 deletions hclsyntax/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,10 @@ func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostic
}
suggestions = append(suggestions, attrS.Name)
}
suggestion := nameSuggestion(name, suggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
} else {
// Is there a block of the same name?
for _, blockS := range schema.Blocks {
if blockS.Type == name {
suggestion = fmt.Sprintf(" Did you mean to define a block of type %q?", name)
break
}
}
}

diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported argument",
Detail: fmt.Sprintf("An argument named %q is not expected here.%s", name, suggestion),
Detail: fmt.Sprintf("An argument named %q is not expected here", name),
Subject: &attr.NameRange,
})
}
Expand All @@ -94,27 +81,10 @@ func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostic
for _, block := range b.Blocks {
blockTy := block.Type
if _, hidden := remain.hiddenBlocks[blockTy]; !hidden {
var suggestions []string
for _, blockS := range schema.Blocks {
suggestions = append(suggestions, blockS.Type)
}
suggestion := nameSuggestion(blockTy, suggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
} else {
// Is there an attribute of the same name?
for _, attrS := range schema.Attributes {
if attrS.Name == blockTy {
suggestion = fmt.Sprintf(" Did you mean to define argument %q? If so, use the equals sign to assign it a value.", blockTy)
break
}
}
}

diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported block type",
Detail: fmt.Sprintf("Blocks of type %q are not expected here.%s", blockTy, suggestion),
Detail: fmt.Sprintf("Blocks of type %q are not expected here", blockTy),
Subject: &block.TypeRange,
})
}
Expand Down
36 changes: 0 additions & 36 deletions json/didyoumean.go

This file was deleted.

52 changes: 0 additions & 52 deletions json/didyoumean_test.go

This file was deleted.

7 changes: 1 addition & 6 deletions json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,11 @@ func parseKeyword(p *peeker) (node, hcl.Diagnostics) {
},
}
default:
var dym string
if suggest := keywordSuggestion(s); suggest != "" {
dym = fmt.Sprintf(" Did you mean %q?", suggest)
}

return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Invalid JSON keyword",
Detail: fmt.Sprintf("%q is not a valid JSON keyword.%s", s, dym),
Detail: fmt.Sprintf("%q is not a valid JSON keyword", s),
Subject: &tok.Range,
},
}
Expand Down
7 changes: 1 addition & 6 deletions json/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,10 @@ func (b *body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostic
}

if _, ok := hiddenAttrs[k]; !ok {
suggestion := nameSuggestion(k, nameSuggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
}

diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Extraneous JSON object property",
Detail: fmt.Sprintf("No argument or block type is named %q.%s", k, suggestion),
Detail: fmt.Sprintf("No argument or block type is named %q", k),
Subject: &attr.NameRange,
Context: attr.Range().Ptr(),
})
Expand Down
15 changes: 1 addition & 14 deletions traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,11 @@ func (t Traversal) TraverseAbs(ctx *EvalContext) (cty.Value, Diagnostics) {
}
}

suggestions := make([]string, 0, len(ctx.Variables))
thisCtx = ctx
for thisCtx != nil {
for k := range thisCtx.Variables {
suggestions = append(suggestions, k)
}
thisCtx = thisCtx.parent
}
suggestion := nameSuggestion(name, suggestions)
if suggestion != "" {
suggestion = fmt.Sprintf(" Did you mean %q?", suggestion)
}

return cty.DynamicVal, Diagnostics{
{
Severity: DiagError,
Summary: "Unknown variable",
Detail: fmt.Sprintf("There is no variable named %q.%s", name, suggestion),
Detail: fmt.Sprintf("There is no variable named %q", name),
Subject: &root.SrcRange,
},
}
Expand Down

0 comments on commit 45940ae

Please sign in to comment.