Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active February 25, 2020 20:54
Show Gist options
  • Save cowboy/d29d24f3dbfb26cc33aa to your computer and use it in GitHub Desktop.
Save cowboy/d29d24f3dbfb26cc33aa to your computer and use it in GitHub Desktop.
JavaScript: chained method indentation
// Inspired by the eslint-plugin-react wrap-multilines rule
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md
//
// I don't know why, but this is making me really happy right now.
// Instead of this
var someName = namespace.someObj.method1({
prop: 'value',
}).method2('foo', 'bar');
// Or this
var someName = namespace.someObj
.method1({
prop: 'value',
})
.method2('foo', 'bar');
// Do this
var someName = (
namespace.someObj
.method1({
prop: 'value',
})
.method2('foo', 'bar')
);
@insin
Copy link

insin commented Oct 14, 2015

What do the parens bring to the party if it's not a return statement?

@patik
Copy link

patik commented Oct 14, 2015

@insin it makes it clear that the statement wraps over several lines.

@zzzzBov
Copy link

zzzzBov commented Oct 15, 2015

...but it's already clear by reading the code that the statement wraps over several lines. I'd go one step further though and propose:

var someName =
  namespace
    .someObj
    .method1({
      prop: 'value'
    })
    .method2('foo', 'bar');

with the optional parens for return statements to fix automatic semicolon insertion.

@cowboy
Copy link
Author

cowboy commented Oct 15, 2015

The parens aren't required, I just like how they feel like a visual "container" to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment