简体   繁体   中英

Eslint: newline in Arrays/Objects/Function element depending on line size

I am trying to write a custom styleguide for our codebase, stating that there should be line breaks in expression elements ( [] arrays, () arguments, {} objects) only if the code line "appears too long" for readability. I'd really like this to be an autofixable rule

I know there is a minItems argument for the *-element-newline rule, but I was wondering if I could specify a charlength instead. For instance an array with the same number of items could either be written on one line or several depending on the sum of each elements charlength

const shouldBeOneLine = [req, res, middleware, next]
const shouldBeMultiline = [
  request,
  response,
  (token, salt) => salt + Foo.bar(token),
  next
]

So far I am relying on the consistent argument of the rule, but I still have to manually put linebreaks if the expression becomes too long. I know I could write a plugin for that matter, but it seems to be a common usecase so I'm confident there should already be a solution out there.

There is a max-len rule that allows you to specify the max number of characters on one line. Unfortunately, it's not auto-fixable. Also, it applies to all code, and not just in arrays, arguments, and objects.

Documentation: max-len

There is a discussion on Github regarding the auto-fix implementation for this rule: https://github.com/eslint/eslint/issues/11325

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM