Skip to content

Commit 63cbe21

Browse files
authored
fix: disallow export { foo as default } in <script module> (#16447)
* fix: disallow `export { foo as default }` in `<script module>` * add test * fix test
1 parent 27c90df commit 63cbe21

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

.changeset/curly-buttons-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: disallow `export { foo as default }` in `<script module>`

packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ export function ExportNamedDeclaration(node, context) {
1111
// visit children, so bindings are correctly initialised
1212
context.next();
1313

14+
if (
15+
context.state.ast_type &&
16+
node.specifiers.some((specifier) =>
17+
specifier.exported.type === 'Identifier'
18+
? specifier.exported.name === 'default'
19+
: specifier.exported.value === 'default'
20+
)
21+
) {
22+
e.module_illegal_default_export(node);
23+
}
24+
1425
if (node.declaration?.type === 'VariableDeclaration') {
1526
// in runes mode, forbid `export let`
1627
if (
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "module_illegal_default_export",
4+
"message": "A component cannot have a default export",
5+
"start": {
6+
"line": 3,
7+
"column": 4
8+
},
9+
"end": {
10+
"line": 3,
11+
"column": 32
12+
}
13+
}
14+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script module>
2+
let answer = 42;
3+
export { answer as default};
4+
</script>

0 commit comments

Comments
 (0)