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 lone returns in migration tool, turn on strict mode #5831

Merged
merged 6 commits into from
Aug 6, 2022

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Aug 5, 2022

Adding strict mode uncovered that we previously failed on lone return; statements. This PR fixes that - but it brought up an interesting question: Should lone return; statements still be allowed in the new load function? Right now it's implemented as "no, you need to adjust this". If we allow it, we should adjust the types for the handler functions in +page.server.js to allow it as well. If we don't allow it, we should adjust the load function return type to reflect hat.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

🦋 Changeset detected

Latest commit: 4bdfeda

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte-migrate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann benmccann changed the title [fix handle lone returns, turn on strict mode [fix] handle lone returns, turn on strict mode Aug 5, 2022
@benmccann benmccann changed the title [fix] handle lone returns, turn on strict mode [fix] handle lone returns in migration tool, turn on strict mode Aug 5, 2022

if (!set_cookie_value || is_string_like(set_cookie_value)) {
safe_headers = true;
if (expr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we changed the implementation of rewrite_returns instead? I don't think there's any reason we'd need to do anything with a return;, we can just leave it be:

-if (ts.isReturnStatement(node)) {
+if (ts.isReturnStatement(node) && node.expression) {

Copy link
Member Author

@dummdidumm dummdidumm Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on how we answer the questions in the PR description - so yeah, definitely possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At present the type of Load is this:

export interface Load<
Params extends Record<string, string> = Record<string, string>,
InputData extends Record<string, any> = Record<string, any>,
OutputData extends Record<string, any> = Record<string, any>
> {
(event: LoadEvent<Params, InputData>): MaybePromise<OutputData | void>;
}

i.e. a void return is fine, and so assuming we're okay with that then return; is fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but currently the RequestHandler type does not allow a void return - which makes me think that we should probably align these. So Load is ok with a void, so GET should, too. In the case of standalone endpoints we always need to return a response now. If we assume that people have their returns correctly already, we can just skip the return; case - if you agree with that, I'll adjust the code accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If standalone endpoint returns void today, it'll be an error, so I think it's fine for RequestHandler to require a Response return value — it'll just continue to error if it doesn't

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True - ok I'll adjust this to leave lone returns as-is then

@Rich-Harris Rich-Harris merged commit f30e8fd into master Aug 6, 2022
@Rich-Harris Rich-Harris deleted the migration-return-and-strict branch August 6, 2022 19:55
@github-actions github-actions bot mentioned this pull request Aug 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants