Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gha #100

Merged
merged 1 commit into from
Aug 6, 2024
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
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
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,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
Loading