-
-
Notifications
You must be signed in to change notification settings - Fork 60
Add svelte/no-reactive-literals rule
#203
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # (svelte/no-reactive-literals) | ||
|
|
||
| > Don't assign literal values in reactive statements | ||
|
|
||
| ## :book: Rule Details | ||
|
|
||
| This rule reports on any assignment of a static, unchanging value within a reactive statement because it's not necessary. | ||
|
|
||
| <ESLintCodeBlock> | ||
|
|
||
| <!--eslint-skip--> | ||
|
|
||
| ```svelte | ||
| <script> | ||
| /* eslint svelte/no-reactive-literals: "error" */ | ||
| /* ✓ GOOD */ | ||
| let foo = "bar"; | ||
|
|
||
| /* ✗ BAD */ | ||
| $: foo = "bar"; | ||
| </script> | ||
| ``` | ||
| </ESLintCodeBlock> | ||
|
|
||
| ## :wrench: Options | ||
|
|
||
| Nothing | ||
|
|
||
| ## :mag: Implementation | ||
|
|
||
| - [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/no-reactive-literals.ts) | ||
| - [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/no-reactive-literals.ts) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { TSESTree } from "@typescript-eslint/types" | ||
| import { createRule } from "../utils" | ||
|
|
||
| export default createRule("no-reactive-literals", { | ||
| meta: { | ||
| docs: { | ||
| description: "Don't assign literal values in reactive statements", | ||
| category: "Best Practices", | ||
| recommended: false, | ||
| }, | ||
| hasSuggestions: true, | ||
| schema: [], | ||
| messages: { | ||
| noReactiveLiterals: `Do not assign literal values inside reactive statements unless absolutely necessary.`, | ||
| fixReactiveLiteral: `Move the literal out of the reactive statement into an assignment`, | ||
| }, | ||
| type: "suggestion", | ||
| }, | ||
| create(context) { | ||
| return { | ||
| [`SvelteReactiveStatement > ExpressionStatement > AssignmentExpression${[ | ||
| // $: foo = "foo"; | ||
| // $: foo = 1; | ||
| `[right.type="Literal"]`, | ||
|
|
||
| // $: foo = []; | ||
| `[right.type="ArrayExpression"][right.elements.length=0]`, | ||
|
|
||
| // $: foo = {}; | ||
| `[right.type="ObjectExpression"][right.properties.length=0]`, | ||
| ].join(",")}`](node: TSESTree.AssignmentExpression) { | ||
| // Move upwards to include the entire reactive statement | ||
| const parent = node.parent?.parent | ||
|
|
||
| if (!parent) { | ||
| return false | ||
| } | ||
|
|
||
| const source = context.getSourceCode() | ||
|
|
||
| return context.report({ | ||
| node: parent, | ||
| loc: parent.loc, | ||
| messageId: "noReactiveLiterals", | ||
| suggest: [ | ||
| { | ||
| messageId: "fixReactiveLiteral", | ||
| fix(fixer) { | ||
| return [ | ||
| // Insert "let" + whatever was in there | ||
| fixer.insertTextBefore(parent, `let ${source.getText(node)}`), | ||
|
|
||
| // Remove the original reactive statement | ||
| fixer.remove(parent), | ||
| ] | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
| }, | ||
| } | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/fixtures/rules/no-reactive-literals/invalid/test01-errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| [ | ||
| { | ||
| "message": "Do not assign literal values inside reactive statements unless absolutely necessary.", | ||
| "line": 3, | ||
| "column": 5, | ||
| "suggestions": [ | ||
| { | ||
| "desc": "Move the literal out of the reactive statement into an assignment", | ||
| "messageId": "fixReactiveLiteral", | ||
| "output": "<!-- prettier-ignore -->\n<script>\n let foo = \"foo\"\n $: bar = [];\n $: baz = {};\n</script>\n" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "message": "Do not assign literal values inside reactive statements unless absolutely necessary.", | ||
| "line": 4, | ||
| "column": 5, | ||
| "suggestions": [ | ||
| { | ||
| "desc": "Move the literal out of the reactive statement into an assignment", | ||
| "messageId": "fixReactiveLiteral", | ||
| "output": "<!-- prettier-ignore -->\n<script>\n $: foo = \"foo\";\n let bar = []\n $: baz = {};\n</script>\n" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "message": "Do not assign literal values inside reactive statements unless absolutely necessary.", | ||
| "line": 5, | ||
| "column": 5, | ||
| "suggestions": [ | ||
| { | ||
| "desc": "Move the literal out of the reactive statement into an assignment", | ||
| "messageId": "fixReactiveLiteral", | ||
| "output": "<!-- prettier-ignore -->\n<script>\n $: foo = \"foo\";\n $: bar = [];\n let baz = {}\n</script>\n" | ||
| } | ||
| ] | ||
| } | ||
| ] |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/no-reactive-literals/invalid/test01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <!-- prettier-ignore --> | ||
| <script> | ||
| $: foo = "foo"; | ||
| $: bar = []; | ||
| $: baz = {}; | ||
| </script> |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/no-reactive-literals/valid/test01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <!-- prettier-ignore --> | ||
| <script> | ||
| $: foo = `${"bar"}baz` | ||
| $: bar = [ "bar" ] | ||
| $: baz = { qux : true } | ||
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { RuleTester } from "eslint" | ||
| import rule from "../../../src/rules/no-reactive-literals" | ||
| import { loadTestCases } from "../../utils/utils" | ||
|
|
||
| const tester = new RuleTester({ | ||
| parserOptions: { | ||
| ecmaVersion: 2020, | ||
| sourceType: "module", | ||
| }, | ||
| }) | ||
|
|
||
| tester.run( | ||
| "no-reactive-literals", | ||
| rule as any, | ||
| loadTestCases("no-reactive-literals"), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.