Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit b385707

Browse files
committed
Add ability to disable certain lint checks; fixes #69
1 parent 64c782a commit b385707

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ A ***reporter*** is a function that accepts exactly 1 argument of type `LintWarn
5656
Bootlint exports a `bootlint` property on the global `window` object.
5757
In a browser environment, the following public APIs are available:
5858

59-
* `bootlint.lintCurrentDocument(reporter)`: Lints the HTML of the current document and calls the `reporter()` function repeatedly with each lint problem as an argument.
59+
* `bootlint.lintCurrentDocument(reporter, disabledIds)`: Lints the HTML of the current document and calls the `reporter()` function repeatedly with each lint problem as an argument.
60+
* `reporter` is a *reporter* function (see above for a definition). It will be called repeatedly with each lint problem as an argument.
61+
* `disabledIds` is an array of string linter IDs to disable
6062
* Returns nothing (i.e. `undefined`)
61-
* `bootlint.showLintReportForCurrentDocument()`: Lints the HTML of the current document and reports the linting results to the user.
63+
* `bootlint.showLintReportForCurrentDocument(disabledIds)`: Lints the HTML of the current document and reports the linting results to the user.
6264
* If there are any lint warnings, one general notification message will be `window.alert()`-ed to the user. Each warning will be output individually using `console.warn()`.
65+
* `disabledIds` is an array of string linter IDs to disable
6366
* Returns nothing (i.e. `undefined`)
6467

65-
In a browser environment, after Bootlint has loaded, `bootlint.showLintReportForCurrentDocument()` will be invoked once.
68+
In a browser environment, after Bootlint has loaded, `bootlint.showLintReportForCurrentDocument([])` will be executed once.
6669

6770
### Node.js
6871

@@ -75,14 +78,15 @@ function reporter(lint) {
7578
console.log(lint.id, lint.message);
7679
}
7780

78-
bootlint.lintHtml("<!DOCTYPE html><html>...", reporter); // calls reporter() repeatedly with each lint problem as an argument
81+
bootlint.lintHtml("<!DOCTYPE html><html>...", reporter, []); // calls reporter() repeatedly with each lint problem as an argument
7982
```
8083

8184
In a Node.js environment, Bootlint exposes the following public API:
8285

83-
* `bootlint.lintHtml(html, reporter)`: Lints the given HTML for a webpage and returns the linting results.
86+
* `bootlint.lintHtml(html, reporter, disabledIds)`: Lints the given HTML for a webpage and returns the linting results.
8487
* `html` is the HTML to lint, as a string
85-
* `reporter` is a function that should accept exactly 1 argument. It will be called repeatedly with each lint problem as an argument.
88+
* `reporter` is a *reporter* function (see above for a definition). It will be called repeatedly with each lint problem as an argument.
89+
* `disabledIds` is an array of string linter IDs to disable
8690
* Returns nothing (i.e. `undefined`)
8791

8892
## Contributing

src/bootlint.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,15 @@ var cheerio = require('cheerio');
596596
});
597597
});
598598

