Skip to content

feat: simplify derived object destructuring #12781

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 5 commits into from
Aug 10, 2024
Merged

Conversation

ottomated
Copy link
Contributor

@ottomated ottomated commented Aug 10, 2024

The following $derived

let { foo, bar: [a, b, { baz }]} = $derived(stuff);

currently compiles to

let derived_object = $.derived(() => stuff),
  derived_values = $.derived(() => {
    let { foo, bar: [a, b, { baz }] } = $.get(derived_object);

    return [foo, a, b, baz];
  }),
  foo = $.derived(() => $.get(derived_values)[0]),
  a = $.derived(() => $.get(derived_values)[1]),
  b = $.derived(() => $.get(derived_values)[2]),
  baz = $.derived(() => $.get(derived_values)[3]);

By re-using the extract_paths function, this PR simplifies the output to

let derived_object = $.derived(() => stuff),
  foo = $.derived(() => $.get(derived_object).foo),
  a = $.derived(() => $.get(derived_object).bar[0]),
  b = $.derived(() => $.get(derived_object).bar[1]),
  baz = $.derived(() => $.get(derived_object).bar[2].baz);

which should be more performant because it avoids destructuring twice, and doesn't create as many derived sources.

Also added a test for destructuring an array, which was missing before.

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

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
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

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

Copy link

changeset-bot bot commented Aug 10, 2024

🦋 Changeset detected

Latest commit: bfb9a4f

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

This PR includes changesets to release 1 package
Name Type
svelte 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

@ottomated ottomated changed the title simplify derived object destructuring feat: simplify derived object destructuring Aug 10, 2024
@ottomated ottomated marked this pull request as draft August 10, 2024 03:38
@Rich-Harris
Copy link
Member

Fantastic! We can actually go a step further, and eliminate the intermediate derived entirely in simple cases:

let foo = $.derived(() => $.get(stuff).foo),
  a = $.derived(() => $.get(stuff).bar[0]),
  b = $.derived(() => $.get(stuff).bar[1]),
  baz = $.derived(() => $.get(stuff).bar[2].baz);

@Rich-Harris Rich-Harris merged commit 7de3e3b into sveltejs:main Aug 10, 2024
9 checks passed
@Rich-Harris Rich-Harris mentioned this pull request Aug 10, 2024
5 tasks
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.

2 participants