Skip to content

Commit 13aef52

Browse files
authored
put nested class warning on the declaration, not the usage site (#9592)
Co-authored-by: Rich Harris <[email protected]>
1 parent fe9c0bc commit 13aef52

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,19 @@ export const validation_runes_js = {
580580
private_derived_state
581581
});
582582
},
583-
NewExpression(node, context) {
584-
const callee = node.callee;
585-
586-
const binding = callee.type === 'Identifier' ? context.state.scope.get(callee.name) : null;
587-
const is_module = context.state.ast_type === 'module';
583+
ClassDeclaration(node, context) {
588584
// In modules, we allow top-level module scope only, in components, we allow the component scope,
589585
// which is function_depth of 1. With the exception of `new class` which is also not allowed at
590586
// component scope level either.
591-
const allowed_depth = is_module ? 0 : 1;
587+
const allowed_depth = context.state.ast_type === 'module' ? 0 : 1;
592588

593-
if (
594-
(callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) ||
595-
(binding !== null &&
596-
binding.initial !== null &&
597-
binding.initial.type === 'ClassDeclaration' &&
598-
binding.scope.function_depth > allowed_depth)
599-
) {
600-
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
589+
if (context.state.scope.function_depth > allowed_depth) {
590+
warn(context.state.analysis.warnings, node, context.path, 'avoid-nested-class');
591+
}
592+
},
593+
NewExpression(node, context) {
594+
if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
595+
warn(context.state.analysis.warnings, node, context.path, 'avoid-inline-class');
601596
}
602597
}
603598
};
@@ -741,6 +736,8 @@ export const validation_runes = merge(validation, a11y_validators, {
741736
}
742737
}
743738
},
739+
// TODO this is a code smell. need to refactor this stuff
744740
ClassBody: validation_runes_js.ClassBody,
741+
ClassDeclaration: validation_runes_js.ClassDeclaration,
745742
NewExpression: validation_runes_js.NewExpression
746743
});

packages/svelte/src/compiler/warnings.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ const state = {
193193

194194
/** @satisfies {Warnings} */
195195
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.`
196+
'avoid-inline-class': () =>
197+
`Avoid 'new class' — instead, declare the class at the top level scope`,
198+
'avoid-nested-class': () => `Avoid declaring classes below the top level scope`
199199
};
200200

201201
/** @satisfies {Warnings} */
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[
22
{
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.",
3+
"code": "avoid-nested-class",
4+
"message": "Avoid declaring classes below the top level scope",
55
"start": {
6-
"column": 12,
7-
"line": 6
6+
"column": 2,
7+
"line": 3
88
},
99
"end": {
10-
"column": 21,
11-
"line": 6
10+
"column": 3,
11+
"line": 5
1212
}
1313
}
1414
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
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.",
3+
"code": "avoid-inline-class",
4+
"message": "Avoid 'new class' — instead, declare the class at the top level scope",
55
"start": {
66
"column": 12,
77
"line": 3

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
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.",
3+
"code": "avoid-inline-class",
4+
"message": "Avoid 'new class' — instead, declare the class at the top level scope",
55
"start": {
66
"column": 11,
77
"line": 2

0 commit comments

Comments
 (0)