Skip to content

Commit

Permalink
Add stricter static checks
Browse files Browse the repository at this point in the history
Update jshint version.
Enforce stricter jshint checks:
* Add missing Crafty references to source files.
* Fix leaks of vars into global namespace.
* Removes unused vars.
* Add more stricter checks
* Switch to strict equality comparison.
fixes part of craftyjs#951.
  • Loading branch information
mkucko committed May 7, 2016
1 parent 62c4950 commit 77b8160
Show file tree
Hide file tree
Showing 57 changed files with 241 additions and 226 deletions.
15 changes: 15 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"eqeqeq": true,
"esversion": 5,
"freeze": true,
"futurehostile": true,
"noarg": true,
"nonbsp": true,
"nonew": true,
"strict": "implied",
"undef": true,
"unused": true,
"validthis": true,
"browser": true,
"node": true
}
16 changes: 8 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function (grunt) {

var apiServer = require("./build/api-gen/dynamic-server.js");
function runApiServer() {
var done = this.async();
this.async();
apiServer(grunt, "./build/api.json");
setTimeout(function(){
open("http://localhost:8080");
Expand Down Expand Up @@ -107,19 +107,19 @@ module.exports = function (grunt) {
},

jshint: {
options: {
jshintrc: true
},
misc: ['Gruntfile.js'],
src: ['src/**/*.js'],
tests: ['tests/**/*.js'],
options: {
trailing: true,
ignores: ['tests/lib/*.js'],
globals: {
}
}
tests: ['tests/**/*.js', '!tests/lib/**/*.js']
},


qunit: {
options: {
noGlobals: true
},
browser: ['tests/index.html']
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"grunt-banner": "^0.6.0",
"grunt-browserify": "^4.0.1",
"grunt-contrib-connect": "^0.11.2",
"grunt-contrib-jshint": "^0.11.3",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-qunit": "^0.7.0",
"grunt-contrib-uglify": "^0.11.0",
"grunt-contrib-watch": "^0.6.1",
Expand Down
2 changes: 1 addition & 1 deletion src/controls/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Crafty.c("Draggable", {
dragDirection: function (dir) {
if (typeof dir === 'undefined') {
this._dir = null;
} else if (("" + parseInt(dir, 10)) == dir) { //dir is a number
} else if (+dir === dir) { //dir is a number
this._dir = {
x: Math.cos(dir / 180 * Math.PI),
y: Math.sin(dir / 180 * Math.PI)
Expand Down
50 changes: 23 additions & 27 deletions src/controls/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,17 @@ Crafty.extend({
if (!Crafty.mouseObjs) return;
Crafty.lastEvent = e;

var maxz = -1,
tar = e.target ? e.target : e.srcElement,
var tar = e.target ? e.target : e.srcElement,
closest,
q,
i = 0,
l,
pos = Crafty.domHelper.translate(e.clientX, e.clientY),
x, y,
type = e.type;

//Normalize button according to http://unixpapa.com/js/mouse.html
if (typeof e.which === 'undefined') {
e.mouseButton = (e.button < 2) ? Crafty.mouseButtons.LEFT : ((e.button == 4) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT);
e.mouseButton = (e.button < 2) ? Crafty.mouseButtons.LEFT : ((e.button === 4) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT);
} else {
e.mouseButton = (e.which < 2) ? Crafty.mouseButtons.LEFT : ((e.which == 2) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT);
e.mouseButton = (e.which < 2) ? Crafty.mouseButtons.LEFT : ((e.which === 2) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT);
}

e.realX = x = Crafty.mousePos.x = pos.x;
Expand All @@ -176,9 +172,9 @@ Crafty.extend({
closest.trigger("MouseDown", e);
} else if (type === "mouseup") {
closest.trigger("MouseUp", e);
} else if (type == "dblclick") {
} else if (type === "dblclick") {
closest.trigger("DoubleClick", e);
} else if (type == "click") {
} else if (type === "click") {
closest.trigger("Click", e);
} else if (type === "mousemove") {
closest.trigger("MouseMove", e);
Expand All @@ -200,7 +196,7 @@ Crafty.extend({
Crafty.viewport.mouselook('start', e);
} else if (type === "mousemove") {
Crafty.viewport.mouselook('drag', e);
} else if (type == "mouseup") {
} else if (type === "mouseup") {
Crafty.viewport.mouselook('stop');
}
}
Expand Down Expand Up @@ -310,7 +306,7 @@ Crafty.extend({

if (idx >= 0) {
if(typeof this.fingers[idx].entity !== "undefined")
if (this.fingers[idx].entity == closest) {
if (this.fingers[idx].entity === closest) {
this.fingers[idx].entity.trigger("TouchMove", touches[i]);
} else {
if (typeof closest === "object") closest.trigger("TouchStart", touches[i]);
Expand All @@ -325,14 +321,14 @@ Crafty.extend({

handleEnd: function (e) {
var touches = e.changedTouches,
eventName = e.type == "touchcancel" ? "TouchCancel" : "TouchEnd";
eventName = e.type === "touchcancel" ? "TouchCancel" : "TouchEnd";
for (var i = 0, l = touches.length; i < l; i++) {
var idx = this.fingerDownIndexById(touches[i].identifier);

if (idx >= 0) {
if (this.fingers[idx].entity)
this.fingers[idx].entity.trigger(eventName);
this.fingers.splice(idx, 1);
if (this.fingers[idx].entity)
this.fingers[idx].entity.trigger(eventName);
this.fingers.splice(idx, 1);
}
}
},
Expand All @@ -349,18 +345,18 @@ Crafty.extend({
for (var i = 0, l = this.fingers.length; i < l; i++) {
var id = this.fingers[i].identifier;

if (id == idToFind) {
return i;
}
if (id === idToFind) {
return i;
}
}
return -1;
},

fingerDownIndexByEntity: function(entityToFind) {
for (var i = 0, l = this.fingers.length; i < l; i++) {
var ent = this.fingers[i].entity;

if (ent == entityToFind) {
if (ent === entityToFind) {
return i;
}
}
Expand Down Expand Up @@ -390,7 +386,7 @@ Crafty.extend({
);
first.target.dispatchEvent(simulatedEvent);
// trigger click when it should be triggered
if (lastEvent !== null && lastEvent.type == 'mousedown' && type == 'mouseup') {
if (lastEvent !== null && lastEvent.type === 'mousedown' && type === 'mouseup') {
type = 'click';
simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
Expand Down Expand Up @@ -438,8 +434,8 @@ Crafty.extend({
closest, current, q, l, i, maxz = -Infinity;

//if it's a DOM element with component we are done
if (tar.nodeName != "CANVAS") {
while (typeof (tar.id) != 'string' && tar.id.indexOf('ent') == -1) {
if (tar.nodeName !== "CANVAS") {
while (typeof (tar.id) !== 'string' && tar.id.indexOf('ent') === -1) {
tar = tar.parentNode;
}
var ent = Crafty(parseInt(tar.id.replace('ent', ''), 10));
Expand Down Expand Up @@ -515,13 +511,13 @@ Crafty.extend({
* @example
* ~~~
* Crafty.bind('KeyDown', function(e) {
* if (e.key == Crafty.keys.LEFT_ARROW) {
* if (e.key === Crafty.keys.LEFT_ARROW) {
* Crafty.viewport.x++;
* } else if (e.key == Crafty.keys.RIGHT_ARROW) {
* } else if (e.key === Crafty.keys.RIGHT_ARROW) {
* Crafty.viewport.x--;
* } else if (e.key == Crafty.keys.UP_ARROW) {
* } else if (e.key === Crafty.keys.UP_ARROW) {
* Crafty.viewport.y++;
* } else if (e.key == Crafty.keys.DOWN_ARROW) {
* } else if (e.key === Crafty.keys.DOWN_ARROW) {
* Crafty.viewport.y--;
* }
* });
Expand Down Expand Up @@ -557,7 +553,7 @@ Crafty.extend({
//prevent bubbling up for all keys except backspace and F1-F12.
//Among others this prevent the arrow keys from scrolling the parent page
//of an iframe hosting the game
if (Crafty.selected && !(e.key == 8 || e.key >= 112 && e.key <= 135)) {
if (Crafty.selected && !(e.key === 8 || e.key >= 112 && e.key <= 135)) {
if (original.stopPropagation) original.stopPropagation();
else original.cancelBubble = true;

Expand Down
3 changes: 3 additions & 0 deletions src/core/animation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var Crafty = require('../core/core.js');


/**@
* #Crafty.easing
* @category Animation
Expand Down
16 changes: 8 additions & 8 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var Crafty = function (selector) {
};
// Internal variables
var GUID, frame, components, entities, handlers, onloads,
slice, rlist, rspace, milliSecPerFrame;
slice, rlist, rspace;


components = {}; // Map of components and their functions
Expand Down Expand Up @@ -225,7 +225,7 @@ Crafty.fn = Crafty.prototype = {
*
* @see Crafty Core#.setName
*/
getName: function (name) {
getName: function () {
return this._entityName;
},

Expand Down Expand Up @@ -909,7 +909,7 @@ Crafty.fn = Crafty.prototype = {
clone.addComponent(comp);
}
for (prop in this) {
if (prop != "0" && prop != "_global" && prop != "_changed" && typeof this[prop] != "function" && typeof this[prop] != "object") {
if (prop !== "0" && prop !== "_global" && prop !== "_changed" && typeof this[prop] !== "function" && typeof this[prop] !== "object") {
clone[prop] = this[prop];
}
}
Expand Down Expand Up @@ -1120,7 +1120,7 @@ Crafty._callbackMethods = {
// They are spliced out when _runCallbacks is invoked, not here
// (This function might be called in the middle of a callback, which complicates the logic)
for (var i = 0; i < callbacks.length; i++) {
if (!fn || callbacks[i] == fn) {
if (!fn || callbacks[i] === fn) {
delete callbacks[i];
}
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ Crafty.extend({
step: function () {
var drawTimeStart, dt, lastFrameTime, loops = 0;

currentTime = new Date().getTime();
var currentTime = new Date().getTime();
if (endTime > 0)
Crafty.trigger("MeasureWaitTime", currentTime - endTime);

Expand Down Expand Up @@ -1551,7 +1551,7 @@ Crafty.extend({
* @see Crafty.timer.steptype
*/
FPS: function (value) {
if (typeof value == "undefined")
if (typeof value === "undefined")
return FPS;
else {
FPS = value;
Expand Down Expand Up @@ -2003,7 +2003,7 @@ function UID() {
*/

function clone(obj) {
if (obj === null || typeof (obj) != 'object')
if (obj === null || typeof (obj) !== 'object')
return obj;

var temp = obj.constructor(); // changed
Expand All @@ -2015,7 +2015,7 @@ function clone(obj) {

// export Crafty
if (typeof define === 'function') { // AMD
define('crafty', [], function () {
define('crafty', [], function () { // jshint ignore:line
return Crafty;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/extensions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Crafty = require('./core');
var Crafty = require('../core/core.js');
var document = (typeof window !== "undefined") && window.document;

/**@
Expand Down
6 changes: 3 additions & 3 deletions src/core/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ module.exports = {
return f.substr(f.lastIndexOf('.') + 1).toLowerCase();
},
getFilePath = function(type,f) {
return (f.search("://") === -1 ? (type == "audio" ? paths.audio + f : paths.images + f) : f);
return (f.search("://") === -1 ? (type === "audio" ? paths.audio + f : paths.images + f) : f);
},
// returns null if 'a' is not already a loaded asset, obj otherwise
isAsset = function(a) {
Expand All @@ -255,7 +255,7 @@ module.exports = {
return Crafty.support.audio && Crafty.audio.supports(getExt(f));
},
isValidImage = function(f) {
return Crafty.image_whitelist.indexOf(getExt(f)) != -1;
return Crafty.image_whitelist.indexOf(getExt(f)) !== -1;
},
onImgLoad = function(obj,url) {
obj.onload = pro;
Expand Down Expand Up @@ -411,7 +411,7 @@ module.exports = {
var current, fileUrl, type, asset,
paths = Crafty.paths(),
getFilePath = function(type,f) {
return (f.search("://") === -1 ? (type == "audio" ? paths.audio + f : paths.images + f) : f);
return (f.search("://") === -1 ? (type === "audio" ? paths.audio + f : paths.images + f) : f);
};

for (type in data) {
Expand Down
5 changes: 4 additions & 1 deletion src/core/model.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var Crafty = require('../core/core.js');


/**@
* #Model
* @category Model
Expand Down Expand Up @@ -46,7 +49,7 @@ module.exports = {
* update the name data attribute on the model.
*/
_changed_triggers: function(data, options) {
var key, trigger_data;
var key;
options = Crafty.extend.call({pre: ''}, options);
for (key in data) {
this.trigger('Change[' + options.pre + key + ']', data[key]);
Expand Down
2 changes: 1 addition & 1 deletion src/core/systems.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Crafty._registerLazySystem = function(name, obj) {

// Each system has its properties and methods copied onto an object of this type
Crafty.CraftySystem = (function(){
systemID = 1;
var systemID = 1;
return function(name, template) {
this.name = name;
if (!template) return this;
Expand Down
2 changes: 1 addition & 1 deletion src/core/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports = {
var index = this._delays.length;
while (--index >= 0) {
var item = this._delays[index];
if(item && item.callback == callback){
if(item && item.callback === callback){
this._delays[index] = false;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/tween.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var Crafty = require('../core/core.js');


/**@
* #Tween
* @category Animation
Expand Down Expand Up @@ -106,7 +109,7 @@ module.exports = {
*/
cancelTween: function(target){
if (typeof target === "string"){
if (typeof this.tweenGroup[target] == "object" )
if (typeof this.tweenGroup[target] === "object" )
delete this.tweenGroup[target][target];
} else if (typeof target === "object") {
for (var propname in target)
Expand Down
2 changes: 1 addition & 1 deletion src/crafty-headless.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function() {
reset: function() {}
};
Crafty.c("Keyboard", {
isDown: function (key) {
isDown: function () {
return false;
}
});
Expand Down
Loading

0 comments on commit 77b8160

Please sign in to comment.