Skip to content

[fix] prevent unused types warning #6293

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

Merged
merged 3 commits into from
Aug 26, 2022
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/neat-cameras-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] prevent unused types warning
16 changes: 16 additions & 0 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,13 @@ export function tweak_types(content, names) {
arg.name.end,
`: Parameters<${type}>[0]` + (add_parens ? ')' : '')
);
} else {
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
code.append(`;${type};`);
}
} else {
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
code.append(`;${type};`);
}

modified = true;
Expand All @@ -569,6 +575,16 @@ export function tweak_types(content, names) {
}
});

if (modified) {
// Ignore all type errors so they don't show up twice when svelte-check runs
// Account for possible @ts-check which would overwrite @ts-nocheck
if (code.original.startsWith('// @ts-check')) {
code.prependLeft('// @ts-check'.length, '\n// @ts-nocheck\n');
} else {
code.prepend('// @ts-nocheck\n');
}
}

return {
modified,
code: code.toString(),
Expand Down
43 changes: 38 additions & 5 deletions packages/kit/src/core/sync/write_types/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import path from 'path';
import fs from 'fs';
import { format } from 'prettier';
Expand Down Expand Up @@ -101,7 +102,8 @@ test('Rewrites types for a TypeScript module', () => {
assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`
`// @ts-nocheck

export const GET = ({ params }: Parameters<Get>[0]) => {
return {
a: 1
Expand All @@ -125,13 +127,14 @@ test('Rewrites types for a TypeScript module without param', () => {
assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`
`// @ts-nocheck

export const GET = () => {
return {
a: 1
};
};
`
;Get;`
);
});

Expand All @@ -150,7 +153,8 @@ test('Rewrites types for a JavaScript module with `function`', () => {
assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`
`// @ts-nocheck

/** @param {Parameters<import('./$types').Get>[0]} event */
export function GET({ params }) {
return {
Expand All @@ -176,7 +180,36 @@ test('Rewrites types for a JavaScript module with `const`', () => {
assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`
`// @ts-nocheck

/** @param {Parameters<import('./$types').Get>[0]} event */
export const GET = ({ params }) => {
return {
a: 1
};
};
`
);
});

test('Appends @ts-nocheck after @ts-check', () => {
const source = `// @ts-check
/** @type {import('./$types').Get} */
export const GET = ({ params }) => {
return {
a: 1
};
};
`;

const rewritten = tweak_types(source, new Set(['GET']));

assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`// @ts-check
// @ts-nocheck

/** @param {Parameters<import('./$types').Get>[0]} event */
export const GET = ({ params }) => {
return {
Expand Down