libgraphql-parser 0.0.5

A blazing fast, error-focused, lossless GraphQL parser for schema, executable, and mixed documents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Tests for `GraphQLParseError` construction and formatting.
//!
//! These tests verify error construction, note management, and display
//! formatting work correctly.
//!
//! Written by Claude Code, reviewed by a human.

use crate::ByteSpan;
use crate::GraphQLErrorNote;
use crate::GraphQLErrorNoteKind;
use crate::GraphQLParseError;
use crate::GraphQLParseErrorKind;
use crate::ReservedNameContext;
use crate::smallvec::SmallVec;
use crate::SourceMap;
use crate::SourcePosition;
use crate::SourceSpan;

/// Helper to create an UnexpectedToken error kind.
fn unexpected_token_kind() -> GraphQLParseErrorKind {
    GraphQLParseErrorKind::UnexpectedToken {
        expected: vec![":".to_string()],
        found: "String".to_string(),
    }
}

/// Helper to create an UnclosedDelimiter error kind.
fn unclosed_delimiter_kind() -> GraphQLParseErrorKind {
    GraphQLParseErrorKind::UnclosedDelimiter {
        delimiter: "{".to_string(),
    }
}

// =============================================================================
// Part 3.2: Constructor Tests
// =============================================================================

/// Verifies that `GraphQLParseError::new()` creates an error with empty notes.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_new_creates_empty_notes() {
    let error = GraphQLParseError::new(
        "Expected `:`",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    assert_eq!(error.message(), "Expected `:`");
    assert!(matches!(
        error.kind(),
        GraphQLParseErrorKind::UnexpectedToken { .. }
    ));
    assert!(error.notes().is_empty());
}

/// Verifies that `GraphQLParseError::with_notes()` creates an error with
/// pre-populated notes.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_with_notes_constructor() {
    let mut notes = SmallVec::new();
    notes.push(GraphQLErrorNote::general("Additional context"));
    notes.push(GraphQLErrorNote::help("Try adding a colon here"));

    let error = GraphQLParseError::with_notes(
        "Expected `:`",
        unexpected_token_kind(),
        notes,
        SourceSpan::zero(),
    );

    assert_eq!(error.message(), "Expected `:`");
    assert_eq!(error.notes().len(), 2);
}

/// Verifies that `GraphQLParseError::from_lexer_error()` correctly converts
/// a lexer error with its notes.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_from_lexer_error() {
    let mut lexer_notes = SmallVec::new();
    lexer_notes.push(GraphQLErrorNote::general(
        "Lexer detected unterminated string",
    ));

    let error = GraphQLParseError::from_lexer_error(
        "Unterminated string",
        lexer_notes,
        SourceSpan::zero(),
    );

    assert_eq!(error.message(), "Unterminated string");
    assert!(matches!(
        error.kind(),
        GraphQLParseErrorKind::LexerError
    ));
    assert_eq!(error.notes().len(), 1);
}

// =============================================================================
// Part 3.2: Note Management Tests
// =============================================================================

/// Verifies that `add_note()` appends a general note without a span.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_add_note() {
    let mut error = GraphQLParseError::new(
        "Primary error",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    error.add_note("This is additional context");

    assert_eq!(error.notes().len(), 1);
    let note = &error.notes()[0];
    assert!(matches!(note.kind, GraphQLErrorNoteKind::General));
    assert_eq!(note.message, "This is additional context");
    assert!(note.span.is_none());
}

/// Verifies that `add_note_with_span()` appends a general note with a location.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_add_note_with_span() {
    let mut error = GraphQLParseError::new(
        "Primary error",
        unclosed_delimiter_kind(),
        SourceSpan::zero(),
    );

    error.add_note_with_span("Opening `{` here", SourceSpan::zero());

    assert_eq!(error.notes().len(), 1);
    let note = &error.notes()[0];
    assert!(matches!(note.kind, GraphQLErrorNoteKind::General));
    assert!(note.span.is_some());
}

