@@ -14,32 +14,6 @@ const tryHug = (node, operators) => {
1414 return node ;
1515} ;
1616
17- // The parser wrongly groups nested Conditionals in the falseExpression
18- // in the following way:
19- //
20- // (a ? b : c) ? d : e;
21- //
22- // By reorganizing the group we have more flexibility when printing:
23- //
24- // a ? b : (c ? d : e);
25- //
26- // this is closer to the executed code and prints the same output.
27- const rearrangeConditional = ( ctx ) => {
28- while ( ctx . condition . type === 'Conditional' ) {
29- const falseExpression = {
30- type : 'Conditional' ,
31- condition : ctx . condition . falseExpression ,
32- trueExpression : ctx . trueExpression ,
33- falseExpression : ctx . falseExpression
34- } ;
35- rearrangeConditional ( falseExpression ) ;
36-
37- ctx . falseExpression = falseExpression ;
38- ctx . trueExpression = ctx . condition . trueExpression ;
39- ctx . condition = ctx . condition . condition ;
40- }
41- } ;
42-
4317function parse ( text , _parsers , options = _parsers ) {
4418 const compiler = coerce ( options . compiler ) ;
4519 const parsed = parser . parse ( text , { loc : true , range : true } ) ;
@@ -87,7 +61,6 @@ function parse(text, _parsers, options = _parsers) {
8761 ctx . value = options . singleQuote ? `hex'${ value } '` : `hex"${ value } "` ;
8862 } ,
8963 Conditional ( ctx ) {
90- rearrangeConditional ( ctx ) ;
9164 // We can remove parentheses only because we are sure that the
9265 // `condition` must be a single `bool` value.
9366 while (
@@ -116,34 +89,34 @@ function parse(text, _parsers, options = _parsers) {
11689 ctx . left = tryHug ( ctx . left , [ '*' , '/' , '%' ] ) ;
11790 break ;
11891 case '**' :
119- // If the compiler has not been given as an option using we leave a**b**c.
92+ // If the compiler has not been given as an option using we leave
93+ // a**b**c.
12094 if ( ! compiler ) break ;
12195
122- if ( satisfies ( compiler , '< 0.8.0' ) ) {
123- // If the compiler is less than 0.8.0 then a**b**c is formatted as
124- // ( a**b) **c.
125- ctx . left = tryHug ( ctx . left , [ '**' ] ) ;
96+ if ( satisfies ( compiler , '>= 0.8.0' ) ) {
97+ // If the compiler is greater than or equal to 0.8.0 then a**b**c
98+ // is formatted as a**(b **c) .
99+ ctx . right = tryHug ( ctx . right , [ '**' ] ) ;
126100 break ;
127101 }
128102 if (
129- ctx . left . type === 'BinaryOperation' &&
130- ctx . left . operator === '**'
103+ ctx . right . type === 'BinaryOperation' &&
104+ ctx . right . operator === '**'
131105 ) {
132- // the parser still organizes the a**b**c as (a**b)**c so we need
133- // to restructure it.
134- ctx . right = {
106+ // the parser organizes the a**b**c as a**(b**c) so we need to
107+ // restructure it.
108+ const left = {
109+ type : 'BinaryOperation' ,
110+ operator : '**' ,
111+ left : ctx . left ,
112+ right : ctx . right . left
113+ } ;
114+ ctx . left = {
135115 type : 'TupleExpression' ,
136- components : [
137- {
138- type : 'BinaryOperation' ,
139- operator : '**' ,
140- left : ctx . left . right ,
141- right : ctx . right
142- }
143- ] ,
116+ components : [ left ] ,
144117 isArray : false
145118 } ;
146- ctx . left = ctx . left . left ;
119+ ctx . right = ctx . right . right ;
147120 }
148121 break ;
149122 case '<<' :
0 commit comments