hcl/hclsyntax: Produce better error message for invalid apostrophe

This commit is contained in:
Radek Simko 2019-01-25 13:51:43 +00:00
parent d43e8fd7d9
commit 6d20f625a6
2 changed files with 52 additions and 0 deletions

View File

@ -2109,6 +2109,44 @@ block "valid" {}
},
},
},
{
`a = 'str'`,
2, // Invalid character and expression
&Body{
Attributes: Attributes{
"a": {
Name: "a",
Expr: &LiteralValueExpr{
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 5, Byte: 4},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
},
NameRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 2, Byte: 1},
},
EqualsRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 3, Byte: 2},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
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: 1, Column: 10, Byte: 9},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
},
},
},
{
"a = sort(data.first.ref.attr)[count.index]\n",
0,

View File

@ -184,6 +184,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
toldBitwise := 0
toldExponent := 0
toldBacktick := 0
toldApostrophe := 0
toldSemicolon := 0
toldTabs := 0
toldBadUTF8 := 0
@ -238,6 +239,19 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
if toldBacktick <= 2 {
toldBacktick++
}
case TokenApostrophe:
if (toldApostrophe % 2) == 0 {
newDiag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "The ' character is not valid. Use double quotes (\") to enclose strings.",
Subject: &tok.Range,
}
diags = append(diags, newDiag)
}
if toldApostrophe <= 2 {
toldApostrophe++
}
case TokenSemicolon:
if toldSemicolon < 1 {
diags = append(diags, &hcl.Diagnostic{