Hygienic, non-syntactic macros for JavaScript via a Babel plugin.
JavaScript
260
77 commits
updated Mar 12, 2016
This is a Babel plugin adds support for hygienic, non-syntactic macros for JavaScript.
Note: Now requires Babel 6.
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
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().
DEFINE_TRANSFORM which is similar to DEFINE_MACRO but allows direct AST manipulation, not merely replacement.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"]
}
traverse to get to find the desired nodethis and arguments in macrosreturn & return without valuePublished by codemix under a permissive MIT License, see LICENSE.md.
JavaScript
100.0%
Hygienic, non-syntactic macros for JavaScript via a Babel plugin.
JavaScript
260
77 commits
updated Mar 12, 2016
This is a Babel plugin adds support for hygienic, non-syntactic macros for JavaScript.
Note: Now requires Babel 6.
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
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().
DEFINE_TRANSFORM which is similar to DEFINE_MACRO but allows direct AST manipulation, not merely replacement.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"]
}
traverse to get to find the desired nodethis and arguments in macrosreturn & return without valuePublished by codemix under a permissive MIT License, see LICENSE.md.
JavaScript
100.0%