parser: port zig fmt on/off, defer, block slice tests

Port tests:
- "line and doc comment following 'zig fmt: off'"
- "doc and line comment following 'zig fmt: off'"
- "line comment following 'zig fmt: on'"
- "doc comment following 'zig fmt: on'"
- "line and doc comment following 'zig fmt: on'"
- "doc and line comment following 'zig fmt: on'"
- "block in slice expression"
- "defer"

Fix defer node data: body goes in lhs (not rhs) to match .node
union variant.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-02-10 18:10:11 +00:00
parent 26725d687a
commit 5d4e50075a
2 changed files with 96 additions and 2 deletions

View File

@@ -2086,8 +2086,8 @@ static AstNodeIndex expectStatement(Parser* p, bool allow_defer_var) {
.tag = AST_NODE_DEFER,
.main_token = nextToken(p),
.data = {
.lhs = 0,
.rhs = expectBlockExprStatement(p),
.lhs = expectBlockExprStatement(p),
.rhs = 0,
},
});
case TOKEN_KEYWORD_ERRDEFER: {

View File

@@ -2145,6 +2145,100 @@ test "zig fmt: anytype type" {
);
}
test "zig fmt: line and doc comment following 'zig fmt: off'" {
try testCanonical(
\\// zig fmt: off
\\// test 1
\\/// test 2
\\const e = f;
);
}
test "zig fmt: doc and line comment following 'zig fmt: off'" {
try testCanonical(
\\// zig fmt: off
\\/// test 1
\\// test 2
\\const e = f;
);
}
test "zig fmt: line comment following 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
\\const e = f;
\\// zig fmt: on
\\// test
\\const e = f;
\\
);
}
test "zig fmt: doc comment following 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
\\const e = f;
\\// zig fmt: on
\\/// test
\\const e = f;
\\
);
}
test "zig fmt: line and doc comment following 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
\\const e = f;
\\// zig fmt: on
\\// test1
\\/// test2
\\const e = f;
\\
);
}
test "zig fmt: doc and line comment following 'zig fmt: on'" {
try testCanonical(
\\// zig fmt: off
\\const e = f;
\\// zig fmt: on
\\/// test1
\\// test2
\\const e = f;
\\
);
}
test "zig fmt: block in slice expression" {
try testCanonical(
\\const a = b[{
\\ _ = x;
\\}..];
\\const c = d[0..{
\\ _ = x;
\\ _ = y;
\\}];
\\const e = f[0..1 :{
\\ _ = x;
\\ _ = y;
\\ _ = z;
\\}];
\\
);
}
test "zig fmt: defer" {
try testCanonical(
\\test "defer" {
\\ defer foo();
\\ defer {
\\ bar();
\\ }
\\}
\\
);
}
test "zig fmt: container initializers" {
try testCanonical(
\\const a0 = []u8{};