Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node-anchor-not-indented #584

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading