Skip to content

Commit 1b2e92b

Browse files
authored
fix: Fix Safari 10–13 sticky regular expression quirks (#15)
* Fix constRe match for Safari * Add quirk fix for floatPartRe * Update operationDefinitionRe just in case * Update character classes for names to be more precise * Add changeset
1 parent 44c47aa commit 1b2e92b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

.changeset/curvy-geese-hope.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 browser quirk occurring in Safari 10–13 causing sticky regular expressions in the parser to match when they shouldn't / match too eagerly.

src/parser.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function ignored() {
6565
idx--;
6666
}
6767

68-
const nameRe = /[_\w][_\d\w]*/y;
68+
const nameRe = /[_A-Za-z]\w*/y;
6969
function name(): ast.NameNode | undefined {
7070
let match: string | undefined;
7171
if ((match = advance(nameRe))) {
@@ -76,10 +76,15 @@ function name(): ast.NameNode | undefined {
7676
}
7777
}
7878

79-
const constRe = /null|true|false/y;
80-
const variableRe = /\$[_\w][_\d\w]*/y;
79+
// NOTE(Safari10 Quirk): This needs to be wrapped in a non-capturing group
80+
const constRe = /(?:null|true|false)/y;
81+
82+
const variableRe = /\$[_A-Za-z]\w*/y;
8183
const intRe = /-?\d+/y;
82-
const floatPartRe = /(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
84+
85+
// NOTE(Safari10 Quirk): This cannot be further simplified
86+
const floatPartRe = /(?:\.\d+)?[eE][+-]?\d+|\.\d+/y;
87+
8388
const complexStringRe = /\\/g;
8489
const blockStringRe = /"""(?:[\s\S]+(?="""))?"""/y;
8590
const stringRe = /"(?:[^"\r\n]+)?"/y;
@@ -413,7 +418,9 @@ function fragmentDefinition(): ast.FragmentDefinitionNode | undefined {
413418
}
414419
}
415420

416-
const operationDefinitionRe = /query|mutation|subscription/y;
421+
// NOTE(Safari10 Quirk): This *might* need to be wrapped in a group, but worked without it too
422+
const operationDefinitionRe = /(?:query|mutation|subscription)/y;
423+
417424
function operationDefinition(): ast.OperationDefinitionNode | undefined {
418425
let _operation: string | undefined;
419426
let _name: ast.NameNode | undefined;

0 commit comments

Comments
 (0)