Repository: sveltejs/eslint-plugin-svelte
Pull request #1140 (2025-03-18 00:20:34)

fix(no-useless-mustaches): Wrong auto-fix for quoted mustaches
fixed #1139

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: 94068f848d3f1d949c35efcff56a89764af64fe5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/sveltejs/eslint-plugin-svelte/new/fix-no-useless-mustaches?filename=.changeset/wise-fireants-obey.md&value=---%0A%22eslint-plugin-svelte%22%3A%20patch%0A---%0A%0Afix(no-useless-mustaches)%3A%20Wrong%20auto-fix%20for%20quoted%20mustaches%0A)


github-actions[bot]: <!-- posted by pkg.pr.new-comment.mjs -->

## Try the Instant Preview in Online Playground

[ESLint Online Playground](https://eslint-online-playground.netlify.app/?overrideDeps=%7B%22eslint-plugin-svelte%22%3A%22https%3A%2F%2Fpkg.pr.new%2Feslint-plugin-svelte%401140%22%7D#eslint-plugin-svelte%20with%20typescript)

## Install the Instant Preview to Your Local

```
npm i https://pkg.pr.new/eslint-plugin-svelte@1140
```

## Published Instant Preview Packages:

- eslint-plugin-svelte: https://pkg.pr.new/eslint-plugin-svelte@1140

[View Commit](https://github.com/sveltejs/eslint-plugin-svelte/commit/94068f848d3f1d949c35efcff56a89764af64fe5)

Patch:
diff --git a/.changeset/wise-fireants-obey.md b/.changeset/wise-fireants-obey.md
new file mode 100644
index 0000000..8f37980
--- /dev/null
+++ b/.changeset/wise-fireants-obey.md
@@ -0,0 +1,5 @@
+---
+"eslint-plugin-svelte": patch
+---
+
+fix(no-useless-mustaches): Wrong auto-fix for quoted mustaches
diff --git a/packages/eslint-plugin-svelte/src/rules/no-useless-mustaches.ts b/packages/eslint-plugin-svelte/src/rules/no-useless-mustaches.ts
index 05d3af2..d8da0cc 100644
--- a/packages/eslint-plugin-svelte/src/rules/no-useless-mustaches.ts
+++ b/packages/eslint-plugin-svelte/src/rules/no-useless-mustaches.ts
@@ -142,14 +142,21 @@ export default createRule('no-useless-mustaches', {
 							node.parent.key.range[1],
 							node.parent.value[0].range[0]
 						);
-						if (!div.endsWith('"') && !div.endsWith("'")) {
+						const quote = div.endsWith('"') ? 'quot' : div.endsWith("'") ? 'apos' : null;
+						if (!quote) {
 							return [
 								fixer.insertTextBefore(node.parent.value[0], '"'),
 								fixer.replaceText(node, unescaped.replace(/"/gu, '&quot;')),
 								fixer.insertTextAfter(node.parent.value[node.parent.value.length - 1], '"')
 							];
 						}
-						return fixer.replaceText(node, unescaped);
+
+						return fixer.replaceText(
+							node,
+							quote === 'quot'
+								? unescaped.replace(/"/gu, '&quot;')
+								: unescaped.replace(/'/gu, '&apos;')
+						);
 					}
 					return fixer.replaceText(node, unescaped.replace(/</gu, '&lt;').replace(/>/gu, '&gt;'));
 				}


Test patch:
diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-errors.yaml b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-errors.yaml
new file mode 100644
index 0000000..ee6ce5e
--- /dev/null
+++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-errors.yaml
@@ -0,0 +1,16 @@
+- message: Unexpected mustache interpolation with a string literal value.
+  line: 2
+  column: 17
+  suggestions: null
+- message: Unexpected mustache interpolation with a string literal value.
+  line: 4
+  column: 17
+  suggestions: null
+- message: Unexpected mustache interpolation with a string literal value.
+  line: 6
+  column: 17
+  suggestions: null
+- message: Unexpected mustache interpolation with a string literal value.
+  line: 8
+  column: 17
+  suggestions: null
diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-input.svelte
new file mode 100644
index 0000000..43dbb27
--- /dev/null
+++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-input.svelte
@@ -0,0 +1,8 @@
+<!--prettier-ignore-->
+<div data-text="{'\'"'}" />
+<!--prettier-ignore-->
+<div data-text="{"'\""}" />
+<!--prettier-ignore-->
+<div data-text='{'\'"'}' />
+<!--prettier-ignore-->
+<div data-text='{"'\""}' />
diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-output.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-output.svelte
new file mode 100644
index 0000000..fd9b147
--- /dev/null
+++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/no-useless-mustaches/invalid/quote-test02-output.svelte
@@ -0,0 +1,8 @@
+<!--prettier-ignore-->
+<div data-text="'&quot;" />
+<!--prettier-ignore-->
+<div data-text="'&quot;" />
+<!--prettier-ignore-->
+<div data-text='&apos;"' />
+<!--prettier-ignore-->
+<div data-text='&apos;"' />