Skip to content

Commit

Permalink
fix: respect unicode chars in t macro (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal committed Apr 8, 2021
1 parent 93afb72 commit 7597621
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/macro/src/macroJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,55 @@ describe("js macro", () => {
},
])
})

it("message with unicode chars is respected", () => {
const macro = createMacro()
const exp = parseExpression('t`Message \\u0020`')
const tokens = macro.tokenizeTemplateLiteral(exp)
expect(tokens).toEqual([
{
type: "text",
value: 'Message \\u0020',
},
])
})

it("message with double scaped literals it's stripped", () => {
const macro = createMacro()
const exp = parseExpression('t\`Passing \\`${argSet}\\` is not supported.\`')
const tokens = macro.tokenizeTemplateLiteral(exp)
expect(tokens).toEqual([
{
type: "text",
value: 'Passing `',
},
{
name: "argSet",
type: "arg",
value: {
end: 20,
loc: {
end: {
column: 20,
line: 1,
},
identifierName: "argSet",
start: {
column: 14,
line: 1,
},
},
name: "argSet",
start: 14,
type: "Identifier",
},
},
{
type: "text",
value: "` is not supported.",
},
])
})
})

describe("tokenizeChoiceMethod", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ export default class MacroJs {
* We clean '//\` ' to just '`'
*/
clearBackslashes(value: string){
// it's an unicode char so we should keep them
if (value.includes('\\u')) return value
// if not we replace the extra scaped literals
return value.replace(removeExtraScapedLiterals, "`")
}

Expand Down
3 changes: 3 additions & 0 deletions packages/macro/src/macroJsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ export default class MacroJSX {
* We clean '//\` ' to just '`'
* */
clearBackslashes(value: string){
// it's an unicode char so we should keep them
if (value.includes('\\u')) return value
// if not we replace the extra scaped literals
return value.replace(removeExtraScapedLiterals, "`")
}

Expand Down

1 comment on commit 7597621

@vercel
Copy link

@vercel vercel bot commented on 7597621 Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.