Skip to content

Commit

Permalink
Remove options from params in functions
Browse files Browse the repository at this point in the history
* Rewrite signatures in parsers, compilers. Closes GH-24.
* Remove `options` from parameters in `parse`, `stringify`, `process`
  Closes GH-23.
  • Loading branch information
wooorm committed Feb 11, 2017
1 parent 6f1b3e3 commit 0db0928
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 303 deletions.
54 changes: 14 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var extend = require('extend');
var bail = require('bail');
var vfile = require('vfile');
var trough = require('trough');
var buffer = require('is-buffer');
var string = require('x-is-string');

/* Expose an abstract processor. */
Expand All @@ -18,7 +17,7 @@ var slice = [].slice;
/* Process pipeline. */
var pipeline = trough()
.use(function (p, ctx) {
ctx.tree = p.parse(ctx.file, ctx.options);
ctx.tree = p.parse(ctx.file);
})
.use(function (p, ctx, next) {
p.run(ctx.tree, ctx.file, function (err, tree, file) {
Expand All @@ -32,7 +31,7 @@ var pipeline = trough()
});
})
.use(function (p, ctx) {
ctx.file.contents = p.stringify(ctx.tree, ctx.file, ctx.options);
ctx.file.contents = p.stringify(ctx.tree, ctx.file);
});

