Skip to content

Commit

Permalink
Remove processor as first param from attachers
Browse files Browse the repository at this point in the history
MAJOR BREAKING CHANGE: The first parameters is now no longer given
as an argument to attachers (a.k.a., plug-ins).  The processor is
now given to attachers as the context object (“this”) instead.

This now allows plugins to be more easily used without unified
(something that I believe is good for the ecosystem).  Additionally,
this makes plugins usable inside other plugins as “utilities”, which
was previously rather awkward: https://github.com/wooorm/rehype-format/
blob/7715bf1/index.js#L4

Closes GH-21.
  • Loading branch information
wooorm committed Feb 11, 2017
1 parent 111d3c6 commit 6f1b3e3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function unified() {
}

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

if (isFunction(transformer)) {
transformers.use(transformer);
Expand Down
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ in the following ways:

Plug-in’s are a concept which materialise as [**attacher**][attacher]s.

#### `function attacher(processor[, options])`
#### `function attacher([options])`

An attacher is the thing passed to [`use`][use]. It configures the
processor and in turn can receive options.
Expand All @@ -281,9 +281,12 @@ Attachers can configure processors, such as by interacting with parsers
and compilers, linking them to other processors, or by specifying how
the syntax tree is handled.

###### Context

The context object is set to the invoked on [`processor`][processor].

###### Parameters

* `processor` ([`processor`][processor]) — Context on which it’s used;
* `options` (`*`, optional) — Configuration.

###### Returns
Expand Down Expand Up @@ -615,7 +618,7 @@ To make the processor concrete, invoke it: use `processor()` instead of `process

[use]: #processoruseplugin-options

[attacher]: #function-attacherprocessor-options
[attacher]: #function-attacheroptions

[transformer]: #function-transformernode-file-next

Expand Down
4 changes: 2 additions & 2 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ test('unified()', function (t) {

t.equal(typeof p, 'function', 'should return a function');

p.use(function (processor) {
p.use(function () {
count++;
processor.data('foo', 'bar');
this.data('foo', 'bar');
});

count = 0;
Expand Down
30 changes: 15 additions & 15 deletions test/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('process(file[, options][, done])', function (t) {
st.plan(11);

p = unified()
.use(function (processor) {
.use(function () {
function Parser(file, options, processor) {
st.equal(file, f, 'should pass `file` to `Parser`');
st.equal(options, o, 'should pass `options` to `Parser`');
Expand All @@ -70,15 +70,15 @@ test('process(file[, options][, done])', function (t) {
}

Parser.prototype.parse = parse;
processor.Parser = Parser;
this.Parser = Parser;
})
.use(function () {
return function (tree, file) {
st.equal(tree, n, 'should pass `tree` to transformers');
st.equal(file, f, 'should pass `file` to transformers');
};
})
.use(function (processor) {
.use(function () {
function Compiler(file, options, processor) {
st.equal(file, f, 'should pass `file` to `Compiler`');
st.equal(options, o, 'should pass `options` to `Compiler`');
Expand All @@ -93,7 +93,7 @@ test('process(file[, options][, done])', function (t) {

Compiler.prototype.compile = compile;

processor.Compiler = Compiler;
this.Compiler = Compiler;
});

p.process(f, o, function (err, file) {
Expand All @@ -115,7 +115,7 @@ test('process(file[, options][, done])', function (t) {
st.plan(12);

p = unified()
.use(function (processor) {
.use(function () {
function Parser(file, options, processor) {
st.equal(
file,
Expand All @@ -141,7 +141,7 @@ test('process(file[, options][, done])', function (t) {
}

Parser.prototype.parse = parse;
processor.Parser = Parser;
this.Parser = Parser;
})
.use(function () {
return function (tree, file) {
Expand All @@ -158,7 +158,7 @@ test('process(file[, options][, done])', function (t) {
);
};
})
.use(function (processor) {
.use(function () {
function Compiler(file, options, processor) {
st.equal(
file,
Expand Down Expand Up @@ -191,7 +191,7 @@ test('process(file[, options][, done])', function (t) {

Compiler.prototype.compile = compile;

processor.Compiler = Compiler;
this.Compiler = Compiler;
});

p.process(f, function (err, file) {
Expand All @@ -204,9 +204,9 @@ test('process(file[, options][, done])', function (t) {
);
});

p = unified().use(function (processor) {
processor.Parser = simple.Parser;
processor.Compiler = simple.Compiler;
p = unified().use(function () {
this.Parser = simple.Parser;
this.Compiler = simple.Compiler;
});

st.throws(
Expand All @@ -221,16 +221,16 @@ test('process(file[, options][, done])', function (t) {

t.test('process(file)', function (st) {
var p = unified()
.use(function (processor) {
processor.Parser = simple.Parser;
.use(function () {
this.Parser = simple.Parser;
})
.use(function () {
return function () {
return new Error('bravo');
};
})
.use(function (processor) {
processor.Compiler = noop.Compiler;
.use(function () {
this.Compiler = noop.Compiler;
});

st.throws(
Expand Down
36 changes: 18 additions & 18 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ test('use(plugin[, options])', function (t) {

t.plan(11);

p.use(function (processor, options) {
t.equal(processor, p, 'should invoke a plugin with `processor`');
p.use(function (options) {
t.equal(this, p, 'should invoke a plugin with `processor` as the context');
t.equal(options, o, 'should invoke a plugin with `options`');
}, o);

p.use([
function (processor) {
t.equal(processor, p, 'should support a list of plugins (#1)');
function () {
t.equal(this, p, 'should support a list of plugins (#1)');
},
function (processor) {
t.equal(processor, p, 'should support a list of plugins (#2)');
function () {
t.equal(this, p, 'should support a list of plugins (#2)');
}
]);

p.use([function (processor) {
t.equal(processor, p, 'should support a list of one plugin');
p.use([function () {
t.equal(this, p, 'should support a list of one plugin');
}]);

p.use([function (processor, options) {
p.use([function (options) {
t.equal(options, o, 'should support a plugin--options tuple');
}, o]);

p.use([
[function (processor, options) {
[function (options) {
t.equal(options, o, 'should support a matrix (#1)');
}, o],
[function (processor) {
t.equal(processor, p, 'should support a matrix (#2)');
[function () {
t.equal(this, p, 'should support a matrix (#2)');
}]
]);

Expand Down Expand Up @@ -85,8 +85,8 @@ test('use(processor)', function (t) {

fixture = q;

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

Expand Down Expand Up @@ -117,12 +117,12 @@ test('use(processor)', function (t) {
'should attach a transformer (#3)'
);

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

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

t.equal(p.Parser, ParserA);
Expand Down

0 comments on commit 6f1b3e3

Please sign in to comment.