Skip to content

Commit 9d922f3

Browse files
authored
Fix wrong autofix in shorthand-attribute rule (#100)
1 parent 9c4e207 commit 9d922f3

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

src/rules/shorthand-attribute.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRule } from "../utils"
2+
import { getAttributeValueQuoteAndRange } from "../utils/ast-utils"
23

34
export default createRule("shorthand-attribute", {
45
meta: {
@@ -45,8 +46,28 @@ export default createRule("shorthand-attribute", {
4546
node,
4647
messageId: "expectedShorthand",
4748
*fix(fixer) {
48-
yield fixer.remove(node.key)
49-
yield fixer.remove(sourceCode.getTokenAfter(node.key)!)
49+
const quoteAndRange = getAttributeValueQuoteAndRange(
50+
node,
51+
sourceCode,
52+
)
53+
if (!quoteAndRange) {
54+
return
55+
}
56+
if (
57+
quoteAndRange.quote === "double" ||
58+
quoteAndRange.quote === "single"
59+
) {
60+
yield fixer.removeRange([
61+
node.range[0],
62+
quoteAndRange.firstToken.range[1],
63+
])
64+
yield fixer.remove(quoteAndRange.lastToken)
65+
} else {
66+
yield fixer.removeRange([
67+
node.range[0],
68+
quoteAndRange.range[0],
69+
])
70+
}
5071
},
5172
})
5273
}

tests/fixtures/rules/shorthand-attribute/invalid/always/test01-errors.json

+10
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"message": "Expected shorthand attribute.",
99
"line": 10,
1010
"column": 9
11+
},
12+
{
13+
"message": "Expected shorthand attribute.",
14+
"line": 13,
15+
"column": 9
16+
},
17+
{
18+
"message": "Expected shorthand attribute.",
19+
"line": 15,
20+
"column": 9
1121
}
1222
]

tests/fixtures/rules/shorthand-attribute/invalid/always/test01-input.svelte

+6
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@
99
<!-- prettier-ignore -->
1010
<button disabled = {disabled}>...</button>
1111
<button {disabled}>...</button>
12+
<!-- prettier-ignore -->
13+
<button disabled="{disabled}">...</button>
14+
<!-- prettier-ignore -->
15+
<button disabled = "{disabled}">...</button>
16+
<!-- prettier-ignore -->
17+
<button disabled = " {disabled} ">...</button>

tests/fixtures/rules/shorthand-attribute/invalid/always/test01-output.svelte

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@
77
<button {disabled}>...</button>
88

99
<!-- prettier-ignore -->
10-
<button {disabled}>...</button>
1110
<button {disabled}>...</button>
11+
<button {disabled}>...</button>
12+
<!-- prettier-ignore -->
13+
<button {disabled}>...</button>
14+
<!-- prettier-ignore -->
15+
<button {disabled}>...</button>
16+
<!-- prettier-ignore -->
17+
<button disabled = " {disabled} ">...</button>

tests/fixtures/rules/shorthand-attribute/invalid/test01-output.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<button {disabled}>...</button>
88

99
<!-- prettier-ignore -->
10-
<button {disabled}>...</button>
10+
<button {disabled}>...</button>
1111
<button {disabled}>...</button>

0 commit comments

Comments
 (0)