Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,24 +580,19 @@ export const validation_runes_js = {
private_derived_state
});
},
NewExpression(node, context) {
const callee = node.callee;

const binding = callee.type === 'Identifier' ? context.state.scope.get(callee.name) : null;
const is_module = context.state.ast_type === 'module';
ClassDeclaration(node, context) {
// In modules, we allow top-level module scope only, in components, we allow the component scope,
// which is function_depth of 1. With the exception of `new class` which is also not allowed at
// component scope level either.
const allowed_depth = is_module ? 0 : 1;
const allowed_depth = context.state.ast_type === 'module' ? 0 : 1;

if (
(callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) ||
(binding !== null &&
binding.initial !== null &&
binding.initial.type === 'ClassDeclaration' &&
binding.scope.function_depth > allowed_depth)
) {
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
if (context.state.scope.function_depth > allowed_depth) {
warn(context.state.analysis.warnings, node, context.path, 'avoid-nested-class');
}
},
NewExpression(node, context) {
if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
warn(context.state.analysis.warnings, node, context.path, 'avoid-inline-class');
}
}
};
Expand Down Expand Up @@ -741,6 +736,8 @@ export const validation_runes = merge(validation, a11y_validators, {
}
}
},
// TODO this is a code smell. need to refactor this stuff
ClassBody: validation_runes_js.ClassBody,
ClassDeclaration: validation_runes_js.ClassDeclaration,
NewExpression: validation_runes_js.NewExpression
});
6 changes: 3 additions & 3 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ const state = {

/** @satisfies {Warnings} */
const performance = {
'inline-new-class': () =>
`Creating inline classes will likely cause performance issues. ` +
`Instead, declare the class at the module-level and create new instances from the class reference.`
'avoid-inline-class': () =>
`Avoid 'new class' — instead, declare the class at the top level scope`,
'avoid-nested-class': () => `Avoid declaring classes below the top level scope`
};

/** @satisfies {Warnings} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
{
"code": "inline-new-class",
"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.",
"code": "avoid-nested-class",
"message": "Avoid declaring classes below the top level scope",
"start": {
"column": 12,
"line": 6
"column": 2,
"line": 3
},
"end": {
"column": 21,
"line": 6
"column": 3,
"line": 5
}
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "inline-new-class",
"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.",
"code": "avoid-inline-class",
"message": "Avoid 'new class' — instead, declare the class at the top level scope",
"start": {
"column": 12,
"line": 3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "inline-new-class",
"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.",
"code": "avoid-inline-class",
"message": "Avoid 'new class' — instead, declare the class at the top level scope",
"start": {
"column": 11,
"line": 2
Expand Down