-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: tighten up
export default
validation
through #14363 I noticed our `export default` validation wasn't quite right: - missed checking for derived/state exports - the "cannot have a default export" error was only thrown if you did `export default` from the instance script, but it shouldn't matter from which component part you export it; it's never ok
- Loading branch information
1 parent
32a1453
commit a62f408
Showing
10 changed files
with
83 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: tighten up `export default` validation |
9 changes: 7 additions & 2 deletions
9
packages/svelte/src/compiler/phases/2-analyze/visitors/ExportDefaultDeclaration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ges/svelte/tests/compiler-errors/samples/export-default-derived-state-indirect/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
error: { | ||
code: 'derived_invalid_export', | ||
message: | ||
'Cannot export derived state from a module. To expose the current derived value, export a function returning its value', | ||
position: [61, 83] | ||
} | ||
}); |
5 changes: 5 additions & 0 deletions
5
...svelte/tests/compiler-errors/samples/export-default-derived-state-indirect/main.svelte.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let count = $state(0); | ||
|
||
const double = $derived(count * 2); | ||
|
||
export default double; |
10 changes: 10 additions & 0 deletions
10
packages/svelte/tests/compiler-errors/samples/export-default-state-indirect/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
error: { | ||
code: 'state_invalid_export', | ||
message: | ||
"Cannot export state from a module if it is reassigned. Either export a function returning the state value or only mutate the state value's properties", | ||
position: [93, 118] | ||
} | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/svelte/tests/compiler-errors/samples/export-default-state-indirect/main.svelte.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
let primitive = $state('nope'); | ||
|
||
export function update_primitive() { | ||
primitive = 'yep'; | ||
} | ||
|
||
export default primitive; |
14 changes: 14 additions & 0 deletions
14
packages/svelte/tests/validator/samples/default-export-module/errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"code": "module_illegal_default_export", | ||
"message": "A component cannot have a default export", | ||
"start": { | ||
"line": 2, | ||
"column": 1 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 19 | ||
} | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
packages/svelte/tests/validator/samples/default-export-module/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script module> | ||
export default 42; | ||
</script> |