Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

aster-equery

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aster-equery

Replace nodes with pattern-matching selectors in aster.

latest
Source
npmnpm
Version
0.1.3
Version published
Weekly downloads
11
120%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

aster-plugin

FAQs

Package last updated on 09 Nov 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts