From 8e196a7a42aebca27027630b888166b2f09cc4f3 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 07:46:26 +0900 Subject: [PATCH 1/6] fix(prefer-const): allow ESLint core rule's properties --- .changeset/good-carrots-notice.md | 5 +++++ packages/eslint-plugin-svelte/src/rule-types.ts | 3 +-- packages/eslint-plugin-svelte/src/rules/prefer-const.ts | 5 ++--- .../fixtures/rules/prefer-const/valid/1238/_config.json | 8 ++++++++ .../fixtures/rules/prefer-const/valid/1238/input.svelte | 1 + 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 .changeset/good-carrots-notice.md create mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json create mode 100644 packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/input.svelte diff --git a/.changeset/good-carrots-notice.md b/.changeset/good-carrots-notice.md new file mode 100644 index 000000000..a5e498b4a --- /dev/null +++ b/.changeset/good-carrots-notice.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-svelte': patch +--- + +fix(prefer-const): allow ESLint core rule's properties diff --git a/packages/eslint-plugin-svelte/src/rule-types.ts b/packages/eslint-plugin-svelte/src/rule-types.ts index 2fd9c7db2..647fa7f2e 100644 --- a/packages/eslint-plugin-svelte/src/rule-types.ts +++ b/packages/eslint-plugin-svelte/src/rule-types.ts @@ -570,9 +570,8 @@ type SveltePreferClassDirective = []|[{ }] // ----- svelte/prefer-const ----- type SveltePreferConst = []|[{ - destructuring?: ("any" | "all") - ignoreReadBeforeAssign?: boolean excludedRunes?: string[] + [k: string]: unknown | undefined }] // ----- svelte/require-event-prefix ----- type SvelteRequireEventPrefix = []|[{ diff --git a/packages/eslint-plugin-svelte/src/rules/prefer-const.ts b/packages/eslint-plugin-svelte/src/rules/prefer-const.ts index 842bbc924..9e435ba33 100644 --- a/packages/eslint-plugin-svelte/src/rules/prefer-const.ts +++ b/packages/eslint-plugin-svelte/src/rules/prefer-const.ts @@ -59,8 +59,6 @@ export default createRule('prefer-const', { { type: 'object', properties: { - destructuring: { enum: ['any', 'all'] }, - ignoreReadBeforeAssign: { type: 'boolean' }, excludedRunes: { type: 'array', items: { @@ -68,7 +66,8 @@ export default createRule('prefer-const', { } } }, - additionalProperties: false + // allow ESLint core rule's properties + additionalProperties: true } ] }, diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json new file mode 100644 index 000000000..d2cb041d5 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json @@ -0,0 +1,8 @@ +{ + "options": [ + { + "destructuring": "all", + "ignoreReadonly": true + } + ] +} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/input.svelte new file mode 100644 index 000000000..190a18037 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/input.svelte @@ -0,0 +1 @@ +123 From 65808ba4736004fc03a023c751b62f8a226b9721 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 07:57:12 +0900 Subject: [PATCH 2/6] update docs --- docs/rules/prefer-const.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/rules/prefer-const.md b/docs/rules/prefer-const.md index 8029ea881..9a1282c7d 100644 --- a/docs/rules/prefer-const.md +++ b/docs/rules/prefer-const.md @@ -54,14 +54,14 @@ This rule reports the same as the base ESLint `prefer-const` rule, except that i ``` - `destructuring`: The kind of the way to address variables in destructuring. There are 2 values: - - `any` (default): if any variables in destructuring should be const, this rule warns for those variables. - - `all`: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them. -- `ignoreReadonly`: If `true`, this rule will ignore variables that are read between the declaration and the _first_ assignment. + - `any` (default) - If any variables in destructuring should be const, this rule warns for those variables. + - `all`: If all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them. +- `ignoreReadBeforeAssign`: This is an option to avoid conflicting with `no-use-before-define` rule (without "nofunc" option). If `true` is specified, this rule will ignore variables that are read between the declaration and the first assignment. Default is `false`. - `excludedRunes`: An array of rune names that should be ignored. Even if a rune is declared with `let`, it will still be ignored. ## :books: Further Reading -- See [ESLint `prefer-const` rule](https://eslint.org/docs/latest/rules/prefer-const) for more information about the base rule. +- See [ESLint prefer-const rule](https://eslint.org/docs/latest/rules/prefer-const) for more information about the base rule. ## :rocket: Version From 08c2fdd69180cff8806d8b92021eab53eed185aa Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 08:02:02 +0900 Subject: [PATCH 3/6] improve --- packages/eslint-plugin-svelte/src/rule-types.ts | 2 ++ packages/eslint-plugin-svelte/src/rules/prefer-const.ts | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin-svelte/src/rule-types.ts b/packages/eslint-plugin-svelte/src/rule-types.ts index 647fa7f2e..81657270d 100644 --- a/packages/eslint-plugin-svelte/src/rule-types.ts +++ b/packages/eslint-plugin-svelte/src/rule-types.ts @@ -570,6 +570,8 @@ type SveltePreferClassDirective = []|[{ }] // ----- svelte/prefer-const ----- type SveltePreferConst = []|[{ + destructuring?: ("any" | "all") + ignoreReadBeforeAssign?: boolean excludedRunes?: string[] [k: string]: unknown | undefined }] diff --git a/packages/eslint-plugin-svelte/src/rules/prefer-const.ts b/packages/eslint-plugin-svelte/src/rules/prefer-const.ts index 9e435ba33..e28765baa 100644 --- a/packages/eslint-plugin-svelte/src/rules/prefer-const.ts +++ b/packages/eslint-plugin-svelte/src/rules/prefer-const.ts @@ -59,6 +59,8 @@ export default createRule('prefer-const', { { type: 'object', properties: { + destructuring: { enum: ['any', 'all'] }, + ignoreReadBeforeAssign: { type: 'boolean' }, excludedRunes: { type: 'array', items: { @@ -66,7 +68,7 @@ export default createRule('prefer-const', { } } }, - // allow ESLint core rule's properties + // Allow ESLint core rule properties in case new options are added in the future. additionalProperties: true } ] From f9601488be51c97f1e2781a6b897ad09eb25918a Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 08:03:01 +0900 Subject: [PATCH 4/6] fix --- docs/rules/prefer-const.md | 2 +- .../tests/fixtures/rules/prefer-const/valid/1238/_config.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/rules/prefer-const.md b/docs/rules/prefer-const.md index 9a1282c7d..9ba918cce 100644 --- a/docs/rules/prefer-const.md +++ b/docs/rules/prefer-const.md @@ -46,7 +46,7 @@ This rule reports the same as the base ESLint `prefer-const` rule, except that i "error", { "destructuring": "any", - "ignoreReadonly": true, + "additionalProperties": true, "excludedRunes": ["$props", "$derived"] } ] diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json index d2cb041d5..88200a87e 100644 --- a/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/prefer-const/valid/1238/_config.json @@ -2,7 +2,7 @@ "options": [ { "destructuring": "all", - "ignoreReadonly": true + "additionalProperties": true } ] } From 2d49a6e564579958e2b14bb8a1cc2a97dc4883a9 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 08:03:25 +0900 Subject: [PATCH 5/6] fix --- docs/rules/prefer-const.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/prefer-const.md b/docs/rules/prefer-const.md index 9ba918cce..f158d47b6 100644 --- a/docs/rules/prefer-const.md +++ b/docs/rules/prefer-const.md @@ -46,7 +46,7 @@ This rule reports the same as the base ESLint `prefer-const` rule, except that i "error", { "destructuring": "any", - "additionalProperties": true, + "additionalProperties": false, "excludedRunes": ["$props", "$derived"] } ] From 389946e6be5bb11f31323774267f669f856e2d08 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 2 Jun 2025 08:05:36 +0900 Subject: [PATCH 6/6] update changeset --- .changeset/good-carrots-notice.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/good-carrots-notice.md b/.changeset/good-carrots-notice.md index a5e498b4a..6a7f03087 100644 --- a/.changeset/good-carrots-notice.md +++ b/.changeset/good-carrots-notice.md @@ -2,4 +2,4 @@ 'eslint-plugin-svelte': patch --- -fix(prefer-const): allow ESLint core rule's properties +fix(prefer-const): Use `additionalProperties` instead of `ignoreReadonly` to match the ESLint core rule option name.