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

perform an additional dirty check (#27)

Changed files
+28 -4
.changeset
src
+5
.changeset/olive-jokes-drum.md
···
+
---
+
'@0no-co/graphqlsp': patch
+
---
+
+
Check the `dirty` state of the file an additional time to prevent writing to the file when the user types directly after saving
+15 -4
src/index.ts
···
import { Cursor } from './cursor';
import { loadSchema } from './getSchema';
import { getToken } from './token';
-
import { findAllTaggedTemplateNodes, findNode, getSource } from './utils';
+
import {
+
findAllTaggedTemplateNodes,
+
findNode,
+
getSource,
+
isFileDirty,
+
} from './utils';
import { resolveTemplate } from './resolve';
import { generateTypedDocumentNodes } from './types/generate';
···
const nameParts = name.split('.');
nameParts[nameParts.length - 1] = 'generated.ts';
parts[parts.length - 1] = nameParts.join('.');
-
const contents = fs.readFileSync(filename, 'utf-8');
-
const currentText = source.getFullText();
-
if (contents !== currentText) {
+
if (isFileDirty(filename, source)) {
return [...newDiagnostics, ...originalDiagnostics];
}
···
texts.join('\n'),
scalars
).then(() => {
+
if (isFileDirty(filename, source)) {
+
return;
+
}
+
nodes.forEach((node, i) => {
const queryText = texts[i] || '';
const parsed = parse(queryText);
···
const typeImport = parentChildren.find(x =>
isImportTypeNode(x)
) as ImportTypeNode;
+
if (typeImport && typeImport.getText().includes(exportName)) return;
const span = { length: 1, start: node.end };
+
+
// TODO: if we have a typeImport but the name has been updated/is wrong
+
// we need to leave the "as x" typecast
const text =
source.text.substring(0, span.start) +
imp +
+8
src/utils.ts
···
isNoSubstitutionTemplateLiteral,
isTaggedTemplateExpression,
} from 'typescript';
+
import fs from 'fs';
+
+
export function isFileDirty(fileName: string, source: ts.SourceFile) {
+
const contents = fs.readFileSync(fileName, 'utf-8');
+
const currentText = source.getFullText();
+
+
return currentText !== contents;
+
}
export function findNode(
sourceFile: ts.SourceFile,