babel-plugin-macros 184

Hygienic, non-syntactic macros for JavaScript via a Babel plugin.

codemix/babel-plugin-macros

Written in JavaScript by 2 contributors

Contribute

Need Support?


we offer maintenance, support and development services for all our open source projects.

Contact Us

Babel Macros

This is a Babel plugin adds support for hygienic, non-syntactic macros for JavaScript.

Build Status

NPM

Note: Now requires Babel 6.

What?

Turns code like this:

DEFINE_MACRO(MAP, (input, visitor) => {
  const length = input.length;
  const result = new Array(length);
  for (let i = 0; i < length; i++) {
    result[i] = visitor(input[i], i, input);
  }
  return result;
});

function demo () {
  return MAP([1,2,3,4], item => item + 1);
}

Into code like this:

function demo() {
  var _input = [1, 2, 3, 4];
  var _map = undefined;

  var _length = _input.length;
  var _result = new Array(_length);
  for (var _i2 = 0; _i2 < _length; _i2++) {
    _result[_i2] = _input[_i2] + 1;
  }
  _map = _result;

  return _map;
}

Also you may use more native syntax to define macro

macro: function MACRO() {
  console.log('MACRO called');
}
MACRO();

Macro calls can also be chained, so if you declare a new macro called, e.g. FILTER:

DEFINE_MACRO(FILTER, (input, visitor) => {
  const filtered = [];
  for (let i = 0; i < input.length; i++) {
    if (visitor(input[i], i, input)) {
      filtered.push(input[i]);
    }
  }
  return filtered;
});

You'll then be able to combine the two macros in one statement, without needing to nest:

MAP([1, 2, 3], item => item + 1).FILTER(item => item < 4).length; // 2

Why?

Because macros are incredibly useful! Also because they make it easy to write extremely fast code without sacrificing readability. When compiled with closure elimination the above code is 10x faster than native Array.prototype.map().

Note: This is super experimental, use at your own risk!

Todo List

  • [ ] Allow macros in macro definitions (currently causes infinite loop)
  • [ ] Allow macros to be imported and exported across files.
  • [ ] Add DEFINE_TRANSFORM which is similar to DEFINE_MACRO but allows direct AST manipulation, not merely replacement.

Installation

First, install via npm.

npm install --save-dev babel-plugin-macros

Then, in your babel configuration (usually in your .babelrc file), add "macros" to your list of plugins:

{
  "plugins": ["macros"]
}

ChangeLog

  • 0.0.1 base implementation
  • 1.0.0 update for babel@6 API
  • 1.0.1 fix npm package
  • 1.0.2 fix crash when missed some arguments
  • 1.0.3 fix behavior of same-name macros in different scopes.
    before this change same-name macros are re-declared.
    now macros in different scopes - are different macros.
  • 1.0.4 fix wrong scoping for equal-named macros
    fix combining more than 2 macros
  • 1.0.5 optimization. Replacement traverse to get to find the desired node
  • 1.0.6 honest exception for infinite recursion in macros
  • 1.0.7 add checking types in runtime
  • 1.0.8 block using this and arguments in macros
  • 1.0.9 fix multiple return & return without value
  • 1.0.10 optimize performance of compile. x10 at tests
  • 1.0.11 depedency for lodash no more needed
  • 1.0.12 new syntax for define macro, using label
  • 1.0.13 Implement function inlining for macro arguments
  • 1.0.14 fix for different inlined function types in macro-argument
  • 1.0.15 fix crash for case if babel-core use same babel-traverse, but in own sub-directory (without npm dedupe)

License

Published by codemix under a permissive MIT License, see LICENSE.md.





44 other JavaScript projects


fast.jsJavaScript 2930

Faster user-land reimplementations for several common builtin native JavaScript functions.

babel-plugin-typecheckJavaScript 823

Static and runtime type checking for JavaScript in the form of a Babel plugin.

babel-plugin-closure-eliminationJavaScript 223

A Babel plugin which eliminates closures from your JavaScript wherever possible.

