From 15e7ba5e989b744930480a7087fdaac3450bb47e Mon Sep 17 00:00:00 2001 From: magodo Date: Sun, 18 Aug 2024 20:42:49 +0800 Subject: [PATCH 1/2] Extend symbols with concrete `*hcl.(Attribute/Block/Expression)` --- decoder/symbol.go | 12 +++- decoder/symbols.go | 4 ++ decoder/symbols_hcl_test.go | 119 +++++++++++++++++++++++------------ decoder/symbols_json_test.go | 81 +++++++++++++++++------- 4 files changed, 151 insertions(+), 65 deletions(-) diff --git a/decoder/symbol.go b/decoder/symbol.go index 71ffe3f2..f6b2044c 100644 --- a/decoder/symbol.go +++ b/decoder/symbol.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/hcl-lang/lang" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" ) type symbolImplSigil struct{} @@ -27,6 +28,7 @@ type Symbol interface { type BlockSymbol struct { Type string Labels []string + Block *hcl.Block path lang.Path rng hcl.Range @@ -71,8 +73,9 @@ func (bs *BlockSymbol) Path() lang.Path { // AttributeSymbol is Symbol implementation representing an attribute type AttributeSymbol struct { - AttrName string - ExprKind lang.SymbolExprKind + AttrName string + ExprKind lang.SymbolExprKind + Attribute *hcl.Attribute path lang.Path rng hcl.Range @@ -115,6 +118,11 @@ type ExprSymbol struct { ExprName string ExprKind lang.SymbolExprKind + // Expr is set if the outter Expression is a *hclsyntax.TupleConsExpr + Expr hcl.Expression + // Item is set if the outter Expression is a *hclsyntax.ObjectConsExpr + Item hclsyntax.ObjectConsItem + path lang.Path rng hcl.Range nestedSymbols []Symbol diff --git a/decoder/symbols.go b/decoder/symbols.go index 68b18e58..6c2ef304 100644 --- a/decoder/symbols.go +++ b/decoder/symbols.go @@ -104,6 +104,7 @@ func (d *PathDecoder) symbolsForBody(body hcl.Body, bodySchema *schema.BodySchem symbols = append(symbols, &AttributeSymbol{ AttrName: name, ExprKind: symbolExprKind(attr.Expr), + Attribute: attr, path: d.path, rng: attr.Range, nestedSymbols: d.nestedSymbolsForExpr(attr.Expr), @@ -124,6 +125,7 @@ func (d *PathDecoder) symbolsForBody(body hcl.Body, bodySchema *schema.BodySchem symbols = append(symbols, &BlockSymbol{ Type: block.Type, Labels: block.Labels, + Block: block.Block, path: d.path, rng: block.Range, nestedSymbols: d.symbolsForBody(block.Body, bSchema), @@ -171,6 +173,7 @@ func (d *PathDecoder) nestedSymbolsForExpr(expr hcl.Expression) []Symbol { symbols = append(symbols, &ExprSymbol{ ExprName: fmt.Sprintf("%d", i), ExprKind: symbolExprKind(item), + Expr: item, path: d.path, rng: item.Range(), nestedSymbols: d.nestedSymbolsForExpr(item), @@ -187,6 +190,7 @@ func (d *PathDecoder) nestedSymbolsForExpr(expr hcl.Expression) []Symbol { symbols = append(symbols, &ExprSymbol{ ExprName: key.AsString(), ExprKind: symbolExprKind(item.ValueExpr), + Item: item, path: d.path, rng: hcl.RangeBetween(item.KeyExpr.Range(), item.ValueExpr.Range()), nestedSymbols: d.nestedSymbolsForExpr(item.ValueExpr), diff --git a/decoder/symbols_hcl_test.go b/decoder/symbols_hcl_test.go index 33b244d5..c52fbb88 100644 --- a/decoder/symbols_hcl_test.go +++ b/decoder/symbols_hcl_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/hcl-lang/lang" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" @@ -81,7 +82,8 @@ provider "google" { "aws_vpc", "main", }, - path: lang.Path{Path: dirPath}, + Block: f1.InnermostBlockAtPos(hcl.Pos{Byte: 1}), + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "first.tf", Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, @@ -89,9 +91,10 @@ provider "google" { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "cidr_block", - ExprKind: lang.LiteralTypeKind{Type: cty.String}, - path: lang.Path{Path: dirPath}, + AttrName: "cidr_block", + ExprKind: lang.LiteralTypeKind{Type: cty.String}, + Attribute: bodyMustJustAttributes(t, f1.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["cidr_block"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "first.tf", Start: hcl.Pos{Line: 3, Column: 3, Byte: 31}, @@ -106,7 +109,8 @@ provider "google" { Labels: []string{ "google", }, - path: lang.Path{Path: dirPath}, + Block: f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}), + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, @@ -114,9 +118,10 @@ provider "google" { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "project", - ExprKind: lang.LiteralTypeKind{Type: cty.String}, - path: lang.Path{Path: dirPath}, + AttrName: "project", + ExprKind: lang.LiteralTypeKind{Type: cty.String}, + Attribute: bodyMustJustAttributes(t, f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["project"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 3, Column: 3, Byte: 23}, @@ -125,9 +130,10 @@ provider "google" { nestedSymbols: []Symbol{}, }, &AttributeSymbol{ - AttrName: "region", - ExprKind: lang.LiteralTypeKind{Type: cty.String}, - path: lang.Path{Path: dirPath}, + AttrName: "region", + ExprKind: lang.LiteralTypeKind{Type: cty.String}, + Attribute: bodyMustJustAttributes(t, f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["region"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 4, Column: 3, Byte: 55}, @@ -139,7 +145,7 @@ provider "google" { }, } - diff := cmp.Diff(expectedSymbols, symbols) + diff := cmp.Diff(expectedSymbols, symbols, cmpopts.IgnoreFields(BlockSymbol{}, "Block")) if diff != "" { t.Fatalf("unexpected symbols: %s", diff) } @@ -189,7 +195,8 @@ resource "aws_instance" "test" { "aws_instance", "test", }, - path: lang.Path{Path: dirPath}, + Block: f.InnermostBlockAtPos(hcl.Pos{Byte: 1}), + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, @@ -197,9 +204,10 @@ resource "aws_instance" "test" { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "subnet_ids", - ExprKind: lang.TupleConsExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "subnet_ids", + ExprKind: lang.TupleConsExprKind{}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -219,6 +227,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.String, }, + Expr: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"].Expr.(*hclsyntax.TupleConsExpr).ExprList()[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -240,6 +249,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.String, }, + Expr: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"].Expr.(*hclsyntax.TupleConsExpr).ExprList()[1], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -259,9 +269,10 @@ resource "aws_instance" "test" { }, }, &AttributeSymbol{ - AttrName: "configuration", - ExprKind: lang.ObjectConsExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "configuration", + ExprKind: lang.ObjectConsExprKind{}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -281,6 +292,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.String, }, + Item: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"].Expr.(*hclsyntax.ObjectConsExpr).Items[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -302,6 +314,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.Number, }, + Item: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"].Expr.(*hclsyntax.ObjectConsExpr).Items[1], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -323,6 +336,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.Bool, }, + Item: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"].Expr.(*hclsyntax.ObjectConsExpr).Items[2], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -342,9 +356,10 @@ resource "aws_instance" "test" { }, }, &AttributeSymbol{ - AttrName: "random_kw", - ExprKind: lang.ReferenceExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "random_kw", + ExprKind: lang.ReferenceExprKind{}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["random_kw"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -414,7 +429,8 @@ resource "aws_instance" "test" { "aws_instance", "test", }, - path: lang.Path{Path: dirPath}, + path: lang.Path{Path: dirPath}, + Block: f.InnermostBlockAtPos(hcl.Pos{Byte: 1}), rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, @@ -422,9 +438,10 @@ resource "aws_instance" "test" { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "subnet_ids", - ExprKind: lang.TupleConsExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "subnet_ids", + ExprKind: lang.TupleConsExprKind{}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -442,6 +459,7 @@ resource "aws_instance" "test" { &ExprSymbol{ ExprName: "0", ExprKind: lang.ReferenceExprKind{}, + Expr: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"].Expr.(*hclsyntax.TupleConsExpr).ExprList()[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -463,6 +481,7 @@ resource "aws_instance" "test" { ExprKind: lang.LiteralTypeKind{ Type: cty.String, }, + Expr: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["subnet_ids"].Expr.(*hclsyntax.TupleConsExpr).ExprList()[1], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", @@ -482,9 +501,10 @@ resource "aws_instance" "test" { }, }, &AttributeSymbol{ - AttrName: "configuration", - ExprKind: lang.ObjectConsExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "configuration", + ExprKind: lang.ObjectConsExprKind{}, + path: lang.Path{Path: dirPath}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"], rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -503,6 +523,7 @@ resource "aws_instance" "test" { ExprName: "num", ExprKind: lang.ReferenceExprKind{}, path: lang.Path{Path: dirPath}, + Item: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["configuration"].Expr.(*hclsyntax.ObjectConsExpr).Items[1], rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -521,9 +542,10 @@ resource "aws_instance" "test" { }, }, &AttributeSymbol{ - AttrName: "random_kw", - ExprKind: lang.ReferenceExprKind{}, - path: lang.Path{Path: dirPath}, + AttrName: "random_kw", + ExprKind: lang.ReferenceExprKind{}, + Attribute: bodyMustJustAttributes(t, f.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["random_kw"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf", Start: hcl.Pos{ @@ -593,7 +615,8 @@ provider "google" { Labels: []string{ "google", }, - path: lang.Path{Path: dirPath}, + Block: f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}), + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 2, Column: 1, Byte: 1}, @@ -601,9 +624,10 @@ provider "google" { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "project", - ExprKind: lang.LiteralTypeKind{Type: cty.String}, - path: lang.Path{Path: dirPath}, + AttrName: "project", + ExprKind: lang.LiteralTypeKind{Type: cty.String}, + Attribute: bodyMustJustAttributes(t, f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["project"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 3, Column: 3, Byte: 23}, @@ -612,9 +636,10 @@ provider "google" { nestedSymbols: []Symbol{}, }, &AttributeSymbol{ - AttrName: "region", - ExprKind: lang.LiteralTypeKind{Type: cty.String}, - path: lang.Path{Path: dirPath}, + AttrName: "region", + ExprKind: lang.LiteralTypeKind{Type: cty.String}, + Attribute: bodyMustJustAttributes(t, f2.InnermostBlockAtPos(hcl.Pos{Byte: 1}).Body)["region"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf", Start: hcl.Pos{Line: 4, Column: 3, Byte: 55}, @@ -631,3 +656,19 @@ provider "google" { t.Fatalf("unexpected symbols: %s", diff) } } + +func bodyMustJustAttributes(t *testing.T, body hcl.Body) hcl.Attributes { + attrs, diags := body.JustAttributes() + if diags.HasErrors() { + t.Fatalf("failed to call JustAttributes: %v", diags.Error()) + } + return attrs +} + +func bodyMustContent(t *testing.T, body hcl.Body, schema *hcl.BodySchema) *hcl.BodyContent { + bc, diags := body.Content(schema) + if diags.HasErrors() { + t.Fatal(diags.Error()) + } + return bc +} diff --git a/decoder/symbols_json_test.go b/decoder/symbols_json_test.go index bd65bce0..f28e4424 100644 --- a/decoder/symbols_json_test.go +++ b/decoder/symbols_json_test.go @@ -166,6 +166,9 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { t.Fatal(err) } + body1 := bodyMustContent(t, f1.Body, bodySchema.ToHCLSchema()) + body2 := bodyMustContent(t, f2.Body, bodySchema.ToHCLSchema()) + expectedSymbols := []Symbol{ &BlockSymbol{ Type: "resource", @@ -173,7 +176,8 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { "aws_vpc", "main", }, - path: lang.Path{Path: dirPath}, + Block: body1.Blocks[0], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "first.tf.json", Start: hcl.Pos{Line: 4, Column: 15, Byte: 49}, @@ -181,8 +185,9 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "cidr_block", - path: lang.Path{Path: dirPath}, + AttrName: "cidr_block", + Attribute: bodyMustJustAttributes(t, body1.Blocks[0].Body)["cidr_block"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "first.tf.json", Start: hcl.Pos{Line: 5, Column: 9, Byte: 59}, @@ -197,7 +202,8 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { Labels: []string{ "google", }, - path: lang.Path{Path: dirPath}, + Block: body2.Blocks[0], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf.json", Start: hcl.Pos{Line: 3, Column: 15, Byte: 32}, @@ -205,8 +211,9 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "project", - path: lang.Path{Path: dirPath}, + AttrName: "project", + Attribute: bodyMustJustAttributes(t, body2.Blocks[0].Body)["project"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf.json", Start: hcl.Pos{Line: 4, Column: 7, Byte: 40}, @@ -215,8 +222,9 @@ func TestDecoder_Symbols_json_basic(t *testing.T) { nestedSymbols: []Symbol{}, }, &AttributeSymbol{ - AttrName: "region", - path: lang.Path{Path: dirPath}, + AttrName: "region", + Attribute: bodyMustJustAttributes(t, body2.Blocks[0].Body)["region"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "second.tf.json", Start: hcl.Pos{Line: 5, Column: 7, Byte: 74}, @@ -302,6 +310,16 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { t.Fatal(pDiags) } + body := bodyMustContent(t, f.Body, bodySchema.ToHCLSchema()) + bodyConfiguration := bodyMustContent(t, body.Blocks[0].Body, bodySchema.Blocks["resource"].DependentBody[schema.NewSchemaKey(schema.DependencyKeys{ + Labels: []schema.LabelDependent{ + { + Index: 0, + Value: "aws_instance", + }, + }, + })].ToHCLSchema()) + dirPath := t.TempDir() d := NewDecoder(&testPathReader{ paths: map[string]*PathContext{ @@ -323,6 +341,7 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { &BlockSymbol{ Type: "resource", Labels: []string{"aws_instance", "test"}, + Block: body.Blocks[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", @@ -331,8 +350,9 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "subnet_ids", - path: lang.Path{Path: dirPath}, + AttrName: "subnet_ids", + Attribute: bodyMustJustAttributes(t, body.Blocks[0].Body)["subnet_ids"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 5, Column: 9, Byte: 64}, @@ -343,6 +363,7 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { &BlockSymbol{ Type: "configuration", Labels: []string{}, + Block: bodyConfiguration.Blocks[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", @@ -351,8 +372,9 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "name", - path: lang.Path{Path: dirPath}, + AttrName: "name", + Attribute: bodyMustJustAttributes(t, bodyConfiguration.Blocks[0].Body)["name"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 7, Column: 11, Byte: 137}, @@ -361,8 +383,9 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { nestedSymbols: []Symbol{}, }, &AttributeSymbol{ - AttrName: "num", - path: lang.Path{Path: dirPath}, + AttrName: "num", + Attribute: bodyMustJustAttributes(t, bodyConfiguration.Blocks[0].Body)["num"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 8, Column: 11, Byte: 163}, @@ -371,8 +394,9 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { nestedSymbols: []Symbol{}, }, &AttributeSymbol{ - AttrName: "boolattr", - path: lang.Path{Path: dirPath}, + AttrName: "boolattr", + Attribute: bodyMustJustAttributes(t, bodyConfiguration.Blocks[0].Body)["boolattr"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 9, Column: 11, Byte: 184}, @@ -383,8 +407,9 @@ func TestDecoder_Symbols_json_dependentBody(t *testing.T) { }, }, &AttributeSymbol{ - AttrName: "random_kw", - path: lang.Path{Path: dirPath}, + AttrName: "random_kw", + Attribute: bodyMustJustAttributes(t, body.Blocks[0].Body)["random_kw"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 11, Column: 9, Byte: 220}, @@ -461,6 +486,9 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { t.Fatal(pDiags) } + body := bodyMustContent(t, f.Body, bodySchema.ToHCLSchema()) + bodyConfiguration := bodyMustContent(t, body.Blocks[0].Body, bodySchema.Blocks["resource"].Body.ToHCLSchema()) + dirPath := t.TempDir() d := NewDecoder(&testPathReader{ paths: map[string]*PathContext{ @@ -482,6 +510,7 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { &BlockSymbol{ Type: "resource", Labels: []string{"aws_instance", "test"}, + Block: body.Blocks[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", @@ -490,8 +519,9 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "subnet_ids", - path: lang.Path{Path: dirPath}, + AttrName: "subnet_ids", + Attribute: bodyMustJustAttributes(t, body.Blocks[0].Body)["subnet_ids"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 5, Column: 9, Byte: 64}, @@ -502,6 +532,7 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { &BlockSymbol{ Type: "configuration", Labels: []string{}, + Block: bodyConfiguration.Blocks[0], path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", @@ -510,8 +541,9 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { }, nestedSymbols: []Symbol{ &AttributeSymbol{ - AttrName: "num", - path: lang.Path{Path: dirPath}, + AttrName: "num", + Attribute: bodyMustJustAttributes(t, bodyConfiguration.Blocks[0].Body)["num"], + path: lang.Path{Path: dirPath}, rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 8, Column: 11, Byte: 175}, @@ -522,8 +554,9 @@ func TestDecoder_Symbols_json_unknownExpression(t *testing.T) { }, }, &AttributeSymbol{ - AttrName: "random_kw", - path: lang.Path{Path: dirPath}, + AttrName: "random_kw", + path: lang.Path{Path: dirPath}, + Attribute: bodyMustJustAttributes(t, body.Blocks[0].Body)["random_kw"], rng: hcl.Range{ Filename: "test.tf.json", Start: hcl.Pos{Line: 12, Column: 9, Byte: 295}, From 9a70a92b946de4f42dd2decc0d464038fc155cb1 Mon Sep 17 00:00:00 2001 From: magodo Date: Sun, 18 Aug 2024 21:04:27 +0800 Subject: [PATCH 2/2] update comment --- decoder/symbol.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/decoder/symbol.go b/decoder/symbol.go index f6b2044c..86635fff 100644 --- a/decoder/symbol.go +++ b/decoder/symbol.go @@ -118,9 +118,9 @@ type ExprSymbol struct { ExprName string ExprKind lang.SymbolExprKind - // Expr is set if the outter Expression is a *hclsyntax.TupleConsExpr + // Expr is set if the parent Expression is a *hclsyntax.TupleConsExpr Expr hcl.Expression - // Item is set if the outter Expression is a *hclsyntax.ObjectConsExpr + // Item is set if the parent Expression is a *hclsyntax.ObjectConsExpr Item hclsyntax.ObjectConsItem path lang.Path