diff options
author | Zac Echola <zac.echola@so.mnscu.edu> | 2015-09-16 16:41:00 +0300 |
---|---|---|
committer | Zac Echola <zac.echola@so.mnscu.edu> | 2015-09-16 16:41:00 +0300 |
commit | 2858a99892b24024ea3719047ff97c031bfdd024 (patch) | |
tree | 3fc11c94b54c226fc7307f7a84d4c8a108c55ec9 | |
parent | e9e57f8606baa53743e1e54cfa88f187e744000d (diff) | |
parent | bec72c2d4d7fa20fcacad9be4846fb9c7e4aef9e (diff) |
Merge pull request #57 from freezy-sk/56-relaxerror-with-paths
Relaxerror can be defined as object of error IDs and filepath masks
-rw-r--r-- | Gruntfile.js | 8 | ||||
-rw-r--r-- | README.md | 19 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | tasks/bootlint.js | 35 | ||||
-rw-r--r-- | test/bootlint_test.js | 8 | ||||
-rw-r--r-- | test/fixtures/missing-jquery.html | 24 |
6 files changed, 88 insertions, 9 deletions
diff --git a/Gruntfile.js b/Gruntfile.js index 28e6401..a1f3e28 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -41,12 +41,18 @@ module.exports = function(grunt) { }, relaxerror: { options: { - relaxerror: ['E001'], + relaxerror: { + 'E001': [], + 'W005': [ + 'test/fixtures/missing-jquery.html' + ] + }, }, files: { 'tmp/relaxerror': [ 'test/fixtures/missing-doctype.html', 'test/fixtures/missing-charset.html', + 'test/fixtures/missing-jquery.html', ] } }, @@ -87,10 +87,24 @@ Shows all errors and warnings before stopping the task. (Overrides `stoponerror` #### options.relaxerror -* Type: `Array` +* Type: `Array|Object` * Default: `[]` -Array of [bootlint problem ID codes](https://github.com/twbs/bootlint/wiki) (`String`s) to explicitly ignore. +Array of [bootlint problem ID codes][] (`String`s) to explicitly ignore. + +Object of [bootlint problem ID codes][] as **keys** and filepath masks as array **value**. + +##### Example + +``` +relaxerror: { + 'E001': [], + 'W005': [ + 'path/to/file.html', + 'file/path/*.mask' + ] +}, +``` ## Contributing @@ -117,3 +131,4 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Code released under [the MIT license](https://github.com/twbs/grunt-bootlint/blob/master/LICENSE-MIT). +[bootlint problem ID codes]: https://github.com/twbs/bootlint/wiki diff --git a/package.json b/package.json index 2f3f8d6..3a8aab1 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ }, "dependencies": { "bootlint": "^0.12.0", - "chalk": "^1.0.0" + "chalk": "^1.0.0", + "micromatch": "^2.2.0" }, "devDependencies": { "grunt": "~0.4.5", diff --git a/tasks/bootlint.js b/tasks/bootlint.js index 7574596..a0ef01c 100644 --- a/tasks/bootlint.js +++ b/tasks/bootlint.js @@ -11,6 +11,7 @@ module.exports = function(grunt) { var bootlint = require('bootlint'); var chalk = require('chalk'); + var micromatch = require('micromatch'); grunt.registerMultiTask('bootlint', 'An HTML linter for Bootstrap projects', function() { @@ -68,7 +69,8 @@ module.exports = function(grunt) { }; - bootlint.lintHtml(src, reporter, options.relaxerror); + var disabledIds = getDisabledIdsForFilepath(filepath); + bootlint.lintHtml(src, reporter, disabledIds); totalFileCount++; }); @@ -81,6 +83,35 @@ module.exports = function(grunt) { grunt.log.ok(totalFileCount + ' file(s) lint free.'); } }); - }); + function getDisabledIdsForFilepath(filepath) { + // Relaxerror defined as array without filepaths + if (options.relaxerror instanceof Array) { + return options.relaxerror; + } + + // Relaxerror as object with error IDs as keys and filepaths as values + var disabledIds = Object.keys(options.relaxerror); + + // Lookup disabled IDs filepaths + var returnIds = disabledIds.filter(function(key) { + var paths = options.relaxerror[key]; + + // handle 'E001': true, 'E001': [] + if (!(paths instanceof Array) || paths.length === 0) { + return true; + } + + // handle 'E001': ['*'] + if (paths.indexOf('*') !== -1) { + return true; + } + + // test filepath pattern + return micromatch.any(filepath, paths); + }); + + return returnIds; + } + }); }; diff --git a/test/bootlint_test.js b/test/bootlint_test.js index f907305..e3f0317 100644 --- a/test/bootlint_test.js +++ b/test/bootlint_test.js @@ -37,13 +37,13 @@ exports.bootlint = { 'Should print file path'); test.ok(result.stdout.indexOf("Document is missing a DOCTYPE declaration") >= 0, 'Should warn about missing a DOCTYPE'); - test.ok(result.stdout.indexOf("8 lint error(s) found across 4 file(s)") >= 0, + test.ok(result.stdout.indexOf("9 lint error(s) found across 5 file(s)") >= 0, 'Should print number of lint errors and files'); test.done(); }); }, relaxerror: function(test) { - test.expect(3); + test.expect(4); grunt.util.spawn({ grunt: true, args: ['bootlint:relaxerror', '--no-color'], @@ -52,7 +52,9 @@ exports.bootlint = { 'Should not warn about missing a DOCTYPE'); test.ok(result.stdout.indexOf("W001") >= 0, 'Should warn about missing charset'); - test.ok(result.stdout.indexOf("1 lint error(s) found across 2 file(s)") >= 0, + test.ok(result.stdout.indexOf("W005") === -1, + 'Should not warn about missing jQuery'); + test.ok(result.stdout.indexOf("1 lint error(s) found across 3 file(s)") >= 0, 'Should print correct number of lint errors and files'); test.done(); }); diff --git a/test/fixtures/missing-jquery.html b/test/fixtures/missing-jquery.html new file mode 100644 index 0000000..1529a75 --- /dev/null +++ b/test/fixtures/missing-jquery.html @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <title>Test</title> + <!--[if lt IE 9]> + <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> + <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> + <![endif]--> + + <link rel="stylesheet" href="../../lib/qunit-1.14.0.css"> + <script src="../../lib/qunit-1.14.0.js"></script> + <script src="../../../dist/browser/bootlint.js"></script> + <script src="../generic-qunit.js"></script> + </head> + <body> + <div id="qunit"></div> + <ol id="bootlint"> + <li data-lint="Document is missing a jQuery"></li> + </ol> + </body> +</html> |