Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import type { RuleContext } from '../types.js';
import { extractStoreReferences } from './reference-helpers/svelte-store.js';
import { getSourceCode } from '../utils/compat.js';

export default createRule('derived-has-same-inputs-outputs', {
meta: {
Expand All @@ -11,6 +12,7 @@ export default createRule('derived-has-same-inputs-outputs', {
recommended: false,
conflictWithPrettier: false
},
fixable: 'code',
schema: [],
messages: {
unexpected: "The argument name should be '{{name}}'."
Expand Down Expand Up @@ -53,7 +55,20 @@ export default createRule('derived-has-same-inputs-outputs', {
node: fn,
loc: fnParam.loc,
messageId: 'unexpected',
data: { name: expectedName }
data: { name: expectedName },
fix: (fixer) => {
const scope = getSourceCode(context).getScope(fn.body);
const variable = scope.variables.find((variable) => variable.name === fnParam.name);

if (!variable) {
return fixer.replaceText(fnParam, expectedName);
}

return [
fixer.replaceText(fnParam, expectedName),
...variable.references.map((ref) => fixer.replaceText(ref.identifier, expectedName))
];
}
});
}
}
Expand Down Expand Up @@ -81,7 +96,19 @@ export default createRule('derived-has-same-inputs-outputs', {
node: fn,
loc: element.loc,
messageId: 'unexpected',
data: { name: expectedName }
data: { name: expectedName },
*fix(fixer) {
const scope = getSourceCode(context).getScope(fn.body);
const variable = scope.variables.find((variable) => variable.name === element.name);

yield fixer.replaceText(element, expectedName);

if (variable) {
for (const ref of variable.references) {
yield fixer.replaceText(ref.identifier, expectedName);
}
}
}
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin-svelte/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,6 @@ export interface SourceCode {
getCommentsAfter(nodeOrToken: NodeOrToken | AST.Token): AST.Comment[];

getCommentsInside(node: NodeOrToken): AST.Comment[];

getScope(node: NodeOrToken): Scope;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,27 @@
line: 18
column: 22
suggestions: null
- message: The argument name should be '$a'.
line: 21
column: 13
suggestions: null
- message: The argument name should be '$a'.
line: 28
column: 13
suggestions: null
- message: The argument name should be '$e'.
line: 37
column: 19
suggestions: null
- message: The argument name should be '$f'.
line: 37
column: 22
suggestions: null
- message: The argument name should be '$e'.
line: 46
column: 19
suggestions: null
- message: The argument name should be '$f'.
line: 46
column: 22
suggestions: null
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,39 @@ derived([null, l], ([$m, $n]) => {
derived([o, null], ([$p, $q]) => {
/** do nothing */
});
derived(a, (b) => {
doSomethingWith(b);

somethingWithACallback(() => {
b;
});
});
derived(a, (b) => {
b;

somethingWithACallback(() => {
// purposely shadow the var, this should not be updated
const b = 303;
b;
});
});
derived([e, f], ([g, h]) => {
g;
h;

somethingWithACallback(() => {
g;
h;
});
});
derived([e, f], ([g, h]) => {
g;
h;

somethingWithACallback(() => {
const g = 303;
const h = 808;
g;
h;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { derived } from 'svelte/store';

derived(a, ($a) => {
/** do nothing */
});
derived(c, ($c, set) => {
/** do nothing */
});
derived([e, f], ([$e, $f]) => {
/** do nothing */
});
derived([i, j], ([$i, $j], set) => {
/** do nothing */
});
derived([null, l], ([$m, $l]) => {
/** do nothing */
});
derived([o, null], ([$o, $q]) => {
/** do nothing */
});
derived(a, ($a) => {
doSomethingWith($a);

somethingWithACallback(() => {
$a;
});
});
derived(a, ($a) => {
$a;

somethingWithACallback(() => {
// purposely shadow the var, this should not be updated
const b = 303;
b;
});
});
derived([e, f], ([$e, h]) => {
$e;
h;

somethingWithACallback(() => {
$e;
h;
});
});
derived([e, f], ([$e, h]) => {
$e;
h;

somethingWithACallback(() => {
const g = 303;
const h = 808;
g;
h;
});
});
Loading