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

[fix] Handle function without params when writing TS proxy #5928

Merged
merged 1 commit into from Aug 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-apricots-approve.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Handle function without params when writing TS proxy
6 changes: 5 additions & 1 deletion packages/kit/src/core/sync/write_types.js
Expand Up @@ -616,7 +616,11 @@ export function tweak_types(ts, content, names) {

const rhs = declaration.initializer;

if (rhs && (ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs))) {
if (
rhs &&
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
rhs.parameters.length
) {
const arg = rhs.parameters[0];

const add_parens = content[arg.pos - 1] !== '(';
Expand Down
24 changes: 24 additions & 0 deletions packages/kit/src/core/sync/write_types.spec.js
Expand Up @@ -27,6 +27,30 @@ test('Rewrites types for a TypeScript module', () => {
);
});

test('Rewrites types for a TypeScript module without param', () => {
const source = `
export const GET: Get = () => {
return {
a: 1
};
};
`;

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

assert.equal(rewritten?.exports, ['GET']);
assert.equal(
rewritten?.code,
`
export const GET = () => {
return {
a: 1
};
};
`
);
});

test('Rewrites types for a JavaScript module with `function`', () => {
const source = `
/** @type {import('./$types').Get} */
Expand Down