hashicorp / hashicorp/hcl

json: Reflect escaping characters in `Traversal`.`SourceRange()`

Open
#598 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

## Context

As indicated by the [inline comment](https://github.com/hashicorp/hcl/blob/e54a1960efd6cdfe35ecb8cc098bed33cd6001a8/json/structure.go#L520-L536) and discovered as part of adding support for reference related code intelligence via language server in https://github.com/hashicorp/hcl-lang/pull/185 a traversal inside a JSON config which includes string key, e.g.

```json
{"attr": "${test[\"key\"]}"}
```
has a reported range which is off by 2 for each/any string key. i.e. with every map/object key this becomes more inaccurate.

```go
hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line:1, Column:13, Byte:12},
End: hcl.Pos{Line:1, Column:24, Byte:23},
}
```
The expected range would be

```go
hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line:1, Column:13, Byte:12},
End: hcl.Pos{Line:1, Column:26, Byte:25},
}
```

See https://go.dev/play/p/pnDgnpEeLXA

## Proposal

Report accurate range for traversals with string keys inside JSON.

Contributor guide

No contributing guide indexed for this repository

Research direction

Inspect the inline comment and implementation in json/structure.go around lines 520-536. Reproduce the traversal example from the issue or the linked Go playground, then verify that SourceRange() accounts for escaped string keys and reports the expected end position. Done means ranges remain accurate as additional string keys are included.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.