-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduction of experimentalTernaries * rearranging Conditionals to experiment with the formatting * cleanup and closer the format suggested by prettier * tuning up the breaking rules * breaking lines and indenting non experimental ternaries. * adding and removing parentheses in conditional if condition breaks * fixing build script * removing unneeded group * Fixing indentation within a FunctionCall * cleanup and documentation * Adding README information on Experimental Ternaries * Extra comment * more readable bracket selection * condition can be a Conditional if it's wrapped in a parentheses * not using hardline inside an ifBreak to avoid unexpected group break propagation * moving check for parent function outside the breaking group, fixing an edge case and the linter error * clean up code and adding group around falseExpression * removing unnecessary conditions and hardlines and cleaning up the documentation * Adding extra examples * undoing unnecessary change * group reorganisation that allows us to get rid of ifBreaks and hardlineWithoutBreakParent cleaning the resulting code * unneeded check for new line on falseExpression * cleaner output * cleaning up documentation * Adding spaces after `:` to align properly with the trueExpression * adding indentation if we fill a tab before falseExpression * undoing refactor for performance issues
- Loading branch information
Showing
12 changed files
with
2,492 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,88 @@ | ||
import { doc } from 'prettier'; | ||
import { printSeparatedItem } from '../common/printer-helpers.js'; | ||
|
||
const { group, indent, line } = doc.builders; | ||
const { group, hardline, ifBreak, indent, line, softline } = doc.builders; | ||
|
||
export const Conditional = { | ||
print: ({ path, print }) => | ||
group([ | ||
path.call(print, 'condition'), | ||
indent([ | ||
line, | ||
'? ', | ||
path.call(print, 'trueExpression'), | ||
line, | ||
': ', | ||
path.call(print, 'falseExpression') | ||
]) | ||
let groupIndex = 0; | ||
const experimentalTernaries = (node, path, print, options) => { | ||
const parent = path.getParentNode(); | ||
const isNested = parent.type === 'Conditional'; | ||
const isNestedAsTrueExpression = isNested && parent.trueExpression === node; | ||
const falseExpressionIsNested = node.falseExpression.type === 'Conditional'; | ||
|
||
// If the `condition` breaks into multiple lines, we add parentheses, | ||
// unless it already is a `TupleExpression`. | ||
const condition = path.call(print, 'condition'); | ||
const conditionDoc = group([ | ||
node.condition.type === 'TupleExpression' | ||
? condition | ||
: ifBreak(['(', printSeparatedItem(condition), ')'], condition), | ||
' ?' | ||
]); | ||
|
||
// To switch between "case-style" and "curious" ternaries we force a new line | ||
// before a nested `trueExpression` if the current `Conditional` is also a | ||
// `trueExpression`. | ||
const trueExpressionDoc = indent([ | ||
isNestedAsTrueExpression ? hardline : line, | ||
path.call(print, 'trueExpression') | ||
]); | ||
|
||
const conditionAndTrueExpressionGroup = group( | ||
[conditionDoc, trueExpressionDoc], | ||
{ id: `Conditional.trueExpressionDoc-${groupIndex}` } | ||
); | ||
|
||
groupIndex += 1; | ||
|
||
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single | ||
// space. | ||
let fillTab = ' '; | ||
if ( | ||
!falseExpressionIsNested && // avoid processing if it's not needed | ||
(options.tabWidth > 2 || options.useTabs) | ||
) { | ||
fillTab = options.useTabs ? '\t' : ' '.repeat(options.tabWidth - 1); | ||
} | ||
|
||
// A nested `falseExpression` is always printed in a new line. | ||
const falseExpression = path.call(print, 'falseExpression'); | ||
const falseExpressionDoc = [ | ||
isNested ? hardline : line, | ||
':', | ||
falseExpressionIsNested | ||
? [' ', falseExpression] | ||
: ifBreak([fillTab, indent(falseExpression)], [' ', falseExpression], { | ||
// We only add `fillTab` if we are sure the trueExpression is indented | ||
groupId: conditionAndTrueExpressionGroup.id | ||
}) | ||
]; | ||
|
||
const document = group([conditionAndTrueExpressionGroup, falseExpressionDoc]); | ||
|
||
return parent.type === 'VariableDeclarationStatement' | ||
? indent([softline, document]) | ||
: document; | ||
}; | ||
|
||
const traditionalTernaries = (path, print) => | ||
group([ | ||
path.call(print, 'condition'), | ||
indent([ | ||
// Nested trueExpression and falseExpression are always printed in a new | ||
// line | ||
path.getParentNode().type === 'Conditional' ? hardline : line, | ||
'? ', | ||
path.call(print, 'trueExpression'), | ||
line, | ||
': ', | ||
path.call(print, 'falseExpression') | ||
]) | ||
]); | ||
|
||
export const Conditional = { | ||
print: ({ node, path, print, options }) => | ||
options.experimentalTernaries | ||
? experimentalTernaries(node, path, print, options) | ||
: traditionalTernaries(path, print) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.