Skip to content

Commit bcd8b0f

Browse files
committed
feature: @putout/plugin-apply-template-literals: improve support of newlines
1 parent cfedbc1 commit bcd8b0f

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

packages/plugin-apply-template-literals/lib/apply-template-literals.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,27 @@ const {
66
isTemplateLiteral,
77
} = types;
88

9-
module.exports.report = () => `Use template literals instead of binary expressions`;
9+
module.exports.report = () => `Use template literals ('\`\`') instead of binary expressions ('+')`;
1010

11-
module.exports.filter = ({parentPath}) => {
12-
if (isBinaryExpression(parentPath) || isTemplateLiteral(parentPath.parentPath))
13-
return false;
14-
15-
let is = true;
16-
17-
parentPath.traverse({
18-
StringLiteral(path) {
19-
const {value} = path.node;
20-
21-
if (value.includes('\r'))
22-
is = false;
23-
24-
if (value.includes('\n'))
25-
is = false;
26-
},
27-
});
28-
29-
return is;
30-
};
11+
module.exports.filter = ({parentPath}) => !(isBinaryExpression(parentPath) || isTemplateLiteral(parentPath.parentPath));
3112

3213
module.exports.replace = () => ({
33-
'"__a" + __identifier__b + "__c"': '`__a${__identifier__b}__c`',
34-
'"__a" + __b(__args)+ "__c"': '`__a${__b(__args)}__c`',
35-
'__identifier__a + "__b" + __identifier__c + __identifier__d': '`${__identifier__a}__b${__identifier__c}${__identifier__d}`',
36-
'__identifier__a + "__b"': '`${__identifier__a}__b`',
37-
'"__a" + __identifier__b': '`__a${__identifier__b}`',
38-
'String(__a) + ".tar.gz"': '`${__a}.tar.gz`',
14+
'"__a" + __identifier__b + "__c"': replaceNewlines('`__a${__identifier__b}__c`', ['__a', '__c']),
15+
'"__a" + __b(__args) + "__c"': replaceNewlines('`__a${__b(__args)}__c`', ['__a', '__c']),
16+
'__identifier__a + "__b" + __identifier__c + __identifier__d': replaceNewlines('`${__identifier__a}__b${__identifier__c}${__identifier__d}`', ['__b']),
17+
'__identifier__a + "__b"': replaceNewlines('`${__identifier__a}__b`', ['__b']),
18+
'"__a" + __identifier__b': replaceNewlines('`__a${__identifier__b}`', ['__a']),
19+
'String(__a) + "__b"': replaceNewlines('`${__a}__b`', ['__b']),
3920
});
21+
22+
const replaceNewlines = (template, names) => (vars) => {
23+
for (const name of names) {
24+
const {value} = vars[name];
25+
26+
vars[name].value = value
27+
.replace('\n', '\\n')
28+
.replace('\r', '\\r');
29+
}
30+
31+
return template;
32+
};

packages/plugin-apply-template-literals/test/apply-template-literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const test = createTest(__dirname, {
1010
});
1111

1212
test('plugin-apply-template-literals: report: binary', (t) => {
13-
t.report('binary', `Use template literals instead of binary expressions`);
13+
t.report('binary', `Use template literals ('\`\`') instead of binary expressions ('+')`);
1414
t.end();
1515
});
1616

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
process.stdout.write('\r' + percent + '%');
2-
throw new Error('Could not get published versions\n' + err);
1+
process.stdout.write(`\r${percent}%`);
2+
throw new Error(`Could not get published versions\n${err}`);
3+
4+
fn(`\n${name}\n`);
5+
fn(`\n${fn(name)}\n`);
6+
fn(`${a}\n${c}${d}`);
7+
fn(`${a}\n`);
8+
`${name}\n`;

0 commit comments

Comments
 (0)