From 361b980c948b3533b6a29b25a79708a62bf110ca Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 16 Aug 2022 16:41:22 +0200 Subject: [PATCH] [fix] Handle function without params when writing TS proxy Fixes #5927 --- .changeset/brave-apricots-approve.md | 5 ++++ packages/kit/src/core/sync/write_types.js | 6 ++++- .../kit/src/core/sync/write_types.spec.js | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .changeset/brave-apricots-approve.md diff --git a/.changeset/brave-apricots-approve.md b/.changeset/brave-apricots-approve.md new file mode 100644 index 000000000000..8ce577e89d33 --- /dev/null +++ b/.changeset/brave-apricots-approve.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Handle function without params when writing TS proxy diff --git a/packages/kit/src/core/sync/write_types.js b/packages/kit/src/core/sync/write_types.js index b9aca69518f8..0eb629f45e70 100644 --- a/packages/kit/src/core/sync/write_types.js +++ b/packages/kit/src/core/sync/write_types.js @@ -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] !== '('; diff --git a/packages/kit/src/core/sync/write_types.spec.js b/packages/kit/src/core/sync/write_types.spec.js index e614e18783ef..2191e742116e 100644 --- a/packages/kit/src/core/sync/write_types.spec.js +++ b/packages/kit/src/core/sync/write_types.spec.js @@ -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} */