Skip to content
Closed
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
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ impl Fold for LinguiMacroFolder {

n = n.fold_children_with(self);

// Remove empty statements at module level
// This is required to avoid extra semicolons in the output
n.retain(|item| !matches!(item, ModuleItem::Stmt(Stmt::Empty(..))));

if self.ctx.should_add_18n_import {
n.insert(
insert_index,
Expand Down Expand Up @@ -428,6 +432,23 @@ impl Fold for LinguiMacroFolder {

el.fold_children_with(self)
}

fn fold_stmts(&mut self, mut stmts: Vec<Stmt>) -> Vec<Stmt> {
// If no package that we care about is imported, skip the following
// transformation logic.
if !self.has_lingui_macro_imports {
return stmts;
}

// First, process children
stmts = stmts.fold_children_with(self);

// Remove empty statements from the statement list
// This is required to avoid extra semicolons in the output
stmts.retain(|s| !matches!(s, Stmt::Empty(..)));

stmts
}
}

#[plugin_transform]
Expand Down
233 changes: 233 additions & 0 deletions src/tests/empty_statements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
use crate::to;

to!(
should_remove_empty_statements_from_blocks,
r#"
import { t } from "@lingui/core/macro";

function test() {
const msg = t`Hello`;
;
;
const msg2 = t`World`;
;
}
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

function test() {
const msg = $_i18n._({
id: "uzTaYi",
message: "Hello"
});
const msg2 = $_i18n._({
id: "v+jdqd",
message: "World"
});
}
"#
);

to!(
should_remove_empty_statements_at_module_level,
r#"
import { t } from "@lingui/core/macro";

;
;

const msg = t`Hello`;

;
;

export default msg;
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

const msg = $_i18n._({
id: "uzTaYi",
message: "Hello"
});

export default msg;
"#
);

to!(
should_remove_empty_statements_in_nested_blocks,
r#"
import { t } from "@lingui/core/macro";

function test() {
if (true) {
;
const msg = t`Hello`;
;
}
;
}
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

function test() {
if (true) {
const msg = $_i18n._({
id: "uzTaYi",
message: "Hello"
});
}
}
"#
);

to!(
should_remove_empty_statements_in_arrow_functions,
r#"
import { t } from "@lingui/core/macro";

const fn = () => {
;
const msg = t`Test`;
;
return msg;
;
};
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

const fn = ()=>{
const msg = $_i18n._({
id: "NnH3pK",
message: "Test"
});
return msg;
};
"#
);

to!(
should_remove_empty_statements_in_loops,
r#"
import { t } from "@lingui/core/macro";

function test() {
for (let i = 0; i < 10; i++) {
;
const msg = t`Loop ${i}`;
;
}
;
while (true) {
;
const msg2 = t`While`;
;
break;
}
}
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

function test() {
for(let i = 0; i < 10; i++){
const msg = $_i18n._({
id: "c+yTqm",
message: "Loop {i}",
values: {
i: i
}
});
}
while(true){
const msg2 = $_i18n._({
id: "FBBphD",
message: "While"
});
break;
}
}
"#
);

to!(
should_remove_empty_statements_in_try_catch,
r#"
import { t } from "@lingui/core/macro";

function test() {
try {
;
const msg = t`Try`;
;
} catch (e) {
;
const msg2 = t`Catch`;
;
}
}
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

function test() {
try {
const msg = $_i18n._({
id: "/7tku+",
message: "Try"
});
} catch (e) {
const msg2 = $_i18n._({
id: "iKsG5F",
message: "Catch"
});
}
}
"#
);

to!(
should_remove_empty_statements_in_switch,
r#"
import { t } from "@lingui/core/macro";

function test(value) {
switch (value) {
case 1:
;
const msg1 = t`One`;
;
break;
case 2:
;
const msg2 = t`Two`;
;
break;
}
}
"#,
r#"
import { i18n as $_i18n } from "@lingui/core";

function test(value) {
switch(value){
case 1:
const msg1 = $_i18n._({
id: "/DFChQ",
message: "One"
});
break;
case 2:
const msg2 = $_i18n._({
id: "z9dG+S",
message: "Two"
});
break;
}
}
"#
);
18 changes: 9 additions & 9 deletions src/tests/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ to!(
import { Trans as Trans_ } from "@lingui/react";
import { Select } from "./my-select-cmp";

;<Trans_
<Trans_
message={"{count, plural, one {Message} other {Messages}}"}
id={"V4EO9s"}
values={{ count: count }}
/>
/>;

;<Select prop="propValue">Should be untouched</Select>
<Select prop="propValue">Should be untouched</Select>;
"#
);

Expand All @@ -78,12 +78,12 @@ to!(
import { Trans } from "@lingui/react";
import { Trans as Trans_ } from "@lingui/react";

;<Trans_
<Trans_
message={"{count, plural, one {Message} other {Messages}}"}
id={"V4EO9s"}
values={{ count: count }}
/>
;<Trans>Should be untouched</Trans>
/>;
<Trans>Should be untouched</Trans>;
"#
);

Expand Down Expand Up @@ -173,11 +173,11 @@ to!(
r#"
import { Trans as Trans_ } from "@lingui/react";

;<Trans_ message={"{count, plural, one {Message} other {Messages}}"} id={"V4EO9s"} values={{
<Trans_ message={"{count, plural, one {Message} other {Messages}}"} id={"V4EO9s"} values={{
count: count
}}/>
}}/>;

;<Trans_ message={"Hello!"} id={"mAYvqA"}/>;
<Trans_ message={"Hello!"} id={"mAYvqA"}/>;
"#
);
to!(
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod common;
mod empty_statements;
mod imports;
mod js_define_message;
mod js_icu;
Expand Down