Add regression test for YAML nested anchor space issue (#22286)

This test reproduces and characterizes the issue where YAML parsing fails
with "Expected token" when an alias reference is used as a key in a nested
block mapping with a space before the colon.

The issue occurs specifically in this pattern:
```yaml
anchor: &name value
parent:
  *name : other_value  # Fails with "Expected token"
```

While this pattern works correctly:
```yaml
anchor: &name value
*name : other_value    # Works fine at root level
```

The test documents the current behavior and provides a foundation for
implementing the fix in the YAML parser's block mapping logic.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude Bot
2025-08-31 23:58:29 +00:00
parent 1400e05e11
commit 894f9685f8

View File

@@ -0,0 +1,67 @@
import { test, expect } from "bun:test";
// Regression test for GitHub issue #22286
// https://github.com/oven-sh/bun/issues/22286
// "YAML nested anchors fail with 'Expected token'"
test("anchor reference with space before colon in nested mappings", () => {
// Root level alias with space works fine
const rootLevelAlias = "anchor: &test 'value'\n*test : 'other'";
const rootResult = Bun.YAML.parse(rootLevelAlias);
expect(rootResult).toEqual({
anchor: "value",
value: "other"
});
// Nested alias without space gives expected "Unresolved alias" error
const nestedNoSpace = "anchor: &test 'value'\nparent:\n *test: 'other'";
expect(() => Bun.YAML.parse(nestedNoSpace)).toThrow("Unresolved alias");
// Nested alias WITH space should also give "Unresolved alias" but instead gives "Expected token"
// This is the bug we're fixing
const nestedWithSpace = "anchor: &test 'value'\nparent:\n *test : 'other'";
expect(() => Bun.YAML.parse(nestedWithSpace)).toThrow("Expected token");
});
test("original issue reproduction", () => {
// The exact case from the GitHub issue
const originalIssue = `
my_anchor: &MyAnchor "MyAnchor"
my_config:
*MyAnchor :
some_key: "some_value"
`;
// Should parse successfully once the bug is fixed, but currently fails
expect(() => Bun.YAML.parse(originalIssue)).toThrow("Expected token");
});
test("expected behavior after fix", () => {
// Once the bug is fixed, these should work correctly
// Simple case: nested alias with space should resolve like the no-space case
const simpleNestedCase = "anchor: &test 'value'\nparent:\n *test : 'other'";
// Complex case: the original issue should parse to the expected structure
const originalCase = `
my_anchor: &MyAnchor "MyAnchor"
my_config:
*MyAnchor :
some_key: "some_value"
`;
const expectedResult = {
my_anchor: "MyAnchor",
my_config: {
MyAnchor: {
some_key: "some_value"
}
}
};
// These tests are commented out until the bug is fixed
// expect(() => Bun.YAML.parse(simpleNestedCase)).toThrow("Unresolved alias");
// expect(Bun.YAML.parse(originalCase)).toEqual(expectedResult);
});