Skip to content

Commit

Permalink
Merge pull request #43 from vitonsky/29-use-consistent-quotes
Browse files Browse the repository at this point in the history
feat: preserve quote style
  • Loading branch information
vitonsky authored Apr 13, 2024
2 parents 00e4fb9 + 6699938 commit 0f948fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/rules/alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";`,
},
],
});
10 changes: 8 additions & 2 deletions src/rules/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0f948fe

Please sign in to comment.