Skip to content

Commit

Permalink
feature: use-lingui
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored and andrii-bodnar committed Aug 6, 2024
1 parent 2541763 commit ceb449f
Show file tree
Hide file tree
Showing 13 changed files with 805 additions and 321 deletions.
27 changes: 24 additions & 3 deletions src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ pub fn get_jsx_attr<'a>(el: &'a JSXOpeningElement, name: &str) -> Option<&'a JSX
return None;
}

// get_local_ident_from_object_pat_prop(prop, "t")
// const {t} = useLingui() // => Ident("t")
// const {t: _} = useLingui() // => Ident("_")
pub fn get_local_ident_from_object_pat_prop(prop: &ObjectPatProp, imported_symbol: &str) -> Option<BindingIdent> {
return match prop {
ObjectPatProp::KeyValue(key_value)
if key_value.key.as_ident().is_some_and(|ident| ident.sym == imported_symbol.to_string()) =>
{
Some(key_value.value.as_ident().unwrap().clone())
}
ObjectPatProp::Assign(assign)
if assign.key.sym == imported_symbol.to_string() =>
{
Some(assign.key.clone())
}
_ => {
None
}
}
}

pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option<String> {
match val {
// offset="5"
Expand Down Expand Up @@ -146,15 +167,15 @@ pub fn create_key_value_prop(key: &str, value: Box<Expr>) -> PropOrSpread {
)));
}

pub fn create_import(source: JsWord, specifier: Ident) -> ModuleItem {
pub fn create_import(source: JsWord, imported: Ident, local: Ident) -> ModuleItem {
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
phase: ImportPhase::default(),
specifiers: vec![
ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: specifier,
imported: None,
local,
imported: Some(ModuleExportName::Ident(imported)),
is_type_only: false,
})
],
Expand Down
5 changes: 2 additions & 3 deletions src/js_macro_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ impl<'a> JsMacroFolder<'a> {
span: DUMMY_SP,
obj: callee_obj.unwrap_or_else(|| {
self.ctx.should_add_18n_import = true;
let (_, i18n_export) = &self.ctx.options.runtime_modules.i18n;

return Box::new(Ident::new(i18n_export.clone().into(), DUMMY_SP).into());
return Box::new(self.ctx.runtime_idents.i18n.clone().into());
}),
prop: MemberProp::Ident(Ident::new("_".into(), DUMMY_SP)),
}).as_callee(),
Expand Down Expand Up @@ -198,6 +197,6 @@ impl<'a> Fold for JsMacroFolder<'a> {
);
}

expr
expr.fold_children_with(self)
}
}
Loading

0 comments on commit ceb449f

Please sign in to comment.