From 75dbb0b18e2102d271a5cfe78faf54fe22e83ac8 Mon Sep 17 00:00:00 2001 From: Daniel Woelfel Date: Fri, 29 Oct 2021 05:24:42 -0400 Subject: [PATCH] [RFC] fix: fix block string parsing in language parser (#1777) * fix: fix block string parsing in language parser * add changeset Co-authored-by: Rikki Schulte --- .changeset/thick-baboons-glow.md | 10 ++++++++++ .../graphql-language-service-parser/src/Rules.ts | 12 +++++++++++- .../src/onlineParser.ts | 10 ++++++++++ .../graphql-language-service-parser/src/types.ts | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/thick-baboons-glow.md diff --git a/.changeset/thick-baboons-glow.md b/.changeset/thick-baboons-glow.md new file mode 100644 index 00000000000..497cc1f6b12 --- /dev/null +++ b/.changeset/thick-baboons-glow.md @@ -0,0 +1,10 @@ +--- +"graphiql": patch +"codemirror-graphql": patch +"graphql-language-service-parser": patch +"graphql-language-service": patch +"graphql-language-service-server": patch +"graphql-language-service-cli": patch +--- + +adopt block string parsing for variables in language parser diff --git a/packages/graphql-language-service-parser/src/Rules.ts b/packages/graphql-language-service-parser/src/Rules.ts index feae5313bc9..efc9983b4e0 100644 --- a/packages/graphql-language-service-parser/src/Rules.ts +++ b/packages/graphql-language-service-parser/src/Rules.ts @@ -192,7 +192,17 @@ export const ParseRules: { [name: string]: ParseRule } = { } }, NumberValue: [t('Number', 'number')], - StringValue: [t('String', 'string')], + StringValue: [ + { + style: 'string', + match: token => token.kind === 'String', + update(state: State, token: Token) { + if (token.value.startsWith('"""')) { + state.inBlockstring = !token.value.slice(3).endsWith('"""'); + } + }, + }, + ], BooleanValue: [t('Name', 'builtin')], NullValue: [t('Name', 'keyword')], EnumValue: [name('string-2')], diff --git a/packages/graphql-language-service-parser/src/onlineParser.ts b/packages/graphql-language-service-parser/src/onlineParser.ts index 5c6d232ed3b..1321b8f68f1 100644 --- a/packages/graphql-language-service-parser/src/onlineParser.ts +++ b/packages/graphql-language-service-parser/src/onlineParser.ts @@ -81,6 +81,16 @@ function getToken( state: State, options: ParserOptions, ): string { + if (state.inBlockstring) { + if (stream.match(/.*"""/)) { + state.inBlockstring = false; + return 'string'; + } else { + stream.skipToEnd(); + return 'string'; + } + } + const { lexRules, parseRules, eatWhitespace, editorConfig } = options; // Restore state after an empty-rule. if (state.rule && state.rule.length === 0) { diff --git a/packages/graphql-language-service-parser/src/types.ts b/packages/graphql-language-service-parser/src/types.ts index 032a0812b7b..4898dea4c8d 100644 --- a/packages/graphql-language-service-parser/src/types.ts +++ b/packages/graphql-language-service-parser/src/types.ts @@ -52,6 +52,7 @@ export type State = { needsSeperator: boolean; needsAdvance?: boolean; indentLevel?: number; + inBlockstring?: boolean; }; export const AdditionalRuleKinds: _AdditionalRuleKinds = {