Skip to content

Commit 9a9226c

Browse files
committed
error on undefined export
1 parent e0648bf commit 9a9226c

File tree

7 files changed

+49
-1
lines changed

7 files changed

+49
-1
lines changed

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ Expected token %token%
400400
Expected whitespace
401401
```
402402

403+
### export_undefined
404+
405+
```
406+
`%name%` is not defined
407+
```
408+
403409
### global_reference_invalid
404410

405411
```

packages/svelte/messages/compile-errors/script.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
> `$effect()` can only be used as an expression statement
4040
41+
## export_undefined
42+
43+
> `%name%` is not defined
44+
4145
## global_reference_invalid
4246

4347
> `%name%` is an illegal variable name. To reference a global variable called `%name%`, use `globalThis.%name%`

packages/svelte/src/compiler/errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ export function effect_invalid_placement(node) {
168168
e(node, "effect_invalid_placement", "`$effect()` can only be used as an expression statement");
169169
}
170170

171+
/**
172+
* `%name%` is not defined
173+
* @param {null | number | NodeLike} node
174+
* @param {string} name
175+
* @returns {never}
176+
*/
177+
export function export_undefined(node, name) {
178+
e(node, "export_undefined", `\`${name}\` is not defined`);
179+
}
180+
171181
/**
172182
* `%name%` is an illegal variable name. To reference a global variable called `%name%`, use `globalThis.%name%`
173183
* @param {null | number | NodeLike} node

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,17 @@ export function analyze_component(root, source, options) {
692692
analysis.reactive_statements = order_reactive_statements(analysis.reactive_statements);
693693
}
694694

695+
for (const node of analysis.module.ast.body) {
696+
if (node.type === 'ExportNamedDeclaration' && node.specifiers !== null) {
697+
for (const specifier of node.specifiers) {
698+
if (specifier.local.type !== 'Identifier') continue;
699+
700+
const binding = analysis.module.scope.get(specifier.local.name);
701+
if (!binding) e.export_undefined(specifier, specifier.local.name);
702+
}
703+
}
704+
}
705+
695706
if (analysis.event_directive_node && analysis.uses_event_attributes) {
696707
e.mixed_event_handler_syntaxes(
697708
analysis.event_directive_node,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { AST } from '#compiler' */
1+
/** @import { AST, Binding } from '#compiler' */
22
/** @import { Context } from '../types' */
33
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';
44
import * as e from '../../../errors.js';
@@ -31,6 +31,11 @@ export function SnippetBlock(node, context) {
3131
const undefined_exports = context.state.analysis.undefined_exports;
3232
const name = node.expression.name;
3333

34+
if (can_hoist) {
35+
const binding = /** @type {Binding} */ (context.state.scope.get(name));
36+
context.state.analysis.module.scope.declarations.set(name, binding);
37+
}
38+
3439
if (!can_hoist && undefined_exports.has(name)) {
3540
e.snippet_invalid_export(/** @type {any} */ (undefined_exports.get(name)));
3641
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'export_undefined',
6+
message: '`blah` is not defined',
7+
position: [26, 30]
8+
}
9+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script module>
2+
export { blah };
3+
</script>

0 commit comments

Comments
 (0)