Skip to content

Commit

Permalink
prettierx: replace --no-align-ternary-lines with --offset-ternary-exp…
Browse files Browse the repository at this point in the history
…ressions

with updated formatting as discussed in:

- #41

resolves #41 (on the prettierx side)

and closes #415 (proposal in PR #415)

- #415

BREAKING CHANGE
  • Loading branch information
brodybits committed Feb 2, 2021
1 parent e54408d commit de919a0
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 205 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -20,11 +20,11 @@ prettierx <options> <file(s)>
## Additional prettierX options

- `--align-object-properties` (`alignObjectProperties: true`): Align colons in multiline object literals (not applied with any of the JSON parsers).
- `--offset-ternary-expressions` (`offsetTernaryExpressions: true`): Indent and align ternary expression branches more consistently with "Standard JS" (similar to the corresponding eslint option).
- `--space-before-function-paren` (`spaceBeforeFunctionParen: true`): Put a space before function parenthesis in all declarations (similar to the corresponding eslint option). (Default is to put a space before function parenthesis for untyped anonymous functions only.)
- `--generator-star-spacing` (`generatorStarSpacing: true`): Put spaces around the star (`*`) in generator functions (before and after - similar to the corresponding eslint option). (Default is after only.)
- `--yield-star-spacing` (`yieldStarSpacing: true`): Put spaces around the star (`*`) in `yield*` expressions (before and after - similar to the corresponding eslint option). (Default is after only.)
- `--no-indent-chains` (`indentChains: false`): Disable indents at the start of chained calls.
- `--no-align-ternary-lines` (`alignTernaryLines: false`): Disable default alignment of ternary expression lines, which may be in conflict with "Standard JS" formatting in certain nested ternary cases ... see [`docs/options.md`](docs/options.md).
- `--break-before-else` (`breakBeforeElse: true`): Always add a line break before else.
- <code>--import-formatting <auto&#124;oneline></code> (<code>importFormatting: "<auto&#124;oneline>"</code>): Formatting of import statements, may be `oneline` to avoid conflict with VSCode "Organize Imports" feature.
- `--html-void-tags` (`htmlVoidTags: true`): Format void HTML elements as void tags.
Expand Down Expand Up @@ -56,9 +56,9 @@ The following options should be used to _format_ the code _as consistently as po
- `--single-quote` (`singleQuote: true`)
- `--jsx-single-quote` (`jsxSingleQuote: true`)
- `--no-semi` (`semi: false`)
- `--offset-ternary-expressions` (`offsetTernaryExpressions: true`)
- `--yield-star-spacing` (`yieldStarSpacing: true`)
- `--trailing-comma none` (`trailingComma: "none"`)
- and possibly `--no-align-ternary-lines` (`alignTernaryLines: false`) - see [`docs/options.md`](docs/options.md) for some more information

