Skip to content

Indentation/alignment within ternary branches (consequent/alternate) #927

@josephfrazier

Description

@josephfrazier

(extracted from #811 (comment))

Currently (commit 05595b5), standard formats the following code:

let options
const events = typeof options.trigger === 'string'
  ? options.trigger.split(' ').filter(trigger => {
      return ['click', 'hover', 'focus'].indexOf(trigger) !== -1
    })
  : []
console.log(events)

as:

let options
const events = typeof options.trigger === 'string'
  ? options.trigger.split(' ').filter(trigger => {
    return ['click', 'hover', 'focus'].indexOf(trigger) !== -1
  })
  : []
console.log(events)

That is, it unindents the function body and closing brace. Here's the diff:

diff --git a/p.js b/s.js
index c778a36e..0ccb242f 100644
--- a/p.js
+++ b/s.js
@@ -1,7 +1,7 @@
 let options
 const events = typeof options.trigger === 'string'
   ? options.trigger.split(' ').filter(trigger => {
-      return ['click', 'hover', 'focus'].indexOf(trigger) !== -1
-    })
+    return ['click', 'hover', 'focus'].indexOf(trigger) !== -1
+  })
   : []
 console.log(events)

If ESLint makes it possible (see eslint/eslint#6606 and eslint/eslint#7698), would it be acceptable to have standard use the original indentation in this case?

I might be able to hack together a way to test breakage by postprocessing standard's output with prettier-miscellaneous, which produces standard-formatted code with prettier --no-semi --single-quote --jsx-single-quote --space-before-function-paren (with the exception of this issue, as far as I know).

EDIT: This is similar to #521

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions