Skip to content

chore: add inline new class warning #9583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2023
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
5 changes: 5 additions & 0 deletions .changeset/cold-birds-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: add inline new class warning
14 changes: 9 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,20 @@ export function analyze_module(ast, options) {
}
}

/** @type {import('../types').RawWarning[]} */
const warnings = [];

const analysis = {
warnings
};

walk(
/** @type {import('estree').Node} */ (ast),
{ scope },
{ scope, analysis },
// @ts-expect-error TODO clean this mess up
merge(set_scope(scopes), validation_runes_js, runes_scope_js_tweaker)
);

/** @type {import('../types').RawWarning[]} */
const warnings = [];

// If we are in runes mode, then check for possible misuses of state runes
for (const [, scope] of scopes) {
for (const [name, binding] of scope.declarations) {
Expand Down Expand Up @@ -608,7 +612,7 @@ const legacy_scope_tweaker = {
}
};

/** @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, { scope: Scope }>} */
/** @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, { scope: Scope, analysis: { warnings: import('../types').RawWarning[] } }>} */
const runes_scope_js_tweaker = {
VariableDeclarator(node, { state }) {
if (node.init?.type !== 'CallExpression') return;
Expand Down
23 changes: 22 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,26 @@ export const validation_runes_js = {
...context.state,
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';
// 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;

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');
}
}
};

Expand Down Expand Up @@ -721,5 +741,6 @@ export const validation_runes = merge(validation, a11y_validators, {
}
}
},
ClassBody: validation_runes_js.ClassBody
ClassBody: validation_runes_js.ClassBody,
NewExpression: validation_runes_js.NewExpression
});
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
SwitchStatement: create_block_scope,

ClassDeclaration(node, { state, next }) {
if (node.id) state.scope.declare(node.id, 'normal', 'const');
if (node.id) state.scope.declare(node.id, 'normal', 'const', node);
next();
},

Expand Down
8 changes: 8 additions & 0 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,20 @@ const state = {
`State referenced in its own scope will never update. Did you mean to reference it inside a closure?`
};

/** @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.`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have something in here like "or use getters/setters instead" because many people use it as a shortcut for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to tell people to use getters/setters here, or to just hoist their class?

};

/** @satisfies {Warnings} */
const warnings = {
...css,
...attributes,
...runes,
...a11y,
...performance,
...state
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
function bar() {
class Foo {
foo = $state(0)
}
const a = new Foo();
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +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.",
"start": {
"column": 12,
"line": 6
},
"end": {
"column": 21,
"line": 6
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
class Foo {
foo = $state(0)
}
function bar() {
const a = new Foo();
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
function bar() {
const a = new class Foo {
foo = $state(0)
}
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +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.",
"start": {
"column": 12,
"line": 3
},
"end": {
"column": 3,
"line": 5
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script context="module">
const a = new class Foo {
foo = $state(0)
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
const a = new class {
foo = $state(0)
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +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.",
"start": {
"column": 11,
"line": 2
},
"end": {
"column": 2,
"line": 4
}
}
]