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

Runtime error if methods in use:obj.nested.method are deeper than 2 level #5844

Closed
Renerick opened this issue Jan 1, 2021 · 1 comment · Fixed by #5845
Closed

Runtime error if methods in use:obj.nested.method are deeper than 2 level #5844

Renerick opened this issue Jan 1, 2021 · 1 comment · Fixed by #5845

Comments

@Renerick
Copy link

Renerick commented Jan 1, 2021

Describe the bug
Introduced in #3935 support of methods in use:obj.action causes runtime exception if method is nested in multiple objects

<button use:ctx.button.behavior>Open</button>

Logs

ctx[0]['button.behavior'] is not a function

To Reproduce
https://svelte.dev/repl/b9576a0efc17479287e6bba62c1e9e85?version=3.31.0

Expected behavior
No exceptions, actions are applied to HTML node as usual
OR
compiler error that prevents usage of nested objects in action definition

Stacktraces
Seems like code causing the error is generated in /src/compiler/compile/render_dom/wrappers/shared/add_actions.ts:35

Information about your Svelte project:

  • Firefox 84.0.1 x64
  • Windows 10
  • Svelte 3.31.0
  • Node 15.4.0
  • Snowpack + Rollup

Severity
Somewhat annoying, but not critical. There is a workaround for now, but I think this should be addressed at the compiler level, at least to prevent unexpected runtime crashes.

<script>
	let ctx = {
                // or just use flat objects without extra nesting, obviously
		'button.behavior':  (node) => {
			console.log(node);
		},
	};
	
</script>

<button use:ctx.button.behavior>Open </button>

UPD: after some fiddling, using flat objects and avoiding nesting is the best option for now, since different IDEs react differently when workaround above is present. VSCode highlights it as missing property, WebStorm - does not

@Conduitry
Copy link
Member

This is fixed in 3.31.1 - https://svelte.dev/repl/b9576a0efc17479287e6bba62c1e9e85?version=3.31.1 - use:ctx.button.behavior now results in ctx[0].button.behavior rather than ctx[0]['button.behavior'].

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 a pull request may close this issue.

2 participants