From 89899eabf8c2f7926acd9f85cbd19733f3ffd2f4 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Tue, 10 Jul 2018 03:12:38 +0300 Subject: ES6-ify code. --- .jshintrc | 3 +- tasks/bootlint.js | 100 +++++++++++++++++++++++++------------------------- test/bootlint_test.js | 66 ++++++++++++++++----------------- 3 files changed, 85 insertions(+), 84 deletions(-) diff --git a/.jshintrc b/.jshintrc index bcad78f..3d6c2ca 100644 --- a/.jshintrc +++ b/.jshintrc @@ -2,13 +2,14 @@ "camelcase": true, "curly": true, "eqeqeq": true, + "esnext": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, "node": true, - "nonbsp": true, "noempty": true, + "nonbsp": true, "quotmark": "single", "strict": true, "sub": true, diff --git a/tasks/bootlint.js b/tasks/bootlint.js index f3af6b4..f15b653 100644 --- a/tasks/bootlint.js +++ b/tasks/bootlint.js @@ -9,22 +9,20 @@ 'use strict'; module.exports = function(grunt) { - var bootlint = require('bootlint'); - var chalk = require('chalk'); - var micromatch = require('micromatch'); - + const bootlint = require('bootlint'); + const chalk = require('chalk'); + const micromatch = require('micromatch'); grunt.registerMultiTask('bootlint', 'An HTML linter for Bootstrap projects', function() { - - var options = this.options({ + const options = this.options({ stoponerror: false, stoponwarning: false, showallerrors: false, relaxerror: [] }); - var totalErrCount = 0; - var totalFileCount = 0; + let totalErrCount = 0; + let totalFileCount = 0; function getDisabledIdsForFilepath(filepath) { // Relaxerror defined as array without filepaths @@ -33,11 +31,11 @@ module.exports = function(grunt) { } // Relaxerror as object with error IDs as keys and filepaths as values - var disabledIds = Object.keys(options.relaxerror); + const disabledIds = Object.keys(options.relaxerror); // Lookup disabled IDs filepaths - var returnIds = disabledIds.filter(function(key) { - var paths = options.relaxerror[key]; + const returnIds = disabledIds.filter((key) => { + const paths = options.relaxerror[key]; // handle 'E001': true, 'E001': [] if (!(paths instanceof Array) || paths.length === 0) { @@ -45,7 +43,7 @@ module.exports = function(grunt) { } // handle 'E001': ['*'] - if (paths.indexOf('*') !== -1) { + if (paths.includes('*')) { return true; } @@ -57,65 +55,67 @@ module.exports = function(grunt) { } // Iterate over all specified file groups. - this.files.forEach(function(f) { + this.files.forEach((f) => { - f.src.filter(function(filepath) { + f.src.filter((filepath) => { if (!grunt.file.exists(filepath)) { - grunt.log.warn('Source file "' + filepath + '" not found.'); + grunt.log.warn(`Source file "${filepath}" not found.`); return false; } return true; }) - .forEach(function(filepath) { - - var src = grunt.file.read(filepath); - var reporter = function (lint) { - var isError = lint.id[0] === 'E'; - var isWarning = lint.id[0] === 'W'; - var lintId = isError ? chalk.bgGreen.white(lint.id) : chalk.bgRed.white(lint.id); - var output = false; - - if (lint.elements) { - lint.elements.each(function (_, element) { - var loc = element.startLocation; - grunt.log.warn(filepath + ':' + (loc.line + 1) + ':' + (loc.column + 1), lintId, lint.message); - totalErrCount++; - output = true; - }); + .forEach((filepath) => { - } + const src = grunt.file.read(filepath); + const reporter = (lint) => { + const isError = lint.id[0] === 'E'; + const isWarning = lint.id[0] === 'W'; + const lintId = isError ? chalk.bgGreen.white(lint.id) : chalk.bgRed.white(lint.id); + let output = false; - if (!output) { - grunt.log.warn(filepath + ':', lintId, lint.message); - totalErrCount++; - } + if (lint.elements) { + lint.elements.each((_, element) => { + const loc = element.startLocation; + + grunt.log.warn(`${filepath}:${loc.line + 1}:${loc.column + 1}`, lintId, lint.message); + totalErrCount++; + output = true; + }); - if (!options.showallerrors) { - if (isError && options.stoponerror || isWarning && options.stoponwarning) { - grunt.fail.warn('Too many bootlint errors.'); } - } - }; + if (!output) { + grunt.log.warn(`${filepath}:`, lintId, lint.message); + totalErrCount++; + } - var disabledIds = getDisabledIdsForFilepath(filepath); - bootlint.lintHtml(src, reporter, disabledIds); - totalFileCount++; - }); + if (!options.showallerrors) { + if (isError && options.stoponerror || isWarning && options.stoponwarning) { + grunt.fail.warn('Too many bootlint errors.'); + } + } + + }; + + const disabledIds = getDisabledIdsForFilepath(filepath); + + bootlint.lintHtml(src, reporter, disabledIds); + totalFileCount++; + }); - var errorStr = grunt.util.pluralize(totalErrCount, 'error/errors'); - var fileStr = grunt.util.pluralize(totalFileCount, 'file/files'); + const errorStr = grunt.util.pluralize(totalErrCount, 'error/errors'); + const fileStr = grunt.util.pluralize(totalFileCount, 'file/files'); if (totalErrCount > 0) { if (options.showallerrors) { - grunt.fail.warn(totalErrCount + ' lint ' + errorStr + ' found across ' + totalFileCount + ' ' + fileStr + '.'); + grunt.fail.warn(`${totalErrCount} lint ${errorStr} found across ${totalFileCount} ${fileStr}.`); } else { - grunt.log.writeln().fail(totalErrCount + ' lint ' + errorStr + ' found across ' + totalFileCount + ' ' + fileStr + '.'); + grunt.log.writeln().fail(`${totalErrCount} lint ${errorStr} found across ${totalFileCount} ${fileStr}.`); grunt.log.writeln().fail('For details, look up the lint problem IDs in the Bootlint wiki: https://github.com/twbs/bootlint/wiki'); } } else { - grunt.log.ok(totalFileCount + ' ' + fileStr + ' lint free.'); + grunt.log.ok(`${totalFileCount} ${fileStr} lint free.`); } }); diff --git a/test/bootlint_test.js b/test/bootlint_test.js index dfc3cb2..526e12b 100644 --- a/test/bootlint_test.js +++ b/test/bootlint_test.js @@ -1,6 +1,6 @@ 'use strict'; -var grunt = require('grunt'); +const grunt = require('grunt'); /* ======== A Handy Little Nodeunit Reference ======== @@ -23,106 +23,106 @@ var grunt = require('grunt'); */ exports.bootlint = { - defaultOptions: function(test) { + defaultOptions(test) { test.expect(3); grunt.util.spawn({ grunt: true, args: ['bootlint:defaultOptions', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('test/fixtures/missing-doctype.html') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('test/fixtures/missing-doctype.html'), 'Should print file path'); - test.ok(result.stdout.indexOf('Document is missing a DOCTYPE declaration') >= 0, + test.ok(result.stdout.includes('Document is missing a DOCTYPE declaration'), 'Should warn about missing a DOCTYPE'); - test.ok(result.stdout.indexOf('9 lint errors found across 5 files') >= 0, + test.ok(result.stdout.includes('9 lint errors found across 5 files'), 'Should print number of lint errors and files'); test.done(); }); }, - relaxerror: function(test) { + relaxerror(test) { test.expect(4); grunt.util.spawn({ grunt: true, args: ['bootlint:relaxerror', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('E001') === -1, + }, (err, result) => { + test.ok(!result.stdout.includes('E001'), 'Should not warn about missing a DOCTYPE'); - test.ok(result.stdout.indexOf('W001') >= 0, + test.ok(result.stdout.includes('W001'), 'Should warn about missing charset'); - test.ok(result.stdout.indexOf('W005') === -1, + test.ok(!result.stdout.includes('W005'), 'Should not warn about missing jQuery'); - test.ok(result.stdout.indexOf('1 lint error found across 3 files') >= 0, + test.ok(result.stdout.includes('1 lint error found across 3 files'), 'Should print correct number of lint errors and files'); test.done(); }); }, - stoponerror: function(test) { + stoponerror(test) { test.expect(2); grunt.util.spawn({ grunt: true, args: ['bootlint:stoponerror', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('E001') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('E001'), 'Should warn about missing a DOCTYPE'); - test.ok(result.stdout.indexOf('W001') === -1, + test.ok(!result.stdout.includes('W001'), 'Should not warn about anything after E001'); test.done(); }); }, - stoponwarning: function(test) { + stoponwarning(test) { test.expect(3); grunt.util.spawn({ grunt: true, args: ['bootlint:stoponwarning', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('E001') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('E001'), 'Should display error of missing a DOCTYPE'); - test.ok(result.stdout.indexOf('W001') >= 0, + test.ok(result.stdout.includes('W001'), 'Should warn about W001'); - test.ok(result.stdout.indexOf('E029') === -1, + test.ok(!result.stdout.includes('E029'), 'Should not warn about anything after W001'); test.done(); }); }, - stoponboth: function(test) { + stoponboth(test) { test.expect(1); grunt.util.spawn({ grunt: true, args: ['bootlint:stoponboth', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('E001') === -1, + }, (err, result) => { + test.ok(!result.stdout.includes('E001'), 'Should not warn about E001'); test.done(); }); }, - showallerrors: function(test) { + showallerrors(test) { test.expect(1); grunt.util.spawn({ grunt: true, args: ['bootlint:showallerrors', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('8 lint errors found across 3 files. Use --force to continue.') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('8 lint errors found across 3 files. Use --force to continue.'), 'Should show all errors before hard fail.'); test.done(); }); }, - showallerrorswithstop: function(test) { + showallerrorswithstop(test) { test.expect(1); grunt.util.spawn({ grunt: true, args: ['bootlint:showallerrorswithstop', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('8 lint errors found across 3 files. Use --force to continue.') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('8 lint errors found across 3 files. Use --force to continue.'), 'Should show all errors before hard fail even if stopon* is set.'); test.done(); }); }, - pass: function(test) { + pass(test) { test.expect(1); grunt.util.spawn({ grunt: true, args: ['bootlint:pass', '--no-color'] - }, function(err, result) { - test.ok(result.stdout.indexOf('1 file lint free.') >= 0, + }, (err, result) => { + test.ok(result.stdout.includes('1 file lint free.'), 'Should print correct number of lint free files'); test.done(); }); -- cgit v1.2.3