Skip to content

Commit 4e501d2

Browse files
committed
address feedback
1 parent 8be9f04 commit 4e501d2

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,18 @@ export const validation_runes_js = {
584584
const callee = node.callee;
585585

586586
const binding = callee.type === 'Identifier' ? context.state.scope.get(callee.name) : null;
587+
const is_module = context.state.ast_type === 'module';
588+
// In modules, we allow top-level module scope only, in components, we allow the component scope,
589+
// which is function_depth of 1. With the exception of `new class` which is also not allowed at
590+
// component scope level either.
591+
const allowed_depth = is_module ? 0 : 1;
592+
587593
if (
588-
(callee.type === 'ClassExpression' && context.state.scope.function_depth !== 0) ||
594+
(callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) ||
589595
(binding !== null &&
590596
binding.initial !== null &&
591597
binding.initial.type === 'ClassDeclaration' &&
592-
binding.scope.function_depth !== 0)
598+
binding.scope.function_depth > allowed_depth)
593599
) {
594600
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
595601
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script>
2-
class Foo {
3-
foo = $state(0)
2+
function bar() {
3+
class Foo {
4+
foo = $state(0)
5+
}
6+
const a = new Foo();
47
}
5-
const a = new Foo();
68
</script>

packages/svelte/tests/validator/samples/inline-new-class-2/warnings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"code": "inline-new-class",
44
"message": "Creating inline classes will likely cause performance issues. Instead, declare the class at the module-level and create new instances from the class reference.",
55
"start": {
6-
"column": 11,
7-
"line": 5
6+
"column": 12,
7+
"line": 6
88
},
99
"end": {
10-
"column": 20,
11-
"line": 5
10+
"column": 21,
11+
"line": 6
1212
}
1313
}
1414
]

0 commit comments

Comments
 (0)