Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
sourceMap,
sourceCode,
preserveBlankLines,
wrapIIFE,
FORMAT_MINIFY,
FORMAT_DEFAULTS;

Expand Down Expand Up @@ -190,7 +191,8 @@
parentheses: true,
semicolons: true,
safeConcatenation: false,
preserveBlankLines: false
preserveBlankLines: false,
wrapIIFE: false
},
moz: {
comprehensionExpressionStartsWithAssignment: false,
Expand Down Expand Up @@ -739,6 +741,10 @@
prefix = sourceCode.substring(extRange[0], range[0]);
count = (prefix.match(/\n/g) || []).length;

if (typeof result === 'string') {
result = [result];
}

if (count > 0) {
result.push(stringRepeat('\n', count));
result.push(addIndent(generateComment(comment)));
Expand Down Expand Up @@ -1060,14 +1066,15 @@
result = ['{'];
}
}
if (!stmt.body[0].leadingComments) {
if (!stmt.body[0].leadingComments && stmt.body[0].range) {
generateBlankLines(stmt.range[0], stmt.body[0].range[0], result);
}
}

// handle spaces between lines
if (i > 0) {
if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) {
if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments &&
stmt.body[i - 1].range && stmt.body[i].range) {
generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result);
}
}
Expand Down Expand Up @@ -1099,7 +1106,7 @@
if (preserveBlankLines) {
// handle spaces after the last line
if (i === iz - 1) {
if (!stmt.body[i].trailingComments) {
if (!stmt.body[i].trailingComments && stmt.body[i].range) {
generateBlankLines(stmt.body[i].range[1], stmt.range[1], result);
}
}
Expand Down Expand Up @@ -1701,14 +1708,15 @@
if (preserveBlankLines) {
// handle spaces before the first line
if (i === 0) {
if (!stmt.body[0].leadingComments) {
if (!stmt.body[0].leadingComments && stmt.body[i].range) {
generateBlankLines(stmt.range[0], stmt.body[i].range[0], result);
}
}

// handle spaces between lines
if (i > 0) {
if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) {
if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments &&
stmt.body[i - 1].range && stmt.body[i].range) {
generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result);
}
}
Expand All @@ -1729,7 +1737,7 @@
if (preserveBlankLines) {
// handle spaces after the last line
if (i === iz - 1) {
if (!stmt.body[i].trailingComments) {
if (!stmt.body[i].trailingComments && stmt.body[i].range) {
generateBlankLines(stmt.body[i].range[1], stmt.range[1], result);
}
}
Expand Down Expand Up @@ -1872,7 +1880,7 @@
},

CallExpression: function (expr, precedence, flags) {
var result, i, iz;
var result, i, iz, isIIFE;
// F_ALLOW_UNPARATH_NEW becomes false.
result = [this.generateExpression(expr.callee, Precedence.Call, E_TTF)];
result.push('(');
Expand All @@ -1887,7 +1895,12 @@
if (!(flags & F_ALLOW_CALL)) {
return ['(', result, ')'];
}
return parenthesize(result, Precedence.Call, precedence);

isIIFE = expr.callee.id === null && expr.callee.params.length === 0;

return (isIIFE && wrapIIFE) ?
parenthesize(result, 0, 1) :
parenthesize(result, Precedence.Call, precedence);
},

NewExpression: function (expr, precedence, flags) {
Expand Down Expand Up @@ -2546,6 +2559,7 @@
sourceMap = options.sourceMap;
sourceCode = options.sourceCode;
preserveBlankLines = options.format.preserveBlankLines && sourceCode !== null;
wrapIIFE = options.format.wrapIIFE;
extra = options;

if (sourceMap) {
Expand Down