@@ -6,6 +6,8 @@ const extendFixRange = require('./utils/extend-fix-range');
66const needsSemicolon = require ( './utils/needs-semicolon' ) ;
77const isSameReference = require ( './utils/is-same-reference' ) ;
88const getIndentString = require ( './utils/get-indent-string' ) ;
9+ const { getParenthesizedText} = require ( './utils/parentheses' ) ;
10+ const shouldAddParenthesesToConditionalExpressionChild = require ( './utils/should-add-parentheses-to-conditional-expression-child' ) ;
911
1012const messageId = 'prefer-ternary' ;
1113
@@ -53,17 +55,16 @@ const create = context => {
5355 return ! generatedNames || ! generatedNames . has ( name ) ;
5456 } ) ;
5557
56- const getParenthesizedText = node => {
57- const text = sourceCode . getText ( node ) ;
58- return (
59- isParenthesized ( node , sourceCode ) ||
60- node . type === 'AwaitExpression' ||
61- // Lower precedence, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
62- node . type === 'AssignmentExpression' ||
63- node . type === 'YieldExpression' ||
64- node . type === 'SequenceExpression'
65- ) ?
66- `(${ text } )` : text ;
58+ const getText = node => {
59+ let text = getParenthesizedText ( node , sourceCode ) ;
60+ if (
61+ ! isParenthesized ( node , sourceCode ) &&
62+ shouldAddParenthesesToConditionalExpressionChild ( node )
63+ ) {
64+ text = `(${ text } )` ;
65+ }
66+
67+ return text ;
6768 } ;
6869
6970 const isSingleLineNode = node => {
@@ -206,13 +207,13 @@ const create = context => {
206207 node,
207208 messageId,
208209 * fix ( fixer ) {
209- const testText = getParenthesizedText ( node . test ) ;
210+ const testText = getText ( node . test ) ;
210211 const consequentText = typeof result . consequent === 'string' ?
211212 result . consequent :
212- getParenthesizedText ( result . consequent ) ;
213+ getText ( result . consequent ) ;
213214 const alternateText = typeof result . alternate === 'string' ?
214215 result . alternate :
215- getParenthesizedText ( result . alternate ) ;
216+ getText ( result . alternate ) ;
216217
217218 let { type, before, after} = result ;
218219
0 commit comments