@@ -37,8 +37,9 @@ function obfuscateJsWithAst(
37
37
return "{{obfuscated}}" ;
38
38
}
39
39
40
- // strip unnecessary space, e.g. " a b c " => "a b c"
41
- str = stripUnnecessarySpace ? str . replace ( / \s + / g, " " ) . trim ( ) : str ;
40
+ // strip unnecessary space, e.g. " a b c " => "a b c "
41
+ str = stripUnnecessarySpace ? str . replace ( / \s + / g, " " ) . trimStart ( ) //? avoid trimming the end to keep the space between classes
42
+ : str ;
42
43
43
44
const { obfuscatedContent, usedKeys : obfuscateUsedKeys } = obfuscateKeys ( selectorConversion , str ) ;
44
45
if ( obfuscatedContent !== str ) {
@@ -118,7 +119,7 @@ function searchStringLiterals(path: NodePath<t.Node>,
118
119
searchStringLiterals ( arg , callback , scannedNodes ) ;
119
120
} ) ;
120
121
}
121
- }
122
+ }
122
123
}
123
124
/* binary expression (e.g. const a = "hello" + "world") */
124
125
else if ( t . isBinaryExpression ( path . node ) ) {
@@ -294,7 +295,7 @@ function searchStringLiterals(path: NodePath<t.Node>,
294
295
searchStringLiterals ( handlerBody , callback , scannedNodes ) ;
295
296
}
296
297
}
297
- }
298
+ }
298
299
/* member expression (e.g. "scroll-top".replace("-", "_")); "scroll-top ".concat("visible"); */
299
300
else if ( t . isMemberExpression ( path . node ) ) {
300
301
const object = path . get ( "object" ) ;
@@ -313,7 +314,47 @@ function searchStringLiterals(path: NodePath<t.Node>,
313
314
searchStringLiterals ( arg , callback , scannedNodes ) ;
314
315
} ) ;
315
316
}
316
- } else {
317
+ }
318
+ /* template literal (e.g. `hello ${name}`) */
319
+ else if ( t . isTemplateLiteral ( path . node ) ) {
320
+ const quasis = path . get ( "quasis" ) ;
321
+ const expressions = path . get ( "expressions" ) ;
322
+ if ( Array . isArray ( quasis ) ) {
323
+ quasis . forEach ( quasi => {
324
+ searchStringLiterals ( quasi , callback , scannedNodes ) ;
325
+ } ) ;
326
+ }
327
+ if ( Array . isArray ( expressions ) ) {
328
+ expressions . forEach ( expression => {
329
+ searchStringLiterals ( expression , callback , scannedNodes ) ;
330
+ } ) ;
331
+ }
332
+ }
333
+ /* template element (e.g. `hello ${name}`) */
334
+ else if ( t . isTemplateElement ( path . node ) ) {
335
+ const node = path . node as t . TemplateElement ;
336
+
337
+ if ( node . value ) {
338
+ const { raw, cooked } = node . value ;
339
+
340
+ // Replace the "raw" and "cooked" values of the template element
341
+ if ( raw ) {
342
+ // If the raw is not empty
343
+ const rawReplacement = callback ( raw ) ;
344
+ if ( rawReplacement !== undefined ) {
345
+ node . value . raw = rawReplacement ;
346
+ }
347
+ }
348
+ if ( cooked ) {
349
+ // If the cooked is not empty and undefined
350
+ const cookedReplacement = callback ( cooked ) ;
351
+ if ( cookedReplacement !== undefined ) {
352
+ node . value . cooked = cookedReplacement ;
353
+ }
354
+ }
355
+ }
356
+ }
357
+ else {
317
358
path . traverse ( {
318
359
Identifier ( innerPath ) {
319
360
searchStringLiterals ( innerPath , callback , scannedNodes ) ;
@@ -329,4 +370,4 @@ function searchStringLiterals(path: NodePath<t.Node>,
329
370
export {
330
371
searchStringLiterals ,
331
372
obfuscateJsWithAst ,
332
- }
373
+ }
0 commit comments