/// Verifies that `add_help()` appends a help note without a span.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_add_help() {
    let mut error = GraphQLParseError::new(
        "Missing colon",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    error.add_help("Did you mean: `fieldName: Type`?");

    assert_eq!(error.notes().len(), 1);
    let note = &error.notes()[0];
    assert!(matches!(note.kind, GraphQLErrorNoteKind::Help));
    assert_eq!(
        note.message,
        "Did you mean: `fieldName: Type`?",
    );
    assert!(note.span.is_none());
}

/// Verifies that `add_help_with_span()` appends a help note with a location.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_add_help_with_span() {
    let mut error = GraphQLParseError::new(
        "Unknown directive location",
        GraphQLParseErrorKind::InvalidSyntax,
        SourceSpan::zero(),
    );

    error.add_help_with_span(
        "Did you mean `FIELD`?",
        SourceSpan::zero(),
    );

    assert_eq!(error.notes().len(), 1);
    let note = &error.notes()[0];
    assert!(matches!(note.kind, GraphQLErrorNoteKind::Help));
    assert!(note.span.is_some());
}

/// Verifies that `add_spec()` appends a specification reference note.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_add_spec() {
    let mut error = GraphQLParseError::new(
        "Invalid enum value",
        GraphQLParseErrorKind::ReservedName {
            name: "true".to_string(),
            context: ReservedNameContext::EnumValue,
        },
        SourceSpan::zero(),
    );

    error.add_spec(
        "https://spec.graphql.org/September2025/#sec-Enums",
    );

    assert_eq!(error.notes().len(), 1);
    let note = &error.notes()[0];
    assert!(matches!(note.kind, GraphQLErrorNoteKind::Spec));
}

/// Verifies that multiple notes can be added in sequence.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_multiple_notes() {
    let mut error = GraphQLParseError::new(
        "Unclosed brace",
        unclosed_delimiter_kind(),
        SourceSpan::zero(),
    );

    error.add_note(
        "Expected `}` to close type definition",
    );
    error.add_note_with_span(
        "Opening `{` here",
        SourceSpan::zero(),
    );
    error.add_help(
        "Add a closing `}` at the end of the type definition",
    );

    assert_eq!(error.notes().len(), 3);
}

// =============================================================================
// Part 3.3: Error Display Formatting Tests
// =============================================================================

/// Verifies that `format_oneline()` produces single-line error format
/// using the pre-resolved `SourceSpan`.
///
/// Format: "file:line:col: error: message"
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_oneline() {
    // Build a resolved span at line 4, col 11 (0-indexed) = line 5,
    // col 12 (1-indexed).
    let resolved = SourceSpan::new(
        SourcePosition::new(4, 11, Some(11), 55),
        SourcePosition::new(4, 16, Some(16), 60),
    );
    let error = GraphQLParseError::new(
        "Expected `:` after field name",
        unexpected_token_kind(),
        resolved,
    );

    let formatted = error.format_oneline();

    assert_eq!(
        formatted,
        "<input>:5:12: error: Expected `:` after field name",
    );
}

/// Verifies that `format_detailed()` without source produces basic format.
///
/// When no source is provided, we can still show location info but not source
/// snippets. Without source, resolve_offset returns None, so line:col defaults
/// to 1:1.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_detailed_without_source() {
    let error = GraphQLParseError::new(
        "Unexpected token",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    let formatted = error.format_detailed(None);

    assert!(formatted.contains("error:"));
    assert!(formatted.contains("Unexpected token"));
    assert!(formatted.contains("-->"));
    // Without source, SourceSpan::zero() shows 1:1
    assert!(formatted.contains("1:1"));
}

/// Verifies that `format_detailed()` with source includes source snippet.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_detailed_with_source() {
    let source =
        "type Query {\n    userName String\n}";
    // "String" starts at byte offset 26 in the source
    let sm = SourceMap::new_with_source(source, None);
    let resolved = sm
        .resolve_span(ByteSpan::new(26, 32))
        .unwrap_or_else(SourceSpan::zero);

    let error = GraphQLParseError::new(
        "Expected `:` after field name",
        unexpected_token_kind(),
        resolved,
    );

    let formatted = error.format_detailed(Some(source));

    assert!(formatted.contains("error:"));
    assert!(formatted.contains(
        "Expected `:` after field name",
    ));
    // Should include the source line
    assert!(formatted.contains("userName String"));
}

