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.

Support changing operationName (#40)

* Support changing operationName

* Apply suggestions from code review

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>

---------

Co-authored-by: Jovi De Croock <decroockjovi@gmail.com>

authored by

Daniel
Jovi De Croock
and committed by
GitHub
b537b682 e7a0ba51

+31 -10
+31 -10
packages/graphqlsp/src/index.ts
··· 223 223 const exportName = isFragment 224 224 ? `${name}FragmentDoc` 225 225 : `${name}Document`; 226 - const imp = ` as typeof import('./${nameParts 226 + let imp = ` as typeof import('./${nameParts 227 227 .join('.') 228 228 .replace('.ts', '')}').${exportName}`; 229 229 ··· 237 237 238 238 const span = { length: 1, start: node.end }; 239 239 240 - // TODO: if we have a typeImport but the name has been updated/is wrong 241 - // we need to leave the "as x" typecast 242 - const text = 243 - source.text.substring(0, span.start) + 244 - imp + 245 - source.text.substring( 246 - span.start + span.length, 247 - source.text.length 248 - ); 240 + let text = ''; 241 + if (typeImport) { 242 + // We only want the oldExportName here to be present 243 + // that way we can diff its length vs the new one 244 + const oldExportName = typeImport 245 + .getText() 246 + .split('.') 247 + .pop() 248 + 249 + // Remove ` as ` from the beginning, 250 + // this because getText() gives us everything 251 + // but ` as ` meaning we need to keep that part 252 + // around. 253 + imp = imp.slice(4); 254 + text = source.text.replace(typeImport.getText(), imp); 255 + span.length = imp.length + ((oldExportName || '').length - exportName.length); 256 + } else { 257 + text = 258 + source.text.substring(0, span.start) + 259 + imp + 260 + source.text.substring( 261 + span.start + span.length, 262 + source.text.length 263 + ); 264 + } 265 + 249 266 const scriptInfo = 250 267 info.project.projectService.getScriptInfo(filename); 251 268 const snapshot = scriptInfo!.getSnapshot(); ··· 254 271 source.update(text, { span, newLength: imp.length }); 255 272 scriptInfo!.editContent(0, snapshot.getLength(), text); 256 273 info.languageServiceHost.writeFile!(source.fileName, text); 274 + if (!!typeImport) { 275 + // To update the types, otherwise data is stale 276 + scriptInfo!.reloadFromFile(); 277 + } 257 278 scriptInfo!.registerFileUpdate(); 258 279 // script info contains a lot of utils that might come in handy here 259 280 // to save even if the user has local changes, if we could make that work