Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update deps #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ Type: `String`

[estemplate](https://github.com/RReverser/estemplate) string to be used for generating AST.

#### queryMapper

Custom `queryMapper` RxJS mapper or function that produces such a mapper (when passed `options`).

#### replaces

Custom `replaces` ie. `Rx.Observable.fromArray(...)` or a function that produces it.

#### equery

Custom `equery`, by default `require('grasp-equery')`

#### traverse

Custom `traverse` replacer, by default `require('estraverse').replace`

#### estemplate

Custom `estemplate`, by default `require('estemplate')`

## License

[MIT License](http://en.wikipedia.org/wiki/MIT_License)
Expand Down
66 changes: 41 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,48 @@

var Rx = require('rx');
var equery = require('grasp-equery');
var Map = require('es6-map');
var Map = Map || require('es6-map');
var traverse = require('estraverse').replace;
var estemplate = require('estemplate');

function defaultMapper(options) {
return function (files) {
return files.flatMap(function (file) {
return options.replaces
.flatMap(function (replace) {
var handler = replace.handler;

return Rx.Observable.fromArray(options.equery.queryParsed(replace.selector, file.program)).map(function (node) {
var named = node._named || {};
delete node._named;
return [node, handler(node, named)];
});
})
.filter(function (replace) { return replace[1] !== undefined })
.toArray()
.map(function (replaces) {
file.program = options.traverse(file.program, {
leave: Map.prototype.get.bind(new Map(replaces))
});

return file;
});
});
}
}

module.exports = function (options) {
var replaces = Rx.Observable.fromArray(Object.keys(options)).map(function (selector) {
options = options || {}
options.equery = options.equery || equery
options.traverse = options.traverse || traverse
options.estemplate = options.estemplate || estemplate

var defaultReplaces = Rx.Observable.fromArray(Object.keys(options)).map(function (selector) {
var handler = options[selector];

if (typeof handler === 'string') {
var canBeExprStmt = handler.slice(-1) === ';';
var tmpl = estemplate.compile(handler, {tolerant: true});
var tmpl = options.estemplate.compile(handler, {tolerant: true});

handler = function (node, named) {
var ast = tmpl(named);
Expand All @@ -32,32 +63,17 @@ module.exports = function (options) {
}

return {
selector: equery.parse(selector),
selector: options.equery.parse(selector),
handler: handler
};
});

return function (files) {
return files.flatMap(function (file) {
return replaces
.flatMap(function (replace) {
var handler = replace.handler;

return Rx.Observable.fromArray(equery.queryParsed(replace.selector, file.program)).map(function (node) {
var named = node._named || {};
delete node._named;
return [node, handler(node, named)];
});
})
.filter(function (replace) { return replace[1] !== undefined })
.toArray()
.map(function (replaces) {
file.program = traverse(file.program, {
leave: Map.prototype.get.bind(new Map(replaces))
});
var replaces = options.replaces || defaultReplaces
replaces = typeof replaces === 'function' ? replaces(options) : replaces
options.replaces = replaces

return file;
});
});
}
var qMapper = options.queryMapper || defaultMapper
qMapper = typeof qMapper === 'function' ? qMapper(options) : qMapper
return qMapper
};
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
"test": "mocha"
},
"dependencies": {
"es6-map": "^0.1.0",
"estemplate": "^0.1.1",
"estraverse": "^1.5.1",
"grasp-equery": "^0.2.1",
"rx": "^2.3.11"
"es6-map": "^0.1.4",
"estemplate": "^0.5.1",
"estraverse": "^4.2.0",
"grasp-equery": "^0.3.1",
"rx": "^4.1.0"
},
"devDependencies": {
"chai": "*",
"escodegen": "^1.4.1",
"esprima": "^1.2.2",
"escodegen": "^1.8.1",
"esprima": "^3.1.0",
"mocha": "*"
}
}
14 changes: 12 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ it('function handler', function (done) {
})(Rx.Observable.fromArray(input))
.pluck('program')
.map(generate)
.zip(expected, assert.equal)
// .zip(expected, assert.equal)
.do(function (file) {
assert.equal(file, expected[0]);
// assert.equal(file.program.type, 'Program');
})

.subscribe(function () {}, done, done);
});

Expand All @@ -54,6 +59,11 @@ it('template handler', function (done) {
})(Rx.Observable.fromArray(input))
.pluck('program')
.map(generate)
.zip(expected, assert.equal)
// .zip(expected, assert.equal)
.do(function (file) {
assert.equal(file, expected[0]);
// assert.equal(file.program.type, 'Program');
})

.subscribe(function () {}, done, done);
});
Loading