/// Verifies that `format_detailed()` renders notes with different kinds.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_detailed_with_notes() {
    let mut error = GraphQLParseError::new(
        "Unclosed `{`",
        unclosed_delimiter_kind(),
        SourceSpan::zero(),
    );
    error.add_note(
        "Expected `}` to close type definition",
    );
    error.add_help(
        "Check that all braces are properly matched",
    );

    let formatted = error.format_detailed(None);

    assert!(formatted.contains("= note:"));
    assert!(formatted.contains(
        "Expected `}` to close type definition",
    ));
    assert!(formatted.contains("= help:"));
    assert!(formatted.contains(
        "Check that all braces are properly matched",
    ));
}

/// Verifies that spec notes are rendered correctly.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_detailed_with_spec_note() {
    let mut error = GraphQLParseError::new(
        "Invalid enum value name",
        GraphQLParseErrorKind::ReservedName {
            name: "null".to_string(),
            context: ReservedNameContext::EnumValue,
        },
        SourceSpan::zero(),
    );
    error.add_spec(
        "https://spec.graphql.org/September2025/#sec-Enums",
    );

    let formatted = error.format_detailed(None);

    assert!(formatted.contains("= spec:"));
    assert!(formatted.contains("spec.graphql.org"));
}

// =============================================================================
// Part 3.2: Accessor Tests
// =============================================================================

/// Verifies that `message()` returns the error message.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_message_accessor() {
    let error = GraphQLParseError::new(
        "Test message",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    assert_eq!(error.message(), "Test message");
}

/// Verifies that `source_span()` returns the source span with correct byte
/// offsets.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_source_span_accessor() {
    let span = SourceSpan::new(
        SourcePosition::new(0, 0, None, 20),
        SourcePosition::new(0, 5, None, 25),
    );
    let error = GraphQLParseError::new(
        "Error",
        unexpected_token_kind(),
        span,
    );

    assert_eq!(
        error.source_span().start_inclusive.byte_offset(),
        20,
    );
    assert_eq!(
        error.source_span().end_exclusive.byte_offset(),
        25,
    );
}

/// Verifies that `kind()` returns the error kind.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_kind_accessor() {
    let error = GraphQLParseError::new(
        "Error",
        unclosed_delimiter_kind(),
        SourceSpan::zero(),
    );

    assert!(matches!(
        error.kind(),
        GraphQLParseErrorKind::UnclosedDelimiter { .. }
    ));
}

/// Verifies that `notes()` returns the notes vector.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_notes_accessor() {
    let mut notes = SmallVec::new();
    notes.push(GraphQLErrorNote::general("note 1"));
    notes.push(GraphQLErrorNote::help("note 2"));

    let error = GraphQLParseError::with_notes(
        "Error",
        unexpected_token_kind(),
        notes,
        SourceSpan::zero(),
    );

    assert_eq!(error.notes().len(), 2);
}

// =============================================================================
// Part 3.3: Display Trait Test
// =============================================================================

/// Verifies that `format_source_snippet` correctly handles source text with
/// bare carriage return (`\r`) line endings (legacy Mac style).
///
/// The GraphQL spec (Section 2.2 "Source Text") recognizes `\r` as a line
/// terminator. `SourceMap::compute_line_starts()` correctly handles this, but
/// the snippet formatter must also split lines using the same logic rather
/// than relying on Rust's `str::lines()`, which does NOT treat bare `\r` as a
/// line terminator.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_detailed_with_bare_cr_line_endings() {
    // Source with bare \r line endings (no \n):
    // Line 0: "type Query {"
    // Line 1: "  hello: String"
    // Line 2: "}"
    let source = "type Query {\r  hello: String\r}";

    // "hello" starts at offset 15 (after "type Query {\r  ")
    let sm = SourceMap::new_with_source(source, None);
    let resolved = sm
        .resolve_span(ByteSpan::new(15, 20))
        .unwrap_or_else(SourceSpan::zero);

    let error = GraphQLParseError::new(
        "test error on CR-only source",
        unexpected_token_kind(),
        resolved,
    );

    let formatted = error.format_detailed(Some(source));

    // The formatted output should contain a source snippet with
    // line number "2" (1-indexed for line index 1).
    assert!(
        formatted.contains(" 2 |"),
        "Snippet should show line number 2 for the \
         \\r-separated line containing 'hello', but \
         got:\n{formatted}",
    );
    // Underline carets should appear under "hello"
    assert!(
        formatted.contains("^^^^^"),
        "Snippet should underline 'hello' with 5 carets, \
         but got:\n{formatted}",
    );
}

