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

Imports used exclusively in the <template> part of Vue SFCs are considered "never used" by Volar #1757

Closed
simensol opened this issue Aug 24, 2022 · 6 comments
Labels
duplicate This issue or pull request already exists

Comments

@simensol
Copy link

simensol commented Aug 24, 2022

This issue was originally raised in simonhaenisch/prettier-plugin-organize-imports, a library which sorts imports and removes unused ones.

Originally posted by @simonhaenisch in simonhaenisch/prettier-plugin-organize-imports#71 (comment):

[simonhaenisch/prettier-plugin-organize-imports] is using @volar/vue-typescript as a drop-in replacement to create the language service, and after that it's just a matter of calling the organizeImports method.

It seems that imports exclusively used in the <template> part of Vue SFCs are incorrectly considered as "never used" by Volar. Here is a minimal working example:

package.json:

...
    "devDependencies": {
        "@volar/vue-typescript": "^0.40.1",
        "prettier": "^2.7.1",
        "prettier-plugin-organize-imports": "^3.1.0",
...

types.ts:

export enum ThemeColor {
    PRIMARY = "primary",
    SECONDARY = "secondary",
    ACCENT = "accent",
    ERROR = "error",
    INFO = "info",
    SUCCESS = "success",
    WARNING = "warning"
}

sfc.vue:

<script setup lang="ts">
import { ThemeColor } from "@/types"
</script>

<template>
    Color is: {{ ThemeColor.PRIMARY }}
</template>

import { ThemeColor } from "@/types" is incorrectly considered as "never used" by Volar and then removed by simonhaenisch/prettier-plugin-organize-imports. If I add const color = ThemeColor.PRIMARY to the <script setup> part of sfc.vue, the import is not considered "never used" by Volar.

The same happens with functions used exclusively in the <template> part of Vue SFCs:

helpers.ts:

export function helloWorld(): string {
    return "Hello Worlds"
}

sfc.vue:

<script setup lang="ts">
import { helloWorld } from "@/helpers"
</script>

<template>
    {{ helloWorld() }}
</template>

Here, import { helloWorld } from "@/helpers" is marked as "never used" by Volar. However, if I add const hello = helloWorld() to the <script setup> part, the import is kept untouched.

@simonhaenisch
Copy link

Could be related to path aliases as well btw? Have you tried whether it's reproducible with a relative path instead of @/?

@simensol
Copy link
Author

Same isse with

<script setup lang="ts">
import { ThemeColor } from "../utilities/types"
</script>

<template>Color is: {{ ThemeColor.PRIMARY }}</template>

@craigrileyuk
Copy link

I'm getting the same problem too, and I'm not directly using Typescript in my project.

@johnsoncodehk
Copy link
Member

Duplicate of #1729

@johnsoncodehk johnsoncodehk marked this as a duplicate of #1729 Aug 27, 2022
@johnsoncodehk johnsoncodehk added the duplicate This issue or pull request already exists label Aug 27, 2022
@simonhaenisch
Copy link

Thanks for the quick fix! Will this be released as 0.40.2 or 0.41.0?

@johnsoncodehk
Copy link
Member

@simonhaenisch it's v0.40.2 because there are no breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants