-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle template and string literals correctly
- Loading branch information
Showing
8 changed files
with
125 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as t from '@babel/types'; | ||
|
||
export function isStringLike(node) { | ||
return t.isStringLiteral(node) || t.isTemplateLiteral(node); | ||
} | ||
|
||
export function combineStringLike(a, b) { | ||
if (isStringLikeEmpty(a) && isStringLikeEmpty(b)) { | ||
return t.stringLiteral(''); | ||
} | ||
|
||
if (t.isStringLiteral(a) && t.isStringLiteral(b)) { | ||
return t.stringLiteral(a.value + ' ' + b.value); | ||
} | ||
|
||
if (t.isTemplateLiteral(a) && t.isTemplateLiteral(b)) { | ||
const expressions = [...a.expressions, ...b.expressions]; | ||
const quasis = [...a.quasis]; | ||
|
||
quasis[quasis.length - 1] = templateElement( | ||
quasis[quasis.length - 1].value.raw + ' ' + b.quasis[0].value.raw, | ||
); | ||
|
||
quasis.push(...b.quasis.slice(1)); | ||
|
||
return templateOrStringLiteral(quasis, expressions); | ||
} | ||
|
||
if (t.isTemplateLiteral(a) && t.isStringLiteral(b)) { | ||
const expressions = [...a.expressions]; | ||
const quasis = [...a.quasis]; | ||
|
||
const i = quasis.length - 1; | ||
quasis[i] = templateElement(quasis[i].value.raw + ' ' + b.value, true); | ||
|
||
return templateOrStringLiteral(quasis, expressions); | ||
} | ||
|
||
if (t.isStringLiteral(a) && t.isTemplateLiteral(b)) { | ||
const expressions = [...b.expressions]; | ||
const quasis = [...b.quasis]; | ||
|
||
const i = 0; | ||
quasis[i] = templateElement(a.value + ' ' + quasis[i].value.raw, true); | ||
|
||
return templateOrStringLiteral(quasis, expressions); | ||
} | ||
|
||
throw new Error('Unable to handle that input'); | ||
} | ||
|
||
export function isStringLikeEmpty(node) { | ||
if (t.isStringLiteral(node)) { | ||
return node.value.length === 0; | ||
} | ||
|
||
if (t.isTemplateLiteral(node)) { | ||
return node.expressions.length === 0 && node.quasis.every(q => q.value.raw.length === 0); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function templateOrStringLiteral(quasis, expressions) { | ||
if (expressions.length === 0) { | ||
return t.stringLiteral(quasis[0].value.raw); | ||
} | ||
|
||
return t.templateLiteral(quasis, expressions); | ||
} | ||
|
||
function templateElement(value, tail = false) { | ||
return { | ||
type: 'TemplateElement', | ||
value: { | ||
raw: value, | ||
}, | ||
tail, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
test/fixtures/combine-string-literals/string-and-template/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const x = clsx('foo', `bar-${baz}`); | ||
const y = clsx(`bar-${baz}`, 'foo'); |
2 changes: 2 additions & 0 deletions
2
test/fixtures/combine-string-literals/string-and-template/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const x = clsx(`foo bar-${baz}`); | ||
const y = clsx(`bar-${baz} foo`); |
6 changes: 4 additions & 2 deletions
6
test/fixtures/remove-unnecessary-calls/string-and-conditional-argument/code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const classA = clsx('foo bar', foo ? 'a' : 'b'); | ||
const classB = clsx(foo ? 'a' : 'b', 'foo bar'); | ||
const x1 = clsx('foo bar', foo ? 'a' : 'b'); | ||
const x2 = clsx(foo ? 'a' : 'b', 'foo bar'); | ||
const x3 = clsx('foo bar', foo ? 'a' : ``); | ||
const x4 = clsx('foo bar', foo ? 'a' : ''); |
6 changes: 4 additions & 2 deletions
6
test/fixtures/remove-unnecessary-calls/string-and-conditional-argument/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
const classA = 'foo bar ' + (foo ? 'a' : 'b'); | ||
const classB = 'foo bar ' + (foo ? 'a' : 'b'); | ||
const x1 = 'foo bar ' + (foo ? 'a' : 'b'); | ||
const x2 = 'foo bar ' + (foo ? 'a' : 'b'); | ||
const x3 = 'foo bar' + (foo ? ' a' : ''); | ||
const x4 = 'foo bar' + (foo ? ' a' : ''); |