From 014c8e118728b8f2dd986a3848400b45dd901647 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Fri, 14 Jan 2022 17:44:00 +0900 Subject: [PATCH] Fix wrong autofix in shorthand-attribute rule --- src/rules/shorthand-attribute.ts | 25 +++++++++++++++++-- .../invalid/always/test01-errors.json | 10 ++++++++ .../invalid/always/test01-input.svelte | 6 +++++ .../invalid/always/test01-output.svelte | 8 +++++- .../invalid/test01-output.svelte | 2 +- 5 files changed, 47 insertions(+), 4 deletions(-) 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 @@ - +