Skip to content

Commit 3c39490

Browse files
authored
Fix string and block string matches being too eager (#17)
* Add test cases for strings greedily matching * Fix blockStringRe and stringRe eagerly parsing * Add changeset
1 parent 66f7686 commit 3c39490

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

.changeset/loud-countries-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@0no-co/graphql.web': patch
3+
---
4+
5+
Fix string and block string matches eagerly matching past the end boundary of strings and ignoring escaped closing characters. In certain cases, `"""` and `"` boundaries would be skipped if any other string boundary follows in the input document.

src/__tests__/parser.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,18 @@ describe('parseValue', () => {
478478
value: '\t\t',
479479
block: false,
480480
});
481+
482+
expect(parseValue('" \\" "')).toEqual({
483+
kind: Kind.STRING,
484+
value: ' " ',
485+
block: false,
486+
});
487+
488+
expect(parseValue('"x" "x"')).toEqual({
489+
kind: Kind.STRING,
490+
value: 'x',
491+
block: false,
492+
});
481493
});
482494

483495
it('parses objects', () => {
@@ -548,6 +560,18 @@ describe('parseValue', () => {
548560
value: 'first\nsecond',
549561
block: true,
550562
});
563+
564+
expect(parseValue('""" \\""" """')).toEqual({
565+
kind: Kind.STRING,
566+
value: ' """ ',
567+
block: true,
568+
});
569+
570+
expect(parseValue('"""x""" """x"""')).toEqual({
571+
kind: Kind.STRING,
572+
value: 'x',
573+
block: true,
574+
});
551575
});
552576

553577
it('allows variables', () => {

src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const intRe = /-?\d+/y;
8686
const floatPartRe = /(?:\.\d+)?[eE][+-]?\d+|\.\d+/y;
8787

8888
const complexStringRe = /\\/g;
89-
const blockStringRe = /"""(?:[\s\S]+(?="""))?"""/y;
90-
const stringRe = /"(?:[^"\r\n]+)?"/y;
89+
const blockStringRe = /"""(?:[\s\S]*?[^\\])?"""/y;
90+
const stringRe = /"(?:[^\r\n]*?[^\\])?"/y;
9191

9292
function value(constant: true): ast.ConstValueNode;
9393
function value(constant: boolean): ast.ValueNode;

0 commit comments

Comments
 (0)