Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect unicode chars in t macro #1032

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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