Skip to content

Commit

Permalink
update rust version
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Aug 6, 2024
1 parent ceb449f commit 34e259d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: nightly-2024-07-21
override: true
- uses: Swatinem/rust-cache@v2
- uses: actions-rs/cargo@v1
Expand Down
22 changes: 14 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Install rust

You can follow instructions at ['Install Rust' page from the official rust website](https://www.rust-lang.org/tools/install)
You can follow instructions
at ['Install Rust' page from the official rust website](https://www.rust-lang.org/tools/install)

## Add wasm target to rust

Expand All @@ -9,6 +10,7 @@ rustup target add wasm32-wasi
```

## Running tests

```bash
# run all test suite
cargo test
Expand All @@ -26,19 +28,21 @@ cargo test jsx_
# (alias for `cargo build --target wasm32-wasi`)
cargo build-wasi --release
```

Then wasm binary would be on the path: `./target/wasm32-wasi/release/lingui_macro_plugin.wasm`

You can check it in your own project or in the `examples/nextjs-13` example in this repo by specifying full path to the WASM binary:
You can check it in your own project or in the `examples/nextjs-13` example in this repo by specifying full path to the
WASM binary:

```ts
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
swcPlugins: [
['/Users/tim/projects/lingui-macro-plugin/target/wasm32-wasi/release/lingui_macro_plugin.wasm', {}],
],
},
};
experimental: {
swcPlugins: [
['/Users/tim/projects/lingui-macro-plugin/target/wasm32-wasi/release/lingui_macro_plugin.wasm', {}],
],
},
};

module.exports = nextConfig;
```
Expand All @@ -48,3 +52,5 @@ module.exports = nextConfig;
It's important to build a plugin with the same Rust version used to build SWC itself.

This project uses `rust-toolchain` file in the root of project to define rust version.

To update Rust, put new version into `rust-toolchain` and call `rustup update` command
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2023-03-20
nightly-2024-07-21
148 changes: 64 additions & 84 deletions src/jsx_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use swc_core::ecma::{
visit::{Visit, VisitWith},
};
use swc_core::ecma::ast::{*};
use swc_core::common::DUMMY_SP;
use crate::ast_utils::{get_jsx_attr, get_jsx_attr_value_as_string};
use crate::tokens::{IcuChoice, ChoiceCase, CaseOrOffset, MsgToken, TagOpening};
use regex::{Regex};
use crate::macro_utils::MacroCtx;
use crate::tokens::{CaseOrOffset, ChoiceCase, IcuChoice, MsgToken, TagOpening};
use once_cell::sync::Lazy;
use regex::Regex;
use swc_core::common::DUMMY_SP;
use swc_core::ecma::ast::*;
use swc_core::ecma::atoms::JsWord;
use swc_core::ecma::visit::{Visit, VisitWith};
use swc_core::plugin::errors::HANDLER;
use crate::macro_utils::{ MacroCtx};

pub struct TransJSXVisitor<'a> {
pub tokens: Vec<MsgToken>,
Expand All @@ -20,12 +18,13 @@ impl<'a> TransJSXVisitor<'a> {
pub fn new(ctx: &'a MacroCtx) -> TransJSXVisitor<'a> {
TransJSXVisitor {
tokens: Vec::new(),
ctx
ctx,
}
}
}

static PLURAL_OPTIONS_WHITELIST: Lazy<Regex> = Lazy::new(|| Regex::new(r"(_[\d\w]+|zero|one|two|few|many|other)").unwrap());
static PLURAL_OPTIONS_WHITELIST: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(_[\d\w]+|zero|one|two|few|many|other)").unwrap());
static NUM_OPTION: Lazy<Regex> = Lazy::new(|| Regex::new(r"_(\d+)").unwrap());
static WORD_OPTION: Lazy<Regex> = Lazy::new(|| Regex::new(r"_(\w+)").unwrap());

Expand All @@ -37,45 +36,45 @@ static TRIM_END: Lazy<Regex> = Lazy::new(|| Regex::new(r"[ ]+$").unwrap());

// taken from babel repo -> packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts
fn clean_jsx_element_literal_child(value: &str) -> String {
let lines: Vec<&str> = value.split('\n').collect();
let mut last_non_empty_line = 0;
let lines: Vec<&str> = value.split('\n').collect();
let mut last_non_empty_line = 0;

for (i, line) in lines.iter().enumerate() {
if line.trim().len() > 0 {
last_non_empty_line = i;
for (i, line) in lines.iter().enumerate() {
if line.trim().len() > 0 {
last_non_empty_line = i;
}
}
}

let mut result = String::new();
let mut result = String::new();

for (i, line) in lines.iter().enumerate() {
let is_first_line = i == 0;
let is_last_line = i == lines.len() - 1;
let is_last_non_empty_line = i == last_non_empty_line;
for (i, line) in lines.iter().enumerate() {
let is_first_line = i == 0;
let is_last_line = i == lines.len() - 1;
let is_last_non_empty_line = i == last_non_empty_line;

// replace rendered whitespace tabs with spaces
let mut trimmed_line = line.replace("\t", " ");
// replace rendered whitespace tabs with spaces
let mut trimmed_line = line.replace("\t", " ");

// trim whitespace touching a newline
if !is_first_line {
trimmed_line = TRIM_START.replace(&trimmed_line, "").to_string();
}
// trim whitespace touching a newline
if !is_first_line {
trimmed_line = TRIM_START.replace(&trimmed_line, "").to_string();
}

// trim whitespace touching an endline
if !is_last_line {
trimmed_line = TRIM_END.replace(&trimmed_line, "").to_string();;
}
// trim whitespace touching an endline
if !is_last_line {
trimmed_line = TRIM_END.replace(&trimmed_line, "").to_string();
}

if !trimmed_line.is_empty() {
if !is_last_non_empty_line {
trimmed_line.push(' ');
}
if !trimmed_line.is_empty() {
if !is_last_non_empty_line {
trimmed_line.push(' ');
}

result.push_str(&trimmed_line);
result.push_str(&trimmed_line);
}
}
}

result
result
}

fn is_allowed_plural_option(key: &str) -> Option<JsWord> {
Expand Down Expand Up @@ -114,12 +113,14 @@ impl<'a> TransJSXVisitor<'a> {
tokens.push(MsgToken::String(string));
}

JSXAttrValue::JSXExprContainer(JSXExprContainer { expr: JSXExpr::Expr(exp), .. }) => {
JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(exp),
..
}) => {
match exp.as_ref() {
// some={"# books"}
Expr::Lit(Lit::Str(str)) => {
tokens.push(MsgToken::String(str.value.clone().to_string()))
}
Expr::Lit(Lit::Str(str)) => tokens
.push(MsgToken::String(str.value.clone().to_string())),
// some={`# books ${name}`}
Expr::Tpl(tpl) => {
tokens.extend(self.ctx.tokenize_tpl(tpl));
Expand All @@ -132,9 +133,7 @@ impl<'a> TransJSXVisitor<'a> {
tokens.extend(visitor.tokens)
}

_ => {
tokens.push(MsgToken::Expression(exp.clone()))
}
_ => tokens.push(MsgToken::Expression(exp.clone())),
}
}

