@@ -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
3213module . 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+ } ;
0 commit comments