Microsoft/vscode

[html] "Cannot redeclare block-scoped variable" in script type module

Open

#169,117 创建于 2022年12月14日

在 GitHub 查看
 (5 评论) (5 反应) (1 负责人)TypeScript (10,221 fork)batch import
bughelp wantedhtml

仓库指标

Star
 (74,848 star)
PR 合并指标
 (平均合并 11小时 43分钟) (30 天内合并 1,000 个 PR)

描述

Does this issue occur when all extensions are disabled?: Yes

System:

  • VS Code Version: Version: 1.74.0
  • OS Version: macOS 13.0.1

Steps to Reproduce:

  1. Create a new HTML file
  2. Add two <script type="module"> and declare a variable with the same name in both (see example code below).
  3. Observe the red squiggly line underneath the variable names saying Cannot redeclare block-scoped variable.
image

When a script tag has the type attribute set to "module", the source text should be treated as an ES module with it's own scope, not the global scope.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts

Example HTML

<!DOCTYPE html>
<script type="module">
  const x = 1;
</script>
<script type="module">
  const x = 2;
</script>

贡献者指南