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

refactor(macro): remove whitespace cleaning from js macro #1897

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
28 changes: 3 additions & 25 deletions packages/babel-plugin-lingui-macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,11 @@ import {
} from "./constants"
import { generateMessageId } from "@lingui/message-utils/generateMessageId"

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

function normalizeWhitespace(text: string): string {
return text.replace(keepSpaceRe, " ").replace(keepNewLineRe, "\n").trim()
}

function buildICUFromTokens(tokens: Tokens) {
const messageFormat = new ICUMessageFormat()
const { message, values } = messageFormat.fromTokens(tokens)

return { message: normalizeWhitespace(message), values }
return { message, values }
}

export type MacroJsOpts = {
Expand Down Expand Up @@ -444,14 +437,7 @@ export class MacroJs {
const expressions = tpl.get("expressions") as NodePath<Expression>[]

return tpl.get("quasis").flatMap((text, i) => {
// if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
// This regex will detect if a string contains unicode chars, when they're we should interpolate them
// why? because platforms like react native doesn't parse them, just doing a JSON.parse makes them UTF-8 friendly
const value = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g.test(
text.node.value.raw
)
? text.node.value.cooked
: text.node.value.raw
const value = text.node.value.cooked

let argTokens: Token[] = []
const currExp = expressions[i]
Expand All @@ -463,7 +449,7 @@ export class MacroJs {
}
const textToken: TextToken = {
type: "text",
value: this.clearBackslashes(value),
value,
}
return [...(value ? [textToken] : []), ...argTokens]
})
Expand Down Expand Up @@ -544,14 +530,6 @@ export class MacroJs {
}
}

/**
* We clean '//\` ' to just '`'
*/
clearBackslashes(value: string) {
// if not we replace the extra escaped literals
return value.replace(/\\`/g, "`")
}

createI18nCall(
messageDescriptor: ObjectExpression,
linguiInstance?: Expression
Expand Down
20 changes: 2 additions & 18 deletions packages/babel-plugin-lingui-macro/src/macroJsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,7 @@ export class MacroJSX {
const expressions = exp.get("expressions") as NodePath<Expression>[]

return exp.get("quasis").flatMap(({ node: text }, i) => {
// if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
// This regex will detect if a string contains unicode chars, when they're we should interpolate them
// why? because platforms like react native doesn't parse them, just doing a JSON.parse makes them UTF-8 friendly
const value = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g.test(text.value.raw)
? text.value.cooked
: text.value.raw
const value = text.value.cooked

let argTokens: Token[] = []
const currExp = expressions[i]
Expand All @@ -314,10 +309,7 @@ export class MacroJSX {
: [this.tokenizeExpression(currExp)]
}

return [
...(value ? [this.tokenizeText(this.clearBackslashes(value))] : []),
...argTokens,
]
return [...(value ? [this.tokenizeText(value)] : []), ...argTokens]
})
}

Expand Down Expand Up @@ -466,14 +458,6 @@ export class MacroJSX {
return path.isIdentifier() ? path.node.name : String(this.expressionIndex())
}

/**
* We clean '//\` ' to just '`'
**/
clearBackslashes(value: string): string {
// if not we replace the extra scaped literals
return value.replace(/\\`/g, "`")
}

isLinguiComponent = (
path: NodePath,
name: JsxMacroName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { t } from "@lingui/core/macro"

t`Multiline\
with continuation`
with continuation`
55 changes: 28 additions & 27 deletions packages/babel-plugin-lingui-macro/test/js-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const cases: TestCase[] = [
name: "Variables with escaped double quotes are correctly formatted",
input: `
import { t } from '@lingui/core/macro';
t\`Variable \"name\" \`;
t\`Variable \"name\"\`;
`,
expected: `
import { i18n as _i18n } from "@lingui/core";
Expand Down Expand Up @@ -153,32 +153,33 @@ const cases: TestCase[] = [
name: "Anything variables except simple identifiers are used as positional arguments",
input: `
import { t } from '@lingui/core/macro';
t\`
Property \${props.name},\\
function \${random()},\\
array \${array[index]},\\
constant \${42},\\
object \${new Date()}\\
anything \${props.messages[index].value()}
\`
`,
t\`\
Property \${props.name},\
function \${random()},\
array \${array[index]},\
constant \${42},\
object \${new Date()}\
anything \${props.messages[index].value()}\
\`
`,
expected: `
import { i18n as _i18n } from "@lingui/core";
_i18n._(
/*i18n*/
{
id: "X1jIKa",
message: "Property {0}, function {1}, array {2}, constant {3}, object {4} anything {5}",
import { i18n as _i18n } from "@lingui/core";
_i18n._(
/*i18n*/
{
id: "vVZNZ5",
message:
" Property {0}, function {1}, array {2}, constant {3}, object {4} anything {5}",
values: {
0: props.name,
1: random(),
2: array[index],
3: 42,
4: new Date(),
5: props.messages[index].value(),
},
}
);
0: props.name,
1: random(),
2: array[index],
3: 42,
4: new Date(),
5: props.messages[index].value(),
},
}
);
`,
},
{
Expand All @@ -193,8 +194,8 @@ const cases: TestCase[] = [
_i18n._(
/*i18n*/
{
id: "EfogM+",
message: "Multiline\\nstring",
id: "+8iwDA",
message: "Multiline\\n string",
}
);
`,
Expand Down
Loading