Skip to content

Commit

Permalink
Merge pull request twbs#182 from twbs/W005
Browse files Browse the repository at this point in the history
improve W005 checker by properly parsing URL and checking filename
  • Loading branch information
cvrebert committed Nov 13, 2014
2 parents 58f8812 + 41f1b89 commit 6bbb189
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"browser": {
"binary-search": false,
"cheerio": "jquery",
"url": "./src/url.js",
"./cli": false,
"./location": false
},
Expand Down
21 changes: 15 additions & 6 deletions src/bootlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/*eslint-env node */

var cheerio = require('cheerio');
var parseUrl = require('url').parse;
var semver = require('semver');
var _location = require('./location');
var LocationIndex = _location.LocationIndex;
Expand Down Expand Up @@ -359,19 +360,27 @@ var LocationIndex = _location.LocationIndex;

// check for jQuery <script>s
var jqueries = $([
'script[src*="jquery.min"]',
'script[src*="jQuery.min"]',
'script[src*="jquery.js"]',
'script[src*="jQuery.js"]'
'script[src*="jquery"]',
'script[src*="jQuery"]'
].join(','));
if (!jqueries.length) {
reporter(NO_JQUERY);
return;
}
jqueries.each(function () {
var script = $(this);
var matches = script.attr('src').match(/\d+\.\d+\.\d+/g);
if (!matches) {
var pathSegments = parseUrl(script.attr('src')).pathname.split('/');
var filename = pathSegments[pathSegments.length - 1];
if (!/^j[qQ]uery(\.min)?\.js$/.test(filename)) {
return;
}
var matches = pathSegments.map(function (segment) {
var match = segment.match(/^\d+\.\d+\.\d+$/);
return match ? match[0] : null;
}).filter(function (match) {
return match !== null;
});
if (!matches.length) {
return;
}
var version = matches[matches.length - 1];
Expand Down
38 changes: 38 additions & 0 deletions src/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*eslint-env node, browser */
/* jshint browser: true */
/**
* Simple lightweight shim of Node.js's `url.parse()`
* ( http://nodejs.org/docs/latest/api/url.html )
* for use within browsers.
*/
(function () {
'use strict';

// Only properties common to both browsers and Node.js are supported.
// For what browsers support, see https://developer.mozilla.org/en-US/docs/Web/API/URLUtils
var URL_PROPERTIES = [
'hash',
'host',
'hostname',
'href',
'pathname',
'port',
'protocol',
'search'
];

/**
* @param {string} urlStr URL to parse
* @returns {object} Object with fields representing the various parts of the parsed URL.
*/
function parse(urlStr) {
var anchor = document.createElement('a');
anchor.href = urlStr;
var urlObj = {};
URL_PROPERTIES.forEach(function (property) {
urlObj[property] = anchor[property];
});
return urlObj;
}
exports.parse = parse;
})();

0 comments on commit 6bbb189

Please sign in to comment.