tomblachut / svelte-intellij

Svelte components in WebStorm and friends
MIT License
482 stars 38 forks source link

Support resolving to module context exports from other files #241

Closed shorn closed 1 year ago

shorn commented 3 years ago

I am using Typescript with Svelte, IDEA version 2021.1.3 and plugin version 0.20.0

It is valid Svelte code to declare module based constant exports in a svelte component like this:

<script context="module">
    export const answer = 42;
</script>

This constant can then be imported in a different svelte component with this code:

import { answer } from './Component.svelte'

See this REPL for an example of working code: , which I found linked at this StackOverflow answer.

The Svelte plugin will report in IDEA that the import is in error because cannot resolve symbol 'answer'. However the Svelte app compiles and works fine.

I can work around this by declaring the constant in a third standard .ts file and importing it into both Svelte components.

Or, I can add // noinspection TypeScriptCheckImport to disable the import check (and thus the error) for that statement.

This issue is raised to request that the plugin be enhanced to make IDEA aware of exports from Svelte context="module" script blocks.

r00t3g commented 3 years ago

It seems to be mentioned in #223 already

tomblachut commented 3 years ago

@r00t3g yeah you're right, anyway let's keep this issue since there's nice workaround specified.

Here's the original mention:


I ran into a similiar problem with context="module". Their exports are also not visible although it compiles and works fine

Provider.svelte

<script context='module' lang='ts'>
  export const foo:string = 'bar';
</script>

Main.svelte (with workaround)

<script lang='ts'>
  import {foo as _foo} from './Provider.svelte';  // foo is in red - not found
  const foo = _foo as string;  // Workaround to get the typing back
</script>

Originally posted by @Treverix in https://github.com/tomblachut/svelte-intellij/issues/223#issuecomment-859934247

lxhom commented 2 years ago

I've found another workaround that's pretty simple and automatically imports types:

Control.svelte
<script lang="ts" context="module">
    import App, * as _App from "./App.svelte"
    let { gui, Button } = _App;
    ...
</script>
<App />
App.svelte
<script lang="ts" context="module">
    ...
    let gui = new GUIClass();
    enum Button { Encrypt, Decrypt, Help }
    ...
    export { gui, Button }
</script>
...

Works like a charm in WebStorm. 2021-11-15-104038_746x328_scrot

GavinSiver commented 1 year ago

I am also experiencing this issue - it would be nice if we could get support for this 🙂

tomblachut commented 1 year ago

Migrated to WEB-54714 Svelte: modules exported from a 'context="module"' script block of the other component not resolved