• Stars
    star
    261
  • Rank 156,630 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 8 years ago

Reviews

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

Repository Details

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

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.

More Repositories

1

fast.js

Faster user-land reimplementations for several common builtin native JavaScript functions.
JavaScript
3,412
star
2

ts-sql

A SQL database implemented purely in TypeScript type annotations.
TypeScript
3,181
star
3

babel-plugin-typecheck

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

deprank

Use PageRank to find the most important files in your codebase.
TypeScript
878
star
5

yii2-localeurls

Automatic locale/language management for URLs
PHP
412
star
6

babel-plugin-closure-elimination

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

babel-plugin-contracts

Design by Contract for JavaScript via a Babel plugin.
JavaScript
266
star
8

oriento

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

htmling

Polymer / HTML5 templating syntax for node.js
JavaScript
177
star
10

yii2-dockerized

A template for docker based Yii 2 applications
PHP
169
star
11

yii2-excelexport

A utility to quickly create Excel files from query results or raw data
PHP
102
star
12

gitignore-parser

A simple .gitignore parser for node.js
JavaScript
97
star
13

contractual

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

babel-plugin-trace

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

yii2-configloader

Build configuration arrays from config files and env vars.
PHP
61
star
16

yii2-streamlog

A Yii 2 log target for streams in URL format
PHP
52
star
17

yii2-dockerbase

Yii 2 base image for dockerized yii2 projects
Shell
39
star
18

YiiElasticSearch

Elastic Search client for Yii
PHP
33
star
19

malloc

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

reign

A persistent, typed objects implementation for node.js and the browser.
JavaScript
23
star
21

binary-protocol

Easy, fast, writers and readers for implementing custom binary protocols in node.js.
JavaScript
20
star
22

oauth2yii

An OAuth2 client / server extension for the Yii framework
PHP
17
star
23

modeling

Fast and flexible data models for node.js and the browser.
JavaScript
15
star
24

restyii

A RESTful extension for Yii.
PHP
15
star
25

babel-plugin-hyperhtml

Babel plugin which compiles JSX into hyperHTML
JavaScript
12
star
26

yii2-excel-message

Translate messages via Excel files
PHP
12
star
27

backing

Provides a virtual address space for large segments of memory via JavaScript ArrayBuffers, and operations for allocating and freeing within the address space, optionally via a simple reference counting garbage collector.
JavaScript
11
star
28

validating

Quick and easy validators for node.js and the browser.
JavaScript
10
star
29

babel-plugin-conditional

Conditionally applies a set of babel plugins based on the result of an expression evaluated at runtime.
JavaScript
10
star
30

yii2-bs3activeform

A Bootstrap 3 enhanced ActiveForm for Yii 2
PHP
9
star
31

url-route

Web component providing URL routing
JavaScript
9
star
32

htmling-demo-app

HTMLing demo running on express
CSS
8
star
33

garbage-collector

A garbage collector for JavaScript built on top of typed arrays.
JavaScript
8
star
34

geonames-importer

Imports geonames data into elasticsearch
JavaScript
7
star
35

orientdb-protobufs

An experiment to see how the orientdb binary protocol could look if it used protocol buffers.
Java
6
star
36

handlebarsphp

Transpiles handlebars templates into native PHP templates
PHP
6
star
37

atomicbuffers

Atomic `readInt32()`, `writeInt32()`, `readUInt32()` and `writeUInt32()` for node.js buffers.
JavaScript
6
star
38

classing

Fluent classes for node.js and the browser.
JavaScript
6
star
39

dispatching

Tiny routing / dispatch library for node and the browser.
JavaScript
5
star
40

casting

Tiny type casting library for node.js and the browser.
JavaScript
5
star
41

php-orientdb

A fast PHP driver for the OrientDB binary protocol.
PHP
5
star
42

obligations

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

AccessRestrictable

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.
PHP
2
star
44

component-testing-library

A library for testing component driven UIs
TypeScript
2
star
45

bootstrap-css

Twitter Bootstrap CSS / LESS packaged for component.js instead of bower
CSS
2
star
46

miming

Processing and formatting for various mime types.
JavaScript
2
star
47

bs3activeform

A lightweight utility to render Bootstrap 3 forms in Yii
PHP
2
star
48

handlebarsgen

An extendable static code generator for handlebars templates, targetting languages other than JavaScript, e.g. PHP
CoffeeScript
2
star
49

malloc-append

Simple append-only alloc() implementation on top of buffers and array buffers.
JavaScript
1
star
50

jsx-email-nextjs

Reproduce an error with renderToStaticMarkup() in Next.js
TypeScript
1
star
51

bootstrap-tooltip

Twitter Bootstrap Tooltip plugin packaged for component.js instead of bower
JavaScript
1
star
52

bencha

Mocha-esque UI for the excellent benchmarkjs benchmarking library
CoffeeScript
1
star
53

bootstrap-transition

Twitter Bootstrap Transition plugin packaged for component.js instead of bower
JavaScript
1
star
54

urlrouter

Tiny URL routing for the browser
CoffeeScript
1
star
55

bootstrap-affix

Twitter Bootstrap Affix plugin packaged for component.js instead of bower
JavaScript
1
star
56

bootstrap-scrollspy

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