Skip to content

Commit

Permalink
Add support for passing a processor to use
Browse files Browse the repository at this point in the history
Closes GH-17.
  • Loading branch information
wooorm committed Oct 10, 2016
1 parent b9077ce commit 39fa4b5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function unified() {
processor.abstract = abstract;

/* Plug-ins. */
processor.attachers = attachers;
processor.use = use;

/* Streaming. */
Expand Down Expand Up @@ -255,16 +256,20 @@ function unified() {
* * a tuple of one attacher and options.
* * a matrix: list containing any of the above and
* matrices.
* * a processor: another processor to use all its
* plugins (except parser if there’s already one).
*
* @param {...*} value - See description.
* @return {Processor} - The operated on processor.
*/
function use(value) {
var args = slice.call(arguments, 0);
var params = args.slice(1);
var parser;
var index;
var length;
var transformer;
var result;

assertConcrete('use');

Expand Down Expand Up @@ -294,6 +299,21 @@ function unified() {
/* Store attacher. */
attachers.push(args);

/* Use a processor (except its parser if there’s already one.
* Note that the processor is stored on `attachers`, making
* it possibly mutating in the future, but also ensuring
* the parser isn’t overwritten in the future either. */
if (isProcessor(value)) {
parser = processor.Parser;
result = use(value.attachers);

if (parser) {
processor.Parser = parser;
}

return result;
}

/* Single attacher. */
transformer = value.apply(null, [processor].concat(params));

Expand Down Expand Up @@ -635,3 +655,13 @@ function isCompiler(compiler) {
function isParser(parser) {
return isFunction(parser) && parser.prototype && isFunction(parser.prototype.parse);
}

/**
* Check if `processor` is a unified processor.
*
* @param {*} processor - Value.
* @return {boolean} - Whether `processor` is a processor.
*/
function isProcessor(processor) {
return isFunction(processor) && isFunction(processor.use) && isFunction(processor.process);
}
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,18 @@ that plug-in with optional options.
* `processor.use(plugin[, options])`;
* `processor.use(plugins[, options])`;
* `processor.use(list)`;
* `processor.use(matrix)`.
* `processor.use(matrix)`;
* `processor.use(processor)`.

###### Parameters

* `plugin` ([`Plugin`][plugin]);
* `options` (`*`, optional) — Configuration for `plugin`.
* `plugins` (`Array.<Function>`) — List of plugins;
* `list` (`Array`) — `plugin` and `options` in an array;
* `matrix` (`Array`) — array where each entry is a `list`;
* `matrix` (`Array`) — Array where each entry is a `list`;
* `processor` ([`Processor`][processor]) — Other processor whose
plugins to use (except for a parser).

###### Returns

Expand Down
74 changes: 74 additions & 0 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,77 @@ test('use(plugin[, options])', function (t) {

t.end();
});

/* Processors. */
test('use(processor)', function (t) {
var p = unified();
var q = unified();
var o = {};
var n;
var res;
var fixture;

t.plan(12);

res = p.use(q);

t.equal(res, p, 'should return the origin processor');
t.equal(p.attachers[0][0], q, 'should store attachers');

p = unified();
q = unified();

fixture = q;

q.use(function (processor, options) {
t.equal(processor, fixture, 'should invoke a plugin with `processor`');
t.equal(options, o, 'should invoke a plugin with `options`');
}, o);

fixture = p;

p.use(q);

q = unified();
p = unified().use(q);
n = {type: 'test'};

q.use(function () {
return function (node, file) {
t.equal(node, n, 'should attach a transformer (#1)');
t.ok('message' in file, 'should attach a transformer (#2)');

throw new Error('Alpha bravo charlie');
};
});

p.use(q);

t.throws(
function () {
p.run(n);
},
/Error: Alpha bravo charlie/,
'should attach a transformer (#3)'
);

p = unified().use(function (processor) {
processor.Parser = ParserA;
});

q = unified().use(function (processor) {
processor.Parser = ParserB;
});

t.equal(p.Parser, ParserA);
t.equal(q.Parser, ParserB);

p.use(q);

t.equal(p.Parser, ParserA);

function ParserA() {}
function ParserB() {}

t.end();
});

0 comments on commit 39fa4b5

Please sign in to comment.