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: allow for arrays in member access in directives #11470

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

paoloricciuti
Copy link
Member

Svelte 5 rewrite

Closes #11467 ...this uses a slightly more advanced regex to determine if a part of a member access should use a literal or an identifier to access it.

I've also added the code to handle this absurd situation

<div use:obj.my-arr[0] />

i don't know if actually want to remove this and just ignore this case 😄

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 May 4, 2024

🦋 Changeset detected

Latest commit: df749d4

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

@Rich-Harris
Copy link
Member

This fails with e.g. x[0][0] and also x["y-z"]. We could fix those things, but since none of it works in Svelte 4 and it's pretty wacky code to write, I'm inclined to just leave it as-is — just use {@const ...}. I think supporting x.y-z in the first place was a mistake (it might predate {@const ...}).

@paoloricciuti
Copy link
Member Author

This fails with e.g. x[0][0] and also x["y-z"]. We could fix those things, but since none of it works in Svelte 4 and it's pretty wacky code to write, I'm inclined to just leave it as-is — just use {@const ...}. I think supporting x.y-z in the first place was a mistake (it might predate {@const ...}).

Wait really? I remember specifically testing for those cases. Can you approve the deploy on vercel so I can take a look? I'm on my phone

@paoloricciuti
Copy link
Member Author

Btw I agree that this kind of stuff is pretty un-useful

@paoloricciuti
Copy link
Member Author

This fails with e.g. x[0][0] and also x["y-z"]. We could fix those things, but since none of it works in Svelte 4 and it's pretty wacky code to write, I'm inclined to just leave it as-is — just use {@const ...}. I think supporting x.y-z in the first place was a mistake (it might predate {@const ...}).

With this

<script>
	let arr = $state([[()=>{}]])
</script>

<button use:arr[0][0]>
	clicks
</button>

the generated code is

import * as $ from "svelte/internal/client";

var root = $.template(`<button>clicks</button>`);

export default function App($$anchor) {
  let arr = $.proxy([[() => {}]]);
  var button = root();

  $.action(button, ($$node) => arr[0][0]($$node));
  $.append($$anchor, button);
}

which is correct and doesn't errors out.

With this

<script>
	let x = $state({"y-z": ()=>{}})
</script>

<button use:x.y-z>
	clicks
</button>

the generated code is

import * as $ from "svelte/internal/client";

var root = $.template(`<button>clicks</button>`);

export default function App($$anchor) {
  let x = $.proxy({ "y-z": () => {} });
  var button = root();

  $.action(button, ($$node) => x["y-z"]($$node));
  $.append($$anchor, button);
}

which is also correct and doesn't errors out.

Were you referring to a different code?

You can't do this

<script>
	let x = $state({"y-z": ()=>{}})
</script>

<button use:x["y-z"]>
	clicks
</button>

but that's actually a parsing error in svelte. We could fix it but i'ts not worth imho

@Rich-Harris
Copy link
Member

Huh, weird — I could have sworn it was failing for me locally with [0][0] but I can't seem to reproduce it now

@paoloricciuti
Copy link
Member Author

Huh, weird — I could have sworn it was failing for me locally with [0][0] but I can't seem to reproduce it now

Classic old version of the compiler still in cache or yet to finish build? Sometimes it even happen between refreshes if roll-up is a tad slower.

I tested all it came to mind but if there's some case not covered I'm happy to add that

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.

Array access not supported in the use or transition directives
2 participants