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

Sveltosis: Treat arrow functions as regular function declarations #1188

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

Conversation

dritter
Copy link

@dritter dritter commented May 17, 2023

Hi there!

This is a clumsy approach to treat arrow functions equal to regular function declarations.

I usually write my functions as arrow functions like so (as a matter of personal taste, I find it more readable):

<script>
const myClickHandler = (event) => {
  console.log('do something')
}
</script>

<button on:click={myClickHandler}>Click Me</button>

Before this PR the generated code looked like this:

<script>
  let myClickHandler = null;
</script>

<button
  on:click={(event) => {
    myClickHandler(event);
  }}>Click Me</button
>

So the ArrowFunction was detected as VariableDeclaration, which is not wrong, but the generated code wouldn't work anyway.

Edit:
With this PR, the generated code looks like this (have to check that).
It should keep the ArrowFunction as is. I'm running out of time for today :/

This is not perfect, but I created this PR to start the discussion. :)

Caveats

This might have some issues depending how the ArrowFunction was declared. E.g. if destructuring is involved, the code breaks very likely.

Fixes #1182

@dritter dritter requested a review from samijaber as a code owner May 17, 2023 16:02
@vercel
Copy link

vercel bot commented May 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mitosis-fiddle ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 31, 2023 4:33pm

by passing the AssignmentExpression to handleFunctionDeclaration().
@dritter
Copy link
Author

dritter commented May 31, 2023

Okay.. The current version of fatArrow.svelte as input generates the following svelte file:

<script>
  clickHandler1 = (event) => {
    console.log("do something 1");
  };

  function clickHandler2(event) {
    console.log("do something 2");
  }
</script>

<button
  on:click={(event) => {
    clickHandler1(event);
  }}>Button 1</button
>
<button
  on:click={(event) => {
    clickHandler2(event);
  }}>Button 2</button
>

(note the missing const).

In vue this looks worse:

<template>
  <div>
    <button @click="clickHandler1($event)">Button 1</button>
    <button @click="clickHandler2($event)">Button 2</button>
  </div>
</template>

<script>
import { defineComponent } from "vue";

export default defineComponent({
  name: "fat-arrow",

  methods: {
    clickHandler1: (this.clickHandler1 = (event) => {
      console.log("do something 1");
    }),
    clickHandler2: function clickHandler2(event) {
      console.log("do something 2");
    },
  },
});
</script>

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.

Fat arrow functions and regular functions handled differently
1 participant