From 6588f57281bd0c743b669c39b5324f1af7476a67 Mon Sep 17 00:00:00 2001 From: Yiffy Dev Date: Sat, 2 Nov 2024 09:56:15 +0500 Subject: [PATCH] feat!: use macro call name for scss module name in usey --- sabry_procmacro/tests/macros.rs | 84 +++++++++++++++++++++++++ sabry_procmacro/tests/samicro.sass | 2 + sabry_procmacro/tests/scmicro.scss | 3 + sabry_procmacro_impl/src/impls/sassy.rs | 2 - sabry_procmacro_impl/src/impls/usey.rs | 8 ++- 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 sabry_procmacro/tests/macros.rs create mode 100644 sabry_procmacro/tests/samicro.sass create mode 100644 sabry_procmacro/tests/scmicro.scss diff --git a/sabry_procmacro/tests/macros.rs b/sabry_procmacro/tests/macros.rs new file mode 100644 index 0000000..22bd4a3 --- /dev/null +++ b/sabry_procmacro/tests/macros.rs @@ -0,0 +1,84 @@ +use std::{collections::HashSet, fs}; + +use sabry_procmacro::{sassy, usey}; + +sassy!(scmod:scss {" + @mixin abc(){ + color: red; + } + .sel1{ + @include abc; + } +"}); +sassy!(samod:sass {" + @mixin abc() + color: green + .sel1 + @include abc +"}); + +sassy!(scmicromod:scss "sabry_procmacro/tests/scmicro.scss"); +sassy!(samicromod:sass "sabry_procmacro/tests/samicro.sass"); + +#[test] +fn sassy_contract() { + assert_eq!(scmod!(syntax), "scss"); + assert_eq!( + scmod!(), + " +@mixin abc(){ + color: red; +} +.sel1{ + @include abc; +} +" + ); + + assert_eq!(scmicromod!(syntax), "scss"); + assert_eq!( + scmicromod!(), + fs::read_to_string("tests/scmicro.scss").unwrap() + ); + + assert_eq!(samod!(syntax), "sass"); + assert_eq!( + samod!(), + " +@mixin abc() + color: green +.sel1 + @include abc +" + ); + + assert_eq!(samicromod!(syntax), "sass"); + assert_eq!( + samicromod!(), + fs::read_to_string("tests/samicro.sass").unwrap() + ); +} + +#[test] +fn usey_contract() { + use samicromod as aliasedmod; + + let _usey = usey!(aliasedmod!(), scmicromod!()); + let expect_modules = + HashSet::from(["aliasedmod.sass".to_string(), "scmicromod.scss".to_string()]); + let expect_codes = HashSet::from([ + fs::read_to_string("tests/samicro.sass").unwrap(), + fs::read_to_string("tests/scmicro.scss").unwrap(), + ]); + + let mut real_modules = HashSet::new(); + let mut real_codes = HashSet::new(); + + _usey.iter().for_each(|(module, code)| { + real_modules.insert(module.clone()); + real_codes.insert(code.clone()); + }); + + assert_eq!(expect_modules, real_modules); + assert_eq!(expect_codes, real_codes) +} diff --git a/sabry_procmacro/tests/samicro.sass b/sabry_procmacro/tests/samicro.sass new file mode 100644 index 0000000..7bc961b --- /dev/null +++ b/sabry_procmacro/tests/samicro.sass @@ -0,0 +1,2 @@ +.sel1 + color: red diff --git a/sabry_procmacro/tests/scmicro.scss b/sabry_procmacro/tests/scmicro.scss new file mode 100644 index 0000000..07d5482 --- /dev/null +++ b/sabry_procmacro/tests/scmicro.scss @@ -0,0 +1,3 @@ +.sel1 { + color: red; +} diff --git a/sabry_procmacro_impl/src/impls/sassy.rs b/sabry_procmacro_impl/src/impls/sassy.rs index c15c303..ef5d401 100644 --- a/sabry_procmacro_impl/src/impls/sassy.rs +++ b/sabry_procmacro_impl/src/impls/sassy.rs @@ -28,7 +28,6 @@ pub fn sassy_macro_impl(input: TokenStream) -> TokenStream { } let sourcesass = code.to_string(); - let identname = ident.to_string(); let macro_doc = format!("Arbitrary {:?} code declared with `sassy!`. Pretty usable in tandem with `usey!` and `magic()` at build time", mode); quote! { @@ -36,7 +35,6 @@ pub fn sassy_macro_impl(input: TokenStream) -> TokenStream { #[macro_export] macro_rules! #ident { () => {#sourcesass}; - (module) => {#identname}; (syntax) => {#mode}; } } diff --git a/sabry_procmacro_impl/src/impls/usey.rs b/sabry_procmacro_impl/src/impls/usey.rs index 730bb4e..caf31ef 100644 --- a/sabry_procmacro_impl/src/impls/usey.rs +++ b/sabry_procmacro_impl/src/impls/usey.rs @@ -34,7 +34,11 @@ impl StyleCall { let call_bang = &self.call.bang_token; // the following calls are expected by macro genereted by `sassy!` let call_syntax = quote! {#call_path #call_bang (syntax)}; - let call_file = quote! {#call_path #call_bang (module)}; + + let module_name = call_path + .get_ident() + .expect("BUG: failed to get identifier for macro call") + .to_string(); if self.vis_pub { quote! { @@ -42,7 +46,7 @@ impl StyleCall { } } else { quote! { - (format!("{}.{}", #call_file, #call_syntax), #call_code .to_string()) + (format!("{}.{}", #module_name, #call_syntax), #call_code .to_string()) } } }