eslint-plugin-svelte 3.20.0 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -296,6 +296,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
296
296
  | Rule ID | Description | |
297
297
  |:--------|:------------|:---|
298
298
  | [svelte/infinite-reactive-loop](https://sveltejs.github.io/eslint-plugin-svelte/rules/infinite-reactive-loop/) | Svelte runtime prevents calling the same reactive statement twice in a microtask. But between different microtask, it doesn't prevent. | :star: |
299
+ | [svelte/no-bind-value-on-checkable-inputs](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-bind-value-on-checkable-inputs/) | disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">` | :bulb: |
299
300
  | [svelte/no-dom-manipulating](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dom-manipulating/) | disallow DOM manipulating | :star: |
300
301
  | [svelte/no-dupe-else-if-blocks](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-else-if-blocks/) | disallow duplicate conditions in `{#if}` / `{:else if}` chains | :star: |
301
302
  | [svelte/no-dupe-on-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-on-directives/) | disallow duplicate `on:` directives | :star: |
package/lib/main.d.ts CHANGED
@@ -14,7 +14,7 @@ export declare const configs: {
14
14
  export declare const rules: Record<string, Rule.RuleModule>;
15
15
  export declare const meta: {
16
16
  name: "eslint-plugin-svelte";
17
- version: "3.20.0";
17
+ version: "3.21.0";
18
18
  };
19
19
  export declare const processors: {
20
20
  '.svelte': typeof processor;
package/lib/meta.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare const name: "eslint-plugin-svelte";
2
- export declare const version: "3.20.0";
2
+ export declare const version: "3.21.0";
package/lib/meta.js CHANGED
@@ -2,4 +2,4 @@
2
2
  // This file has been automatically generated,
3
3
  // in order to update its content execute "pnpm run update"
4
4
  export const name = 'eslint-plugin-svelte';
5
- export const version = '3.20.0';
5
+ export const version = '3.21.0';
@@ -115,6 +115,11 @@ export interface RuleOptions {
115
115
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-at-html-tags/
116
116
  */
117
117
  'svelte/no-at-html-tags'?: Linter.RuleEntry<[]>;
118
+ /**
119
+ * disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">`
120
+ * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-bind-value-on-checkable-inputs/
121
+ */
122
+ 'svelte/no-bind-value-on-checkable-inputs'?: Linter.RuleEntry<[]>;
118
123
  /**
119
124
  * disallow DOM manipulating
120
125
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dom-manipulating/
@@ -0,0 +1,2 @@
1
+ declare const _default: import("src/types.js").RuleModule;
2
+ export default _default;
@@ -0,0 +1,73 @@
1
+ import { getStaticValue } from '@eslint-community/eslint-utils';
2
+ import { getScope } from '../utils/ast-utils.js';
3
+ import { createRule } from '../utils/index.js';
4
+ export default createRule('no-bind-value-on-checkable-inputs', {
5
+ meta: {
6
+ hasSuggestions: true,
7
+ docs: {
8
+ description: 'disallow useless `bind:value` on `<input type="checkbox">` and `<input type="radio">`',
9
+ category: 'Possible Errors',
10
+ recommended: false
11
+ },
12
+ schema: [],
13
+ type: 'problem',
14
+ messages: {
15
+ checkboxValueBinding: '`bind:value` does not work on checkbox inputs. Did you mean `bind:checked` or `bind:group`?',
16
+ radioValueBinding: '`bind:value` does not work on radio inputs. Did you mean `bind:group`?', // svelte compiler disallows `bind:checked` on radios
17
+ checkedSuggestion: 'Change `bind:value` to `bind:checked`.',
18
+ groupSuggestion: 'Change `bind:value` to `bind:group`.'
19
+ }
20
+ },
21
+ create(context) {
22
+ return {
23
+ "SvelteElement[name.name='input']"(node) {
24
+ function getType() {
25
+ const typeAttr = node.startTag?.attributes.find((attr) => attr.type === 'SvelteAttribute' && attr.key.name.toLowerCase() === 'type');
26
+ if (!typeAttr)
27
+ return null;
28
+ if (typeAttr.value.length !== 1)
29
+ return null;
30
+ const typeValue = typeAttr.value[0];
31
+ if (typeValue.type === 'SvelteLiteral') {
32
+ // <input type="checkbox" />
33
+ return typeValue.value.toLowerCase();
34
+ }
35
+ if (typeValue.type === 'SvelteMustacheTag') {
36
+ const staticValue = getStaticValue(typeValue.expression, getScope(context, typeValue.expression));
37
+ if (typeof staticValue?.value !== 'string')
38
+ return null;
39
+ // <input type={"checkbox"} />
40
+ return staticValue.value.toLowerCase();
41
+ }
42
+ return null;
43
+ }
44
+ const type = getType();
45
+ if (!type || (type !== 'checkbox' && type !== 'radio'))
46
+ return;
47
+ const bindValue = node.startTag?.attributes.find((attr) => attr.type === 'SvelteDirective' &&
48
+ attr.kind === 'Binding' &&
49
+ attr.key.name.name === 'value');
50
+ if (!bindValue)
51
+ return;
52
+ function suggestion(suggestionType, bindValue // make typescript happy
53
+ ) {
54
+ return {
55
+ messageId: `${suggestionType}Suggestion`,
56
+ fix(fixer) {
57
+ if (bindValue.shorthand)
58
+ return fixer.replaceText(bindValue, `bind:${suggestionType}={value}`);
59
+ return fixer.replaceText(bindValue.key, `bind:${suggestionType}`);
60
+ }
61
+ };
62
+ }
63
+ context.report({
64
+ node: bindValue,
65
+ messageId: `${type}ValueBinding`,
66
+ suggest: type === 'checkbox'
67
+ ? [suggestion('checked', bindValue), suggestion('group', bindValue)]
68
+ : [suggestion('group', bindValue)]
69
+ });
70
+ }
71
+ };
72
+ }
73
+ });
@@ -20,6 +20,7 @@ import noAddEventListener from '../rules/no-add-event-listener.js';
20
20
  import noAtConstTags from '../rules/no-at-const-tags.js';
21
21
  import noAtDebugTags from '../rules/no-at-debug-tags.js';
22
22
  import noAtHtmlTags from '../rules/no-at-html-tags.js';
23
+ import noBindValueOnCheckableInputs from '../rules/no-bind-value-on-checkable-inputs.js';
23
24
  import noDomManipulating from '../rules/no-dom-manipulating.js';
24
25
  import noDupeElseIfBlocks from '../rules/no-dupe-else-if-blocks.js';
25
26
  import noDupeOnDirectives from '../rules/no-dupe-on-directives.js';
@@ -104,6 +105,7 @@ export const rules = [
104
105
  noAtConstTags,
105
106
  noAtDebugTags,
106
107
  noAtHtmlTags,
108
+ noBindValueOnCheckableInputs,
107
109
  noDomManipulating,
108
110
  noDupeElseIfBlocks,
109
111
  noDupeOnDirectives,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-svelte",
3
- "version": "3.20.0",
3
+ "version": "3.21.0",
4
4
  "description": "ESLint plugin for Svelte using AST",
5
5
  "repository": {
6
6
  "type": "git",