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 consistent line breaks inside braces (object-curly-newline) #782

Closed
feross opened this issue Feb 10, 2017 · 2 comments

Comments

@feross
Copy link
Member

commented Feb 10, 2017

I propose that we enforce the following:

// ok
var one = { a: 1 }

// ok
var two = { a: 1, b: 2 }

// not ok, should be split onto multiple lines
var three = { a: 1, b: 2, c: 3 }

// ok, preemptively splitting onto multiple lines is always ok
var oneM = {
  a: 1
}

// ok
var twoM = {
  a: 1,
  b: 2
}

// ok
var threeM = {
  a: 1,
  b: 2,
  c: 3
}

// not ok
var wat = { a: 1
}

// not ok
var wat2 = {
a: 1 }

// not ok
var wat3 = { a: 1,
  b: 2,
  c: 3 }

The "consistent" option in this rule is not part of ESLint yet, but when it is, we should enable it:

Rule:

"object-curly-newline": ["error", { "multiline": true, "minProperties": 2, "consistent": true }]

For context, see:

@feross feross added this to the standard v10 milestone Feb 10, 2017

@feross feross modified the milestones: standard v10, standard v11 Mar 1, 2017

@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

This comment has been minimized.

Copy link
Member Author

commented Aug 14, 2019

Thanks for the feedback on this rule, everyone.

In re-reading what I proposed here, I think @mafintosh's downvote is actually the right call. We have a policy of never accepting rules which involve "magic numbers". The requirement to count the number of properties and change style based on the difference between 2 vs. 3 properties is not in the spirit of standard. These types of rules are not obvious nor easy to remember and we always want standard style code to be easy to write without requiring the use of a tool to format code.

That said, if we just remove the "minProperties": 2 part of the rule, the remaining rule is actually quite reasonable! Here's what this modified rule does:

// ok
var one = { a: 1 }

// ok
var two = { a: 1, b: 2 }

// ok
var three = { a: 1, b: 2, c: 3 }

// ok
var oneM = {
  a: 1
}

// ok
var twoM = {
  a: 1,
  b: 2
}

// ok
var threeM = {
  a: 1,
  b: 2,
  c: 3
}

// not ok
var wat = { a: 1
}

// not ok
var wat2 = {
a: 1 }

// not ok
var wat3 = { a: 1,
  b: 2,
  c: 3 }

This seems totally uncontroversial and very useful to enforce. All 4 ecosystem repos which fail after this rule is enabled actually have bad style. Here are some examples:

const options = { native: false, // not ok
    skipCovers: true,
    fileSize: file.length,
    observer: event => {
      ipc.send('wt-audio-metadata', infoHash, index, event.metadata)
    } } // not ok
  const bestScore = ['audio', 'video', 'image'].map(mediaType => {
    return {
      type: mediaType,
      size: calculateDataLengthByExtension(torrent, mediaExtensions[mediaType]) } // not ok
  })
      model.Course
        .find({ $or: [ // not ok
          { name: regexForQuery(query) },
          { searchName: regexForQuery(query) }
        ] })
  var opts = {
    pkg: { binary: { // not ok
      module_name: 'module_name',
      module_path: 'module_path'
    } }
  }

I am going to enable this in standard 14. Hope this makes sense to everyone, including you too @mafintosh!

@feross feross closed this in 7e61b87 Aug 14, 2019

@mightyiam

This comment has been minimized.

Copy link
Contributor

commented Aug 14, 2019

Beautiful. Right call.

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