Skip to content

Commit

Permalink
fix: scaped literals double backslash formatting (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal committed Nov 30, 2020
1 parent 23b06b5 commit fc8c628
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { COMMENT, ID, MESSAGE, EXTRACT_MARK } from "./constants"

const keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g
const keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g
const removeExtraScapedLiterals = /(?:\\(.))/

function normalizeWhitespace(text) {
return text.replace(keepSpaceRe, " ").replace(keepNewLineRe, "\n").trim()
Expand Down Expand Up @@ -269,7 +270,7 @@ export default class MacroJs {

return {
type: "text",
value,
value: this.clearBackslashes(value),
}
}),
expressions: R.map((exp: babelTypes.Expression) =>
Expand All @@ -279,7 +280,6 @@ export default class MacroJs {
),
}),
(exp) => zip(exp.quasis, exp.expressions),
// @ts-ignore
R.flatten,
R.filter(Boolean)
)
Expand Down Expand Up @@ -356,6 +356,13 @@ export default class MacroJs {
}
}

/**
* We clean '//\` ' to just '`'
*/
clearBackslashes(value: string) {
return value.replace(removeExtraScapedLiterals, "`")
}

/**
* Custom matchers
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/macro/src/macroJsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { zip, makeCounter } from "./utils"
import { ID, COMMENT, MESSAGE } from "./constants"

const pluralRuleRe = /(_[\d\w]+|zero|one|two|few|many|other)/
const removeExtraScapedLiterals = /(?:\\(.))/
const jsx2icuExactChoice = (value) =>
value.replace(/_(\d+)/, "=$1").replace(/_(\w+)/, "$1")

Expand Down Expand Up @@ -233,7 +234,7 @@ export default class MacroJSX {
const value = text.value.raw
if (value === "") return null

return this.tokenizeText(value)
return this.tokenizeText(this.clearBackslashes(value))
}),
expressions: R.map((exp: babelTypes.Expression) =>
this.types.isCallExpression(exp)
Expand Down Expand Up @@ -345,6 +346,13 @@ export default class MacroJSX {
return this.types.isIdentifier(exp) ? exp.name : this.expressionIndex()
}

/**
* We clean '//\` ' to just '`'
*/
clearBackslashes(value: string) {
return value.replace(removeExtraScapedLiterals, "`")
}

/**
* Custom matchers
*/
Expand Down
26 changes: 26 additions & 0 deletions packages/macro/test/js-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ export default [
})
`,
},
{
name: "Variables with scaped template literals are correctly formatted",
input: `
import { t } from '@lingui/macro';
t\`Variable \\\`\${name}\\\`\`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Variable \`{name}\`", {
name: name
})
`,
},
{
name: "Variables with scaped double quotes are correctly formatted",
input: `
import { t } from '@lingui/macro';
t\`Variable \"name\" \`;
`,
expected: `
import { i18n } from "@lingui/core";
/*i18n*/
i18n._("Variable \\"name\\"")
`,
},
{
name: "Variables should be deduplicated",
input: `
Expand Down

1 comment on commit fc8c628

@vercel
Copy link

@vercel vercel bot commented on fc8c628 Nov 30, 2020

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.