Skip to content

Commit

Permalink
Add en- and disabling multiple rules in one comment
Browse files Browse the repository at this point in the history
Why:

*   Disabling more than one rule currently needs multiple HTML
    comments. Those are often added, after each other. But markdown
    needs blank lines between HTML to distinguish it, thus when
    lint-comments were adjacent without black-lines they were
    ignored.

How:

*    Space-delimit the ruleId’s in `lint disable` and `lint-enable`
     comments, and toggle each ruleId.
  • Loading branch information
wooorm committed Aug 31, 2015
1 parent c1136a1 commit 29976d0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
47 changes: 32 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,19 @@ function lint(mdast, options) {
}

/**
* Handle a new-found marker.
* Handle a rule.
*
* @param {VFile} file - Virtual file.
* @param {Object} marker - Marker context.
* @param {Object} parser - Parser instance.
* @param {string} type - Type to toggle to.
* @param {*} ruleId - Rule to toggle.
*/
function onparse(marker, parser) {
var file = parser.file;
function toggle(file, marker, type, ruleId) {
var scope = file.namespace('mdast-lint');
var attributes = marker.attributes.split(' ');
var type = attributes[0];
var ruleId = attributes[1];
var markers;
var currentState;
var previousState;

store(file);

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

return;
}

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

Expand All @@ -309,6 +299,33 @@ function lint(mdast, options) {
}
}

/**
* 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;
}

store(file);

while (++index < length) {
toggle(file, marker, type, ids[index]);
}
}

mdast.use(zone({
'name': 'lint',
'onparse': onparse
Expand Down
47 changes: 32 additions & 15 deletions mdast-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,29 +420,19 @@ function lint(mdast, options) {
}

/**
* Handle a new-found marker.
* Handle a rule.
*
* @param {VFile} file - Virtual file.
* @param {Object} marker - Marker context.
* @param {Object} parser - Parser instance.
* @param {string} type - Type to toggle to.
* @param {*} ruleId - Rule to toggle.
*/
function onparse(marker, parser) {
var file = parser.file;
function toggle(file, marker, type, ruleId) {
var scope = file.namespace('mdast-lint');
var attributes = marker.attributes.split(' ');
var type = attributes[0];
var ruleId = attributes[1];
var markers;
var currentState;
var previousState;

store(file);

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

return;
}

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

Expand All @@ -462,6 +452,33 @@ function lint(mdast, options) {
}
}

/**
* 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;
}

store(file);

while (++index < length) {
toggle(file, marker, type, ids[index]);
}
}

mdast.use(zone({
'name': 'lint',
'onparse': onparse
Expand Down
2 changes: 1 addition & 1 deletion mdast-lint.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions test/fixtures/comments-disable-multiple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Hello

<!--lint disable no-duplicate-headings maximum-line-length-->

## Hello

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello.

<!--lint enable no-duplicate-headings-->

### Hello

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello.
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ describe('Comments', function () {
});
});

describe('Disable multiple rules at once', function () {
assertFile('comments-disable-multiple.md', [
'comments-disable-multiple.md:11:1-11:10: Do not use headings with similar content (5:1)'
], null, {});
});

describe('Inline comments', function () {
assertFile('comments-inline.md', [
'comments-inline.md:1:32-1:38: Do not use HTML in markdown',
Expand Down

0 comments on commit 29976d0

Please sign in to comment.