599-
exports._lint = function ($, reporter) {
599+
exports._lint = function ($, reporter, disabledIdList) {
600+
var disabledIdSet = {};
601+
disabledIdList.forEach(function (disabledId) {
602+
disabledIdSet[disabledId] = true;
603+
});
600604
Object.keys(allLinters).sort().forEach(function (linterId) {
601-
allLinters[linterId]($, reporter);
605+
if (!disabledIdSet[linterId]) {
606+
allLinters[linterId]($, reporter);
607+
}
602608
});
603609
};
604610
if (IN_NODE_JS) {
@@ -607,11 +613,12 @@ var cheerio = require('cheerio');
607613
* Lints the given HTML.
608614
* @param {string} html The HTML to lint
609615
* @param reporter Function to call with each lint problem
616+
* @param {string[]} disabledIds Array of string IDs of linters to disable
610617
* @returns {undefined} Nothing
611618
*/
612-
exports.lintHtml = function (html, reporter) {
619+
exports.lintHtml = function (html, reporter, disabledIds) {
613620
var $ = cheerio.load(html);
614-
this._lint($, reporter);
621+
this._lint($, reporter, disabledIds);
615622
};
616623
}
617624
else {
@@ -621,18 +628,20 @@ var cheerio = require('cheerio');
621628
/**
622629
* Lints the HTML of the current document.
623630
* @param reporter Function to call with each lint problem
631+
* @param {string[]} disabledIds Array of string IDs of linters to disable
624632
* @returns {undefined} Nothing
625633
*/
626-
exports.lintCurrentDocument = function (reporter) {
627-
this._lint($, reporter);
634+
exports.lintCurrentDocument = function (reporter, disabledIds) {
635+
this._lint($, reporter, disabledIds);
628636
};
629637
/**
630638
* Lints the HTML of the current document.
631639
* If there are any lint warnings, one general notification message will be window.alert()-ed to the user.
632640
* Each warning will be output individually using console.warn().
641+
* @param {string[]} disabledIds Array of string IDs of linters to disable
633642
* @returns {undefined} Nothing
634643
*/
635-
exports.showLintReportForCurrentDocument = function () {
644+
exports.showLintReportForCurrentDocument = function (disabledIds) {
636645
var seenLint = false;
637646
var reporter = function (lint) {
638647
if (!seenLint) {
@@ -643,13 +652,13 @@ var cheerio = require('cheerio');
643652
}
644653
console.warn("bootlint:", lint.id, lint.message);
645654
};
646-
this.lintCurrentDocument(reporter);
655+
this.lintCurrentDocument(reporter, disabledIds);
647656
};
648657
/*eslint-disable no-undef, block-scoped-var */
649658
window.bootlint = exports;
650659
/*eslint-enable no-undef, block-scoped-var */
651660
$(function () {
652-
exports.showLintReportForCurrentDocument();
661+
exports.showLintReportForCurrentDocument([]);
653662
});
654663
})();
655664
}

src/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var bootlint = require('./bootlint.js');
99

1010
var totalErrCount = 0;
1111
var totalFileCount = 0;
12+
var disabledIds = [];
1213
var patterns = process.argv.slice(2);
1314
patterns.forEach(function (pattern) {
1415
var filenames = glob.sync(pattern);
@@ -27,7 +28,7 @@ patterns.forEach(function (pattern) {
2728
console.log(filename + ":", err);
2829
return;
2930
}
30-
bootlint.lintHtml(html, reporter);
31+
bootlint.lintHtml(html, reporter, disabledIds);
3132
totalFileCount++;
3233
});
3334
});

test/bootlint_test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ function utf8Fixture(name) {
1515
function utf16Fixture(name) {
1616
return fs.readFileSync(_fixtureNameToFilepath(name), {encoding: 'utf16le'});
1717
}
18-
function lintHtml(html) {
18+
function lintHtml(html, disabledIds) {
1919
var lints = [];
2020
var reporter = function (lint) {
2121
lints.push(lint.message);
2222
};
23-
bootlint.lintHtml(html, reporter);
23+
bootlint.lintHtml(html, reporter, disabledIds || []);
2424
return lints;
2525
}
2626
/*
@@ -62,6 +62,13 @@ exports.bootlint = {
6262
'should not complain when the legacy-compatibility HTML5 doctype is used.');
6363
test.done();
6464
},
65+
'disabling lint checks': function (test) {
66+
test.expect(1);
67+
test.deepEqual(lintHtml(utf8Fixture('bs-v2.html'), ['E002', 'E013']),
68+
[],
69+
'should complain when Bootstrap v2 grid classes are present.');
70+
test.done();
71+
},
6572
'UTF-8 charset meta tag': function (test) {
6673
test.expect(3);
6774
test.deepEqual(lintHtml(utf8Fixture('charset/utf8.html')),

test/fixtures/generic-qunit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
var reporter = function (lint) {
1010
lints.push(lint.message);
1111
};
12-
bootlint.lintCurrentDocument(reporter);
12+
bootlint.lintCurrentDocument(reporter, []);
1313
return lints;
1414
}
1515

0 commit comments

Comments
 (0)