Expand All @@ -143,11 +142,7 @@ impl<'a> TransJSXVisitor<'a> {
}
}

choices.push(CaseOrOffset::Case(
ChoiceCase {
tokens,
key,
}))
choices.push(CaseOrOffset::Case(ChoiceCase { tokens, key }))
}
}
}
Expand All @@ -174,21 +169,18 @@ impl<'a> Visit for TransJSXVisitor<'a> {

if self.ctx.is_lingui_jsx_choice_cmp(&ident) {
let value = match get_jsx_attr(&el, "value").and_then(|attr| attr.value.as_ref()) {
Some(
JSXAttrValue::JSXExprContainer(
JSXExprContainer { expr: JSXExpr::Expr(exp), .. }
)
) => {
exp.clone()
}
_ => {
Box::new(Expr::Lit(Lit::Null(Null {
span: DUMMY_SP
})))
}
Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(exp),
..
})) => exp.clone(),
_ => Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
};

let icu_method = self.ctx.get_ident_export_name(ident).unwrap().to_lowercase();
let icu_method = self
.ctx
.get_ident_export_name(ident)
.unwrap()
.to_lowercase();
let choices = self.visit_icu_macro(el, &icu_method);

self.tokens.push(MsgToken::IcuChoice(IcuChoice {
Expand All @@ -214,25 +206,21 @@ impl<'a> Visit for TransJSXVisitor<'a> {
}

fn visit_jsx_closing_element(&mut self, _el: &JSXClosingElement) {
self.tokens.push(
MsgToken::TagClosing
);
self.tokens.push(MsgToken::TagClosing);
}

fn visit_jsx_text(&mut self, el: &JSXText) {

self.tokens.push(
MsgToken::String(clean_jsx_element_literal_child(&el.raw.to_string()))
);
self.tokens
.push(MsgToken::String(clean_jsx_element_literal_child(
&el.raw.to_string(),
)));
}

fn visit_jsx_expr_container(&mut self, cont: &JSXExprContainer) {
if let JSXExpr::Expr(exp) = &cont.expr {
match exp.as_ref() {
Expr::Lit(Lit::Str(str)) => {
self.tokens.push(
MsgToken::String(str.value.to_string())
);
self.tokens.push(MsgToken::String(str.value.to_string()));
}

// todo write tests and validate
Expand All @@ -241,9 +229,7 @@ impl<'a> Visit for TransJSXVisitor<'a> {
if let Some(tokens) = self.ctx.try_tokenize_call_expr_as_choice_cmp(call) {
self.tokens.extend(tokens);
} else {
self.tokens.push(
MsgToken::Expression(exp.clone())
);
self.tokens.push(MsgToken::Expression(exp.clone()));
}
}

Expand All @@ -252,18 +238,12 @@ impl<'a> Visit for TransJSXVisitor<'a> {
}

Expr::Tpl(tpl) => {
self.tokens.extend(
self.ctx.tokenize_tpl(tpl)
);
self.tokens.extend(self.ctx.tokenize_tpl(tpl));
}
_ => {
self.tokens.push(
MsgToken::Expression(exp.clone())
);
self.tokens.push(MsgToken::Expression(exp.clone()));
}
}
}
}
}


2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(is_some_and)]

use std::collections::HashSet;
use swc_core::common::DUMMY_SP;

Expand Down
Loading

0 comments on commit 34e259d

Please sign in to comment.