Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

resolve interpolated parsed fragments (#105)

authored by

Jovi De Croock and committed by
GitHub
3c4d18d3 35589647

+32
+5
.changeset/fresh-grapes-melt.md
··· 1 + --- 2 + '@0no-co/graphqlsp': patch 3 + --- 4 + 5 + Resolve parsed AST nodes being interpolated into an operation
+27
packages/graphqlsp/src/ast/resolve.ts
··· 2 2 isAsExpression, 3 3 isIdentifier, 4 4 isNoSubstitutionTemplateLiteral, 5 + isObjectLiteralExpression, 5 6 isTaggedTemplateExpression, 6 7 TaggedTemplateExpression, 7 8 } from 'typescript'; 9 + import { print } from 'graphql'; 8 10 import ts from 'typescript/lib/tsserverlibrary'; 9 11 import { findNode } from '.'; 10 12 import { getSource } from '../ast'; ··· 108 110 }, 109 111 }; 110 112 addedCharacters += text.combinedText.length - originalRange.length; 113 + return alteredSpan; 114 + } else if ( 115 + parent.initializer && 116 + isAsExpression(parent.initializer) && 117 + isAsExpression(parent.initializer.expression) && 118 + isObjectLiteralExpression(parent.initializer.expression.expression) 119 + ) { 120 + const astObject = JSON.parse( 121 + parent.initializer.expression.expression.getText() 122 + ); 123 + const resolvedTemplate = print(astObject); 124 + templateText = templateText.replace( 125 + '${' + span.expression.escapedText + '}', 126 + resolvedTemplate 127 + ); 128 + const alteredSpan = { 129 + lines: resolvedTemplate.split('\n').length, 130 + identifier: identifierName, 131 + original: originalRange, 132 + new: { 133 + start: originalRange.start + addedCharacters, 134 + length: resolvedTemplate.length, 135 + }, 136 + }; 137 + addedCharacters += resolvedTemplate.length - originalRange.length; 111 138 return alteredSpan; 112 139 } 113 140