/* Function to create the first processor. */
Expand Down Expand Up @@ -81,14 +80,14 @@ function unified() {

/* Assert a parser is available. */
function assertParser(name) {
if (!isParser(processor.Parser)) {
if (!isFunction(processor.Parser)) {
throw new Error('Cannot `' + name + '` without `Parser`');
}
}

/* Assert a compiler is available. */
function assertCompiler(name) {
if (!isCompiler(processor.Compiler)) {
if (!isFunction(processor.Compiler)) {
throw new Error('Cannot `' + name + '` without `Compiler`');
}
}
Expand Down Expand Up @@ -240,11 +239,13 @@ function unified() {
/* Parse a file (in string or VFile representation)
* into a Unist node using the `Parser` on the
* processor. */
function parse(file, options) {
function parse(doc) {
var file = vfile(doc);

assertConcrete('parse');
assertParser('parse');

return new processor.Parser(vfile(file), options, processor).parse();
return new processor.Parser(doc.toString(), file).parse();
}

/* Run transforms on a Unist node representation of a file
Expand Down Expand Up @@ -278,47 +279,30 @@ function unified() {
/* Stringify a Unist node representation of a file
* (in string or VFile representation) into a string
* using the `Compiler` on the processor. */
function stringify(node, file, options) {
function stringify(node, file) {
assertConcrete('stringify');
assertCompiler('stringify');
assertNode(node);

if (
!options &&
!string(file) &&
!buffer(file) &&
!(typeof file === 'object' && 'messages' in file)
) {
options = file;
file = null;
}

return new processor.Compiler(vfile(file), options, processor).compile(node);
return new processor.Compiler(node, vfile(file)).compile();
}

/* Parse a file (in string or VFile representation)
* into a Unist node using the `Parser` on the processor,
* then run transforms on that node, and compile the
* resulting node using the `Compiler` on the processor,
* and store that result on the VFile. */
function process(file, options, done) {
function process(doc, done) {
var complete = false;
var file;

assertConcrete('process');
assertParser('process');
assertCompiler('process');

if (!done && isFunction(options)) {
done = options;
options = null;
}

file = vfile(file);
file = vfile(doc);

pipeline.run(processor, {
file: file,
options: options || {}
}, function (err) {
pipeline.run(processor, {file: file}, function (err) {
complete = true;

if (done) {
Expand All @@ -344,16 +328,6 @@ function isFunction(fn) {
return typeof fn === 'function';
}

/* Check if `compiler` is a Compiler. */
function isCompiler(compiler) {
return isFunction(compiler) && compiler.prototype && isFunction(compiler.prototype.compile);
}

/* Check if `parser` is a Parser. */
function isParser(parser) {
return isFunction(parser) && parser.prototype && isFunction(parser.prototype.parse);
}

/* Check if `processor` is a unified processor. */
function isProcessor(processor) {
return isFunction(processor) && isFunction(processor.use) && isFunction(processor.process);
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
"bail": "^1.0.0",
"extend": "^3.0.0",
"has": "^1.0.1",
"is-buffer": "^1.1.4",
"once": "^1.3.3",
"trough": "^1.0.0",
"vfile": "^2.0.0",
"x-is-string": "^0.1.0"
Expand Down
24 changes: 10 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ no issues found
* [API](#api)
* [processor()](#processor)
* [processor.use(plugin\[, options\])](#processoruseplugin-options)
* [processor.parse(file|value\[, options\])](#processorparsefilevalue-options)
* [processor.stringify(node\[, file\]\[, options\])](#processorstringifynode-file-options)
* [processor.parse(file|value)](#processorparsefilevalue)
* [processor.stringify(node\[, file\])](#processorstringifynode-file)
* [processor.run(node\[, file\]\[, done\])](#processorrunnode-file-done)
* [processor.process(file|value\[, options\]\[, done\])](#processorprocessfilevalue-options-done)
* [processor.process(file|value\[, done\])](#processorprocessfilevalue-done)
* [processor.data(key\[, value\])](#processordatakey-value)
* [processor.abstract()](#processorabstract)
* [License](#license)
Expand Down Expand Up @@ -331,15 +331,14 @@ the function **may** finish asynchronous, and **must** invoke `next()`.
* `node` ([**Node**][node], optional) — New syntax tree;
* `file` ([**VFile**][file], optional) — New virtual file.

### `processor.parse(file|value[, options])`
### `processor.parse(file|value)`

Parse text to a syntax tree.

###### Parameters

* `file` ([**VFile**][file]);
* `file` ([**VFile**][file])
— Or anything which can be given to `vfile()`.
* `options` (`Object`, optional) — Configuration given to the parser.

###### Returns

Expand All @@ -355,7 +354,7 @@ The instance must expose a `parse` method which is invoked without
arguments, and must return a syntax tree representation of the
[**VFile**][file].

### `processor.stringify(node[, file][, options])`
### `processor.stringify(node[, file])`

Compile a syntax tree to text.

Expand All @@ -364,7 +363,6 @@ Compile a syntax tree to text.
* `node` ([**Node**][node]);
* `file` ([**VFile**][file], optional);
— Or anything which can be given to `vfile()`.
* `options` (`Object`, optional) — Configuration given to the parser.

###### Returns

Expand Down Expand Up @@ -409,7 +407,7 @@ error, or a syntax tree and a file.
* `node` ([**Node**][node]);
* `file` ([**VFile**][file]).

### `processor.process(file|value[, options][, done])`
### `processor.process(file|value[, done])`

Process the given representation of a file as configured on the
processor. The process invokes `parse`, `run`, and `stringify`
Expand All @@ -422,8 +420,6 @@ is thrown if [`done`][process-done] is not supplied.

* `file` ([**VFile**][file]);
* `value` (`string`) — String representation of a file;
* `options` (`Object`, optional) — Configuration for both the parser
and compiler;
* `done` ([`Function`][process-done], optional).

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

[processor]: #processor

[process]: #processorprocessfilevalue-options-done
[process]: #processorprocessfilevalue-done

[parse]: #processorparsefilevalue-options
[parse]: #processorparsefilevalue

[parser]: #processorparser

[stringify]: #processorstringifynode-file-options
[stringify]: #processorstringifynode-file

[compiler]: #processorcompiler

Expand Down
34 changes: 7 additions & 27 deletions test/parse.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

var test = require('tape');
var noop = require('./util/noop');
var unified = require('..');

test('parse(file[, options])', function (t) {
test('parse(file)', function (t) {
var p = unified();
var o;
var n;

t.plan(8);
t.plan(5);

t.throws(
function () {
Expand All @@ -19,35 +17,17 @@ test('parse(file[, options])', function (t) {
'should throw without `Parser`'
);

p.Parser = noop;

t.throws(
function () {
p.parse();
},
/Cannot `parse` without `Parser`/,
'should throw without `Parser#parse`'
);

o = {};
n = {type: 'delta'};

p.Parser = function (file, options, processor) {
p.Parser = function (doc, file) {
t.ok(typeof doc, 'string', 'should pass a document');
t.ok('message' in file, 'should pass a file');
t.equal(file.toString(), 'charlie', 'should pass options');
t.equal(options, o, 'should pass options');
t.equal(processor, p, 'should pass the processor');
};

p.Parser.prototype.parse = function (value) {
t.equal(value, undefined, 'should not pass anything to `parse`');

p.Parser.prototype.parse = function () {
t.equal(arguments.length, 0, 'should not pass anything to `parse`');
return n;
};

t.equal(
p.parse('charlie', o),
n,
'should return the result `Parser#parse` returns'
);
t.equal(p.parse('charlie'), n, 'should return the result `Parser#parse` returns');
});

0 comments on commit 0db0928

Please sign in to comment.