From 669993837e086d6f3ee94e01a2401076d2ab9fb0 Mon Sep 17 00:00:00 2001 From: Robert Vitonsky Date: Sat, 13 Apr 2024 13:46:43 +0200 Subject: [PATCH] feat: preserve quote style --- src/rules/alias.test.ts | 7 +++++++ src/rules/alias.ts | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/rules/alias.test.ts b/src/rules/alias.test.ts index d1f342d..8b866a9 100644 --- a/src/rules/alias.test.ts +++ b/src/rules/alias.test.ts @@ -64,5 +64,12 @@ tester.run('paths-alias', rule, { errors: ['Update import to @bar/x/y/z'], output: `import z from '@bar/x/y/z';`, }, + { + name: 'quotes style for string must be preserved for any fix', + filename: path.resolve('./src/index.ts'), + code: `import z from "./foo/x/y/z";`, + errors: ['Update import to @foo/x/y/z'], + output: `import z from "@foo/x/y/z";`, + }, ], }); diff --git a/src/rules/alias.ts b/src/rules/alias.ts index 68f06be..5218499 100644 --- a/src/rules/alias.ts +++ b/src/rules/alias.ts @@ -129,8 +129,14 @@ const rule: Rule.RuleModule = { node, message: `Update import to ${replacement}`, fix(fixer) { - // TODO: preserve quotes - const quote = `'`; + const acceptableQuoteSymbols = [`'`, `"`]; + const originalStringQuote = node.source.raw?.slice(0, 1); + const quote = + originalStringQuote && + acceptableQuoteSymbols.includes(originalStringQuote) + ? originalStringQuote + : acceptableQuoteSymbols[0]; + return fixer.replaceText( node.source, quote + replacement + quote,