From 894f9685f832e9bd39a080ee92f8eb75884b013b Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Sun, 31 Aug 2025 23:58:29 +0000 Subject: [PATCH] Add regression test for YAML nested anchor space issue (#22286) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- test/regression/issue/22286.test.ts | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 test/regression/issue/22286.test.ts diff --git a/test/regression/issue/22286.test.ts b/test/regression/issue/22286.test.ts new file mode 100644 index 0000000000..9ae4966369 --- /dev/null +++ b/test/regression/issue/22286.test.ts @@ -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); +}); \ No newline at end of file