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

feat: Auto-typing support for entries #1983

Merged
Changes from 4 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
19 changes: 19 additions & 0 deletions packages/svelte2tsx/src/helpers/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ function upserKitRouteFile(
insert(pos, inserted);
}

// add type to entries function if not explicitly typed
const entries = exports.get('entries');
if (
entries?.type === 'function' &&
entries.node.parameters.length === 0 &&
!entries.hasTypeDefinition &&
!basename.includes('layout')
) {
if (!entries.node.type && entries.node.body) {
const returnPos = ts.isArrowFunction(entries.node)
? entries.node.equalsGreaterThanToken.getStart()
: entries.node.body.getStart();
const returnInsertion = surround(
`: ReturnType<import('./$types.js').EntryGenerator> `
);
insert(returnPos, returnInsertion);
}
}

// add type to actions variable if not explicitly typed
const actions = exports.get('actions');
if (actions?.type === 'var' && !actions.hasTypeDefinition && actions.node.initializer) {
Expand Down