Skip to content

Commit

Permalink
JSHint lib/* and test runners.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Stockman committed Jul 9, 2013
1 parent 42dfeb8 commit 8eeaf87
Show file tree
Hide file tree
Showing 26 changed files with 217 additions and 141 deletions.
41 changes: 22 additions & 19 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// browser.js - client-side engine
//
/*global less */

var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);

Expand Down Expand Up @@ -43,7 +44,7 @@ less.watch = function () {
less.env = 'development';
initRunningMode();
}
return this.watchMode = true
return this.watchMode = true;
};

less.unwatch = function () {clearInterval(less.watchTimer); return this.watchMode = false; };
Expand Down Expand Up @@ -105,12 +106,12 @@ less.modifyVars = function(record) {
newVars += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
}
less.refresh(false, newVars)
less.refresh(false, newVars);
};

less.refresh = function (reload, newVars) {
var startTime, endTime;
startTime = endTime = new(Date);
startTime = endTime = new Date();

loadStyleSheets(function (e, root, _, sheet, env) {
if (e) {
Expand All @@ -122,9 +123,9 @@ less.refresh = function (reload, newVars) {
log("parsed " + sheet.href + " successfully.");
createCSS(root.toCSS(less), sheet, env.lastModified);
}
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
endTime = new(Date);
log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms');
(env.remaining === 0) && log("css generated in " + (new Date() - startTime) + 'ms');
endTime = new Date();
}, reload, newVars);

loadStyles(newVars);
Expand All @@ -145,6 +146,7 @@ function loadStyles(newVars) {
lessText += "\n" + newVars;
}

/*jshint loopfunc:true */
// use closure to store current value of i
var callback = (function(style) {
return function (e, cssAST) {
Expand All @@ -158,7 +160,7 @@ function loadStyles(newVars) {
} else {
style.innerHTML = css;
}
}
};
})(style);
new(less.Parser)(env).parse(lessText, callback);
}
Expand Down Expand Up @@ -337,20 +339,20 @@ function loadFile(originalHref, currentFileInfo, callback, env, newVars) {
if (newVars) {
lessText += "\n" + newVars;
}
callback(null, lessText, href, newFileInfo, { lastModified: new Date() })
callback(null, lessText, href, newFileInfo, { lastModified: new Date() });
} catch (e) {
callback(e, null, href);
}
return;
}

xhr(href, env.mime, function (data, lastModified) {
doXHR(href, env.mime, function (data, lastModified) {
// per file cache
fileCache[href] = data;

// Use remote copy (re-parse)
try {
callback(null, data, href, newFileInfo, { lastModified: lastModified })
callback(null, data, href, newFileInfo, { lastModified: lastModified });
} catch (e) {
callback(e, null, href);
}
Expand Down Expand Up @@ -406,7 +408,7 @@ function createCSS(styles, sheet, lastModified) {

// If there is no oldCss, just append; otherwise, only append if we need
// to replace oldCss with an updated stylesheet
if (oldCss == null || keepOldCss === false) {
if (oldCss === null || keepOldCss === false) {
var nextEl = sheet && sheet.nextSibling || null;
(nextEl || document.getElementsByTagName('head')[0]).parentNode.insertBefore(css, nextEl);
}
Expand All @@ -427,7 +429,7 @@ function createCSS(styles, sheet, lastModified) {
}
}

function xhr(url, type, callback, errback) {
function doXHR(url, type, callback, errback) {
var xhr = getXMLHttpRequest();
var async = isFileProtocol ? less.fileAsync : less.async;

Expand Down Expand Up @@ -467,10 +469,11 @@ function xhr(url, type, callback, errback) {

function getXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new(XMLHttpRequest);
return new XMLHttpRequest();
} else {
try {
return new(ActiveXObject)("MSXML2.XMLHTTP.3.0");
/*global ActiveXObject */
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch (e) {
log("browser doesn't support AJAX.");
return null;
Expand All @@ -483,13 +486,13 @@ function removeNode(node) {
}

function log(str) {
if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str) }
if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str); }
}

function error(e, rootHref) {
var id = 'less-error-message:' + extractId(rootHref || "");
var template = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
var elem = document.createElement('div'), timer, content, error = [];
var elem = document.createElement('div'), timer, content, errors = [];
var filename = e.filename || rootHref;
var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1];

Expand All @@ -500,8 +503,8 @@ function error(e, rootHref) {
'</h3>' + '<p>in <a href="' + filename + '">' + filenameNoPath + "</a> ";

var errorline = function (e, i, classname) {
if (e.extract[i] != undefined) {
error.push(template.replace(/\{line\}/, (parseInt(e.line) || 0) + (i - 1))
if (e.extract[i] !== undefined) {
errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1))
.replace(/\{class\}/, classname)
.replace(/\{content\}/, e.extract[i]));
}
Expand All @@ -512,7 +515,7 @@ function error(e, rootHref) {
errorline(e, 1, 'line');
errorline(e, 2, '');
content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':</p>' +
'<ul>' + error.join('') + '</ul>';
'<ul>' + errors.join('') + '</ul>';
} else if (e.stack) {
content += '<br/>' + e.stack.split('\n').slice(1).join('<br/>');
}
Expand Down
5 changes: 3 additions & 2 deletions lib/less/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@
destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
}
}
}
})(require('./tree'));
};

})(require('./tree'));
8 changes: 5 additions & 3 deletions lib/less/extend-visitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(function (tree) {
/*jshint loopfunc:true */

tree.extendFinderVisitor = function() {
this._visitor = new tree.visitor(this);
this.contexts = [];
Expand Down Expand Up @@ -246,7 +248,7 @@
haystackElement = hackstackSelector.elements[hackstackElementIndex];

// if we allow elements before our match we can add a potential match every time. otherwise only at the first element.
if (extend.allowBefore || (haystackSelectorIndex == 0 && hackstackElementIndex == 0)) {
if (extend.allowBefore || (haystackSelectorIndex === 0 && hackstackElementIndex === 0)) {
potentialMatches.push({pathIndex: haystackSelectorIndex, index: hackstackElementIndex, matched: 0, initialCombinator: haystackElement.combinator});
}

Expand All @@ -257,7 +259,7 @@
// then each selector in haystackSelectorPath has a space before it added in the toCSS phase. so we need to work out
// what the resulting combinator will be
targetCombinator = haystackElement.combinator.value;
if (targetCombinator == '' && hackstackElementIndex === 0) {
if (targetCombinator === '' && hackstackElementIndex === 0) {
targetCombinator = ' ';
}

Expand Down Expand Up @@ -388,4 +390,4 @@
}
};

})(require('./tree'));
})(require('./tree'));
17 changes: 10 additions & 7 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ tree.functions = {

function hue(h) {
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
else if (h * 2 < 1) return m2;
else if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
else return m1;
if (h * 6 < 1) { return m1 + (m2 - m1) * h * 6; }
else if (h * 2 < 1) { return m2; }
else if (h * 3 < 2) { return m1 + (m2 - m1) * (2/3 - h) * 6; }
else { return m1; }
}
},

