public static JsExpr concatJsExprs ( List < ? extends JsExpr > jsExprs ) { if ( jsExprs . isEmpty ( ) ) { return EMPTY_STRING ; } if ( jsExprs . size ( ) == 1 ) { return jsExprs . get ( 0 ) ; } int plusOpPrec = Operator . PLUS . getPrecedence ( ) ; StringBuilder resultSb = new StringBuilder ( ) ; boolean isFirst = true ; for ( JsExpr jsExpr : jsExprs ) { // The first operand needs protection only if it's strictly lower precedence. The non-first // operands need protection when they're lower or equal precedence. (This is true for all // left-associative operators.) boolean needsProtection = isFirst ? jsExpr . getPrecedence ( ) < plusOpPrec : jsExpr . getPrecedence ( ) <= plusOpPrec ; if ( isFirst ) { isFirst = false ; } else { resultSb . append ( " + " ) ; } if ( needsProtection ) { resultSb . append ( ' ' ) . append ( jsExpr . getText ( ) ) . append ( ' ' ) ; } else { resultSb . append ( jsExpr . getText ( ) ) ; } } return new JsExpr ( resultSb . toString ( ) , plusOpPrec ) ; }
Builds one JS expression that computes the concatenation of the given JS expressions . The + operator is used for concatenation . Operands will be protected with an extra pair of parentheses if and only if needed .