• Stars
    star
    295
  • Rank 140,902 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Babel Plugin that adds useful build time console functions 🎮

babel-plugin-console

🎮

Adds useful build time console functions


Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples Babel Macro

Problem

Ever find yourself using console.log to work out what the heck is going on within your functions? And then put too many variables into the log and lose context of which value is which variable?

Solution

These problems can be solved by a computer at build time. This plugin allows you to inspect a functions parameters, variables, return value and parent scope by adding meaningful logs around the scope of the function - removing the hassle of manual console.log statements or debugging.

Installation

With npm:

npm install --save-dev babel-plugin-console

With yarn:

yarn add -D babel-plugin-console

Setup

.babelrc

{
  "plugins": ["console"]
}

CLI

babel --plugins console script.js

Node

require('babel-core').transform('code', {
  plugins: ['console'],
})

APIs

console.scope()

Outputs a collection of messages for a functions entire scope

Note: this API is the same as the standard console.log

Syntax

console.scope(obj1 [, obj2, ..., objN]);
console.scope(msg [, subst1, ..., substN]);

Parameters

  • obj1 ... objN A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

  • msg A JavaScript string containing zero or more substitution strings.

  • subst1 ... substN JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.

Usage

Plugin

const add100 = (a) => {
  const oneHundred = 100;
  console.scope('Add 100 to another number');
  return add(a, oneHundred);
};

const add = (a, b) => {
  return a + b;
};

           

Browser:

Browser console scoping add100

Node:

Node console scoping add100

Macros

babel-plugin-console also ships with babel-macros supported 🎉

To use a macro you will need to install and setup babel-macros.

Once you have enabled babel-macros import/require the scope macro with:

import scope from 'babel-plugin-console/scope.macro';
// OR
const scope = require('babel-plugin-console/scope.macro');

Example

Note: this is the same as the above usage and will generate the same logs - only difference is it uses the scope macro 😎

           
import scope from 'babel-plugin-console/scope.macro';

const add100 = (a) => {
  const oneHundred = 100;
  scope('Add 100 to another number');
  return add(a, oneHundred);
};

const add = (a, b) => {
  return a + b;
};

NOTE: There's also a separate package available called scope.macro which you can install and use instead if you prefer to type less.

Inspiration

This tweet led to me watching @kentcdodds's live presentation on ASTs. This plugin is an extension on the captains-log demoed - Thanks Kent!

Other Solutions

You could use a debugger with breakpoints to pause the execution of a function and inspect the values of each variable.

The above add100 function would look like the following if you used a debugger with a breakpoint:

Debugging add100

Contributors


Matt Phillips

💻 📖 🚇 ⚠️

Stephen Bluck

📖

LICENSE

MIT

More Repositories

1

deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
JavaScript
1,006
star
2

jest-expect-message

Add custom message to Jest expects 🃏🗯
JavaScript
357
star
3

jest-chain

Chain Jest matchers together to create one powerful assertion 🃏⛓
JavaScript
274
star
4

babel-jest-assertions

Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️
JavaScript
96
star
5

jest-each

A parameterised testing library for Jest. https://www.npmjs.com/package/jest-each 🏃
JavaScript
92
star
6

babel-plugin-gwt

Data Driven Testing babel plugin inspired by Groovy's Spock framework 🖖
JavaScript
14
star
7

react-point-break

React CSS media queries with breakpoint render props Component and Provider. 🔫
JavaScript
7
star
8

oss-box

Open source project boilerplate generator 📦
JavaScript
4
star
9

ts-prelude

TypeScript
3
star
10

io-ts-graphql-codegen

TypeScript
2
star
11

flow-data-structures

Basic data structures implements using Flowtype
JavaScript
2
star
12

redux-identity

A simple redux reducer that returns the given state
JavaScript
1
star
13

jest-workspace-projects

JavaScript
1
star
14

trait-contract

Utility to build a Trait (Interface) for a given Type and supply an implementation which is validated against the Trait contract
JavaScript
1
star
15

eslint-plugin-jest-extended

ESLint plugin for jest-extended matchers
JavaScript
1
star
16

data-structures

Groovy
1
star
17

reason-refs

C++
1
star
18

es6-prototype-boilerplate

Boilerplate setup for quick prototyping with es6 using budo to build and mocha for testing.
JavaScript
1
star
19

react-native-counter

JavaScript
1
star