Skip to content
Open
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/lovely-moments-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat(prefer-svelte-reactivity): reporting returned variables
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export default createRule('prefer-svelte-reactivity', {
]
},
create(context) {
const returnedVariables: Map<
TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration,
TSESTree.VariableDeclarator[]
> = new Map();
const exportedVars: TSESTree.Node[] = [];
return {
...(getSvelteContext(context)?.svelteFileType === '.svelte.[js|ts]' && {
Expand Down Expand Up @@ -59,6 +63,28 @@ export default createRule('prefer-svelte-reactivity', {
}
}
}),
Identifier(node) {
const enclosingReturn = findEnclosingReturn(node);
if (enclosingReturn === null) {
return;
}
const enclosingFunction = findEnclosingFunction(enclosingReturn);
if (enclosingFunction === null) {
return;
}
const variable = findVariable(context, node);
if (
variable === null ||
variable.identifiers.length < 1 ||
variable.identifiers[0].parent.type !== 'VariableDeclarator'
) {
return;
}
if (!returnedVariables.has(enclosingFunction)) {
returnedVariables.set(enclosingFunction, []);
}
returnedVariables.get(enclosingFunction)?.push(variable.identifiers[0].parent);
},
'Program:exit'() {
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!);
for (const { node, path } of referenceTracker.iterateGlobalReferences({
Expand Down Expand Up @@ -96,6 +122,20 @@ export default createRule('prefer-svelte-reactivity', {
});
}
}
for (const returnedVar of Array.from(returnedVariables.values()).flat()) {
if (isIn(node, returnedVar)) {
context.report({
messageId,
node
});
}
}
if (findEnclosingReturn(node) !== null) {
context.report({
messageId,
node
});
}
if (path[0] === 'Date' && isDateMutable(referenceTracker, node as TSESTree.Expression)) {
context.report({
messageId: 'mutableDateUsed',
Expand Down Expand Up @@ -135,6 +175,33 @@ export default createRule('prefer-svelte-reactivity', {
}
});

function findAncestorOfTypes<T extends string>(
node: TSESTree.Node,
types: string[]
): (TSESTree.Node & { type: T }) | null {
if (types.includes(node.type)) {
return node as TSESTree.Node & { type: T };
}
if (node.parent === undefined || node.parent === null) {
return null;
}
return findAncestorOfTypes(node.parent, types);
}

function findEnclosingReturn(node: TSESTree.Node): TSESTree.ReturnStatement | null {
return findAncestorOfTypes(node, ['ReturnStatement']);
}

function findEnclosingFunction(
node: TSESTree.Node
): TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | null {
return findAncestorOfTypes(node, [
'ArrowFunctionExpression',
'FunctionDeclaration',
'MethodDefinition'
]);
}

function isDateMutable(referenceTracker: ReferenceTracker, ctorNode: TSESTree.Expression): boolean {
return !referenceTracker
.iteratePropertyReferences(ctorNode, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 3
column: 20
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
const fn = () => {
const variable = new Set([1, 2, 1, 3, 3]);
return variable;
}
</script>

{fn().has(42)}
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. This is actually fine as is, right? (It shouldn't be reported, right?)
Could you change it to track the value the function returns, check if there's a mutation, and only report that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been thinking about this more and you are right. I think this needs a change of approach. I will probably make an alternative PR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 3
column: 10
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const fn = () => {
return new Set([1, 2, 1, 3, 3]);
}
</script>

{fn().has(42)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 3
column: 20
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
function fn() {
const variable = new Set([1, 2, 1, 3, 3]);
return variable;
}
</script>

{fn().has(42)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 3
column: 10
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
function fn() {
return new Set([1, 2, 1, 3, 3]);
}
</script>

{fn().has(42)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 4
column: 21
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
class A {
fn() {
const variable = new Set([1, 2, 1, 3, 3]);
return variable;
}
}

const a = new A();
</script>

{a.fn().has(42)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found a mutable instance of the built-in Set class. Use SvelteSet instead.
line: 4
column: 11
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
class A {
fn() {
return new Set([1, 2, 1, 3, 3]);
}
}

const a = new A();
</script>

{a.fn().has(42)}
Loading