unplugin / unplugin-vue2-script-setup

💡 Bring `<script setup>` to Vue 2.
MIT License
603 stars 39 forks source link

[Bug] Variable's in-template usage not detected #115

Closed DamianGlowala closed 2 years ago

DamianGlowala commented 2 years ago

In the following scenario, the CurrentYear variable doesn't get picked up.

<i18n>
{
    "en": {
        "copyrightNotice": "{currentYear} (...)"
    }
}
</i18n>

<template>
    <span>
        {{ $t("copyrightNotice", { currentYear: CurrentYear }) }}
    </span>
</template>

<script setup lang="ts">
import { computed, useContext } from "@nuxtjs/composition-api";

const { $moment } = useContext();
const CurrentYear = computed(() => $moment().year());
</script>

It does work though when variable is present in the following way:

<template>
    <span>
        {{ CurrentYear }}
    </span>
</template>

Is there some background optimisation in place detecting unused variables and ignoring them? If so, unfortunately this is a case when it appears to fail.

DamianGlowala commented 2 years ago

Hey @xiaoxiangmoe, as it's been a while I wonder how the progress is going so far on this issue? Were you able to reproduce the problem? If not, I can provide a reproduction, just let me know 😃

xiaoxiangmoe commented 2 years ago

@DamianGlowala I'm trying to rewrite parseSFC logic. It will take some time.

xiaoxiangmoe commented 2 years ago

@DamianGlowala I can't reproduce it. Could you give me a minimal reproduction?

DamianGlowala commented 2 years ago

Definitely, I will share a reproduction as soon as I get meaningful results. I was overconfident with the above snippets - when I attempted to reproduce the problem myself in a blank project, I couldn't do that in any way. Turns out to be more tricky than I thought. Whenever I have some spare time, I will try to replicate my current project step-by-step and see which bit of code (or library) triggers the bug. Very bizarre. I am intrigued what makes it skip the variable so I will keep digging!

DamianGlowala commented 2 years ago

It turns out this library was a dependency of @nuxtjs/composition-api (version 0.31.0), which had "unplugin-vue2-script-setup": "^0.7.3"... I have updated to the latest version and it all works like a charm :) As trivial as it looks, it wasn't straightforward to spot at first! 😄