Skip to content

Commit

Permalink
Gracefully handle missing anchor targets
Browse files Browse the repository at this point in the history
  • Loading branch information
adamvoss committed Jul 1, 2017
1 parent beb4d45 commit 9199669
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/parser/yamlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
break;
}
case Yaml.Kind.ANCHOR_REF: {
return recursivelyBuildAst(parent, (<Yaml.YAMLAnchorReference>node).value);
const instance = (<Yaml.YAMLAnchorReference>node).value

return recursivelyBuildAst(parent, instance) ||
new NullASTNode(parent, null, node.startPosition, node.endPosition);
}
case Yaml.Kind.INCLUDE_REF: {
// Issue Warning
Expand Down
19 changes: 13 additions & 6 deletions src/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,13 +1365,20 @@ suite('YAML Parser', () => {
assertParse('{\n"far": "boo"\n}', 0);
});

test('parse with anchor reference expands reference', function() {
isValid('- foo: &ref 5\n- bar: *ref')
suite('anchor references', () => {
test('expands reference', function () {
isValid('- foo: &ref 5\n- bar: *ref')

const result = YamlParser.parse('- foo: &ref 5\n- bar: *ref').root
const expected = YamlParser.parse('- foo: 5\n- bar: 5').root
const result = YamlParser.parse('- foo: &ref 5\n- bar: *ref').root
const expected = YamlParser.parse('- foo: 5\n- bar: 5').root

assert.deepStrictEqual(result.getValue(), expected.getValue())
assert.deepStrictEqual(result.getValue(), expected.getValue())
})

test('errors on missing reference', function () {
isInvalid('- bar: *foo')
isInvalid('- foo: &ref 5\n- bar: *re')
})
})

});
});

0 comments on commit 9199669

Please sign in to comment.