strict mode everywhere; add `.jshintrc` to turn codebase into oh-so-perfect code

This commit is contained in:
Christopher Van 2014-09-22 17:06:18 -07:00
Родитель cf29d0c4a3
Коммит 0ad0187685
10 изменённых файлов: 333 добавлений и 145 удалений

86
.jshintrc Normal file
Просмотреть файл

@ -0,0 +1,86 @@
{
// See http://jshint.com/docs/ for 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
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // 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
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // 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" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"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" : 79, // {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`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (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" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"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" : true, // Web Browser (window, document, etc)
"browserify" : true, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jasmine" : false, // Jasmine
"jquery" : false, // jQuery
"mocha" : true, // Mocha
"mootools" : false, // MooTools
"node" : false, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"qunit" : false, // QUnit
"rhino" : false, // Rhino
"shelljs" : false, // ShellJS
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface
// Custom Globals
"globals" : {} // additional predefined global variables
}

111
dist/js/gamepad-client.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2
dist/js/gamepad-client.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

134
dist/js/gamepad-host.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2
dist/js/gamepad-host.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -9,7 +9,7 @@ var error = utils.error;
var trace = utils.trace;
utils.polyfill(window);
utils.polyfill();
utils.lockOrientation('landscape-primary');
@ -28,8 +28,8 @@ document.addEventListener('keyup', function (e) {
trace('User pressed "F"; entering/exiting fullscreen');
return utils.toggleFullScreen();
case 78: // Pressing NF (really just N) should toggle full-screen mode.
trace('User pressed "NF"; exiting fullscreen and will not automatically ' +
'open next time');
trace('User pressed "NF"; exiting fullscreen and will not ' +
'automatically open next time');
localStorage.disableAutoFullScreen = '1';
return utils.toggleFullScreen();
}
@ -51,7 +51,7 @@ document.addEventListener('click', function (e) {
var peerId = utils.getPeerId();
var peer = new Peer('controller_' + peerId, {
var peer = new window.Peer('controller_' + peerId, {
key: settings.PEERJS_KEY,
debug: settings.DEBUG ? 3 : 0
});
@ -78,7 +78,8 @@ conn.on('open', function () {
function send(msg) {
if (settings.DEBUG) {
console.log('Sent: ' + (typeof msg === 'object' ? JSON.stringify(msg) : msg));
console.log('Sent: ' +
(typeof msg === 'object' ? JSON.stringify(msg) : msg));
}
conn.send(msg);
}
@ -115,7 +116,8 @@ function angularShape(canvas, coords) {
}
function linearFill(shape, color1, color2, coords) {
var bg = shape.createLinearGradient(coords[0], coords[1], coords[2], coords[3]);
var bg = shape.createLinearGradient(coords[0], coords[1], coords[2],
coords[3]);
bg.addColorStop(0, color1);
bg.addColorStop(1, color2);
shape.fillStyle = bg;
@ -186,15 +188,16 @@ var gamepadState = {
function bindPress(button, eventName, isPressed) {
document.querySelector('#' + button).addEventListener(eventName, function (e) {
// Handle D-pad presses.
if (e.target && e.target.parentNode === dpad) {
dpad.classList.toggle(this.id);
}
document.querySelector('#' + button)
.addEventListener(eventName, function (e) {
// Handle D-pad presses.
if (e.target && e.target.parentNode === dpad) {
dpad.classList.toggle(this.id);
}
gamepadState[button] = isPressed;
send({type: 'state', data: gamepadState});
});
gamepadState[button] = isPressed;
send({type: 'state', data: gamepadState});
});
}

Просмотреть файл

@ -3,14 +3,14 @@
// var peer = require('./lib/peer');
// var Promise = require('./lib/promise-1.0.0.js'); // jshint ignore:line
var Modal = require('./lib/modal');
var Modal = require('./lib/modal')(window, document);
var settings = require('./settings');
var utils = require('./lib/utils');
var utils = require('./lib/utils')(window, document);
var error = utils.error;
var trace = utils.trace;
utils.polyfill(window);
utils.polyfill();
/**
@ -39,12 +39,12 @@ gamepad.state = {};
* @memberOf gamepad
*/
gamepad.peerHandshake = function (peerId) {
return new Promise(function (resolve, reject) {
return new Promise(function (resolve) {
if (!peerId) {
peerId = utils.getPeerId(); // The host ID.
}
var peer = new Peer(peerId, {
var peer = new window.Peer(peerId, {
key: settings.PEERJS_KEY,
debug: settings.DEBUG ? 3 : 0
});
@ -78,11 +78,13 @@ gamepad.peerConnect = function (peer) {
gamepad._updateState(data.data);
break;
default:
console.warn('WebRTC message received of unknown type: "' + data.type + '"');
console.warn(
'WebRTC message received of unknown type: "' + data.type + '"');
break;
}
trace('Received: ' + (typeof data === 'object' ? JSON.stringify(data) : ''));
trace('Received: ' +
(typeof data === 'object' ? JSON.stringify(data) : ''));
});
conn.on('error', function (err) {
@ -109,16 +111,20 @@ gamepad.pair = function (peerId) {
return new Promise(function (resolve) {
return gamepad.peerHandshake(peerId).then(function (peer) {
var pairId = peer.id; // This should be the same as `peerId`, but this comes from PeerJS, which is the source of truth.
// `pairId` should be the same as `peerId`,
// but `peer.id` is the source of truth.
var pairId = peer.id;
var pairIdEsc = encodeURIComponent(pairId);
var pairUrl = galaxyOrigin + '/client.html?' + pairIdEsc;
// Update the querystring in the address bar.
window.history.replaceState(null, null, window.location.pathname + '?' + pairIdEsc);
window.history.replaceState(null, null,
window.location.pathname + '?' + pairIdEsc);
var content = (
'<div class="modal-inner modal-pair">' +
'<h2>URL</h2><p><a href="' + pairUrl + '" class="pair-url" target="_blank">' + pairUrl + '</a></p>' +
'<h2>URL</h2><p><a href="' + pairUrl +
'" class="pair-url" target="_blank">' + pairUrl + '</a></p>' +
'<h2>Code</h2><p class="pair-code">' + pairIdEsc + '</p>' +
'</div>'
);
@ -219,7 +225,7 @@ gamepad._bind = function (eventName, listener) {
* @param {Function} [listener] (Optional) The listener function to remove.
* @return {Boolean} Was unbinding the listener successful.
*/
Gamepad.prototype.unbind = function (eventName, listener) {
gamepad.prototype.unbind = function (eventName, listener) {
// Remove everything for all event types.
if (typeof eventName === 'undefined') {
this.listeners = {};

Просмотреть файл

@ -1,3 +1,6 @@
module.exports = function (window, document) {
'use strict';
var utils = require('./utils');
@ -65,4 +68,6 @@ Modal.prototype.open = function () {
};
module.exports = Modal;
return Modal;
};

Просмотреть файл

@ -1,5 +1,9 @@
module.exports = function (window, document) {
'use strict';
function trace(text, level) {
console[level || 'log']((window.performance.now() / 1000).toFixed(3) + ': ' + text);
console[level || 'log'](
(window.performance.now() / 1000).toFixed(3) + ': ' + text);
}
@ -13,17 +17,18 @@ function warn(text) {
}
function polyfill(win) {
if (!('performance' in win)) {
win.performance = {
function polyfill() {
if (!('performance' in window)) {
window.performance = {
now: function () {
return +new Date();
}
};
}
if (('origin' in win.location)) {
win.location.origin = win.location.protocol + '//' + win.location.host;
if (('origin' in window.location)) {
window.location.origin = (window.location.protocol + '//' +
window.location.host);
}
}
@ -51,7 +56,7 @@ function fieldFocused(e) {
function hasTouchEvents() {
return ('ontouchstart' in window ||
window.DocumentTouch && document instanceof DocumentTouch);
window.DocumentTouch && document instanceof window.DocumentTouch);
}
function injectCSS(opts) {
@ -92,7 +97,8 @@ function toggleFullScreen() {
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
document.documentElement.webkitRequestFullscreen(
Element.ALLOW_KEYBOARD_INPUT);
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
}
@ -111,16 +117,16 @@ function toggleFullScreen() {
}
function lockOrientation() {
var lo = (screen.LockOrientation ||
screen.mozLockOrientation ||
screen.webkitLockOrientation ||
screen.msLockOrientation);
function lockOrientation(orientation) {
var lo = (window.screen.LockOrientation ||
window.screen.mozLockOrientation ||
window.screen.webkitLockOrientation ||
window.screen.msLockOrientation);
if (!lo) {
return warn('Orientation could not be locked');
}
lo(orientation);
return lo(orientation);
}
@ -132,16 +138,20 @@ function triggerEvent(type) {
}
module.exports.trace = trace;
module.exports.error = error;
module.exports.warn = warn;
module.exports.polyfill = polyfill;
module.exports.getPeerId = getPeerId;
module.exports.fieldFocused = fieldFocused;
module.exports.hasTouchEvents = hasTouchEvents;
module.exports.injectCSS = injectCSS;
module.exports.escape = escape;
module.exports.isFullScreen = isFullScreen;
module.exports.toggleFullScreen = toggleFullScreen;
module.exports.lockOrientation = lockOrientation;
module.exports.triggerEvent = triggerEvent;
return {
trace: trace,
error: error,
warn: warn,
polyfill: polyfill,
getPeerId: getPeerId,
fieldFocused: fieldFocused,
hasTouchEvents: hasTouchEvents,
injectCSS: injectCSS,
escape: escape,
isFullScreen: isFullScreen,
toggleFullScreen: toggleFullScreen,
lockOrientation: lockOrientation,
triggerEvent: triggerEvent
};
};

Просмотреть файл

@ -1,18 +1,23 @@
'use strict';
var settings_local = {};
try {
settings_local = require('./settings_local.js');
} catch (e) {
}
var settings = {
API_URL: 'http://localhost:5000', // This URL to the Galaxy API. No trailing slash.
API_URL: 'http://localhost:5000', // Galaxy API URL. No trailing slash.
DEBUG: false,
PEERJS_KEY: '', // Sign up for a key at http://peerjs.com/peerserver
VERSION: '0.0.1' // Version of the `gamepad.js` script
};
for (var key in settings_local) {
settings[key] = settings_local[key];
}
// Override each default setting with user-defined setting.
Object.keys(settings_local).forEach(function (key) {
settings[key] = settings_local[key];
});
module.exports = settings;