Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce no braces in arrow function body where they can be omitted (arrow-body-style) #792

Open
tunnckoCore opened this issue Feb 21, 2017 · 9 comments

Comments

@tunnckoCore
Copy link

commented Feb 21, 2017

Digging more and more in ESLint rules, i found some good ones which enforces good style and more readable code. This issue is for arrow-body-style rule - fixable.

Suggested configuration.

{
  "arrow-body-style": ["error", "as-needed", { "requireReturnForObjectLiteral": false }]
}

This makes shorter and readable code, that does not returns if there no other assignments in the block body.

Example

// OK
let foo = () => ({})
let foo = () => ({ bar: 0 })

// OK
let foo = () => {}

// NOT OKEY
let foo = () => { return { bar: 0 } }
// CORRECT
let foo = () => ({ bar: 0 })

// OK
let foo = () => {
  var a = true
  return 0
}

// NOT OK
let foo = () => {
  return 0
}
// CORRECT
let foo = () => 0

// OK
let foo = (retv, name) => {
  var a = true
  return { bar: 0 }
}

// OK
let foo = (retv, name) => {
  retv[name] = true
  return retv
}

@feross feross added the enhancement label Feb 21, 2017

@dcousens

This comment has been minimized.

Copy link
Member

commented Feb 22, 2017

ACK, but not sure of ecosystem impact.

@daedalus28

This comment has been minimized.

Copy link

commented Mar 21, 2017

We just started pushing standardjs for all our repos, but there are a lot of eslint rules we used to have that we miss like this one. This really helps developers who are not as familiar with modern js discover how awesome it is.

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Mar 21, 2017

@falmar

This comment has been minimized.

Copy link
Contributor

commented Mar 21, 2017

I use this type of style even without any eslint rules and it is quite amazing less code for more

@daedalus28

This comment has been minimized.

Copy link

commented Mar 21, 2017

@yoshuawuyts what real use case do you have? This is only about arrow functions that are nothing more than return statements. If your return value is such a large expression that you want it on multiple lines, there's nothing preventing you from putting line breaks in the expression (and/or wrapping in parens if you're worried people can't follow).

@tunnckoCore

This comment has been minimized.

Copy link
Author

commented Mar 21, 2017

@daedalus28 it is cool, definitely. But after the suggestion i faced such cases too. It may be in conflict with max-len rule (80) that isn't in standard but I always try to follow it as much as possible. Behind such cases is the reason why i'm not using some autoformatter such as prettier. In some cases I need and must set return explicitly on new line, so the line won't go too long.

And some examples tunnckoCore/ideas#64 (working examples btw ;p)

or this one

const VFile = require('vfile')
const each = require('each-promise')
const fs = require('pify')(require('fs'))

const readFiles = (dir, options) => readdir(dir, options).then((files) => {
  const __files = files.filter((file) => file.stat.isFile())
  if (!__files.length) return files

  return each.parallel(__files.map((file) => () => {
    // notice
    return fs.readFile(file.path).then((contents) => VFile({
      path: file.path,
      stat: file.stat,
      contents: contents
    }))
  }))
})

readFiles('./_xtemplates').then(console.log)

But yea. This cases can be fixed a bit and can look a bit better with splitting the code to functions, which can be enforced by another rule that I suggested - #794 - max-depth and max-nested-callbacks.

@ghost

This comment has been minimized.

Copy link

commented Mar 22, 2017

Too prescriptive. If it makes valid JS, and that JS runs without "bad parts", it probably doesn't belong in Standard.

@feross feross changed the title Proposal: arrow body style Enforces no braces in arrow function body where they can be omitted (arrow-body-style) Apr 4, 2017

@feross feross changed the title Enforces no braces in arrow function body where they can be omitted (arrow-body-style) Enforce no braces in arrow function body where they can be omitted (arrow-body-style) Apr 4, 2017

@feross

This comment has been minimized.

Copy link
Member

commented Apr 4, 2017

Ecosystem impact is 4%.

# tests 238
# pass  229
# fail  9

@feross feross added this to the standard v11 milestone Apr 4, 2017

@tunnckoCore

This comment has been minimized.

Copy link
Author

commented Apr 4, 2017

Cool. 4% for fixable rule sounds pretty much okey.

@feross feross modified the milestones: standard v12, standard v13 Aug 28, 2018

@feross feross modified the milestones: standard v13, standard v14 Jul 5, 2019

@feross feross modified the milestones: standard 14, standard 15 Aug 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
6 participants
You can’t perform that action at this time.