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

Fixes console_dbg! and console! expression output. Bold src info. #174

Merged
merged 2 commits into from
Nov 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 25 additions & 13 deletions crates/console/src/console_dbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
#[macro_export]
macro_rules! console {
() => {
$crate::log!(::std::format!("[{}:{}]", ::std::file!(), ::std::line!()));
$crate::log!(
::std::format!("%c[{}:{}] ", ::std::file!(), ::std::line!()),
"font-weight: bold"
);
};
($val:expr $(,)?) => {
// Use of `match` here is intentional because it affects the lifetimes
// of temporaries - https://stackoverflow.com/a/48732525/1063961
match $val {
tmp => {
$crate::log!(::std::format!("[{}:{}] {} =", ::std::file!(), ::std::line!(), ::std::stringify!($val)), &tmp);
tmp
}
Comment on lines -12 to -18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer necessary, existed for two reasons:

  1. expression called twice
  2. align lifetimes of multiple expressions

1 won't happen anymore because now both macros only run the expression once and bind them to a variable.
2 is not necessary. It's only needed in cases like assert_eq.

{
let v = $val;
$crate::__console_inner!(v $val)
}
};
($($val:expr),+ $(,)?) => {
Expand All @@ -34,18 +33,31 @@ macro_rules! console_dbg {
$crate::console!()
};
($val:expr $(,)?) => {
match $val {
v => {
$crate::console!(::std::format!("{:?}", v));
v
}
{
let v: $crate::__macro::JsValue = ::std::format!("{:?}", $val).into();
$crate::__console_inner!(v $val)
}
};
($($val:expr),+ $(,)?) => {
($($crate::console_dbg!($val)),+,)
};
}

/// This is an implementation detail and *should not* be called directly!
#[doc(hidden)]
#[macro_export]
macro_rules! __console_inner {
($js_value:ident $val:expr) => {{
$crate::log!(
::std::format!("%c[{}:{}] ", ::std::file!(), ::std::line!()),
"font-weight: bold",
::std::format!("{} = ", ::std::stringify!($val)),
&$js_value
);
$js_value
}};
}

#[cfg(test)]
mod tests {
#![allow(dead_code)]
Expand Down