Skip to content
Merged
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
Require a space between function and (, and function and the fu…
…nction's name, and disallow spaces between the function's name and `(`.
  • Loading branch information
ljharb committed Dec 3, 2015
commit 599ca4f1238ed3b57f4f5b721f253d2d1b75cda2
56 changes: 35 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,20 @@ Other Style Guides
var subtract = Function('a', 'b', 'return a - b');
```

- [7.11](#7.11) <a name="7.11"></a> Spacing in a function signature.

> Why? Consistency is good, and you shouldn’t have to add or remove a space when adding or removing a name.
```javascript
// bad
const f = function(){};
const g = function (){};
const h = function() {};

// good
const x = function () {};
const y = function a() {};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the guide (https://github.com/eslint/eslint/blob/master/docs/rules/space-before-function-paren.md) this should be const y = function a () {}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ah, i think i want {"anonymous": "always", "named": "never"} as a config then.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Anyway there's another rule that says anonymous function should be named (or use arrow functions)

```

**[⬆ back to top](#table-of-contents)**

## Arrow Functions
Expand Down Expand Up @@ -690,7 +704,7 @@ Other Style Guides
function Queue(contents = []) {
this._queue = [...contents];
}
Queue.prototype.pop = function() {
Queue.prototype.pop = function () {
const value = this._queue[0];
this._queue.splice(0, 1);
return value;
Expand Down Expand Up @@ -721,7 +735,7 @@ Other Style Guides
Queue.apply(this, contents);
}
inherits(PeekableQueue, Queue);
PeekableQueue.prototype.peek = function() {
PeekableQueue.prototype.peek = function () {
return this._queue[0];
}

Expand All @@ -737,12 +751,12 @@ Other Style Guides

```javascript
// bad
Jedi.prototype.jump = function() {
Jedi.prototype.jump = function () {
this.jumping = true;
return true;
};

Jedi.prototype.setHeight = function(height) {
Jedi.prototype.setHeight = function (height) {
this.height = height;
};

Expand Down Expand Up @@ -974,7 +988,7 @@ Other Style Guides

```javascript
// good
function() {
function () {
test();
console.log('doing stuff..');

Expand All @@ -990,7 +1004,7 @@ Other Style Guides
}

// bad - unnecessary function call
function(hasName) {
function (hasName) {
const name = getName();

if (!hasName) {
Expand All @@ -1003,7 +1017,7 @@ Other Style Guides
}

// good
function(hasName) {
function (hasName) {
if (!hasName) {
return false;
}
Expand Down Expand Up @@ -1063,7 +1077,7 @@ Other Style Guides

anonymous(); // => TypeError anonymous is not a function

var anonymous = function() {
var anonymous = function () {
console.log('anonymous function expression');
};
}
Expand Down Expand Up @@ -1180,10 +1194,10 @@ Other Style Guides
}

// bad
function() { return false; }
function () { return false; }

// good
function() {
function () {
return false;
}
```
Expand Down Expand Up @@ -1323,17 +1337,17 @@ Other Style Guides

```javascript
// bad
function() {
function () {
∙∙∙∙const name;
}

// bad
function() {
function () {
∙const name;
}

// good
function() {
function () {
∙∙const name;
}
```
Expand Down Expand Up @@ -1402,22 +1416,22 @@ Other Style Guides

```javascript
// bad
(function(global) {
(function (global) {
// ...stuff...
})(this);
```

```javascript
// bad
(function(global) {
(function (global) {
// ...stuff...
})(this);↵
```

```javascript
// good
(function(global) {
(function (global) {
// ...stuff...
})(this);↵
```
Expand Down Expand Up @@ -1687,7 +1701,7 @@ Other Style Guides

```javascript
// bad
(function() {
(function () {
const name = 'Skywalker'
return name
})()
Expand Down Expand Up @@ -1857,15 +1871,15 @@ Other Style Guides
// bad
function foo() {
const self = this;
return function() {
return function () {
console.log(self);
};
}

// bad
function foo() {
const that = this;
return function() {
return function () {
console.log(that);
};
}
Expand Down Expand Up @@ -1986,7 +2000,7 @@ Other Style Guides

...

$(this).on('listingUpdated', function(e, listingId) {
$(this).on('listingUpdated', function (e, listingId) {
// do something with listingId
});
```
Expand All @@ -1999,7 +2013,7 @@ Other Style Guides

...

$(this).on('listingUpdated', function(e, data) {
$(this).on('listingUpdated', function (e, data) {
// do something with data.listingId
});
```
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-config-airbnb/rules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ module.exports = {
// require or disallow space before blocks
'space-before-blocks': 2,
// require or disallow space before function opening parenthesis
'space-before-function-paren': [2, 'never'],
// disallow spaces inside parentheses
// https://github.com/eslint/eslint/blob/master/docs/rules/space-before-function-paren.md
'space-before-function-paren': [2, { 'anonymous': 'always', 'named': 'never' }],
// require or disallow spaces inside parentheses
'space-in-parens': [2, 'never'],
// require spaces around operators
'space-infix-ops': 2,
Expand Down