Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

style: enforce spaces after keywords, add spaces #9677

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .jscs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"disallowKeywords": ["with"],
"disallowTrailingWhitespace": true,
"disallowSpaceAfterPrefixUnaryOperators": ["!"],
"disallowSpaceBeforeBinaryOperators": [","]
"disallowSpaceBeforeBinaryOperators": [","],
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"]
}
1 change: 0 additions & 1 deletion .jscs.json.todo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

{
"requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"],
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
"requireSpaceAfterBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
"requireSpaceBeforeBinaryOperators": ["?", ":", "+", "-", "/", "*", "%", "==", "===", "!=", "!==", ">", ">=", "<", "<=", "&&", "||"],
"disallowImplicitTypeConversion": ["string"],
Expand Down
16 changes: 8 additions & 8 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ function copy(source, destination, stackSource, stackDest) {
});
}
for ( var key in source) {
if(source.hasOwnProperty(key)) {
if (source.hasOwnProperty(key)) {
result = copy(source[key], null, stackSource, stackDest);
if (isObject(source[key])) {
stackSource.push(source[key]);
Expand Down Expand Up @@ -882,7 +882,7 @@ function equals(o1, o2) {
if (isArray(o1)) {
if (!isArray(o2)) return false;
if ((length = o1.length) == o2.length) {
for(key=0; key<length; key++) {
for (key=0; key<length; key++) {
if (!equals(o1[key], o2[key])) return false;
}
return true;
Expand All @@ -895,12 +895,12 @@ function equals(o1, o2) {
} else {
if (isScope(o1) || isScope(o2) || isWindow(o1) || isWindow(o2) || isArray(o2)) return false;
keySet = {};
for(key in o1) {
for (key in o1) {
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
if (!equals(o1[key], o2[key])) return false;
keySet[key] = true;
}
for(key in o2) {
for (key in o2) {
if (!keySet.hasOwnProperty(key) &&
key.charAt(0) !== '$' &&
o2[key] !== undefined &&
Expand Down Expand Up @@ -1048,14 +1048,14 @@ function startingTag(element) {
// turns out IE does not let you set .html() on elements which
// are not allowed to have children. So we just ignore it.
element.empty();
} catch(e) {}
} catch (e) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch (e) always looks weird to me, I'm not a fan of that style =\

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree. Similar to function (e), switch (e), void (e), so keywords that have parens right after.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it :)
The only thing left that I would change in Angular's style is adding a space after function (in anonynous functions) =)

var elemHtml = jqLite('<div>').append(element).html();
try {
return element[0].nodeType === NODE_TYPE_TEXT ? lowercase(elemHtml) :
elemHtml.
match(/^(<[^>]+>)/)[1].
replace(/^<([\w\-]+)/, function(match, nodeName) { return '<' + lowercase(nodeName); });
} catch(e) {
} catch (e) {
return lowercase(elemHtml);
}

Expand All @@ -1075,7 +1075,7 @@ function startingTag(element) {
function tryDecodeURIComponent(value) {
try {
return decodeURIComponent(value);
} catch(e) {
} catch (e) {
// Ignore any invalid uri component
}
}
Expand All @@ -1095,7 +1095,7 @@ function parseKeyValue(/**string*/keyValue) {
var val = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
if (!hasOwnProperty.call(obj, key)) {
obj[key] = val;
} else if(isArray(obj[key])) {
} else if (isArray(obj[key])) {
obj[key].push(val);
} else {
obj[key] = [obj[key],val];
Expand Down
4 changes: 2 additions & 2 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ function createInjector(modulesToLoad, strictDi) {

function runInvokeQueue(queue) {
var i, ii;
for(i = 0, ii = queue.length; i < ii; i++) {
for (i = 0, ii = queue.length; i < ii; i++) {
var invokeArgs = queue[i],
provider = providerInjector.get(invokeArgs[0]);

Expand Down Expand Up @@ -793,7 +793,7 @@ function createInjector(modulesToLoad, strictDi) {
length, i,
key;

for(i = 0, length = $inject.length; i < length; i++) {
for (i = 0, length = $inject.length; i < length; i++) {
key = $inject[i];
if (typeof key !== 'string') {
throw $injectorMinErr('itkn',
Expand Down
4 changes: 2 additions & 2 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ function jqLiteController(element, name) {
function jqLiteInheritedData(element, name, value) {
// if element is the document object work with the html element instead
// this makes $(document).scope() possible
if(element.nodeType == NODE_TYPE_DOCUMENT) {
if (element.nodeType == NODE_TYPE_DOCUMENT) {
element = element.documentElement;
}
var names = isArray(name) ? name : [name];
Expand Down Expand Up @@ -983,7 +983,7 @@ forEach({
JQLite.prototype[name] = function(arg1, arg2, arg3) {
var value;

for(var i = 0, ii = this.length; i < ii; i++) {
for (var i = 0, ii = this.length; i < ii; i++) {
if (isUndefined(value)) {
value = fn(this[i], arg1, arg2, arg3);
if (isDefined(value)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ng/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var $AnimateProvider = ['$provide', function($provide) {
* @return {RegExp} The current CSS className expression value. If null then there is no expression value
*/
this.classNameFilter = function(expression) {
if(arguments.length === 1) {
if (arguments.length === 1) {
this.$$classNameFilter = (expression instanceof RegExp) ? expression : null;
}
return this.$$classNameFilter;
Expand Down
2 changes: 1 addition & 1 deletion src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function Browser(window, document, $log, $sniffer) {
} finally {
outstandingRequestCount--;
if (outstandingRequestCount === 0) {
while(outstandingRequestCallbacks.length) {
while (outstandingRequestCallbacks.length) {
try {
outstandingRequestCallbacks.pop()();
} catch (e) {
Expand Down
46 changes: 23 additions & 23 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
*/
var debugInfoEnabled = true;
this.debugInfoEnabled = function(enabled) {
if(isDefined(enabled)) {
if (isDefined(enabled)) {
debugInfoEnabled = enabled;
return this;
}
Expand Down Expand Up @@ -899,7 +899,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* @param {string} classVal The className value that will be added to the element
*/
$addClass : function(classVal) {
if(classVal && classVal.length > 0) {
if (classVal && classVal.length > 0) {
$animate.addClass(this.$$element, classVal);
}
},
Expand All @@ -916,7 +916,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* @param {string} classVal The className value that will be removed from the element
*/
$removeClass : function(classVal) {
if(classVal && classVal.length > 0) {
if (classVal && classVal.length > 0) {
$animate.removeClass(this.$$element, classVal);
}
},
Expand Down Expand Up @@ -969,7 +969,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (booleanKey) {
this.$$element.prop(key, value);
attrName = booleanKey;
} else if(aliasedKey) {
} else if (aliasedKey) {
this[aliasedKey] = value;
observer = aliasedKey;
}
Expand Down Expand Up @@ -1022,7 +1022,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
result += $$sanitizeUri(trim(lastTuple[0]), true);

// and add the last descriptor if any
if( lastTuple.length === 2) {
if (lastTuple.length === 2) {
result += (" " + trim(lastTuple[1]));
}
this[key] = value = result;
Expand Down Expand Up @@ -1089,7 +1089,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
function safeAddClass($element, className) {
try {
$element.addClass(className);
} catch(e) {
} catch (e) {
// ignore, since it means that we are trying to set class on
// SVG element, where class name is read-only.
}
Expand Down Expand Up @@ -1278,7 +1278,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
stableNodeList = nodeList;
}

for(i = 0, ii = linkFns.length; i < ii;) {
for (i = 0, ii = linkFns.length; i < ii;) {
node = stableNodeList[linkFns[i++]];
nodeLinkFn = linkFns[i++];
childLinkFn = linkFns[i++];
Expand Down Expand Up @@ -1346,7 +1346,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
match,
className;

switch(nodeType) {
switch (nodeType) {
case NODE_TYPE_ELEMENT: /* Element */
// use the node name: <directive>
addDirective(directives,
Expand Down Expand Up @@ -1522,7 +1522,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
directiveValue;

// executes all directives on the current element
for(var i = 0, ii = directives.length; i < ii; i++) {
for (var i = 0, ii = directives.length; i < ii; i++) {
directive = directives[i];
var attrStart = directive.$$start;
var attrEnd = directive.$$end;
Expand Down Expand Up @@ -1855,7 +1855,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
isolateBindingContext[scopeName] = value;
});
attrs.$$observers[attrName].$$scope = scope;
if( attrs[attrName] ) {
if ( attrs[attrName] ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to enforce no whitespace after ( and before ) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah using "disallowSpacesInsideParentheses": true.

Valid

if (attrs[attrName]) {

Invalid

if ( attrs[attrName]) {
if (attrs[attrName] ) {
if ( attrs[attrName] ) {

// If the attribute has been provided then we trigger an interpolation to ensure
// the value is there for use in the link fn
isolateBindingContext[scopeName] = $interpolate(attrs[attrName])(scope);
Expand Down Expand Up @@ -1915,7 +1915,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}

// PRELINKING
for(i = 0, ii = preLinkFns.length; i < ii; i++) {
for (i = 0, ii = preLinkFns.length; i < ii; i++) {
linkFn = preLinkFns[i];
invokeLinkFn(linkFn,
linkFn.isolateScope ? isolateScope : scope,
Expand All @@ -1936,7 +1936,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
childLinkFn && childLinkFn(scopeToChild, linkNode.childNodes, undefined, boundTranscludeFn);

// POSTLINKING
for(i = postLinkFns.length - 1; i >= 0; i--) {
for (i = postLinkFns.length - 1; i >= 0; i--) {
linkFn = postLinkFns[i];
invokeLinkFn(linkFn,
linkFn.isolateScope ? isolateScope : scope,
Expand Down Expand Up @@ -1996,7 +1996,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (name === ignoreDirective) return null;
var match = null;
if (hasDirectives.hasOwnProperty(name)) {
for(var directive, directives = $injector.get(name + Suffix),
for (var directive, directives = $injector.get(name + Suffix),
i = 0, ii = directives.length; i<ii; i++) {
try {
directive = directives[i];
Expand All @@ -2008,7 +2008,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
tDirectives.push(directive);
match = directive;
}
} catch(e) { $exceptionHandler(e); }
} catch (e) { $exceptionHandler(e); }
}
}
return match;
Expand All @@ -2025,7 +2025,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
*/
function directiveIsMultiElement(name) {
if (hasDirectives.hasOwnProperty(name)) {
for(var directive, directives = $injector.get(name + Suffix),
for (var directive, directives = $injector.get(name + Suffix),
i = 0, ii = directives.length; i<ii; i++) {
directive = directives[i];
if (directive.multiElement) {
Expand Down Expand Up @@ -2142,7 +2142,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
});
afterTemplateChildLinkFn = compileNodes($compileNode[0].childNodes, childTranscludeFn);

while(linkQueue.length) {
while (linkQueue.length) {
var scope = linkQueue.shift(),
beforeTemplateLinkNode = linkQueue.shift(),
linkRootElement = linkQueue.shift(),
Expand Down Expand Up @@ -2241,7 +2241,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

function wrapTemplate(type, template) {
type = lowercase(type || 'html');
switch(type) {
switch (type) {
case 'svg':
case 'math':
var wrapper = document.createElement('div');
Expand Down Expand Up @@ -2322,7 +2322,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
//skip animations when the first digest occurs (when
//both the new and the old values are the same) since
//the CSS classes are the non-interpolated values
if(name === 'class' && newValue != oldValue) {
if (name === 'class' && newValue != oldValue) {
attr.$updateClass(newValue, oldValue);
} else {
attr.$set(name, newValue);
Expand Down Expand Up @@ -2352,7 +2352,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
i, ii;

if ($rootElement) {
for(i = 0, ii = $rootElement.length; i < ii; i++) {
for (i = 0, ii = $rootElement.length; i < ii; i++) {
if ($rootElement[i] == firstElementToRemove) {
$rootElement[i++] = newNode;
for (var j = i, j2 = j + removeCount - 1,
Expand Down Expand Up @@ -2427,7 +2427,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
function invokeLinkFn(linkFn, scope, $element, attrs, controllers, transcludeFn) {
try {
linkFn(scope, $element, attrs, controllers, transcludeFn);
} catch(e) {
} catch (e) {
$exceptionHandler(e, startingTag($element));
}
}
Expand Down Expand Up @@ -2516,10 +2516,10 @@ function tokenDifference(str1, str2) {
tokens2 = str2.split(/\s+/);

outer:
for(var i = 0; i < tokens1.length; i++) {
for (var i = 0; i < tokens1.length; i++) {
var token = tokens1[i];
for(var j = 0; j < tokens2.length; j++) {
if(token == tokens2[j]) continue outer;
for (var j = 0; j < tokens2.length; j++) {
if (token == tokens2[j]) continue outer;
}
values += (values.length > 0 ? ' ' : '') + token;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ng/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function $ControllerProvider() {
identifier = ident;
}

if(isString(expression)) {
if (isString(expression)) {
match = expression.match(CNTRL_REG),
constructor = match[1],
identifier = identifier || match[3];
Expand Down
6 changes: 3 additions & 3 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
var parserValid = isUndefined(modelValue) ? undefined : true;

if (parserValid) {
for(var i = 0; i < ctrl.$parsers.length; i++) {
for (var i = 0; i < ctrl.$parsers.length; i++) {
modelValue = ctrl.$parsers[i](modelValue);
if (isUndefined(modelValue)) {
parserValid = false;
Expand Down Expand Up @@ -2148,7 +2148,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
forEach(ctrl.$viewChangeListeners, function(listener) {
try {
listener();
} catch(e) {
} catch (e) {
$exceptionHandler(e);
}
});
Expand Down Expand Up @@ -2251,7 +2251,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
idx = formatters.length;

var viewValue = modelValue;
while(idx--) {
while (idx--) {
viewValue = formatters[idx](viewValue);
}
if (ctrl.$viewValue !== viewValue) {
Expand Down
6 changes: 3 additions & 3 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ function classDirective(name, selector) {
var values = [];

outer:
for(var i = 0; i < tokens1.length; i++) {
for (var i = 0; i < tokens1.length; i++) {
var token = tokens1[i];
for(var j = 0; j < tokens2.length; j++) {
if(token == tokens2[j]) continue outer;
for (var j = 0; j < tokens2.length; j++) {
if (token == tokens2[j]) continue outer;
}
values.push(token);
}
Expand Down
Loading