Skip to content

Commit

Permalink
JS linting and building (#353)
Browse files Browse the repository at this point in the history
* Only install gulp when not present

gulp global installation takes a while and there is no reason to reinstall it at
every gulp build.

* Enforce JS code style.

By default jscs doesn't enforce anything. This .jscsrc is based on the angular
style guide endorsed by the angular team : https://github.com/johnpapa/angular-styleguide/tree/master/a1#jscs

This commit fixes all minor details that make the linter happy.
  • Loading branch information
vhf authored and marcua committed Aug 15, 2016
1 parent cd3c426 commit 0b0c705
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .htmlhintrc
@@ -1,3 +1,3 @@
{
"doctype-first": false
"doctype-first": false
}
91 changes: 90 additions & 1 deletion .jscsrc
@@ -1 +1,90 @@
{}
{
"requireCurlyBraces": [
"if",
"else",
"for",
"while",
"do",
"try",
"catch"
],
"requireOperatorBeforeLineBreak": true,
"requireCamelCaseOrUpperCaseIdentifiers": null,
"validateIndentation": 2,
"validateQuoteMarks": { "mark": "'", "escape": true },
"disallowMultipleLineStrings": true,
"disallowMixedSpacesAndTabs": true,
"disallowTrailingWhitespace": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowMultipleVarDecl": null,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBinaryOperators": [
"=",
"+=",
"-=",
"*=",
"/=",
"%=",
"<<=",
">>=",
">>>=",
"&=",
"|=",
"^=",
"+=",
"+",
"-",
"*",
"/",
"%",
"<<",
">>",
">>>",
"&",
"|",
"^",
"&&",
"||",
"===",
"==",
">=",
"<=",
"<",
">",
"!=",
"!=="
],
"requireSpaceAfterBinaryOperators": true,
"requireSpacesInConditionalExpression": true,
"requireSpaceBeforeBlockStatements": true,
"requireLineFeedAtFileEnd": true,
"disallowSpacesInsideArrayBrackets": "all",
"disallowSpacesInsideParentheses": true,
"jsDoc": {
"checkAnnotations": true,
"checkParamNames": true,
"requireParamTypes": true,
"checkReturnTypes": true,
"checkTypes": true
},
"disallowMultipleLineBreaks": true,
"disallowCommaBeforeLineBreak": null,
"disallowDanglingUnderscores": null,
"disallowEmptyBlocks": true,
"disallowTrailingComma": null,
"requireCommaBeforeLineBreak": null,
"requireDotNotation": true,
"requireMultipleVarDecl": null,
"requireParenthesesAroundIIFE": true,
"requireSemicolons": true
}
3 changes: 3 additions & 0 deletions Makefile
@@ -1,5 +1,6 @@
COVERAGE_ANNOTATION=coverage_annotations
TEST_CMD=manage.py test orchestra beanstalk_dispatch --with-xunit --parallel=4 --exclude=assert_test*
GULP := $(shell command -v gulp 2> /dev/null)

clean:
find . -name '*.pyo' -delete
Expand All @@ -23,7 +24,9 @@ coveralls:
cd example_project && coveralls

npm_install:
ifndef GULP
npm install -g gulp
endif
npm install

gulp_build: npm_install
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js
Expand Up @@ -15,10 +15,10 @@
var stylish = require('jshint-stylish');

// html
var htmlhint = require("gulp-htmlhint");
var htmlhint = require('gulp-htmlhint');

// json
var jsonlint = require("gulp-jsonlint");
var jsonlint = require('gulp-jsonlint');

// scss
var sourcemaps = require('gulp-sourcemaps');
Expand Down
Expand Up @@ -21,7 +21,7 @@
// The iframe is actually added only after everything is loaded.
// This way the webpage does not stall for a couple of seconds until
// all the iframe directives load.
$timeout(function(){
$timeout(function() {
var iframe = document.createElement('iframe');
var iframeAttrs = {
'name': '{{title}}',
Expand Down
Expand Up @@ -44,7 +44,6 @@
}
});


// Editor set to read-only upon initialization.
if (scope.readonly) {
scope.editor.editor.disable();
Expand Down Expand Up @@ -183,7 +182,6 @@
return contents.length() + caretPosition.offset;
}


// Post image data to an API endpoint that returns an image URL, then
// adding it to the editor (replacing the given character range)
function uploadImage(file, range, e) {
Expand Down Expand Up @@ -211,7 +209,7 @@
reader.onload = function(e) {
var rawData = e.target.result;
// Remove prepended image type from data string
var imageData = rawData.substring(rawData.indexOf(",") + 1, rawData.length);
var imageData = rawData.substring(rawData.indexOf(',') + 1, rawData.length);

// Calculate data size of b64-encoded string
var imageSize = imageData.length * 3 / 4;
Expand Down
6 changes: 4 additions & 2 deletions orchestra/static/orchestra/common/js/orchestra.filters.js
Expand Up @@ -12,8 +12,10 @@ angular.module('orchestra.common')
// Modified from github.com/petebacondarwin/angular-toArrayFilter
.filter('toArray', function () {
return function (obj, addKey) {
if (!angular.isObject(obj)) return obj;
if ( addKey === false ) {
if (!angular.isObject(obj)) {
return obj;
}
if (addKey === false) {
return Object.keys(obj).map(function(key) {
return obj[key];
});
Expand Down
10 changes: 5 additions & 5 deletions orchestra/static/orchestra/project_management/js/axis.js
Expand Up @@ -54,11 +54,11 @@
var tickSpread = 10;
var xLabelText;
if (this.relativeTime) {
xAxis.tickFormat(function(d, i) {
if (hourTicks.length < tickSpread || i % tickSpread === 0) {
return (d - minDatetime) / hourInMilliseconds;
}
});
xAxis.tickFormat(function(d, i) {
if (hourTicks.length < tickSpread || i % tickSpread === 0) {
return (d - minDatetime) / hourInMilliseconds;
}
});
xLabelText = 'Time (hours)';
} else {
xAxis.tickFormat(function(d, i) {
Expand Down
Expand Up @@ -83,7 +83,7 @@

this.data.steps = steps;

/*jshint -W083 */
/*jshint -W083 */
// Hide error for creating a function in a loop
for (var step_slug in this.data.tasks) {
var task = this.data.tasks[step_slug];
Expand Down
Expand Up @@ -19,7 +19,7 @@
.data(function(assignmentKey) {
var iterations = [];
dataService.assignmentFromKey(assignmentKey).iterations.forEach(function(iteration, i) {
iterations.push(dataService.keyFromIteration(iteration));
iterations.push(dataService.keyFromIteration(iteration));
});
return iterations;
});
Expand Down
Expand Up @@ -43,7 +43,6 @@
return cls;
};


return {
reverting: false,
draw: function() {
Expand Down
1 change: 0 additions & 1 deletion orchestra/static/orchestra/task/js/directives.js
Expand Up @@ -5,7 +5,6 @@
.module('orchestra.task')
.directive('dynamicLoad', dynamicLoad);


dynamicLoad.$inject = ['$compile'];

function dynamicLoad($compile) {
Expand Down
6 changes: 2 additions & 4 deletions orchestra/static/orchestra/task/js/required_fields.js
Expand Up @@ -10,13 +10,11 @@
var requiredFields = {
validators: {
'input-checkbox': [

function(elem) {
return elem.checked;
}
],
'input-text': [

function(elem) {
return elem.value && elem.value.length > 0;
}
Expand Down Expand Up @@ -50,7 +48,7 @@
var requiredFields = this;
requiredFields.invalid = [];

/*jshint -W083 */
/*jshint -W083 */
// Hide error for creating a function in a loop
for (var fieldType in requiredFields.fields) {
var validators = requiredFields.validators[fieldType];
Expand Down Expand Up @@ -104,7 +102,7 @@
* error with `data-error-class="error-class"` on the directive
* element; otherwise, a default is provided for the input type.
*/
angular.module('orchestra.task')
angular.module('orchestra.task')
.directive('orchestraRequiredField', function($compile, requiredFields) {
return {
restrict: 'EA',
Expand Down
2 changes: 0 additions & 2 deletions orchestra/static/orchestra/timing/service.js
Expand Up @@ -46,7 +46,6 @@
});
},


/**
* Creates a new TimeEntry server-side and initializes with returned data.
*/
Expand All @@ -66,7 +65,6 @@
});
},


/**
* Calculate the total work time for a given date.
*/
Expand Down
2 changes: 1 addition & 1 deletion orchestra/static/orchestra/timing/time-entry.js
Expand Up @@ -66,7 +66,7 @@
angular.module('orchestra.timing')
.factory('TimeEntry', function($http) {
var TimeEntry = function(data) {
this.initWithData(data);
this.initWithData(data);
};

/**
Expand Down
72 changes: 36 additions & 36 deletions orchestra/static/orchestra/timing/timecard/directives.js
Expand Up @@ -58,48 +58,48 @@
}
};
})
.directive('enforceIntegers', function(){
.directive('enforceIntegers', function() {
// Non-digit chars are accepted in number inputs in some browsers (e.g., Safari)
// Modified from stackoverflow.com/a/14425022
return {
scope: {
max: '=?',
min: '=?'
},
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.unshift(function(inputValue) {
if (inputValue === undefined || inputValue === null) {
modelCtrl.$setViewValue(0);
modelCtrl.$render();
return;
}
return {
scope: {
max: '=?',
min: '=?'
},
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.unshift(function(inputValue) {
if (inputValue === undefined || inputValue === null) {
modelCtrl.$setViewValue(0);
modelCtrl.$render();
return;
}

var transformedInput = String(inputValue).replace(/[^\d]/g, '');
var transformedInput = String(inputValue).replace(/[^\d]/g, '');

// Remove leading 0 if present
if (transformedInput.length && transformedInput[0] === '0') {
transformedInput = transformedInput.substring(1);
}
// Remove leading 0 if present
if (transformedInput.length && transformedInput[0] === '0') {
transformedInput = transformedInput.substring(1);
}

// Convert to integer
if (transformedInput.length) {
transformedInput = parseInt(transformedInput, 10);
transformedInput = Math.min(transformedInput, scope.max);
transformedInput = Math.max(transformedInput, scope.min);
}
else {
transformedInput = 0;
}
// Convert to integer
if (transformedInput.length) {
transformedInput = parseInt(transformedInput, 10);
transformedInput = Math.min(transformedInput, scope.max);
transformedInput = Math.max(transformedInput, scope.min);
}
else {
transformedInput = 0;
}

if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
if (transformedInput !== inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}

return transformedInput;
});
}
};
return transformedInput;
});
}
};
});
})();
6 changes: 3 additions & 3 deletions orchestra/static/orchestra/timing/timer/directives.js
Expand Up @@ -85,12 +85,12 @@
var entry = timeEntries.addEntry(response.data);
resetTimer();
if (entry.isIncomplete()) {
if (confirm("Your new time entry is missing a description or assignment. Would you like to view your timecard and edit it there?")) {
if (confirm('Your new time entry is missing a description or assignment. Would you like to view your timecard and edit it there?')) {
workTimer.viewTimecard();
}
}
}, function() {
alert('Could not stop timer');
alert('Could not stop timer');
});
};

Expand All @@ -103,7 +103,7 @@
};
$http.post(url, data)
.catch(function() {
alert('Could not update timer description');
alert('Could not update timer description');
});
};

Expand Down

0 comments on commit 0b0c705

Please sign in to comment.