Note that this tool does **not** follow any of the other ["Standard JS"](https://standardjs.com/) rules. It is recommended to use this tool together with eslint, in some form, to archive correct formatting according to ["Standard JS"](https://standardjs.com/).

Expand All @@ -68,9 +68,11 @@ Any known conflicts with ["Standard JS"](https://standardjs.com/) will be tracke

- `--arrow-parens avoid` (`arrowParens: "avoid"`), especially in combination with `--space-in-parens` (`spaceInParens: true`).
- `--break-long-method-chains` (`breakLongMethodChains: true`)
- `--offset-ternary-expressions` (`offsetTernaryExpressions: true`)

## options removed

- `--no-align-ternary-lines` - replaced with: `--offset-ternary-expressions`
- `--paren-spacing` - replaced with finer-grained options:
- `--array-bracket-spacing`
- `--css-paren-spacing`
Expand Down
21 changes: 8 additions & 13 deletions docs/options.md
Expand Up @@ -243,19 +243,6 @@ Put spaces around the star (`*`) in `yield*` expressions (before and after - sim
| ------- | ---------------------- | -------------------------- |
| `false` | `--yield-star-spacing` | `yieldStarSpacing: <bool>` |

## Align ternary lines

Keep or disable default alignment of ternary expression lines, which may be in conflict with "Standard JS" formatting in certain nested ternary cases.

Valid options:

- `true` - Keep default alignment of ternary expression lines.
- `false` - Disable default alignment of ternary expression lines. Adds some more consistency with "Standard JS" in case of certain nested ternary expressions, may lead to some other conflicts with "Standard JS".

| Default | CLI Override | API Override |
| ------- | -------------------------- | --------------------------- |
| `true` | `--no-align-ternary-lines` | `alignTernaryLines: <bool>` |

## break before else

Always add a line break before else.
Expand Down Expand Up @@ -309,6 +296,14 @@ Put spaces between computed property brackets (similar to the corresponding esli
| ------- | ----------------------------- | --------------------------------- |
| `false` | `--computed-property-spacing` | `computedPropertySpacing: <bool>` |

## Offset ternary expressions

Indent and align ternary expression branches more consistently with "Standard JS" (similar to the corresponding eslint option).

| Default | CLI Override | API Override |
| ------- | ------------------------------ | ---------------------------------- |
| `false` | `--offset-ternary-expressions` | `offsetTernaryExpressions: <bool>` |

## Space after unary operator symbols

Put spaces after unary operator symbols, except in the middle of `!!` (similar to the corresponding eslint option). Status: experimental, with limited testing.
Expand Down
9 changes: 4 additions & 5 deletions src/language-js/options.js
Expand Up @@ -153,13 +153,12 @@ module.exports = {
default: false,
description: "Align colons in multiline object literals.",
},
alignTernaryLines: {
offsetTernaryExpressions: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Keep default alignment of ternary expression lines.",
oppositeDescription:
'Disable default alignment of ternary expression lines. Adds some more consistency with "Standard JS" in case of certain nested ternary expressions, may lead to some other conflicts with "Standard JS".',
default: false,
description:
'Indent and align ternary expression branches more consistently with "Standard JS" (similar to the corresponding eslint option).',
},
generatorStarSpacing: {
category: CATEGORY_JAVASCRIPT,
Expand Down
21 changes: 10 additions & 11 deletions src/language-js/printer-estree.js
Expand Up @@ -432,8 +432,8 @@ function printTernaryOperator(path, options, print, operatorOptions) {
consequentNode.type === operatorOptions.conditionalNodeType
? ifBreak("", concat(["(", parenSpace]))
: "",
// [prettierx] alignTernaryLines option support:
options.alignTernaryLines
// [prettierx] offsetTernaryExpressions option support:
!options.offsetTernaryExpressions
? align(2, path.call(print, operatorOptions.consequentNodePropertyName))
: path.call(print, operatorOptions.consequentNodePropertyName),
// [prettierx] spaceInParens option support (...)
Expand All @@ -442,26 +442,27 @@ function printTernaryOperator(path, options, print, operatorOptions) {
: "",
line,
": ",
// [prettierx] alignTernaryLines option support:
!options.alignTernaryLines ||
// [prettierx] offsetTernaryExpressions option support:
options.offsetTernaryExpressions ||
alternateNode.type === operatorOptions.conditionalNodeType
? path.call(print, operatorOptions.alternateNodePropertyName)
: align(2, path.call(print, operatorOptions.alternateNodePropertyName)),
]);
parts.push(
// [prettierx merge] from prettier@2.0.0:
// [prettierx] with offsetTernaryExpressions option support below:
parent.type !== operatorOptions.conditionalNodeType ||
parent[operatorOptions.alternateNodePropertyName] === node ||
isParentTest
? part
: options.useTabs || !options.alignTernaryLines // [prettierx] (...)
: options.useTabs || options.offsetTernaryExpressions // [prettierx] offsetTernaryExpressions option support (...)
? dedent(indent(part))
: align(Math.max(0, options.tabWidth - 2), part)
);

// [prettierx] alignTernaryLines option support:
// Indent the whole ternary if alignTernaryLines:false (like ESLint).
if (!options.alignTernaryLines) {
// [prettierx] offsetTernaryExpressions option support:
// Indent the whole ternary if offsetTernaryExpressions is enabled
// (like ESLint).
if (options.offsetTernaryExpressions) {
forceNoIndent = false;
}
}
Expand Down Expand Up @@ -499,8 +500,6 @@ function printTernaryOperator(path, options, print, operatorOptions) {
* ? d
* : e
*/
// [prettierx] alignTernaryLines option support:
options.alignTernaryLines &&
parent.type === operatorOptions.conditionalNodeType &&
parent[operatorOptions.alternateNodePropertyName] === node
? align(2, testDoc)
Expand Down
16 changes: 8 additions & 8 deletions tests/standard/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -2,11 +2,11 @@

exports[`correct.js 1`] = `
====================================options=====================================
alignTernaryLines: false
arrowParens: "avoid"
endOfLine: "lf"
generatorStarSpacing: true
jsxSingleQuote: true
offsetTernaryExpressions: true
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
semi: false
Expand Down Expand Up @@ -705,11 +705,11 @@ function * generator () {<LF>

exports[`correct-ternaries.js 1`] = `
====================================options=====================================
alignTernaryLines: false
arrowParens: "avoid"
endOfLine: "lf"
generatorStarSpacing: true
jsxSingleQuote: true
offsetTernaryExpressions: true
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
semi: false
Expand Down Expand Up @@ -955,8 +955,8 @@ let icecream =<LF>
a<LF>
? literalline<LF>
: {<LF>
123: 12<LF>
}<LF>
123: 12<LF>
}<LF>
? line<LF>
: softline<LF>
<LF>
Expand Down Expand Up @@ -1056,11 +1056,11 @@ const StorybookLoader = ({ match }) =><LF>

exports[`incorrect.js 1`] = `
====================================options=====================================
alignTernaryLines: false
arrowParens: "avoid"
endOfLine: "lf"
generatorStarSpacing: true
jsxSingleQuote: true
offsetTernaryExpressions: true
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
semi: false
Expand Down Expand Up @@ -1838,11 +1838,11 @@ const dress = isSpace<LF>

exports[`incorrect-ternaries.js 1`] = `
====================================options=====================================
alignTernaryLines: false
arrowParens: "avoid"
endOfLine: "lf"
generatorStarSpacing: true
jsxSingleQuote: true
offsetTernaryExpressions: true
parsers: ["babel", "babel-flow", "flow", "typescript"]
printWidth: 80
semi: false
Expand Down Expand Up @@ -2060,8 +2060,8 @@ let icecream =<LF>
a<LF>
? literalline<LF>
: {<LF>
123: 12<LF>
}<LF>
123: 12<LF>
}<LF>
? line<LF>
: softline<LF>
<LF>
Expand Down
2 changes: 1 addition & 1 deletion tests/standard/jsfmt.spec.js
Expand Up @@ -7,11 +7,11 @@ run_spec(__dirname, ["babel", "babel-flow", "flow", "typescript"], {
// "Standard JS":
yieldStarSpacing: true,
generatorStarSpacing: true,
offsetTernaryExpressions: true,
spaceBeforeFunctionParen: true,
singleQuote: true,
jsxSingleQuote: true,
semi: false,
alignTernaryLines: false,
trailingComma: "none",
// recommended:
arrowParens: "avoid",
Expand Down

0 comments on commit de919a0

Please sign in to comment.