Expand Down Expand Up @@ -223,6 +223,7 @@ tree.functions = {
str = quoted.value;

for (var i = 0; i < args.length; i++) {
/*jshint loopfunc:true */
str = str.replace(/%[sda]/i, function(token) {
var value = token.match(/s/i) ? args[i].value : args[i].toCSS();
return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
Expand Down Expand Up @@ -259,6 +260,7 @@ tree.functions = {
},
_math: function (fn, unit, n) {
if (n instanceof tree.Dimension) {
/*jshint eqnull:true */
return new(tree.Dimension)(fn(parseFloat(n.value)), unit == null ? n.unit : unit);
} else if (typeof(n) === 'number') {
return fn(n);
Expand Down Expand Up @@ -463,10 +465,10 @@ tree.functions = {
// use base 64 unless it's an ASCII or UTF-8 format
var charset = mime.charsets.lookup(mimetype);
useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
if (useBase64) mimetype += ';base64';
if (useBase64) { mimetype += ';base64'; }
}
else {
useBase64 = /;base64$/.test(mimetype)
useBase64 = /;base64$/.test(mimetype);
}

var buf = fs.readFileSync(filePath);
Expand Down Expand Up @@ -532,7 +534,7 @@ tree.functions = {
gradientType = "radial";
gradientDirectionSvg = 'cx="50%" cy="50%" r="75%"';
rectangleDimension = 'x="-50" y="-50" width="101" height="101"';
break
break;
default:
throw { type: "Argument", message: "svg-gradient direction must be 'to bottom', 'to right', 'to bottom right', 'to top right' or 'radial'" };
}
Expand Down Expand Up @@ -607,6 +609,7 @@ var mathFunctions = [{name:"ceil"}, {name:"floor"}, {name: "sqrt"}, {name:"abs"}
{name:"atan", unit: "rad"}, {name:"asin", unit: "rad"}, {name:"acos", unit: "rad"}],
createMathFunction = function(name, unit) {
return function(n) {
/*jshint eqnull:true */
if (unit != null) {
n = n.unify();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var less = {
catch (err) { callback(err); }
});
} else {
ee = new(require('events').EventEmitter);
ee = new (require('events').EventEmitter)();

process.nextTick(function () {
parser.parse(input, function (e, root) {
Expand All @@ -42,10 +42,10 @@ var less = {
var message = "";
var extract = ctx.extract;
var error = [];
var stylize = options.color ? require('./lessc_helper').stylize : function (str) { return str };
var stylize = options.color ? require('./lessc_helper').stylize : function (str) { return str; };

// only output a stack if it isn't a less error
if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red') }
if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red'); }

if (!ctx.hasOwnProperty('index') || !extract) {
return ctx.stack || ctx.message;
Expand Down Expand Up @@ -132,7 +132,7 @@ less.Parser.fileLoader = function (file, currentFileInfo, callback, env) {
newFileInfo.filename = pathname;

callback(null, data, pathname, newFileInfo);
};
}

var isUrl = isUrlRe.test( file );
if (isUrl || isUrlRe.test(currentFileInfo.currentDirectory)) {
Expand Down Expand Up @@ -205,7 +205,7 @@ less.Parser.fileLoader = function (file, currentFileInfo, callback, env) {
});
}
}
}
};

require('./env');
require('./functions');
Expand Down
2 changes: 1 addition & 1 deletion lib/less/lessc_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ var lessc_helper = {
};

// Exports helper functions
for (var h in lessc_helper) { exports[h] = lessc_helper[h] }
for (var h in lessc_helper) { exports[h] = lessc_helper[h]; }
Loading

0 comments on commit 8eeaf87

Please sign in to comment.