commit c67c54c3fbfd41c6fd14205e2d8d4784dfb1c3d2 (tree)
parent 1a8bb5ac1038336c6c49eaa39c6782543883a715
Author: Motiejus Jakštys <motiejus@jakstys.lt>
Date: Tue, 10 Feb 2026 13:08:47 +0000
parser: add zig fmt on/off and slice operator tests
Port tests from upstream parser_test.zig:
- "comment to disable/enable zig fmt"
- "line comment following 'zig fmt: off'"
- "doc comment following 'zig fmt: off'"
- "alternating 'zig fmt: off' and 'zig fmt: on'"
- "spaces around slice operator"
Fix parsePrimaryTypeExpr: don't reject identifier followed by colon;
the colon may be part of slice sentinel syntax, not a label.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/parser.c b/parser.c
@@ -509,12 +509,6 @@ static AstNodeIndex parsePrimaryTypeExpr(Parser* p) {
});
}
case TOKEN_IDENTIFIER:
- if (p->token_tags[p->tok_i + 1] == TOKEN_COLON) {
- fprintf(stderr,
- "parsePrimaryTypeExpr does not support identifier followed by "
- "colon\n");
- exit(1);
- }
return addNode(&p->nodes,
(AstNodeItem) {
.tag = AST_NODE_IDENTIFIER,
diff --git a/parser_test.zig b/parser_test.zig
@@ -1432,3 +1432,67 @@ test "zig fmt: builtin call with trailing comma" {
\\
);
}
+
+test "zig fmt: comment to disable/enable zig fmt" {
+ try testTransform(
+ \\const a = b;
+ \\// zig fmt: off
+ \\const c = d;
+ \\// zig fmt: on
+ \\const e = f;
+ ,
+ \\const a = b;
+ \\// zig fmt: off
+ \\const c = d;
+ \\// zig fmt: on
+ \\const e = f;
+ \\
+ );
+}
+
+test "zig fmt: line comment following 'zig fmt: off'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\// Test
+ \\const e = f;
+ );
+}
+
+test "zig fmt: doc comment following 'zig fmt: off'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\/// test
+ \\const e = f;
+ );
+}
+
+test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" {
+ try testCanonical(
+ \\// zig fmt: off
+ \\// zig fmt: on
+ \\// zig fmt: off
+ \\const e = f;
+ \\// zig fmt: off
+ \\// zig fmt: on
+ \\// zig fmt: off
+ \\const a = b;
+ \\// zig fmt: on
+ \\const c = d;
+ \\// zig fmt: on
+ \\
+ );
+}
+
+test "zig fmt: spaces around slice operator" {
+ try testCanonical(
+ \\var a = b[c..d];
+ \\var a = b[c..d :0];
+ \\var a = b[c + 1 .. d];
+ \\var a = b[c + 1 ..];
+ \\var a = b[c .. d + 1];
+ \\var a = b[c .. d + 1 :0];
+ \\var a = b[c.a..d.e];
+ \\var a = b[c.a..d.e :0];
+ \\
+ );
+}