Skip to content

Commit

Permalink
Add support for rules as any trough ware
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 1, 2016
1 parent 31f82b2 commit d272ff2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 52 deletions.
83 changes: 32 additions & 51 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var decamelize = require('decamelize');
var sort = require('vfile-sort');
var control = require('remark-message-control');
var loadPlugin = require('load-plugin');
var trough = require('trough');
var wrapped = require('wrapped');
var internals = require('./rules');

/**
Expand All @@ -37,64 +39,32 @@ var internals = require('./rules');
* @param {string} id - Identifier.
* @param {Function} rule - Rule
* @param {*} options - Options for respective rule.
* @return {Function} - See `attach` below.
* @return {Function} - Trough ware.
*/
function attachFactory(id, rule, options) {
/**
* Attach the rule to a remark instance, unless `false`
* is passed as an option.
*
* @return {Function?} - See `plugin` below.
*/
function attach() {
/**
* Attach the rule to a remark instance, unless `false`
* is passed as an option.
*
* @param {Node} ast - Root node.
* @param {File} [file] - Virtual file.
* @param {Function} next - Signal end.
*/
function plugin(ast, file, next) {
var scope = file.namespace('remark-lint');

/*
* Track new messages per file.
*/

if (scope.index === undefined || scope.index === null) {
scope.index = file.messages.length;
}
function ruleFactory(id, rule, options) {
var fn = wrapped(rule);

/**
* Add `ruleId` to each new message.
*
* @param {Error?} err - Optional failure.
*/
function done(err) {
var messages = file.messages;
return function (ast, file, next) {
var scope = file.namespace('remark-lint');

while (scope.index < messages.length) {
messages[scope.index].ruleId = id;
messages[scope.index].source = SOURCE;

scope.index++;
}
/* Track new messages per file. */
if (scope.index === undefined || scope.index === null) {
scope.index = file.messages.length;
}

next(err);
}
fn(ast, file, options, function (err) {
var messages = file.messages;

/*
* Invoke `rule`, with `options`
*/
while (scope.index < messages.length) {
messages[scope.index].ruleId = id;
messages[scope.index].source = SOURCE;

rule(ast, file, options, done);
}
scope.index++;
}

return plugin;
next(err);
});
}

return attach;
}

/**
Expand Down Expand Up @@ -180,6 +150,7 @@ function lint(remark, options) {
var enable = [];
var disable = [];
var known = [];
var pipeline = trough();
var setting;
var id;

Expand All @@ -206,9 +177,19 @@ function lint(remark, options) {
}
}

remark.use(attachFactory(id, rules[id], setting));
pipeline.use(ruleFactory(id, rules[id], setting));
}

/*
* Run all rules.
*/

remark.use(function () {
return function (node, file, next) {
pipeline.run(node, file, next);
};
});

/*
* Allow comments to toggle messages.
*/
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
"npm-prefix": "^1.1.1",
"plur": "^2.0.0",
"remark-message-control": "^2.0.0",
"trough": "^1.0.0",
"unist-util-position": "^2.0.1",
"unist-util-visit": "^1.0.0",
"vfile-location": "^2.0.0",
"vfile-sort": "^1.0.0"
"vfile-sort": "^1.0.0",
"wrapped": "^1.0.1"
},
"files": [
"index.js",
Expand Down

0 comments on commit d272ff2

Please sign in to comment.