forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-reactive-functions.ts
56 lines (51 loc) · 1.53 KB
/
no-reactive-functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { TSESTree } from "@typescript-eslint/types"
import { createRule } from "../utils"
export default createRule("no-reactive-functions", {
meta: {
docs: {
description: "",
category: "Best Practices",
recommended: false,
},
hasSuggestions: true,
schema: [],
messages: {
noReactiveFns: `Do not create functions inside reactive statements unless absolutely necessary.`,
fixReactiveFns: `Move the function out of the reactive statement`,
},
type: "suggestion", // "problem", or "layout",
},
create(context) {
return {
// $: foo = () => { ... }
[`SvelteReactiveStatement > ExpressionStatement > AssignmentExpression > :function`](
node: TSESTree.ArrowFunctionExpression,
) {
// Move upwards to include the entire label
const parent = node.parent?.parent?.parent
if (!parent) {
return false
}
const source = context.getSourceCode()
return context.report({
node: parent,
loc: parent.loc,
messageId: "noReactiveFns",
suggest: [
{
messageId: "fixReactiveFns",
fix(fixer) {
const tokens = source.getTokens(parent)
// Replace the entire reactive label with "const"
return fixer.replaceTextRange(
[tokens[0].range[0], tokens[1].range[1]],
"const",
)
},
},
],
})
},
}
},
})