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

export enum in Typescript ends up producing a Svelte compiler warning #7055

Closed
rgossiaux opened this issue Dec 26, 2021 · 3 comments · Fixed by #7232
Closed

export enum in Typescript ends up producing a Svelte compiler warning #7055

rgossiaux opened this issue Dec 26, 2021 · 3 comments · Fixed by #7232

Comments

@rgossiaux
Copy link
Contributor

Describe the bug

I'm not at all confident that this issue belongs in this repo, since it could plausibly go into several different ones, but I thought I'd file it here to begin with since it's ultimately the Svelte compiler which is generating the warning and there is no way to ignore it (adding the ability to silence individual compiler warnings like this in JS, similar to the <!-- svelte-ignore --> template comment, would be one way of resolving this).

Anyway, the issue: Typescript code with export enum from inside <script context="module" lang="ts"> block ends up generating an unused-export-let warning; see sveltejs/language-tools#573 While the language-tools project added special handling to deal with this as an editor warning, it still generates warnings in other parts of the ecosystem. Users of my library may see this from vite-plugin-svelte, for example: rgossiaux/svelte-headlessui#25

Besides the ability to silence the warning, alternate resolutions could be:

  • Modifying preprocessing so that the generated code is different and does not produce this warning;
  • Modifying the check for this warning (if it's possible to do this safely)

Reproduction

It's not a minimal repo, but you can repo this by installing https://github.com/rgossiaux/svelte-headlessui and running npm run dev. But I think something very basic should work too, along the lines of

<script lang="ts" context="module">
  export enum MyEnum {
    One,
    Two,
  }
</script>

Logs

No response

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 26.12 GB / 64.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.8.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23662_1640400023652/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 7.21.0 - /var/folders/rn/2kdgb0jj25q2v656j3qc_ywr0000gn/T/fnm_multishells/23662_1640400023652/bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Safari: 15.2
  npmPackages:
    svelte: ^3.44.0 => 3.44.3

Severity

annoyance

@jtlapp
Copy link

jtlapp commented Jan 21, 2022

Here's how I'm dealing with this in rollup.config.js. Unfortunately, having to list the individual enums makes this an undesirable hack. I didn't see a way to reliably detect the presence of an enum in the frame.

  plugins: [
    svelte({
      /* ... */
      onwarn: (warning, handler) => {
        const exportedEnums = ['InteractiveTreeFlags'];
        const { code, frame } = warning;
        const mentionsEnum = () =>
            exportedEnums.reduce((found, name) => (found || frame.includes(name)), false);
        if (code == "unused-export-let" && mentionsEnum()) {
          return;
        }
        handler(warning);
      }
    }),

rgossiaux added a commit to rgossiaux/svelte that referenced this issue Feb 7, 2022
Fixes sveltejs#7055

I think this might be the best way to fix the linked issue, and I think it's a reasonable change. Since code inside <script context="module"> is basically just regular JS, warnings like this can be delegated to userland tools--I don't think it needs to be the Svelte compiler's business especially when these compiler warnings cannot be ignored inline.
benmccann pushed a commit that referenced this issue Mar 3, 2022
…"> (#7232)

Fixes #7055. This warning can be delegated to userland tools like eslint
himanshiLt pushed a commit to himanshiLt/svelte that referenced this issue Mar 3, 2022
…"> (sveltejs#7232)

Fixes sveltejs#7055. This warning can be delegated to userland tools like eslint
@Conduitry
Copy link
Member

This should be fixed in 3.46.5.

@jtlapp
Copy link

jtlapp commented Apr 19, 2022

Thank you! I was able to delete my onwarn enum handling code using Svelte 3.47.0.

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.

3 participants