Skip to content

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

@Renerick

Description

@Renerick

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions