Skip to content

Commit

Permalink
Fix node-anchor-not-indented (#584)
Browse files Browse the repository at this point in the history
* fix node-anchor-not-indented

* add validation for sequence value
  • Loading branch information
goccy authored Dec 10, 2024
1 parent 1aa3b7d commit ed74344
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
47 changes: 47 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token)
// &anchor
return nil, errors.ErrSyntax("anchor is not allowed in this context", tk.RawToken())
}
if tk.Column() <= keyCol && tk.Type() == token.TagType {
// key: <value does not defined>
// !!tag
return nil, errors.ErrSyntax("tag is not allowed in this context", tk.RawToken())
}

if tk.Column() < keyCol {
// in this case,
Expand Down Expand Up @@ -785,9 +790,43 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token)
if err != nil {
return nil, err
}
if err := p.validateAnchorValueInMapOrSeq(value, keyCol); err != nil {
return nil, err
}
return value, nil
}

func (p *parser) validateAnchorValueInMapOrSeq(value ast.Node, col int) error {
anchor, ok := value.(*ast.AnchorNode)
if !ok {
return nil
}
tag, ok := anchor.Value.(*ast.TagNode)
if !ok {
return nil
}
anchorTk := anchor.GetToken()
tagTk := tag.GetToken()

if anchorTk.Position.Line == tagTk.Position.Line {
// key:
// &anchor !!tag
//
// - &anchor !!tag
return nil
}

if tagTk.Position.Column <= col {
// key: &anchor
// !!tag
//
// - &anchor
// !!tag
return errors.ErrSyntax("tag is not allowed in this context", tagTk)
}
return nil
}

func (p *parser) parseAnchor(ctx *context, g *TokenGroup) (*ast.AnchorNode, error) {
anchorNameGroup := g.First().Group
anchor, err := p.parseAnchorName(ctx.withGroup(anchorNameGroup))
Expand Down Expand Up @@ -1089,6 +1128,11 @@ func (p *parser) parseSequenceValue(ctx *context, seqTk *Token) (ast.Node, error
// &anchor
return nil, errors.ErrSyntax("anchor is not allowed in this sequence context", tk.RawToken())
}
if tk.Column() <= seqCol && tk.Type() == token.TagType {
// - <value does not defined>
// !!tag
return nil, errors.ErrSyntax("tag is not allowed in this sequence context", tk.RawToken())
}

if tk.Column() < seqCol {
// in this case,
Expand Down Expand Up @@ -1120,6 +1164,9 @@ func (p *parser) parseSequenceValue(ctx *context, seqTk *Token) (ast.Node, error
if err != nil {
return nil, err
}
if err := p.validateAnchorValueInMapOrSeq(value, seqCol); err != nil {
return nil, err
}
return value, nil
}

Expand Down
1 change: 0 additions & 1 deletion yaml_test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var failureTestNames = []string{
"multiline-scalar-at-top-level", // pass yamlv3.
"multiline-scalar-at-top-level-1-3", // pass yamlv3.
"nested-implicit-complex-keys", // no json.
"node-anchor-not-indented", // pass yamlv3.
"plain-dashes-in-flow-sequence",
"question-mark-edge-cases/00", // no json.
"question-mark-edge-cases/01", // no json.
Expand Down

0 comments on commit ed74344

Please sign in to comment.