Skip to content

Commit

Permalink
Add more 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.
fixes part of craftyjs#951.
  • Loading branch information
mkucko committed May 4, 2016
1 parent 62c4950 commit 36fa18f
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 30 deletions.
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"esversion": 5,
"freeze": true,
"futurehostile": true,
"noarg": true,
"nonbsp": true,
"nonew": true,
"strict": "implied",
"undef": true,
"validthis": true,
"browser": true,
"node": true,
"qunit": true
}
28 changes: 22 additions & 6 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,13 +107,29 @@ module.exports = function (grunt) {
},

jshint: {
options: grunt.file.readJSON('.jshintrc'),
misc: ['Gruntfile.js'],
src: ['src/**/*.js'],
tests: ['tests/**/*.js'],
options: {
trailing: true,
ignores: ['tests/lib/*.js'],
globals: {
tests: {
options: (function() {
var defaults = grunt.file.readJSON('.jshintrc');

// add test-specific globals
defaults.globals = defaults.globals || {};
defaults.globals.Crafty = false;
// add unit test globals
defaults.globals.createTouchEvent = false;
defaults.globals.Round = false;
defaults.globals.resetStage = false;
// add webdriver test globals
defaults.globals.browser = false;
defaults.globals.signalBarrier = false;
defaults.globals.waitBarrier = false;

return defaults;
})(),
files: {
src: ['tests/**/*.js', '!tests/lib/**/*.js']
}
}
},
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
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
4 changes: 2 additions & 2 deletions src/core/core.js
Original file line number Diff line number Diff line change
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 @@ -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
3 changes: 3 additions & 0 deletions 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
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
3 changes: 3 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions src/graphics/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Crafty.extend({
var active = false,
dragging = false,
lastMouse = {};
old = {};

function stopLook(){
dragging = false;
}
Expand All @@ -481,7 +481,7 @@ Crafty.extend({
case 'move':
case 'drag':
if (!dragging) return;
diff = {
var diff = {
x: arg.clientX - lastMouse.x,
y: arg.clientY - lastMouse.y
};
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/webgl-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Crafty = require('../core/core.js'),
document = window.document;

// Object for abstracting out all the gl calls to handle rendering entities with a particular program
RenderProgramWrapper = function(layer, shader){
function RenderProgramWrapper(layer, shader){
this.shader = shader;
this.layer = layer;
this.context = layer.context;
Expand All @@ -12,7 +12,7 @@ RenderProgramWrapper = function(layer, shader){
this.max_size = 1024;
this._indexArray = new Uint16Array(6 * this.array_size);
this._indexBuffer = layer.context.createBuffer();
};
}

RenderProgramWrapper.prototype = {
// Takes an array of attributes; see WebGLLayer's getProgramWrapper method
Expand Down
6 changes: 3 additions & 3 deletions src/isometric/diamond-iso.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Crafty.extend({
var len = this._tiles[_x][_y].length;
for(var _z=0; _z<len; _z++){
if (this._tiles[_x][_y][_z] && obj === this._tiles[_x][_y][_z]){
tHeight=obj.h/this._tile.height;
var tHeight=obj.h/this._tile.height;
for (var i=0; i<tHeight; i++){
this._tiles[_x][_y][_z+i] = undefined;
}
Expand Down Expand Up @@ -156,8 +156,8 @@ Crafty.extend({
var v2 = (left - this.x)/this._tile.width;
var x = v1+v2;
var y = v1-v2;
inX = x>0 && x<this._map.width;
inY = y>0 && y<this._map.height;
var inX = x>0 && x<this._map.width;
var inY = y>0 && y<this._map.height;
if (!inX || !inY){
return undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/sound/sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ Crafty.extend({
stop: function (id) {
if (!Crafty.support.audio)
return;
var c;
for (var i in this.channels) {
c = this.channels[i];
if ( (!id && c.active) || c._is(id) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/spatial/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ Crafty.math.Matrix2D = (function () {
* @param {Number=0} e
* @param {Number=0} f
*/
Matrix2D = function (a, b, c, d, e, f) {
function Matrix2D (a, b, c, d, e, f) {
if (a instanceof Matrix2D) {
this.a = a.a;
this.b = a.b;
Expand All @@ -720,7 +720,7 @@ Crafty.math.Matrix2D = (function () {
this.f = f;
} else if (arguments.length > 0)
throw "Unexpected number of arguments for Matrix2D()";
}; // class Matrix2D
} // class Matrix2D

Matrix2D.prototype.a = 1;
Matrix2D.prototype.b = 0;
Expand Down
4 changes: 2 additions & 2 deletions tests/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
this.endedListeners.push(listener);
break;
default:
throw new Exception("Not implemented");
throw("Not implemented");
}
};
this.removeEventListener = function(event, listener) {
Expand All @@ -26,7 +26,7 @@
if (ind) this.endedListeners.splice(ind, 1);
break;
default:
throw new Exception("Not implemented");
throw("Not implemented");
}
};

Expand Down
12 changes: 7 additions & 5 deletions tests/common.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
////////////////////////
/// Helper functions ///
////////////////////////
//////////////////////
// Helper functions //
//////////////////////

resetStage = function() {
var global = typeof GLOBAL !== 'undefined' ? GLOBAL : window;

global.resetStage = function() {
Crafty.viewport.reset();
Crafty.viewport.scroll('_x', 0);
Crafty.viewport.scroll('_y', 0);
Crafty.viewport.clampToEntities = true;
};

Round = function(x){
global.Round = function(x){
return Math.round(x*100)/100;
};

Expand Down
2 changes: 1 addition & 1 deletion tests/index-headless.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** CRAFTY-JS **/
Crafty = require('../src/crafty-headless.js')();
GLOBAL.Crafty = require('../src/crafty-headless.js')();
Crafty.init();

/** COMMON TEST CODE **/
Expand Down
4 changes: 2 additions & 2 deletions tests/webdriver/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ window.addEventListener('load', function() {
};

// wait for webdriver signal
window.barrierCallbacks = {};
window.barrierSignals = {};
var barrierCallbacks = window.barrierCallbacks = {};
var barrierSignals = window.barrierSignals = {};
window.waitBarrier = function(label, callback) {
if (barrierSignals[label]) { // signal already received, trigger callback immediately
callback.call(null);
Expand Down

0 comments on commit 36fa18f

Please sign in to comment.