Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-months-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Correctly replace with identical replacement strings
2 changes: 1 addition & 1 deletion packages/example/src/Pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const WeakFields = gql`
weaknesses
someUnknownField
}
` as typeof import('./Pokemon.generated').WeaknessFieldsFragmentDoc;
` as typeof import('./Pokemon.generated').PokemonFieldsFragmentDoc;

export const Pokemon = (data: any) => {
const pokemon = useFragment(PokemonFields, data);
Expand Down
5 changes: 4 additions & 1 deletion packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ export function getGraphQLDiagnostics(
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 {
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/fixture-project/fixtures/rename-complex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 changes: 39 additions & 0 deletions test/e2e/generate-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
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;
Expand All @@ -22,7 +24,9 @@ describe('Type-generation', () => {
afterAll(() => {
try {
fs.unlinkSync(outFile);
fs.unlinkSync(outFileComplex);
fs.unlinkSync(genFile);
fs.unlinkSync(genFileComplex);
fs.unlinkSync(baseGenFile);
} catch {}
});
Expand Down Expand Up @@ -93,4 +97,39 @@ describe('Type-generation', () => {
);
});
}, 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);
});