Skip to content
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
22 changes: 13 additions & 9 deletions crates/lingui_macro/src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_core::ecma::{ast::*, atoms::Atom};
use swc_core::plugin::errors::HANDLER;

fn expression_to_name(expr: &Expr, ctx: &mut MacroCtx) -> String {
let expr = unwrap_ts_as_expr(expr);
let expr = unwrap_ts_only_expr(expr);

match expr {
Expr::Ident(ident) => ident.sym.to_string(),
Expand All @@ -33,7 +33,7 @@ fn expression_to_name(expr: &Expr, ctx: &mut MacroCtx) -> String {
}

fn expression_to_value(expr: Box<Expr>) -> Box<Expr> {
let unwrapped = unwrap_ts_as_expr(&expr);
let unwrapped = unwrap_ts_only_expr(&expr);

match unwrapped {
Expr::Object(object) => {
Expand Down Expand Up @@ -66,14 +66,18 @@ fn expression_to_value(expr: Box<Expr>) -> Box<Expr> {
}
}

// recursively expands TypeScript's as expressions until it reaches a real value
fn unwrap_ts_as_expr(expr: &Expr) -> &Expr {
// recursively unwraps TypeScript-only expression wrappers (`x as T`, `x!`,
// `x satisfies T`) until it reaches a real value, so the inner expression drives
// placeholder naming (e.g. `${x!}` → `{x}`, not `{0}`).
fn unwrap_ts_only_expr(expr: &Expr) -> &Expr {
let mut current = expr;
while let Expr::TsAs(TsAsExpr {
expr: inner_expr, ..
}) = current
{
current = inner_expr;
loop {
current = match current {
Expr::TsAs(TsAsExpr { expr, .. })
| Expr::TsNonNull(TsNonNullExpr { expr, .. })
| Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => expr,
_ => break,
Comment thread
pkasarda marked this conversation as resolved.
};
}
current
}
Expand Down
18 changes: 18 additions & 0 deletions crates/lingui_macro/tests/js_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,21 @@ to!(
t`Hello World`
"#
);

// TS-only expression wrappers unwrap to a named placeholder (parity with `as`),
// matching @lingui/babel-plugin-lingui-macro (see lingui/js-lingui#2622).
to!(
js_non_null_assertion_gets_named_placeholder,
r#"
import { t } from '@lingui/core/macro';
t`Variable ${name!}`;
"#
);

to!(
js_satisfies_gets_named_placeholder,
r#"
import { t } from '@lingui/core/macro';
t`Variable ${name satisfies string}`;
"#
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t } from '@lingui/core/macro';
t`Variable ${name!}`;

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
$_i18n._(/*i18n*/ {
id: "xRRkAE",
message: "Variable {name}",
values: {
name: name!
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t } from '@lingui/core/macro';
t`Variable ${name satisfies string}`;

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
$_i18n._(/*i18n*/ {
id: "xRRkAE",
message: "Variable {name}",
values: {
name: name satisfies string
}
});
Loading