sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
80.25k stars 4.27k forks source link

Function Overloading Bugging Rollup when Reading .svelte Files #14455

Closed elizeuangelo closed 2 days ago

elizeuangelo commented 3 days ago

Describe the bug

When I have a component which includes function overloading (inside <script module>):

export function test(
        type: 'test',
        props: {test: number}
    ): void;
export function test(type: 'test2'): void;
export function test(type: string, props?: Record<string, any>) {
        console.log(type, props);
    }

Everything seens fine on the IDE but I still get this weird Rollup Error when running Vite or on the Playground:

[vite] Error when evaluating SSR module /src/routes/+layout.server.ts:
|- RollupError: Expected '{', got ';'
    at getRollupError (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/rollup@4.27.2/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
    at convertProgram (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/rollup@4.27.2/node_modules/rollup/dist/es/shared/parseAst.js:1084:26)
    at parseAstAsync (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/rollup@4.27.2/node_modules/rollup/dist/es/shared/parseAst.js:2070:106)
    at async ssrTransformScript (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/vite@5.4.11_@types+node@22.9.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:52381:11)
    at async loadAndTransform (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/vite@5.4.11_@types+node@22.9.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:51979:72)
    at async instantiateModule (file:///D:/Coding/Projetos/Playably/github/advent-calendar-template/node_modules/.pnpm/vite@5.4.11_@types+node@22.9.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:52874:44)

Reproduction

https://svelte.dev/playground/721e958b91b744a192d9557d007fd316?version=5.2.9


<script module lang="ts">
  // Component.svelte
  export function test(
          type: 'test',
          props: {test: number}
      ): void;
  export function test(type: 'test2'): void;
  export function test(type: string, props?: Record<string, any>) {
          console.log(type, props);
      }
</script>

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 22.88 GB / 31.92 GB
Binaries:
    Node: 20.11.1 - C:\Program Files\nodejs\node.EXE
    npm: 9.7.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.14.2 - ~\AppData\Local\pnpm\pnpm.CMD
Browsers:
    Chrome: 131.0.6778.86
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.19041.4355
npmPackages:
    svelte: ^5.2.1 => 5.2.1

Severity

annoyance

dummdidumm commented 3 days ago

We need to detect this and either handle it while stripping types or error and require devs to use a TS preprocessor. Since this doesn't require anything besides removing types we should be able to do the former