Skip to content

Commit

Permalink
Update support for configuration comments
Browse files Browse the repository at this point in the history
*   Add a new `ignore` keyword which ignores warnings in the following
    node;
*   Externalise `lib/filter` to `remark-message-control`.
  • Loading branch information
wooorm committed Feb 3, 2016
1 parent b6309f0 commit 870cb99
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 274 deletions.
8 changes: 5 additions & 3 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
],
"repository": "wooorm/remark-lint",
"dependencies": {
"sindresorhus/plur": "^2.0.0",
"wooorm/remark-range": "^2.0.0",
"sindresorhus/decamelize": "^1.0.0",
"wooorm/mdast-util-heading-style": "^1.0.0",
"wooorm/mdast-util-position": "^1.0.0",
"wooorm/mdast-util-to-string": "^1.0.0",
"eush77/npm-prefix": "^1.1.1",
"sindresorhus/plur": "^2.0.0",
"wooorm/remark-message-control": "^1.0.1",
"wooorm/remark-range": "^2.0.0",
"wooorm/unist-util-visit": "^1.0.0",
"wooorm/mdast-zone": "^2.0.0",
"wooorm/vfile-sort": "^1.0.0"
},
"scripts": [
Expand Down
139 changes: 0 additions & 139 deletions lib/filter.js

This file was deleted.

152 changes: 25 additions & 127 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ var SOURCE = 'remark-lint';
var decamelize = require('decamelize');
var sort = require('vfile-sort');
var range = require('remark-range');
var zone = require('mdast-zone');
var control = require('remark-message-control');
var internals = require('./rules');
var filter = require('./filter');
var npmPrefix = require('npm-prefix')();

/*
Expand Down Expand Up @@ -249,162 +248,61 @@ function decamelizeSettings(source) {
*/
function lint(remark, options) {
var settings = decamelizeSettings(options || {});
var reset = settings.reset;
var rules = loadExternals(settings.external);
var id;
var reset = options && options.reset;
var enable = [];
var disable = [];
var known = [];
var setting;
var id;

/*
* Ensure offset information is added.
*/

remark.use(range);

/**
* Get the latest state of a rule.
*
* @param {string} ruleId - Unique rule name.
* @param {File} [file] - File (optional)
*/
function getState(ruleId, file) {
var scope = file && file.namespace('remark-lint');
var ranges = scope && scope.ranges && scope.ranges[ruleId];

if (ranges) {
return ranges[ranges.length - 1].state;
}

setting = settings[ruleId];

if (setting === false) {
return false;
}

return !reset || (setting !== null && setting !== undefined);
}

/**
* Store settings on `file`.
*
* @param {File} file - Virtual file.
*/
function store(file) {
var scope = file.namespace('remark-lint');
var ranges = scope.ranges;
var ruleId;

if (!ranges) {
ranges = {};

for (ruleId in rules) {
ranges[ruleId] = [{
'state': getState(ruleId),
'position': {
'line': 0,
'column': 0
}
}];
}

scope.ranges = ranges;
}
}

remark.use(function () {
return function (ast, file) {
store(file);
};
});

/*
* Add each rule as a seperate plugin.
*/

for (id in rules) {
remark.use(attachFactory(id, rules[id], settings[id]));
}

/**
* Handle a rule.
*
* @param {VFile} file - Virtual file.
* @param {Object} marker - Marker context.
* @param {string} type - Type to toggle to.
* @param {*} ruleId - Rule to toggle.
*/
function toggle(file, marker, type, ruleId) {
var scope = file.namespace('remark-lint');
var markers;
var currentState;
var previousState;
setting = settings[id];

if (!(ruleId in rules)) {
file.fail('Unknown rule: cannot ' + type + ' `\'' + ruleId + '\'`', marker.node);
known.push(id);

return;
}

markers = scope.ranges[ruleId];

previousState = getState(ruleId, file);
currentState = type === 'enable';

if (currentState !== previousState) {
markers.push({
'state': currentState,
'position': marker.node.position.start
});
}
}

/**
* Handle a new-found marker.
*
* @param {Object} marker - Marker context.
* @param {Object} parser - Parser instance.
*/
function onparse(marker, parser) {
var file = parser.file;
var attributes = marker.attributes.split(' ');
var type = attributes[0];
var ids = attributes.slice(1);
var length = ids.length;
var index = -1;

if (type !== 'disable' && type !== 'enable') {
file.fail('Unknown lint keyword `' + type + '`: use either `\'enable\'` or `\'disable\'`', marker.node);

return;
if (!(setting === null || setting === undefined)) {
if (setting === false) {
disable.push(id);
} else {
enable.push(id);
}
}

store(file);

while (++index < length) {
toggle(file, marker, type, ids[index]);
}
remark.use(attachFactory(id, rules[id], setting));
}

remark.use(zone({
'name': 'lint',
'onparse': onparse
}));

/*
* Filter.
* Allow comments to toggle messages.
*/

remark.use(filter);
remark.use(control, {
'name': 'lint',
'source': SOURCE,
'reset': reset,
'known': known,
'enable': enable,
'disable': disable
});

/**
* Transformer sort messages.
*
* @param {Node} node - Syntax tree.
* @param {VFile} file - Virtual file.
* @param {Function} next - Completion handler.
*/
return function (node, file, next) {
return function (node, file) {
sort(file);
next();
};
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"author": "Titus Wormer <tituswormer@gmail.com>",
"dependencies": {
"decamelize": "^1.0.0",
"remark-range": "^2.0.0",
"mdast-util-heading-style": "^1.0.0",
"mdast-util-position": "^1.0.0",
"mdast-util-to-string": "^1.0.0",
"mdast-zone": "^2.0.0",
"npm-prefix": "^1.1.1",
"plur": "^2.0.0",
"remark-message-control": "^1.0.1",
"remark-range": "^2.0.0",
"unist-util-visit": "^1.0.0",
"vfile-sort": "^1.0.0"
},
Expand Down
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ markdown code. Read more about the latter on [**remark**’s
readme][remark-process].

In addition, you can also provide configuration comments to turn a rule
on or off inside a file (note that you cannot change what a setting, such as
`maximum-line-length`, you’re either enabling or disabling warnings).
on or off inside a file. Note that you cannot change what a setting,
such as `maximum-line-length`, checks for, as you’re either enabling
or disabling warnings). Read more about configuration comments in
[**remark-message-control**][message-control]s documentation.

The following file will warn twice for the duplicate headings:

Expand Down Expand Up @@ -234,3 +236,5 @@ excluding `remark-lint-no-` or `remark-lint-`
[remark-process]: https://github.com/wooorm/remark#remarkprocessvalue-options-done

[linter-markdown]: https://atom.io/packages/linter-markdown

[message-control]: https://github.com/wooorm/remark-message-control#markers
Loading

0 comments on commit 870cb99

Please sign in to comment.