Skip to content

Commit b262418

Browse files
Lexer: fix line & column for multiline BLOCK_STRING tokens (graphql#3354)
Fixes graphql#3353
1 parent 958ac6c commit b262418

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/language/__tests__/lexer-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,69 +537,89 @@ describe('Lexer', () => {
537537
kind: TokenKind.BLOCK_STRING,
538538
start: 0,
539539
end: 6,
540+
line: 1,
541+
column: 1,
540542
value: '',
541543
});
542544

543545
expect(lexOne('"""simple"""')).to.contain({
544546
kind: TokenKind.BLOCK_STRING,
545547
start: 0,
546548
end: 12,
549+
line: 1,
550+
column: 1,
547551
value: 'simple',
548552
});
549553

550554
expect(lexOne('""" white space """')).to.contain({
551555
kind: TokenKind.BLOCK_STRING,
552556
start: 0,
553557
end: 19,
558+
line: 1,
559+
column: 1,
554560
value: ' white space ',
555561
});
556562

557563
expect(lexOne('"""contains " quote"""')).to.contain({
558564
kind: TokenKind.BLOCK_STRING,
559565
start: 0,
560566
end: 22,
567+
line: 1,
568+
column: 1,
561569
value: 'contains " quote',
562570
});
563571

564572
expect(lexOne('"""contains \\""" triple quote"""')).to.contain({
565573
kind: TokenKind.BLOCK_STRING,
566574
start: 0,
567575
end: 32,
576+
line: 1,
577+
column: 1,
568578
value: 'contains """ triple quote',
569579
});
570580

571581
expect(lexOne('"""multi\nline"""')).to.contain({
572582
kind: TokenKind.BLOCK_STRING,
573583
start: 0,
574584
end: 16,
585+
line: 1,
586+
column: 1,
575587
value: 'multi\nline',
576588
});
577589

578590
expect(lexOne('"""multi\rline\r\nnormalized"""')).to.contain({
579591
kind: TokenKind.BLOCK_STRING,
580592
start: 0,
581593
end: 28,
594+
line: 1,
595+
column: 1,
582596
value: 'multi\nline\nnormalized',
583597
});
584598

585599
expect(lexOne('"""unescaped \\n\\r\\b\\t\\f\\u1234"""')).to.contain({
586600
kind: TokenKind.BLOCK_STRING,
587601
start: 0,
588602
end: 32,
603+
line: 1,
604+
column: 1,
589605
value: 'unescaped \\n\\r\\b\\t\\f\\u1234',
590606
});
591607

592608
expect(lexOne('"""unescaped unicode outside BMP \u{1f600}"""')).to.contain({
593609
kind: TokenKind.BLOCK_STRING,
594610
start: 0,
595611
end: 38,
612+
line: 1,
613+
column: 1,
596614
value: 'unescaped unicode outside BMP \u{1f600}',
597615
});
598616

599617
expect(lexOne('"""slashes \\\\ \\/"""')).to.contain({
600618
kind: TokenKind.BLOCK_STRING,
601619
start: 0,
602620
end: 19,
621+
line: 1,
622+
column: 1,
603623
value: 'slashes \\\\ \\/',
604624
});
605625

@@ -615,6 +635,8 @@ describe('Lexer', () => {
615635
kind: TokenKind.BLOCK_STRING,
616636
start: 0,
617637
end: 68,
638+
line: 1,
639+
column: 1,
618640
value: 'spans\n multiple\n lines',
619641
});
620642
});

src/language/lexer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ function readEscapedCharacter(lexer: Lexer, position: number): EscapeSequence {
739739
function readBlockString(lexer: Lexer, start: number): Token {
740740
const body = lexer.source.body;
741741
const bodyLength = body.length;
742+
const startLine = lexer.line;
743+
const startColumn = 1 + start - lexer.lineStart;
744+
742745
let position = start + 3;
743746
let chunkStart = position;
744747
let rawValue = '';
@@ -753,11 +756,12 @@ function readBlockString(lexer: Lexer, start: number): Token {
753756
body.charCodeAt(position + 2) === 0x0022
754757
) {
755758
rawValue += body.slice(chunkStart, position);
756-
return createToken(
757-
lexer,
759+
return new Token(
758760
TokenKind.BLOCK_STRING,
759761
start,
760762
position + 3,
763+
startLine,
764+
startColumn,
761765
dedentBlockStringValue(rawValue),
762766
);
763767
}

0 commit comments

Comments
 (0)