|
240 | 240 | return str.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ''); |
241 | 241 | } |
242 | 242 |
|
| 243 | + function startsWithComment(result) { |
| 244 | + var str = toSourceNodeWhenNeeded(result).toString(); |
| 245 | + return (/^\/\*[\s\S]*?\*\/|^\/\/.*/).test(str); |
| 246 | + } |
| 247 | + |
243 | 248 | function removeTrailingWhiteSpaces(str) { |
244 | 249 | return str.replace(/\S(\s+)$/g, ''); |
245 | 250 | } |
|
249 | 254 | str = removeTrailingWhiteSpaces(str); |
250 | 255 | var counter = 0; |
251 | 256 | for (var i = 0; i < str.length; ++i) { |
252 | | - if (str[i] == leftPar) { |
| 257 | + if (str[i] === leftPar) { |
253 | 258 | counter++; |
254 | 259 | } |
255 | | - else if (str[i] == rightPar) { |
256 | | - if (counter == 0) { |
| 260 | + else if (str[i] === rightPar) { |
| 261 | + if (counter === 0) { |
257 | 262 | return false; |
258 | 263 | } |
259 | 264 | counter--; |
260 | 265 | } |
261 | 266 | } |
262 | | - return counter == 0 && str[str.length-1] == rightPar; |
| 267 | + return counter === 0 && str[0] === leftPar && str[str.length-1] === rightPar; |
263 | 268 | } |
264 | 269 |
|
265 | 270 | function isParenthesizedByAnyBracketKind(str) { |
|
272 | 277 | if (!hasLineTerminator(str)) { |
273 | 278 | return false; |
274 | 279 | } |
275 | | - if (path.parent !== null && ( |
276 | | - path.parent.node.type === Syntax.ObjectExpression || |
277 | | - path.parent.node.type === Syntax.ArrayExpression || |
278 | | - path.parent.node.type === Syntax.Property || |
279 | | - path.parent.node.type === Syntax.ImportExpression |
280 | | - )) { |
281 | | - return false; |
282 | | - } |
283 | | - if (path.parent !== null && |
284 | | - path.node.type === Syntax.ImportSpecifier && |
285 | | - path.parent.node.type === Syntax.ImportDeclaration |
286 | | - ) { |
| 280 | + if (path.parent === null) { |
287 | 281 | return false; |
288 | 282 | } |
289 | | - if (path.parent !== null && path.parent.parent !== null && |
290 | | - path.parent.parent.node.type === Syntax.MethodDefinition && |
291 | | - path.parent.node.type === Syntax.FunctionExpression && |
292 | | - path.node.type === Syntax.AssignmentPattern) { |
293 | | - return false; |
294 | | - } |
295 | | - if (stmt.type === Syntax.MethodDefinition) { |
| 283 | + var parentNodeType = path.parent.node.type; |
| 284 | + if (parentNodeType !== Syntax.ReturnStatement && |
| 285 | + parentNodeType !== Syntax.ThrowStatement && |
| 286 | + parentNodeType !== Syntax.ArrowFunctionExpression) { |
296 | 287 | return false; |
297 | | - } |
298 | | - if (path.parent !== null && path.parent.node.type === Syntax.VariableDeclaration) { |
| 288 | + }; |
| 289 | + if (isParenthesizedByAnyBracketKind(str)) |
299 | 290 | return false; |
300 | | - } |
301 | | - if (!isParenthesizedByAnyBracketKind(str)) { |
302 | | - return true; |
303 | | - } |
304 | | - |
305 | | - str = removeComments(str); |
306 | | - var firstNewlineIdx = str.indexOf(newline); |
307 | | - var firstParenthesisIdx = str.match(/[\(\[\{)]/).index; |
308 | | - return firstNewlineIdx < firstParenthesisIdx; |
| 291 | + return true; |
309 | 292 | } |
310 | 293 |
|
311 | 294 | function merge(target, override) { |
|
752 | 735 |
|
753 | 736 | function addComments(stmt, result, path) { |
754 | 737 | var i, len, comment, save, tailingToStatement, specialBase, fragment, |
755 | | - extRange, range, prevRange, prefix, infix, suffix, count; |
| 738 | + extRange, range, prevRange, prefix, infix, suffix, count, |
| 739 | + generatedLeadingComments, hasLeadingComments; |
| 740 | + save = result; |
756 | 741 |
|
757 | 742 | if (stmt.leadingComments && stmt.leadingComments.length > 0) { |
758 | | - save = result; |
759 | 743 |
|
760 | 744 | if (preserveBlankLines) { |
761 | 745 | comment = stmt.leadingComments[0]; |
|
813 | 797 | result.push(addIndent(fragment)); |
814 | 798 | } |
815 | 799 | } |
| 800 | + generatedLeadingComments = true; |
| 801 | + } |
816 | 802 |
|
817 | | - if (!isExpression(stmt)) { |
818 | | - result.push(addIndent(save)); |
819 | | - } else { |
820 | | - var text = toSourceNodeWhenNeeded(result).toString(); |
821 | | - if (shouldParenthesize(text, stmt, path)) { |
822 | | - result = addMultiLineIndent(result); |
823 | | - result = [indent, result]; |
| 803 | + hasLeadingComments = generatedLeadingComments || startsWithComment(result); |
| 804 | + var text = toSourceNodeWhenNeeded(result).toString(); |
| 805 | + var parenthesize = shouldParenthesize(text, stmt, path); |
824 | 806 |
|
825 | | - result = ['(', newline, result]; |
| 807 | + if (!hasLeadingComments) { |
| 808 | + result = save; |
| 809 | + } else if (hasLeadingComments && parenthesize) { |
| 810 | + if (generatedLeadingComments) { |
| 811 | + result = addMultiLineIndent(result); |
| 812 | + result = [indent, result]; |
826 | 813 |
|
827 | | - withIndent(function () { |
828 | | - result.push(addMultiLineIndent(save)); |
829 | | - }); |
| 814 | + result = ['(', newline, result]; |
830 | 815 |
|
831 | | - result.push([newline, base, ')']); |
832 | | - } else { |
833 | | - result.push(addIndent(save)); |
834 | | - } |
| 816 | + withIndent(function () { |
| 817 | + result.push(addMultiLineIndent(save)); |
| 818 | + }); |
| 819 | + |
| 820 | + result.push([newline, base, ')']); |
| 821 | + } else { |
| 822 | + result = ['(', newline, indent, addMultiLineIndent(result), newline, base, ')']; |
835 | 823 | } |
| 824 | + } else if (generatedLeadingComments) { |
| 825 | + result.push(addIndent(save)); |
| 826 | + } else { |
| 827 | + result = save; |
836 | 828 | } |
837 | 829 |
|
838 | 830 | if (stmt.trailingComments) { |
|
0 commit comments