Skip to content

Commit 74a4498

Browse files
committed
feature: @putout/plugin-remove-useless-escape: backtick inside single quote
1 parent 60f6735 commit 74a4498

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

packages/plugin-remove-useless-escape/lib/remove-useless-escape.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ export const traverse = ({push}) => ({
2121
StringLiteral(path) {
2222
const {raw} = path.node;
2323

24+
if (!raw)
25+
return;
26+
2427
if (isEscaped(raw))
2528
push(path);
29+
30+
if (raw.includes('\\`') && !raw.includes('\\\\`'))
31+
return push(path);
2632
},
2733

2834
TemplateLiteral(path) {
@@ -68,9 +74,6 @@ const hasEmoji = (a) => {
6874
};
6975

7076
function isEscaped(raw) {
71-
if (!raw)
72-
return false;
73-
7477
if (!raw.includes('\\'))
7578
return false;
7679

@@ -124,7 +127,8 @@ function unEscape(raw) {
124127
.replaceAll('\\.', '.')
125128
.replace(/(\\),/, ',')
126129
.replaceAll('\\h', 'h')
127-
.replaceAll('\\z', 'z');
130+
.replaceAll('\\z', 'z')
131+
.replaceAll('\\`', '`');
128132

129133
for (const emoji of match(raw)) {
130134
raw = raw.replace(createEncodedRegExp(emoji), emoji);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
path.node.raw.includes(`'`);
2+
3+
const a = 'const a = `hello ${world}`;';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
path.node.raw.includes(`\'`)
22

3+
const a = 'const a = \`hello ${world}\`;';

packages/plugin-remove-useless-escape/test/fixture/slash.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ const expected = montag`
1010

1111
fixture = fixture
1212
.replace(/'/g, `\\'`);
13-
13+
14+
if (raw.includes('\\`'))
15+
return true;
16+
17+
t.report('apply-namespace-to-imported-file', `Use \`import * as dotdot from './b/index.js'\` in '/lib/index.js'`);
1418

0 commit comments

Comments
 (0)