Skip to content

Commit

Permalink
fix: Include range in flow sequence pair maps (fixes #573)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Sep 3, 2024
1 parent b309e23 commit 5b5e290
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compose/resolve-flow-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export function resolveFlowCollection(
const map = new YAMLMap(ctx.schema)
map.flow = true
map.items.push(pair)
const endRange = (valueNode ?? keyNode).range
map.range = [keyNode.range[0], endRange[1], endRange[2]]
;(coll as YAMLSeq).items.push(map)
}
offset = valueNode ? valueNode.range[2] : valueProps.end
Expand Down
1 change: 1 addition & 0 deletions src/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type ParsedNode =
| YAMLMap.Parsed
| YAMLSeq.Parsed

/** `[start, value-end, node-end]` */
export type Range = [number, number, number]

export abstract class NodeBase {
Expand Down
6 changes: 6 additions & 0 deletions tests/doc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ describe('maps with no values', () => {
expect(doc.range).toEqual([0, 4, 4])
})

test('pair in flow seq has correct range (#573)', () => {
const doc = YAML.parseDocument<any, false>('[a:]')
expect(doc.range).toEqual([0, 4, 4])
expect(doc.get(0).range).toEqual([1, 3, 3])
})

test('implicit scalar key after explicit key with no value', () => {
const doc = YAML.parseDocument<YAML.YAMLMap, false>('? - 1\nx:\n')
expect(doc.contents.items).toMatchObject([
Expand Down

0 comments on commit 5b5e290

Please sign in to comment.