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

Conversation

hzoo
Copy link
Contributor

@hzoo hzoo commented Oct 18, 2014

Moving the requireSpaceAfterKeywords rule to jscs with associated changes.

Is it preferred to explicitly specify the keywords in an array?

Or use requireSpaceAfterKeywords = true
which is equivalent to requireSpaceAfterKeywords = [ 'do', 'for', 'if', 'else', 'switch', 'case', 'try', 'catch', 'void', 'while', 'with', 'return', 'typeof', 'function'];

Although it doesn't make sense for function.

cc: @caitp

@@ -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) =)

@@ -1184,7 +1184,7 @@ function $ParseProvider() {
}
if (isAllDefined(value)) {
scope.$$postDigest(function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather function were trailed immediately by parentheses*, unless it's a named function --- can jscs enforce that?

(I meant parentheses, not braces**)

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 can be done.

For all functions (declaration, named expression, anon expression)

"disallowSpacesInFunction": {
    "beforeOpeningRoundBrace": true
}

If you want to be specific:

disallowSpacesInFunctionDeclaration
disallowSpacesInNamedFunctionExpression
disallowSpacesInAnonymousFunctionExpression

You can also enforce a space before the parentheses.

"requireSpacesInFunction": {
    "beforeOpeningCurlyBrace": true
}

So with both rules it should be

function a() {

}

var a = function() {

}

var a = function a() {

}

Copy link
Contributor

Choose a reason for hiding this comment

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

cool, that sounds like a good setup

Copy link
Member

Choose a reason for hiding this comment

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

People, save the space in anonymous functions !!!

@caitp
Copy link
Contributor

caitp commented Oct 18, 2014

lgtm

@caitp
Copy link
Contributor

caitp commented Oct 18, 2014

Is it preferred to explicitly specify the keywords in an array?

Yes, this is fine

@caitp
Copy link
Contributor

caitp commented Oct 18, 2014

If you care to adjust the jscs rules to implement the comments I suggested above, please send another one =)

@caitp caitp closed this in accb22d Oct 18, 2014
@@ -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] ) {

@shahata
Copy link
Contributor

shahata commented Oct 18, 2014

Truthfully I'd really prefer to always have a space after reserved words, including function and catch, but since the de-facto standard in angular is to avoid it I guess we can leave it as is...

@hzoo hzoo deleted the add-jscs-require-space-after-keywords branch October 18, 2014 21:30
@gkalpak
Copy link
Member

gkalpak commented Oct 19, 2014

If I remember correctly, last time I checked the function()/function () ratio was 2:1, so I wouldn't call it the "de-facto standard".
That said, I do find the function () (with space) more consistent and intuitive.

@hzoo
Copy link
Contributor Author

hzoo commented Oct 19, 2014

After doing some testing with jscs,

Checking only function declarations: for function a (e) { };, got 10.
Checking only function declarations: for function a(e) {};, got 1698.

Checking only anonymous function expressions: for function (e), got 1121.
Checking only anonymous function expressions: for function(e), got 9159.

Checking only named function expressions: for function a(e), got 131.
Checking only named function expressions: for function a (e), got 1.

So based on majority it would be

//declaration
function a() {

}
//anon
var a = function() {

};
//named
var a = function a() {

};

But yeah could go either way for anon func expressions since dropping the name in a named function expression would leave the space still there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants