From cff261c4caed469b47fdf3ab11c4990364de9dd3 Mon Sep 17 00:00:00 2001 From: Chris Rebert Date: Sun, 18 Jan 2015 22:58:11 -0800 Subject: [PATCH] Emit different warning message when jQuery and bootstrap.js are both absent; fixes #72 --- Gruntfile.js | 2 +- src/bootlint.js | 45 +++++++++++-------- test/bootlint_test.js | 7 ++- .../jquery/and_bs_js_both_missing.html | 24 ++++++++++ test/fixtures/jquery/missing.html | 3 +- 5 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 test/fixtures/jquery/and_bs_js_both_missing.html diff --git a/Gruntfile.js b/Gruntfile.js index ad0c0ba..22b36c6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -54,7 +54,7 @@ module.exports = function (grunt) { options: { timeout: 10000 }, - files: ['test/fixtures/**/*.html', '!test/fixtures/jquery/missing.html', '!test/fixtures/charset/not-utf8.html'] + files: ['test/fixtures/**/*.html', '!test/fixtures/jquery/missing.html', '!test/fixtures/jquery/and_bs_js_both_missing.html', '!test/fixtures/charset/not-utf8.html'] }, jshint: { options: { diff --git a/src/bootlint.js b/src/bootlint.js index ed5ff2b..a52d297 100644 --- a/src/bootlint.js +++ b/src/bootlint.js @@ -161,6 +161,24 @@ var LocationIndex = _location.LocationIndex; return runs; } + function bootstrapScriptsIn($) { + var longhands = $('script[src*="bootstrap.js"]').filter(function (i, script) { + var url = $(script).attr('src'); + var filename = filenameFromUrl(url); + return filename === "bootstrap.js"; + }); + var minifieds = $('script[src*="bootstrap.min.js"]').filter(function (i, script) { + var url = $(script).attr('src'); + var filename = filenameFromUrl(url); + return filename === "bootstrap.min.js"; + }); + + return { + longhands: longhands, + minifieds: minifieds + }; + } + /** * @param {integer} id Unique string ID for this type of lint error. Of the form "E###" (e.g. "E123"). * @param {string} message Human-readable string describing the error @@ -333,7 +351,10 @@ var LocationIndex = _location.LocationIndex; }); addLinter("W005", function lintJquery($, reporter) { var OLD_JQUERY = "Found what might be an outdated version of jQuery; Bootstrap requires jQuery v" + MIN_JQUERY_VERSION + " or higher"; - var NO_JQUERY = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"; + var NO_JQUERY_BUT_BS_JS = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"; + var NO_JQUERY_NOR_BS_JS = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work; however, you might not be using Bootstrap's JavaScript"; + var bsScripts = bootstrapScriptsIn($); + var hasBsJs = !!(bsScripts.minifieds.length || bsScripts.longhands.length); var theWindow = null; try { /*eslint-disable no-undef, block-scoped-var */ @@ -384,7 +405,7 @@ var LocationIndex = _location.LocationIndex; 'script[src*="jQuery"]' ].join(',')); if (!jqueries.length) { - reporter(NO_JQUERY); + reporter(hasBsJs ? NO_JQUERY_BUT_BS_JS : NO_JQUERY_NOR_BS_JS); return; } jqueries.each(function () { @@ -420,24 +441,10 @@ var LocationIndex = _location.LocationIndex; } }); addLinter("E007", function lintBootstrapJs($, reporter) { - var longhands = $('script[src*="bootstrap.js"]').filter(function (i, script) { - var url = $(script).attr('src'); - var filename = filenameFromUrl(url); - return filename === "bootstrap.js"; - }); - if (!longhands.length) { - return; + var scripts = bootstrapScriptsIn($); + if (scripts.longhands.length && scripts.minifieds.length) { + reporter("Only one copy of Bootstrap's JS should be included; currently the webpage includes both bootstrap.js and bootstrap.min.js", scripts.longhands.add(scripts.minifieds)); } - var minifieds = $('script[src*="bootstrap.min.js"]').filter(function (i, script) { - var url = $(script).attr('src'); - var filename = filenameFromUrl(url); - return filename === "bootstrap.min.js"; - }); - if (!minifieds.length) { - return; - } - - reporter("Only one copy of Bootstrap's JS should be included; currently the webpage includes both bootstrap.js and bootstrap.min.js", longhands.add(minifieds)); }); addLinter("W006", function lintTooltipsOnDisabledElems($, reporter) { var selector = [ diff --git a/test/bootlint_test.js b/test/bootlint_test.js index 765e4f4..5b16509 100644 --- a/test/bootlint_test.js +++ b/test/bootlint_test.js @@ -176,7 +176,7 @@ exports.bootlint = { test.done(); }, 'jQuery': function (test) { - test.expect(4); + test.expect(5); test.deepEqual(lintHtml(utf8Fixture('jquery/present.html')), [], 'should not complain when jQuery is present.'); @@ -188,7 +188,10 @@ exports.bootlint = { 'should complain about old version of jQuery based on URL'); test.deepEqual(lintHtml(utf8Fixture('jquery/missing.html')), ["Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"], - 'should complain when jQuery appears to be missing.'); + "should complain when jQuery appears to be missing."); + test.deepEqual(lintHtml(utf8Fixture('jquery/and_bs_js_both_missing.html')), + ["Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work; however, you might not be using Bootstrap's JavaScript"], + "should complain differently when jQuery appears to be missing but Bootstrap's JS is also missing."); test.done(); }, 'bootstrap[.min].js': function (test) { diff --git a/test/fixtures/jquery/and_bs_js_both_missing.html b/test/fixtures/jquery/and_bs_js_both_missing.html new file mode 100644 index 0000000..c3fa96d --- /dev/null +++ b/test/fixtures/jquery/and_bs_js_both_missing.html @@ -0,0 +1,24 @@ + + + + + + + Test + + + + + + + + +
+
    +
  1. +
+ + diff --git a/test/fixtures/jquery/missing.html b/test/fixtures/jquery/missing.html index c3fa96d..3175d4e 100644 --- a/test/fixtures/jquery/missing.html +++ b/test/fixtures/jquery/missing.html @@ -9,6 +9,7 @@ + @@ -18,7 +19,7 @@
    -
  1. +