Skip to content

Commit 8be9f04

Browse files
committed
Address feedback
1 parent 717687c commit 8be9f04

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,20 @@ export function analyze_module(ast, options) {
201201
}
202202
}
203203

204+
/** @type {import('../types').RawWarning[]} */
205+
const warnings = [];
206+
207+
const analysis = {
208+
warnings
209+
};
210+
204211
walk(
205212
/** @type {import('estree').Node} */ (ast),
206-
{ scope },
213+
{ scope, analysis },
207214
// @ts-expect-error TODO clean this mess up
208215
merge(set_scope(scopes), validation_runes_js, runes_scope_js_tweaker)
209216
);
210217

211-
/** @type {import('../types').RawWarning[]} */
212-
const warnings = [];
213-
214218
// If we are in runes mode, then check for possible misuses of state runes
215219
for (const [, scope] of scopes) {
216220
for (const [name, binding] of scope.declarations) {
@@ -608,7 +612,7 @@ const legacy_scope_tweaker = {
608612
}
609613
};
610614

611-
/** @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, { scope: Scope }>} */
615+
/** @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, { scope: Scope, analysis: { warnings: import('../types').RawWarning[] } }>} */
612616
const runes_scope_js_tweaker = {
613617
VariableDeclarator(node, { state }) {
614618
if (node.init?.type !== 'CallExpression') return;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,14 @@ export const validation_runes_js = {
583583
NewExpression(node, context) {
584584
const callee = node.callee;
585585

586-
if (callee.type === 'ClassExpression' && context.state.scope.function_depth !== 0) {
586+
const binding = callee.type === 'Identifier' ? context.state.scope.get(callee.name) : null;
587+
if (
588+
(callee.type === 'ClassExpression' && context.state.scope.function_depth !== 0) ||
589+
(binding !== null &&
590+
binding.initial !== null &&
591+
binding.initial.type === 'ClassDeclaration' &&
592+
binding.scope.function_depth !== 0)
593+
) {
587594
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
588595
}
589596
}

packages/svelte/src/compiler/phases/scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
437437
SwitchStatement: create_block_scope,
438438

439439
ClassDeclaration(node, { state, next }) {
440-
if (node.id) state.scope.declare(node.id, 'normal', 'const');
440+
if (node.id) state.scope.declare(node.id, 'normal', 'const', node);
441441
next();
442442
},
443443

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
class Foo {
3+
foo = $state(0)
4+
}
5+
const a = new Foo();
6+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "inline-new-class",
4+
"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.",
5+
"start": {
6+
"column": 11,
7+
"line": 5
8+
},
9+
"end": {
10+
"column": 20,
11+
"line": 5
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)