Skip to content

Commit

Permalink
Fix string and block string matches being too eager (#17)
Browse files Browse the repository at this point in the history
* Add test cases for strings greedily matching

* Fix blockStringRe and stringRe eagerly parsing

* Add changeset
  • Loading branch information
kitten committed Jun 6, 2023
1 parent 66f7686 commit 3c39490
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-countries-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphql.web': patch
---

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.
24 changes: 24 additions & 0 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ describe('parseValue', () => {
value: '\t\t',
block: false,
});

expect(parseValue('" \\" "')).toEqual({
kind: Kind.STRING,
value: ' " ',
block: false,
});

expect(parseValue('"x" "x"')).toEqual({
kind: Kind.STRING,
value: 'x',
block: false,
});
});

it('parses objects', () => {
Expand Down Expand Up @@ -548,6 +560,18 @@ describe('parseValue', () => {
value: 'first\nsecond',
block: true,
});

expect(parseValue('""" \\""" """')).toEqual({
kind: Kind.STRING,
value: ' """ ',
block: true,
});

expect(parseValue('"""x""" """x"""')).toEqual({
kind: Kind.STRING,
value: 'x',
block: true,
});
});

it('allows variables', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const intRe = /-?\d+/y;
const floatPartRe = /(?:\.\d+)?[eE][+-]?\d+|\.\d+/y;

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

function value(constant: true): ast.ConstValueNode;
function value(constant: boolean): ast.ValueNode;
Expand Down

0 comments on commit 3c39490

Please sign in to comment.