put nested class warning on the declaration, not the usage site (#9592)

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/9593/head
Rich Harris 2 years ago committed by GitHub
parent fe9c0bc19d
commit 13aef5245b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -580,24 +580,19 @@ export const validation_runes_js = {
private_derived_state private_derived_state
}); });
}, },
NewExpression(node, context) { ClassDeclaration(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';
// In modules, we allow top-level module scope only, in components, we allow the component scope, // 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 // which is function_depth of 1. With the exception of `new class` which is also not allowed at
// component scope level either. // component scope level either.
const allowed_depth = is_module ? 0 : 1; const allowed_depth = context.state.ast_type === 'module' ? 0 : 1;
if ( if (context.state.scope.function_depth > allowed_depth) {
(callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) || warn(context.state.analysis.warnings, node, context.path, 'avoid-nested-class');
(binding !== null && }
binding.initial !== null && },
binding.initial.type === 'ClassDeclaration' && NewExpression(node, context) {
binding.scope.function_depth > allowed_depth) if (node.callee.type === 'ClassExpression' && context.state.scope.function_depth > 0) {
) { warn(context.state.analysis.warnings, node, context.path, 'avoid-inline-class');
warn(context.state.analysis.warnings, node, context.path, 'inline-new-class');
} }
} }
}; };
@ -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, ClassBody: validation_runes_js.ClassBody,
ClassDeclaration: validation_runes_js.ClassDeclaration,
NewExpression: validation_runes_js.NewExpression NewExpression: validation_runes_js.NewExpression
}); });

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

@ -1,14 +1,14 @@
[ [
{ {
"code": "inline-new-class", "code": "avoid-nested-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.", "message": "Avoid declaring classes below the top level scope",
"start": { "start": {
"column": 12, "column": 2,
"line": 6 "line": 3
}, },
"end": { "end": {
"column": 21, "column": 3,
"line": 6 "line": 5
} }
} }
] ]

@ -1,7 +1,7 @@
[ [
{ {
"code": "inline-new-class", "code": "avoid-inline-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.", "message": "Avoid 'new class' — instead, declare the class at the top level scope",
"start": { "start": {
"column": 12, "column": 12,
"line": 3 "line": 3

@ -1,7 +1,7 @@
[ [
{ {
"code": "inline-new-class", "code": "avoid-inline-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.", "message": "Avoid 'new class' — instead, declare the class at the top level scope",
"start": { "start": {
"column": 11, "column": 11,
"line": 2 "line": 2

Loading…
Cancel
Save