Skip to content

Commit

Permalink
atom instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jul 21, 2024
1 parent fce8655 commit e35fe68
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/yak-swc/yak_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use rustc_hash::FxHashMap;
use serde::Deserialize;
use std::path::Path;
use std::vec;
use swc_core::atoms::{atom, Atom};
use swc_core::common::comments::Comment;
use swc_core::common::comments::Comments;
use swc_core::common::{Spanned, DUMMY_SP};
use swc_core::ecma::atoms::hstr::{atom, Atom};
use swc_core::ecma::visit::VisitMutWith;
use swc_core::ecma::{
ast::*,
Expand Down Expand Up @@ -283,7 +283,7 @@ where
bin.right.visit_mut_with(self);
self.current_condition.pop();
}
Expr::Bin(bin @ BinExpr { op: op!("||"),.. }) => {
Expr::Bin(bin @ BinExpr { op: op!("||"), .. }) => {
bin.left.visit_mut_with(self);

// Push negated condition for right side
Expand Down
2 changes: 1 addition & 1 deletion packages/yak-swc/yak_swc/src/utils/assert_css_expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashSet;
use swc_core::atoms::Atom;
use swc_core::common::{Span, Spanned};
use swc_core::ecma::atoms::hstr::Atom;
use swc_core::ecma::visit::VisitMutWith;
use swc_core::ecma::{ast::*, visit::VisitMut};
use swc_core::plugin::errors::HANDLER;
Expand Down
27 changes: 12 additions & 15 deletions packages/yak-swc/yak_swc/src/utils/ast_helper.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use rustc_hash::FxHashMap;
use itertools::Itertools;
use rustc_hash::FxHashMap;

use swc_core::{
common::DUMMY_SP,
ecma::{ast::*, atoms::hstr::Atom},
plugin::errors::HANDLER,
};
use swc_core::atoms::Atom;
use swc_core::{common::DUMMY_SP, ecma::ast::*, plugin::errors::HANDLER};

/// Convert a HashMap to an Object expression
pub fn expr_hash_map_to_object(values: FxHashMap<String, Expr>) -> Expr {
Expand All @@ -31,18 +28,18 @@ pub fn expr_hash_map_to_object(values: FxHashMap<String, Expr>) -> Expr {

/// Convert a member expression to a root identifier and a string vector of properties
/// e.g. `foo.bar.baz` will return `(foo#0, ["foo", "bar", "baz"])`
pub fn member_expr_to_strings(member_expr: &MemberExpr) -> Option<(Ident, Vec<String>)> {
let mut props = vec![];
pub fn member_expr_to_strings(member_expr: &MemberExpr) -> Option<(Ident, Vec<Atom>)> {
let mut props: Vec<Atom> = vec![];
match member_expr.prop.clone() {
MemberProp::Ident(ident) => {
props.push(ident.sym.to_string());
props.push(ident.sym);
}
MemberProp::Computed(computed) => match &*computed.expr {
Expr::Lit(Lit::Str(str)) => {
props.push(str.value.to_string());
props.push(str.value.clone());
}
Expr::Lit(Lit::Num(num)) => {
props.push(num.value.to_string());
props.push(Atom::from(num.value.to_string()));
}
_ => return None,
},
Expand All @@ -51,14 +48,14 @@ pub fn member_expr_to_strings(member_expr: &MemberExpr) -> Option<(Ident, Vec<St
match *member_expr.obj.clone() {
Expr::Ident(ident) => {
let root_ident = ident;
props.insert(0, root_ident.sym.to_string());
props.insert(0, root_ident.sym.clone());
Some((root_ident.clone(), props))
}
Expr::Member(member) => {
let result = member_expr_to_strings(&member)?;
let (root_ident, mut nested_props) = result;
nested_props.extend(props);
nested_props.insert(0, root_ident.sym.to_string());
nested_props.insert(0, root_ident.sym.clone());
Some((root_ident, nested_props))
}
_ => None,
Expand Down Expand Up @@ -100,7 +97,7 @@ pub fn split_ident(ident: Atom) -> (String, String) {
/// There are two use cases:
/// 1. Member expressions (e.g., `colors.primary`) -> Some((colors#0, ["colors", "primary"]))
/// 2. Simple identifiers (e.g., `primaryColor`) -> Some((primaryColor#0, ["primaryColor"]))
pub fn extract_ident_and_parts(expr: &Expr) -> Option<(Ident, Vec<String>)> {
pub fn extract_ident_and_parts(expr: &Expr) -> Option<(Ident, Vec<Atom>)> {
match &expr {
Expr::Member(member) => member_expr_to_strings(member).or_else(|| {
HANDLER.with(|handler| {
Expand All @@ -110,7 +107,7 @@ pub fn extract_ident_and_parts(expr: &Expr) -> Option<(Ident, Vec<String>)> {
});
None
}),
Expr::Ident(ident) => Some((ident.clone(), vec![ident.sym.to_string()])),
Expr::Ident(ident) => Some((ident.clone(), vec![ident.sym.clone()])),
_ => None,
}
}
Expand Down
15 changes: 8 additions & 7 deletions packages/yak-swc/yak_swc/src/utils/encode_module_import.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use css_in_js_parser::{parse_css, ParserState};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use swc_core::atoms::Atom;

/// This function generates a special CSS selector that represents an import from another module,
/// encoding the module path and imported properties to ensure CSS parser compatibility
Expand All @@ -24,7 +25,7 @@ use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
/// - The function uses URL encoding for the import chain to handle special characters
/// - The resulting string is meant to be processed and resolved by the yak css loader
/// - The kind gives a hint how the import can be used - to provide an error message if the import is not supported
pub fn encode_module_import(module_path: &str, import_chain: Vec<String>) -> String {
pub fn encode_module_import(module_path: &str, import_chain: Vec<Atom>) -> String {
let encoded_chain = import_chain
.into_iter()
.map(|part| utf8_percent_encode(&part, NON_ALPHANUMERIC).to_string())
Expand Down Expand Up @@ -62,9 +63,9 @@ mod tests {
let selector = encode_module_import(
"./styles/media",
vec![
"breakpoints".to_string(),
"<xs".to_string(),
"min".to_string(),
Atom::from("breakpoints"),
Atom::from("<xs"),
Atom::from("min"),
],
);
assert_eq!(
Expand All @@ -75,7 +76,7 @@ mod tests {

#[test]
fn test_encode_module_import_single_item_chain() {
let selector = encode_module_import("./styles/media", vec!["breakpoints".to_string()]);
let selector = encode_module_import("./styles/media", vec![Atom::from("breakpoints")]);
assert_eq!(
selector,
"--yak-css-import: url(\"./styles/media:breakpoints\")"
Expand All @@ -86,7 +87,7 @@ mod tests {
fn test_encode_module_import_special_characters() {
let selector = encode_module_import(
"./styles/media",
vec!["breakpoints".to_string(), "xs".to_string()],
vec![Atom::from("breakpoints"), Atom::from("xs")],
);
assert_eq!(
selector,
Expand All @@ -98,7 +99,7 @@ mod tests {
fn test_encode_module_import_special_characters_encoded() {
let selector = encode_module_import(
"./styles/media",
vec!["breakpoints".to_string(), "<:\">".to_string()],
vec![Atom::from("breakpoints"), Atom::from("<:\">")],
);
assert_eq!(
selector,
Expand Down
3 changes: 2 additions & 1 deletion packages/yak-swc/yak_swc/src/yak_imports.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::atoms::Atom;
use swc_core::{
common::DUMMY_SP,
ecma::{ast::*, atoms::hstr::Atom, visit::VisitMut},
ecma::{ast::*, visit::VisitMut},
};

#[derive(Debug)]
Expand Down

0 comments on commit e35fe68

Please sign in to comment.