Skip to content

Commit

Permalink
Merge pull request #651 from hashicorp/f-namespaced-fn-name-range
Browse files Browse the repository at this point in the history
fix: fix NameRange for namespaced functions
  • Loading branch information
ansgarm committed Feb 9, 2024
2 parents c964a71 + 9b78cd3 commit 1e1a6b8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
12 changes: 10 additions & 2 deletions hclsyntax/parser.go
Expand Up @@ -1157,6 +1157,7 @@ func (p *parser) finishParsingFunctionCall(name Token) (Expression, hcl.Diagnost
}

nameStr := string(name.Bytes)
nameEndPos := name.Range.End
for openTok.Type == TokenDoubleColon {
nextName := p.Read()
if nextName.Type != TokenIdent {
Expand All @@ -1179,10 +1180,17 @@ func (p *parser) finishParsingFunctionCall(name Token) (Expression, hcl.Diagnost
// doesn't exist in EvalContext, to return better error messages
// when namespaces are used incorrectly.
nameStr = nameStr + "::" + string(nextName.Bytes)
nameEndPos = nextName.Range.End

openTok = p.Read()
}

nameRange := hcl.Range{
Filename: name.Range.Filename,
Start: name.Range.Start,
End: nameEndPos,
}

if openTok.Type != TokenOParen {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand Down Expand Up @@ -1259,7 +1267,7 @@ Token:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unterminated function call",
Detail: "There is no closing parenthesis for this function call before the end of the file. This may be caused by incorrect parethesis nesting elsewhere in this file.",
Detail: "There is no closing parenthesis for this function call before the end of the file. This may be caused by incorrect parenthesis nesting elsewhere in this file.",
Subject: hcl.RangeBetween(name.Range, openTok.Range).Ptr(),
})
default:
Expand Down Expand Up @@ -1291,7 +1299,7 @@ Token:

ExpandFinal: expandFinal,

NameRange: name.Range,
NameRange: nameRange,
OpenParenRange: openTok.Range,
CloseParenRange: closeTok.Range,
}, diags
Expand Down
99 changes: 98 additions & 1 deletion hclsyntax/parser_test.go
Expand Up @@ -2462,6 +2462,103 @@ block "valid" {}
},
},
},
{
"a = a::namespaced::func(data.first.ref.attr)\n",
0,
&Body{
Attributes: Attributes{
"a": {
Name: "a",
Expr: &FunctionCallExpr{
Name: "a::namespaced::func",
Args: []Expression{
&ScopeTraversalExpr{
Traversal: hcl.Traversal{
hcl.TraverseRoot{
Name: "data",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 25, Byte: 24},
End: hcl.Pos{Line: 1, Column: 29, Byte: 28},
},
},
hcl.TraverseAttr{
Name: "first",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 29, Byte: 28},
End: hcl.Pos{Line: 1, Column: 35, Byte: 34},
},
},
hcl.TraverseAttr{
Name: "ref",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 35, Byte: 34},
End: hcl.Pos{Line: 1, Column: 39, Byte: 38},
},
},
hcl.TraverseAttr{
Name: "attr",
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 39, Byte: 38},
End: hcl.Pos{Line: 1, Column: 44, Byte: 43},
},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 25, Byte: 24},
End: hcl.Pos{Line: 1, Column: 44, Byte: 43},
},
},
},
ExpandFinal: false,
NameRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 5, Byte: 4},
End: hcl.Pos{Line: 1, Column: 24, Byte: 23},
},
OpenParenRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 24, Byte: 23},
End: hcl.Pos{Line: 1, Column: 25, Byte: 24},
},
CloseParenRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 44, Byte: 43},
End: hcl.Pos{Line: 1, Column: 45, Byte: 44},
},
},
SrcRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 45, Byte: 44},
},
NameRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 2, Byte: 1},
},
EqualsRange: hcl.Range{
Filename: "",
Start: hcl.Pos{Line: 1, Column: 3, Byte: 2},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
},
},
Blocks: Blocks{},
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 2, Column: 1, Byte: 45},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 2, Column: 1, Byte: 45},
End: hcl.Pos{Line: 2, Column: 1, Byte: 45},
},
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -3660,7 +3757,7 @@ func TestParseConfigDiagnostics(t *testing.T) {
{
Severity: hcl.DiagError,
Summary: "Unterminated function call",
Detail: "There is no closing parenthesis for this function call before the end of the file. This may be caused by incorrect parethesis nesting elsewhere in this file.",
Detail: "There is no closing parenthesis for this function call before the end of the file. This may be caused by incorrect parenthesis nesting elsewhere in this file.",
Subject: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
Expand Down

0 comments on commit 1e1a6b8

Please sign in to comment.