Skip to content

asterjs/aster-equery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aster-equery

NPM version Build Status

Replace nodes with pattern-matching selectors in aster.

Allows to use pattern-matching (JavaScript code examples with wildcards and some other special syntax) for finding nodes and replacing them with results of corresponding handlers.

Uses grasp-equery behind the scenes, so check out official documentation for syntax details.

Usage

First, install aster-equery as a development dependency:

npm install --save-dev aster-equery

Then, add it to your build script:

var aster = require('aster');
var equery = require('aster-equery');

aster.src('src/**/*.js')
.map(equery({
  'if ($cond) return $expr1; else return $expr2;': function (node, named) {
    return {
      type: 'ReturnStatement',
      argument: {
        type: 'ConditionalExpression',
        test: named.cond,
        consequent: named.expr1,
        alternate: named.expr2
      }
    };
  }
  // , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);

can be also written as:

var aster = require('aster');
var equery = require('aster-equery');

aster.src('src/**/*.js')
.map(equery({
  'if ($cond) return $expr1; else return $expr2;': 'return <%= cond %> ? <%= expr1 %> : <%= expr2 %>'
  // , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);

API

equery(mappings)

mappings

Type: {pattern: handler}

Replacement mappings.

pattern

Type: String

JavaScript example pattern.

handler (option 1: callback)

Type: Function(node, named)

Callback to be called on each found match. It will get two arguments - matched node object and hashmap of named subpatterns.

handler (option 2: template)

Type: String

estemplate string to be used for generating AST.

License

MIT License

About

Replace nodes with pattern-matching selectors in aster.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published