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

start looking for replacements at the start of our node (#82)

Changed files
+62 -2
.changeset
packages
example
graphqlsp
test
e2e
fixture-project
+5
.changeset/lovely-months-type.md
···
+
---
+
'@0no-co/graphqlsp': patch
+
---
+
+
Correctly replace with identical replacement strings
+1 -1
packages/example/src/Pokemon.ts
···
weaknesses
someUnknownField
}
-
` as typeof import('./Pokemon.generated').WeaknessFieldsFragmentDoc;
+
` as typeof import('./Pokemon.generated').PokemonFieldsFragmentDoc;
export const Pokemon = (data: any) => {
const pokemon = useFragment(PokemonFields, data);
+4 -1
packages/graphqlsp/src/diagnostics.ts
···
imp = imp.slice(4);
// We remove the \n
imp = imp.substring(0, imp.length - 1);
-
text = sourceText.replace(typeImport.getText(), imp);
+
const from = node.getStart();
+
text =
+
sourceText.slice(0, from) +
+
sourceText.slice(from).replace(typeImport.getText(), imp);
span.length =
imp.length + ((oldExportName || '').length - exportName.length);
} else {
+13
test/e2e/fixture-project/fixtures/rename-complex.ts
···
+
import { gql } from '@urql/core';
+
+
export const PostFields = gql`
+
fragment PostFields on Post {
+
id
+
}
+
` as typeof import('./rename-complex.generated').PostFieldsFragmentDoc;
+
+
export const Post2Fields = gql`
+
fragment Post2Fields on Post {
+
title
+
}
+
` as typeof import('./rename-complex.generated').PostFieldsFragmentDoc;
+39
test/e2e/generate-types.test.ts
···
const projectPath = path.resolve(__dirname, 'fixture-project');
describe('Type-generation', () => {
const outFile = path.join(projectPath, 'rename.ts');
+
const outFileComplex = path.join(projectPath, 'rename-complex.ts');
const genFile = path.join(projectPath, 'rename.generated.ts');
+
const genFileComplex = path.join(projectPath, 'rename-complex.generated.ts');
const baseGenFile = path.join(projectPath, '__generated__/baseGraphQLSP.ts');
let server: TSServer;
···
afterAll(() => {
try {
fs.unlinkSync(outFile);
+
fs.unlinkSync(outFileComplex);
fs.unlinkSync(genFile);
+
fs.unlinkSync(genFileComplex);
fs.unlinkSync(baseGenFile);
} catch {}
});
···
expect(fs.readFileSync(genFile, 'utf-8')).toContain(
'export const PostListDocument ='
);
+
});
+
}, 20000);
+
+
it('gets renamed correctly (complex)', async () => {
+
server.sendCommand('open', {
+
file: outFileComplex,
+
fileContent: '// empty',
+
scriptKindName: 'TS',
+
} satisfies ts.server.protocol.OpenRequestArgs);
+
+
server.sendCommand('updateOpen', {
+
openFiles: [
+
{
+
file: outFileComplex,
+
fileContent: fs.readFileSync(
+
path.join(projectPath, 'fixtures/rename-complex.ts'),
+
'utf-8'
+
),
+
},
+
],
+
} satisfies ts.server.protocol.UpdateOpenRequestArgs);
+
+
server.sendCommand('saveto', {
+
file: outFileComplex,
+
tmpfile: outFileComplex,
+
} satisfies ts.server.protocol.SavetoRequestArgs);
+
+
await waitForExpect(() => {
+
const contents = fs.readFileSync(outFileComplex, 'utf-8');
+
expect(contents).toContain(` id
+
}
+
\` as typeof import('./rename-complex.generated').PostFieldsFragmentDoc`);
+
expect(contents).toContain(` title
+
}
+
\` as typeof import('./rename-complex.generated').Post2FieldsFragmentDoc`);
});
}, 20000);
});