Skip to content

Log an expression and the result of that expression to the console

Notifications You must be signed in to change notification settings

bgschiller/inspect.macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

inspect.macro

Log an expression and the result of that expression to the console

This is a project I put together to learn more about macros and babel plugins. Also, it scratches an itch of mine. Do you ever find yourself writing

console.log('string description', <complicated expression>);

And then you look at the output, and can't remember precisely what the expression was. You start second-guessing yourself: maybe you did swap the order of the parameters?

So you tab back and forth, trying to compare.

Instead, you can write

import inspect from 'inspect.macro';

inspect(complicatedExpression(involving.many(parts * and * values)));

and your code will be transformed into

console.log(
  'complicatedExpression(involving.many(parts * and * values)) →',
  (function () {
    try {
      return complicatedExpression(involving.many(parts * and * values))
    } catch (e) {
      return 'an error occurred: ' + e;
    }
  }()));

Setup

You'll need to set up babel-plugin-macros. After that, all that's needed is to npm install inspect.macro and import and use it!

See also

  • This is made possible by babel-plugin-macros
  • babel-plugin-console provides console.scope function that logs everything in the current scope
  • Elixir has a function that does this, which was my inspiration.

About

Log an expression and the result of that expression to the console

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published