orientoJavaScript 212

Former official node.js driver for OrientDB. Fast, lightweight, uses the binary protocol. Now deprecated.

htmlingJavaScript 167

Polymer / HTML5 templating syntax for node.js

babel-plugin-contractsJavaScript 128

Design by Contract for JavaScript via a Babel plugin.

YiiBlocksJavaScript 70

A set of building blocks that provide common functions for Yii web applications

contractualJavaScript 52

Unobtrusive, backwards compatible, syntactic sugar for Design by contract in JavaScript.

gitignore-parserJavaScript 34

A simple .gitignore parser for node.js

modelingJavaScript 14

Fast and flexible data models for node.js and the browser.

binary-protocolJavaScript 13

Easy, fast, writers and readers for implementing custom binary protocols in node.js.

YiiJSJavaScript 11

A port of the Yii PHP framework to JavaScript

url-routeJavaScript 10

Web component providing URL routing

validatingJavaScript 10

Quick and easy validators for node.js and the browser.

babel-plugin-traceJavaScript 6

This is a Babel plugin which adds a straightforward, declarative syntax for adding debug logging to JavaScript applications.

mallocJavaScript 6

Simple malloc() & free() implementation for node.js, built on top of array buffers.

geonames-importerJavaScript 6

Imports geonames data into elasticsearch

dispatchingJavaScript 5

Tiny routing / dispatch library for node and the browser.

atomicbuffersJavaScript 5

Atomic `readInt32()`, `writeInt32()`, `readUInt32()` and `writeUInt32()` for node.js buffers.

castingJavaScript 5

Tiny type casting library for node.js and the browser.

classingJavaScript 5

Fluent classes for node.js and the browser.

mimingJavaScript 3

Processing and formatting for various mime types.

bootstrap-tooltipJavaScript 1

Twitter Bootstrap Tooltip plugin packaged for component.js instead of bower

oriento-query-builderJavaScript 1

A standalone query builder for OrientDB, designed for node.js/io.js and the browser.

bootstrap-transitionJavaScript 1

Twitter Bootstrap Transition plugin packaged for component.js instead of bower

obligationsJavaScript 1

Tiny JavaScript library for preconditions and postconditions, intended for use with Contractual.

cover.jsJavaScript

JQuery plugin for covering/uncovering DOM elements

bootstrap-popoverJavaScript

Twitter Bootstrap Popover plugin packaged for component.js instead of bower

bootstrap-scrollspyJavaScript

Twitter Bootstrap Scrollspy plugin packaged for component.js instead of bower

bootstrap-dropdownJavaScript

Twitter Bootstrap Dropdown plugin packaged for component.js instead of bower

bootstrap-tabJavaScript

Twitter Bootstrap Tab plugin packaged for component.js instead of bower

bootstrap-carouselJavaScript

Twitter Bootstrap Carousel plugin packaged for component.js instead of bower

bootstrap-buttonJavaScript

Twitter Bootstrap Button plugin packaged for component.js instead of bower

webrtc-playgroundJavaScript

WebRTC sample code. For testing purposes only.

flex-basicJavaScript

A basic Flex App setup for video broadcasting

bootstrap-modalJavaScript

Twitter Bootstrap Modal plugin packaged for component.js instead of bower

bootstrap-alertJavaScript

Twitter Bootstrap Alert plugin packaged for component.js instead of bower

bootstrap-affixJavaScript

Twitter Bootstrap Affix plugin packaged for component.js instead of bower

ProductManagerJavaScript

A Sample CRUD application structured by Backbone js and backed by a node restfull api.

bootstrap-collapseJavaScript

Twitter Bootstrap Collapse plugin packaged for component.js instead of bower

bootstrap-componentJavaScript

Twitter Bootstrap packaged for component.js instead of bower

bootstrap-typeaheadJavaScript

Twitter Bootstrap Typeahead plugin packaged for component.js instead of bower

elevatorJavaScript

http://samsonradu.github.io/elevator/

tabinetJavaScript

A popular romanian card game