|
| 1 | +import type { TSESTree } from "@typescript-eslint/types" |
| 2 | +import { createRule } from "../utils" |
| 3 | + |
| 4 | +export default createRule("no-extra-reactive-curlies", { |
| 5 | + meta: { |
| 6 | + docs: { |
| 7 | + description: |
| 8 | + "disallow wrapping single reactive statements in curly braces", |
| 9 | + category: "Stylistic Issues", |
| 10 | + recommended: false, |
| 11 | + conflictWithPrettier: false, |
| 12 | + }, |
| 13 | + hasSuggestions: true, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + extraCurlies: `Do not wrap reactive statements in curly braces unless necessary.`, |
| 17 | + removeExtraCurlies: `Remove the unnecessary curly braces.`, |
| 18 | + }, |
| 19 | + type: "suggestion", |
| 20 | + }, |
| 21 | + create(context) { |
| 22 | + return { |
| 23 | + // $: { foo = "bar"; } |
| 24 | + [`SvelteReactiveStatement > BlockStatement[body.length=1]`]: ( |
| 25 | + node: TSESTree.BlockStatement, |
| 26 | + ) => { |
| 27 | + const source = context.getSourceCode() |
| 28 | + |
| 29 | + return context.report({ |
| 30 | + node, |
| 31 | + loc: node.loc, |
| 32 | + messageId: "extraCurlies", |
| 33 | + suggest: [ |
| 34 | + { |
| 35 | + messageId: "removeExtraCurlies", |
| 36 | + fix(fixer) { |
| 37 | + const tokens = source.getTokens(node, { includeComments: true }) |
| 38 | + |
| 39 | + // Remove everything up to the second token, and the entire last token since |
| 40 | + // those are known to be "{" and "}" |
| 41 | + return [ |
| 42 | + fixer.removeRange([tokens[0].range[0], tokens[1].range[0]]), |
| 43 | + |
| 44 | + fixer.removeRange([ |
| 45 | + tokens[tokens.length - 2].range[1], |
| 46 | + tokens[tokens.length - 1].range[1], |
| 47 | + ]), |
| 48 | + ] |
| 49 | + }, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }) |
| 53 | + }, |
| 54 | + } |
| 55 | + }, |
| 56 | +}) |
0 commit comments