GithubHelp home page GithubHelp logo

macros's Introduction

macros

macros is an easy way to define a pre-processor chain for your source.

The macro processor is combined of steps:

  • Regex (string to string)
  • Token Transformations (token to token)
  • AST Transformations (valid JS AST to valid JS AST)
  • Prepend (valid JS str to valid JS str)
  • Append (valid JS str to valid JS str)
  • require (valid JS str to module.exports object)

You define a file extension and a suite of filters that accepts source and returns transformed source.

Examples:

All of the steps are demonstrated in the test file

Regex:

Don't you wish JavaScript had easy parse-time string substitution? ;) Caution: as this is just a straight string substitution, you will probably blow up your code.

macros.register(".cjs", {
  regex: [
    [/development.local/, "example.com"]
  ]
});

Token:

If you want to twiddle your tokens, use this handy token-substitution device.

Example: decafscript is javascript where "f" is the same as "function". You can do it with a simple token transformation. all files with the ".ds" extension will treat "f" as the "function" keyword using this macro:

macros.register(".ds",{
  token: function(tok){
    if(tok.type == "name" && tok.value == "f"){
      tok.type = "keyword";
      tok.value = "function";
    }
    return tok;
  }
});

AST

If you want to do crazy transformations on your source tree, then you can modify the syntax tree directly. macros uses a fork of Uglify-JS, but the AST is the same.

Here's an example with substack's handy burrito! It will wrap all function calls with another function call :D

burrito = require("burrito");

macros.register(".trace", {
  ast: function(ast){
    var str = burrito(ast, function(node){
      if(node.name === 'call') node.wrap('wrapper(%s)'); 
    });
    return burrito.parse(str);
  }
});

macros's People

Contributors

aaronblohowiak avatar agnoster avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.