From 6f1b3e39cbe3b57623d05cc236e950ffe962aa53 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 11 Feb 2017 21:18:27 +0100 Subject: [PATCH] Remove `processor` as first param from attachers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- index.js | 2 +- readme.md | 9 ++++++--- test/core.js | 4 ++-- test/process.js | 30 +++++++++++++++--------------- test/use.js | 36 ++++++++++++++++++------------------ 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index 90cf25c4..a3056d94 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/readme.md b/readme.md index ac9592d0..53e734bd 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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 @@ -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 diff --git a/test/core.js b/test/core.js index 48af4601..3c40052c 100644 --- a/test/core.js +++ b/test/core.js @@ -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; diff --git a/test/process.js b/test/process.js index fc94ae21..4ad38511 100644 --- a/test/process.js +++ b/test/process.js @@ -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`'); @@ -70,7 +70,7 @@ test('process(file[, options][, done])', function (t) { } Parser.prototype.parse = parse; - processor.Parser = Parser; + this.Parser = Parser; }) .use(function () { return function (tree, file) { @@ -78,7 +78,7 @@ test('process(file[, options][, done])', function (t) { 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`'); @@ -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) { @@ -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, @@ -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) { @@ -158,7 +158,7 @@ test('process(file[, options][, done])', function (t) { ); }; }) - .use(function (processor) { + .use(function () { function Compiler(file, options, processor) { st.equal( file, @@ -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) { @@ -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( @@ -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( diff --git a/test/use.js b/test/use.js index 3423a665..0f3898e3 100644 --- a/test/use.js +++ b/test/use.js @@ -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)'); }] ]); @@ -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); @@ -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);