first-approach implementation of nesting selectors

This commit is contained in:
Yiffy Dev 2024-10-30 16:33:50 +05:00 • committed by spushk
commit d488f1a887
Signed by: spushk
GPG key ID: 7797B761E105E8AA
4 changed files with 40 additions and 7 deletions

View file

@ -1003,7 +1003,7 @@ dependencies = [
[[package]] [[package]]
name = "sabry_build" name = "sabry_build"
version = "0.0.0" version = "0.0.1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"sabry_intrnl", "sabry_intrnl",
@ -1015,7 +1015,7 @@ dependencies = [
[[package]] [[package]]
name = "sabry_intrnl" name = "sabry_intrnl"
version = "0.0.0" version = "0.0.1"
dependencies = [ dependencies = [
"base64", "base64",
"blake3", "blake3",
@ -1033,14 +1033,14 @@ dependencies = [
[[package]] [[package]]
name = "sabry_procmacro" name = "sabry_procmacro"
version = "0.0.0" version = "0.0.1"
dependencies = [ dependencies = [
"sabry_procmacro_impl", "sabry_procmacro_impl",
] ]
[[package]] [[package]]
name = "sabry_procmacro_impl" name = "sabry_procmacro_impl"
version = "0.0.0" version = "0.0.1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -5,6 +5,9 @@ styly!(pub const bundlemod {
.someth { .someth {
@include tokens.clickable; @include tokens.clickable;
background-color: black; background-color: black;
&-dark {
background-color: white;
}
} }
}); });
@ -13,5 +16,6 @@ styly!(pub const filemod:sass "src/main.sass");
fn main(){ fn main(){
println!("someth class codemod styles: {}", bundlemod::someth); println!("someth class codemod styles: {}", bundlemod::someth);
println!("someth class filemod styles: {}", filemod::someth); println!("someth class filemod styles: {}", filemod::someth);
println!("someth-dark selector will be: {}", bundlemod::_dark(bundlemod::someth));
println!("Thats how we used the styles! Good luck!!"); println!("Thats how we used the styles! Good luck!!");
} }

View file

@ -294,7 +294,7 @@ impl ScopedSelector {
} }
} }
fn apply_basic_rusty_member_gen_rules(source: &str) -> String { pub fn apply_basic_rusty_member_gen_rules(source: &str) -> String {
let omit_regex = Regex::new(r"(^\-)|(\-$)|[^a-zA-Z0-9\-\_]") let omit_regex = Regex::new(r"(^\-)|(\-$)|[^a-zA-Z0-9\-\_]")
.expect("BUG: can not build omition regex for rusty member generation"); .expect("BUG: can not build omition regex for rusty member generation");

View file

@ -3,7 +3,7 @@ use quote::quote;
use sabry_intrnl::{ use sabry_intrnl::{
compiler::CompilerAdapter, compiler::CompilerAdapter,
config::SabryConfig, config::SabryConfig,
scoper::{ArbitraryScope, ScopedSelector}, scoper::{apply_basic_rusty_member_gen_rules, ArbitraryScope, ScopedSelector},
}; };
use syn::{parse::Parse, Ident, Token}; use syn::{parse::Parse, Ident, Token};
@ -67,7 +67,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
match ms.generator { match ms.generator {
ScopeGenerator::Module { public, constant } => { ScopeGenerator::Module { public, constant } => {
let scope_hash = scope.hash.as_str(); let scope_hash = scope.hash.as_str();
let scope_ident = scope.original_scope.name; let scope_ident = scope.original_scope.name.clone();
let scope_wrapper_ident = let scope_wrapper_ident =
syn::parse_str::<Ident>(&scope_ident.to_string().to_uppercase()) syn::parse_str::<Ident>(&scope_ident.to_string().to_uppercase())
.expect("BUG: We just converted valid Ident to string and uppercased"); .expect("BUG: We just converted valid Ident to string and uppercased");
@ -77,6 +77,33 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
quote! {} quote! {}
}; };
let nests = scope.original_scope.adapter().nesting_selectors();
let special_nesting_members = nests
.iter()
.filter_map(|ns| {
ns.suffix.clone().and_then(|s| {
let interp = s
.as_sass_interpolated()
.and_then(|i| i.elements.first())
.and_then(|e| e.as_static())
.and_then(|s| Some(s.raw.to_string()));
interp
})
})
.map(|s| {
let ident = apply_basic_rusty_member_gen_rules(&s);
let fnident = syn::parse_str::<Ident>(format!("_{ident}").as_str())
.expect("BUG: invalid ident for nesting selector formed");
let formatstr = format!("{{c}}{}", &s);
let doc = format!("Nesting selector. Originated from '&{s}'");
quote! {
#[doc = #doc]
pub fn #fnident(c: &str) -> String {format!(#formatstr)}
}
});
let scope_members = scope let scope_members = scope
.hashed_selectors .hashed_selectors
.iter() .iter()
@ -133,6 +160,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
#[doc = #mod_docs] #[doc = #mod_docs]
#scope_vis mod #scope_ident { #scope_vis mod #scope_ident {
#(#scope_members)* #(#scope_members)*
#(#special_nesting_members)*
} }
} }
} else { } else {
@ -142,6 +170,7 @@ pub fn styly_macro_impl(input: TokenStream) -> TokenStream {
#[doc = #mod_docs] #[doc = #mod_docs]
#scope_vis mod #scope_ident { #scope_vis mod #scope_ident {
#(#scope_members)* #(#scope_members)*
#(#special_nesting_members)*
} }
} }
} }