diff --git a/lib/index.js b/lib/index.js index 7c604222..0bb58a23 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'); /** @@ -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; } /** @@ -180,6 +150,7 @@ function lint(remark, options) { var enable = []; var disable = []; var known = []; + var pipeline = trough(); var setting; var id; @@ -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. */ diff --git a/package.json b/package.json index 9f5f51bd..016ab778 100644 --- a/package.json +++ b/package.json @@ -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",