From d08d77565763f2332907ea148547dce89683ab53 Mon Sep 17 00:00:00 2001 From: Andreas Lind Petersen Date: Mon, 26 May 2014 14:15:13 +0200 Subject: [PATCH] Added jshint config etc. and made the code pass. --- .jshintignore | 2 + .jshintrc | 93 +++++++++++++++++++++++++++++++++++ lib/packers/jimScott.js | 8 ++- lib/spriteBackgroundImages.js | 21 ++++---- package.json | 3 +- 5 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 .jshintignore create mode 100644 .jshintrc diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 0000000..dc767c7 --- /dev/null +++ b/.jshintignore @@ -0,0 +1,2 @@ +node_modules +testdata diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..421be8f --- /dev/null +++ b/.jshintrc @@ -0,0 +1,93 @@ +{ + // JSHint Default Configuration File (as on JSHint website) + // See http://jshint.com/docs/ for more details + + "maxerr" : 50, // {int} Maximum error before stopping + + // Enforcing + "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) + "camelcase" : false, // true: Identifiers must be in camelCase + "curly" : true, // true: Require {} for every new block or scope + "eqeqeq" : true, // true: Require triple equals (===) for comparison + "forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty() + "immed" : true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` + "indent" : 4, // {int} Number of spaces to use for indentation + "latedef" : true, // true: Require variables/functions to be defined before being used + "newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()` + "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` + "noempty" : true, // true: Prohibit use of empty blocks + "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) + "plusplus" : true, // true: Prohibit use of `++` & `--` + "quotmark" : "single", // Quotation mark consistency: + // false : do nothing (default) + // true : ensure whatever is used is consistent + // "single" : require single quotes + // "double" : require double quotes + "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) + "unused" : "vars", // true: Require all defined variables be used + "strict" : false, // true: Requires all functions run in ES5 Strict Mode + "trailing" : true, // true: Prohibit trailing whitespaces + "maxparams" : false, // {int} Max number of formal params allowed per function + "maxdepth" : false, // {int} Max depth of nested blocks (within functions) + "maxstatements" : false, // {int} Max number statements per function + "maxcomplexity" : false, // {int} Max cyclomatic complexity per function + "maxlen" : false, // {int} Max number of characters per line + + // Relaxing + "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) + "boss" : false, // true: Tolerate assignments where comparisons would be expected + "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. + "eqnull" : false, // true: Tolerate use of `== null` + "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) + "esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`) + // (ex: `for each`, multiple try/catch, function expression…) + "evil" : false, // true: Tolerate use of `eval` and `new Function()` + "expr" : false, // true: Tolerate `ExpressionStatement` as Programs + "funcscope" : false, // true: Tolerate defining variables inside control statements" + "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') + "iterator" : false, // true: Tolerate using the `__iterator__` property + "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block + "laxbreak" : false, // true: Tolerate possibly unsafe line breakings + "laxcomma" : false, // true: Tolerate comma-first style coding + "loopfunc" : true, // true: Tolerate functions being defined in loops + "multistr" : false, // true: Tolerate multi-line strings + "proto" : false, // true: Tolerate using the `__proto__` property + "scripturl" : false, // true: Tolerate script-targeted URLs + "smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment + "shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` + "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation + "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` + "validthis" : false, // true: Tolerate using this in a non-constructor function + + // Environments + "browser" : false, // Web Browser (window, document, etc) + "couch" : false, // CouchDB + "devel" : true, // Development/debugging (alert, confirm, etc) + "dojo" : false, // Dojo Toolkit + "jquery" : true, // jQuery + "mootools" : false, // MooTools + "node" : true, // Node.js + "nonstandard" : true, // Widely adopted globals (escape, unescape, etc) + "prototypejs" : false, // Prototype and Scriptaculous + "rhino" : false, // Rhino + "worker" : false, // Web Workers + "wsh" : false, // Windows Scripting Host + "yui" : false, // Yahoo User Interface + + // Legacy + "nomen" : false, // true: Prohibit dangling `_` in variables + "onevar" : false, // true: Allow only one `var` statement per function + "passfail" : false, // true: Stop on first error + "white" : true, // true: Check against strict whitespace and indentation rules + + // Custom Globals + "predef" : [ // additional predefined global variables + "TR", + "TRPAT", + "GETTEXT", + "GETSTATICURL", + "INCLUDE", + "define", + "require" + ] +} diff --git a/lib/packers/jimScott.js b/lib/packers/jimScott.js index 049764e..ea6c5fa 100644 --- a/lib/packers/jimScott.js +++ b/lib/packers/jimScott.js @@ -67,9 +67,7 @@ exports.pack = function (imageInfos, config) { y: 0, width: config.maxWidth || 999999, height: config.maxHeight || 999999 - }, - packingWidth = 0, - packingHeight = 0; + }; // Sort by area, descending: imageInfos.sort(function (a, b) { @@ -85,7 +83,7 @@ exports.pack = function (imageInfos, config) { imageInfos.forEach(function (existingImageInfo) { var imageInfo = _.extend({}, existingImageInfo); if (imageInfo.padding && imageInfo.padding.some(function (v) {return v > 0;})) { - throw new Error("jimScott.pack: Sprite padding not supported"); + throw new Error('jimScott.pack: Sprite padding not supported'); } // Perform the search var coords = findCoords(root, imageInfo.width, imageInfo.height); @@ -94,7 +92,7 @@ exports.pack = function (imageInfos, config) { packingData.width = Math.max(packingData.width, coords.x + imageInfo.width); packingData.height = Math.max(packingData.height, coords.y + imageInfo.height); } else { - throw new Error("jimScott.pack: Cannot fit image"); + throw new Error('jimScott.pack: Cannot fit image'); } _.extend(imageInfo, coords); packingData.imageInfos.push(imageInfo); diff --git a/lib/spriteBackgroundImages.js b/lib/spriteBackgroundImages.js index 9c9c269..6f6a041 100644 --- a/lib/spriteBackgroundImages.js +++ b/lib/spriteBackgroundImages.js @@ -86,7 +86,7 @@ function extractInfoFromCssRule(cssRule, propertyNamePrefix) { var keyName = propertyName.substr(propertyNamePrefix.length).replace(/-([a-z])/g, function ($0, $1) { return $1.toUpperCase(); }); - info[keyName] = cssRule.style[propertyName].replace(/^([\'\"])(.*)\1$/, "$2"); + info[keyName] = cssRule.style[propertyName].replace(/^([\'\"])(.*)\1$/, '$2'); } } return info; @@ -96,16 +96,16 @@ function extractInfoFromCssRule(cssRule, propertyNamePrefix) { module.exports = function () { return function spriteBackgroundImages(assetGraph, cb) { if (!Canvas) { - console.warn("assetgraph-sprite: Canvas not found, skipping"); + console.warn('assetgraph-sprite: Canvas not found, skipping'); return cb(); } // Waiting for https://github.com/LearnBoost/node-canvas/issues/52 - var cairoVersion = Canvas.cairoVersion.split(".").map(function (str) { + var cairoVersion = Canvas.cairoVersion.split('.').map(function (str) { return parseInt(str, 10); }); if (cairoVersion[0] < 1 || cairoVersion[1] < 10) { - console.warn("assetgraph-sprite: Cannot create sprites due to missing canvas.getContext('2d').drawImage() support. Please compile node-canvas with Cairo version 1.10.0 or above."); + console.warn('assetgraph-sprite: Cannot create sprites due to missing canvas.getContext(\'2d\').drawImage() support. Please compile node-canvas with Cairo version 1.10.0 or above.'); return cb(); } @@ -143,7 +143,7 @@ module.exports = function () { } if (spriteGroupName in spriteGroups) { if (spriteGroups[spriteGroupName].placeHolder) { - console.warn("assetgraph-sprite: Multiple definitions of " + spriteGroupName + " sprite"); + console.warn('assetgraph-sprite: Multiple definitions of ' + spriteGroupName + ' sprite'); } spriteGroups[spriteGroupName].placeHolder = _.extend(spriteInfo, { asset: cssAsset, @@ -255,17 +255,18 @@ module.exports = function () { var relationSpriteInfo = getRelationSpriteInfoFromIncomingRelation(incomingRelation), style = incomingRelation.cssRule.style, existingBackgroundPositionValue = style['background-position'], - existingBackgroundValue = style['background'], + existingBackgroundValue = style.background, backgroundOffsetsWereUpdated = false, offsets = [ imageInfo.x, imageInfo.y - ]; + ], + existingOffsets; if (existingBackgroundValue) { var backgroundTokens = existingBackgroundValue.split(/\s+/), - positionTokenIndices = [], - existingOffsets = []; + positionTokenIndices = []; + existingOffsets = []; backgroundTokens.forEach(function (existingBackgroundValueToken, i) { if (/^(?:-?\d+px|0)$/i.test(existingBackgroundValueToken)) { positionTokenIndices.push(i); @@ -296,7 +297,7 @@ module.exports = function () { } else if (existingBackgroundPositionValue) { backgroundPositionPriority = style.getPropertyPriority('background-position'); // FIXME: Silently ignores other units than px - var existingOffsets = existingBackgroundPositionValue.split(' ').map(function (item) { + existingOffsets = existingBackgroundPositionValue.split(' ').map(function (item) { return parseInt(item, 10); }); diff --git a/package.json b/package.json index d238cc7..39f565a 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "devDependencies": { "assetgraph": "=1.2.3", + "jshint": "=2.5.1", "vows": ">=0.6.2" }, "engines": { @@ -31,7 +32,7 @@ "lib": "./lib" }, "scripts": { - "prepublish": "vows" + "lint": "jshint ." }, "main": "lib/spriteBackgroundImages.js" }