/// Verifies that `format_note_snippet` correctly handles bare `\r` line
/// endings for note spans.
///
/// Same underlying issue as the source snippet test above.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_format_note_snippet_with_bare_cr_line_endings() {
    // Source with bare \r line endings:
    // Line 0: "type Query {"
    // Line 1: "  hello: String"
    // Line 2: "}"
    let source = "type Query {\r  hello: String\r}";

    let sm = SourceMap::new_with_source(source, None);
    let resolved = sm
        .resolve_span(ByteSpan::new(0, 1))
        .unwrap_or_else(SourceSpan::zero);

    // Primary error at offset 0 (line 0)
    let mut error = GraphQLParseError::new(
        "primary error",
        unexpected_token_kind(),
        resolved,
    );

    // Note pointing to "hello" on line 1 (0-indexed)
    let note_span = sm
        .resolve_span(ByteSpan::new(15, 20))
        .unwrap_or_else(SourceSpan::zero);
    error.add_note_with_span("see this token", note_span);

    let formatted = error.format_detailed(Some(source));

    // The note snippet should show line number 2 (1-indexed).
    assert!(
        formatted.contains("     2 |"),
        "Note snippet should show line number 2 for the \
         \\r-separated line containing 'hello', but \
         got:\n{formatted}",
    );
}

/// Verifies that Display shows `<input>:1:1` when constructed with
/// `SourceSpan::zero()`.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_display_trait_without_source() {
    let error = GraphQLParseError::new(
        "Test error message",
        unexpected_token_kind(),
        SourceSpan::zero(),
    );

    let display_output = format!("{error}");
    assert_eq!(
        display_output,
        "<input>:1:1: error: Test error message",
    );
}

/// Verifies that Display includes file:line:col from a resolved
/// `SourceSpan` with a file path.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_display_trait_with_resolved_span() {
    use std::path::PathBuf;

    let resolved = SourceSpan::with_file(
        SourcePosition::new(
            /* line = */ 4, /* col_utf8 = */ 11,
            Some(11), /* byte_offset = */ 55,
        ),
        SourcePosition::new(
            /* line = */ 4, /* col_utf8 = */ 16,
            Some(16), /* byte_offset = */ 60,
        ),
        PathBuf::from("schema.graphql"),
    );
    let error = GraphQLParseError::new(
        "Expected `:` after field name",
        unexpected_token_kind(),
        resolved,
    );

    let display_output = format!("{error}");
    assert_eq!(
        display_output,
        "schema.graphql:5:12: error: Expected `:` after \
         field name",
    );
}

/// Verifies Display falls back to `<input>` when resolved span has no
/// file path.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_display_trait_resolved_span_no_file() {
    let resolved = SourceSpan::new(
        SourcePosition::new(2, 5, Some(5), 30),
        SourcePosition::new(2, 10, Some(10), 35),
    );
    let error = GraphQLParseError::new(
        "Unexpected token",
        unexpected_token_kind(),
        resolved,
    );

    let display_output = format!("{error}");
    assert_eq!(
        display_output,
        "<input>:3:6: error: Unexpected token",
    );
}

/// Verifies that errors produced by the parser carry resolved spans
/// with real line/column info, so Display shows useful locations
/// without a SourceMap.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn parse_error_from_parser_has_resolved_span() {
    use crate::GraphQLParser;

    let source = "type Query {\n  name String\n}";
    let parser = GraphQLParser::new(source);
    let result = parser.parse_schema_document();
    assert!(
        result.has_errors(),
        "should have parse errors",
    );

    let error = &result.errors()[0];
    let display = format!("{error}");
    // Should show real location (line 2), not the 1:1 fallback
    assert!(
        display.contains(":2:"),
        "Display should show real line number, got: \
         {display}",
    );
    assert!(
        !display.contains(":1:1: error:"),
        "Display should not show fallback 1:1 position, \
         got: {display}",
    );
}