diff --git a/src/rules/shorthand-attribute.ts b/src/rules/shorthand-attribute.ts
index 923df3da6..39d5e7a68 100644
--- a/src/rules/shorthand-attribute.ts
+++ b/src/rules/shorthand-attribute.ts
@@ -1,4 +1,5 @@
import { createRule } from "../utils"
+import { getAttributeValueQuoteAndRange } from "../utils/ast-utils"
export default createRule("shorthand-attribute", {
meta: {
@@ -45,8 +46,28 @@ export default createRule("shorthand-attribute", {
node,
messageId: "expectedShorthand",
*fix(fixer) {
- yield fixer.remove(node.key)
- yield fixer.remove(sourceCode.getTokenAfter(node.key)!)
+ const quoteAndRange = getAttributeValueQuoteAndRange(
+ node,
+ sourceCode,
+ )
+ if (!quoteAndRange) {
+ return
+ }
+ if (
+ quoteAndRange.quote === "double" ||
+ quoteAndRange.quote === "single"
+ ) {
+ yield fixer.removeRange([
+ node.range[0],
+ quoteAndRange.firstToken.range[1],
+ ])
+ yield fixer.remove(quoteAndRange.lastToken)
+ } else {
+ yield fixer.removeRange([
+ node.range[0],
+ quoteAndRange.range[0],
+ ])
+ }
},
})
}
diff --git a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-errors.json b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-errors.json
index 6eb70b9aa..613ffb32e 100644
--- a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-errors.json
+++ b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-errors.json
@@ -8,5 +8,15 @@
"message": "Expected shorthand attribute.",
"line": 10,
"column": 9
+ },
+ {
+ "message": "Expected shorthand attribute.",
+ "line": 13,
+ "column": 9
+ },
+ {
+ "message": "Expected shorthand attribute.",
+ "line": 15,
+ "column": 9
}
]
diff --git a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-input.svelte b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-input.svelte
index 9dcc08e8c..8c64e8edb 100644
--- a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-input.svelte
+++ b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-input.svelte
@@ -9,3 +9,9 @@
+
+
+
+
+
+
diff --git a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-output.svelte b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-output.svelte
index dd935fa12..19ce64e48 100644
--- a/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-output.svelte
+++ b/tests/fixtures/rules/shorthand-attribute/invalid/always/test01-output.svelte
@@ -7,5 +7,11 @@
-
+
+
+
+
+
+
+
diff --git a/tests/fixtures/rules/shorthand-attribute/invalid/test01-output.svelte b/tests/fixtures/rules/shorthand-attribute/invalid/test01-output.svelte
index dd935fa12..b0a0783b6 100644
--- a/tests/fixtures/rules/shorthand-attribute/invalid/test01-output.svelte
+++ b/tests/fixtures/rules/shorthand-attribute/invalid/test01-output.svelte
@@ -7,5 +7,5 @@
-
+