Skip to content

Commit 717687c

Browse files
committed
chore: add inline new class warning
1 parent 46c572a commit 717687c

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

.changeset/cold-birds-own.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+
chore: add inline new class warning

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@ export const validation_runes_js = {
579579
...context.state,
580580
private_derived_state
581581
});
582+
},
583+
NewExpression(node, context) {
584+
const callee = node.callee;
585+
586+
if (callee.type === 'ClassExpression' && context.state.scope.function_depth !== 0) {
587+
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
588+
}
582589
}
583590
};
584591

@@ -721,5 +728,6 @@ export const validation_runes = merge(validation, a11y_validators, {
721728
}
722729
}
723730
},
724-
ClassBody: validation_runes_js.ClassBody
731+
ClassBody: validation_runes_js.ClassBody,
732+
NewExpression: validation_runes_js.NewExpression
725733
});

packages/svelte/src/compiler/warnings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,20 @@ const state = {
191191
`State referenced in its own scope will never update. Did you mean to reference it inside a closure?`
192192
};
193193

194+
/** @satisfies {Warnings} */
195+
const performance = {
196+
'inline-new-class': () =>
197+
`Creating inline classes will likely cause performance issues. ` +
198+
`Instead, declare the class at the module-level and create new instances from the class reference.`
199+
};
200+
194201
/** @satisfies {Warnings} */
195202
const warnings = {
196203
...css,
197204
...attributes,
198205
...runes,
199206
...a11y,
207+
...performance,
200208
...state
201209
};
202210

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

0 commit comments

Comments
 (0)