Skip to content

Commit

Permalink
Merge pull request #31 from lingui/sync-main
Browse files Browse the repository at this point in the history
Sync with main
  • Loading branch information
andrii-bodnar authored Mar 6, 2023
2 parents c2cdf3c + 8e5752e commit 1a62dfc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: 1.65.0
target: wasm32-wasi
override: true

Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ https://swc.rs/docs/configuration/swcrc
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"experimental": {
"plugins": ["@lingui/swc-plugin", {

// Optional
// Unlike the JS version this option must be passed as object only.
// Docs https://lingui.dev/ref/conf#runtimeconfigmodule
// "runtimeModules": {
// "i18n": ["@lingui/core", "i18n"],
// "trans": ["@lingui/react", "Trans"]
// }
}]
}
}
"plugins": [
[
"@lingui/swc-plugin",
{
// Optional
// Unlike the JS version this option must be passed as object only.
// Docs https://lingui.dev/ref/conf#runtimeconfigmodule
// "runtimeModules": {
// "i18n": ["@lingui/core", "i18n"],
// "trans": ["@lingui/react", "Trans"]
// }
},
],
],
},
},
}
```

Expand Down
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ impl<'a> Fold for LinguiMacroFolder {
let (i18n_source, i18n_export) = self.ctx.options.runtime_modules.i18n.clone();
let (trans_source, trans_export) = self.ctx.options.runtime_modules.trans.clone();

let mut insert_index: usize = 0;
let mut index = 0;

n.retain(|m| {
if let ModuleItem::ModuleDecl(ModuleDecl::Import(imp)) = m {
// drop macro imports
if &imp.src.value == "@lingui/macro" {
self.has_lingui_macro_imports = true;
self.ctx.register_macro_import(imp);
insert_index = index;
return false;
}

Expand All @@ -159,20 +163,21 @@ impl<'a> Fold for LinguiMacroFolder {
}
}

true
index +=1;
true
});

n = n.fold_children_with(self);
n = n.fold_children_with(self);

if !has_i18n_import && self.ctx.should_add_18n_import {
n.insert(0, create_import(i18n_source.into(), quote_ident!(i18n_export[..])));
}
if !has_i18n_import && self.ctx.should_add_18n_import {
n.insert(insert_index, create_import(i18n_source.into(), quote_ident!(i18n_export[..])));
}

if !has_trans_import && self.ctx.should_add_trans_import {
n.insert(0, create_import(trans_source.into(), quote_ident!(trans_export[..])));
}
if !has_trans_import && self.ctx.should_add_trans_import {
n.insert(insert_index, create_import(trans_source.into(), quote_ident!(trans_export[..])));
}

n
n
}

fn fold_expr(&mut self, expr: Expr) -> Expr {
Expand Down
20 changes: 20 additions & 0 deletions src/tests/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,23 @@ to!(
;<Trans message={"Hello!"} id={"mAYvqA"}/>;
"#
);
to!(
// https://github.com/lingui/swc-plugin/issues/21
should_add_imports_after_directive_prologues,
r#"
"use client";
import {t} from "@lingui/macro"
import foo from "bar"
t`Text`
"#,

r#"
"use client";
import { i18n } from "@lingui/core";
import foo from "bar";
i18n._({
id: "xeiujy",
message: "Text"
});
"#
);

0 comments on commit 1a62dfc

Please sign in to comment.