Skip to content

Compiler throws declaration_duplicate for doubled var declarations #15375

@ginpei

Description

@ginpei

Describe the bug

Having two variable declarations using var is valid in JavaScript, but the compiler throws declaration_duplicate error.

I found this issue during my Svelte 5 migration. Here is a scenario:

  1. Had a Svelte 4 project using $$props:

    <script lang="ts">
      const allProps = $$props;
    </script>
  2. npx sv migrate svelte-5 converts the component into like:

    <script lang="ts">
      interface Props {
        [key: string]: any
      }
    
      let { ...props }: Props = $props();
      const allProps = props;
    </script>
  3. Build the project with esbuild-svelte

  4. The error occurs. The message doesn't make sense

    ✘ [ERROR] `p` has already been declared
    https://svelte.dev/e/declaration_duplicate [plugin esbuild-svelte]
    
        src/App.svelte:7:24:
          7 │   const allProps = props;
            ╵                         ^
    …
    

Reproduction

By tidying up the source given to compile(), here is a minimal reproducible example:

const { compile } = require('svelte/compiler');

const source = `
<script lang="ts">
var oops;
var oops;
</script>
`;
compile(source, {});

Logs

[jr [CompileError]: `oops` has already been declared
https://svelte.dev/e/declaration_duplicate] {
  code: 'declaration_duplicate',
  filename: '(unknown)',
  position: [ 34, 38 ],
  start: { line: 4, column: 4, character: 34 },
  end: { line: 4, column: 8, character: 38 },
  frame: '2: <script lang="ts">\n' +
    '3: var oops;\n' +
    '4: var oops;\n' +
    '           ^\n' +
    '5: </script>\n' +
    '6: '
}

System Info

  System:
    OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
    Memory: 2.85 GB / 7.46 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 22.6.0 - ~/.nvm/versions/node/v22.6.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v22.6.0/bin/npm
  npmPackages:
    svelte: ^5.0.0 => 5.20.2 

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions