Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/escodegen.js b/escodegen.js
- index d5618c1..b94c73e 100644
- --- a/escodegen.js
- +++ b/escodegen.js
- @@ -81,7 +81,6 @@
- DoWhileStatement: 'DoWhileStatement',
- DebuggerStatement: 'DebuggerStatement',
- EmptyStatement: 'EmptyStatement',
- - EmptyExpression: 'EmptyExpression',
- ExpressionStatement: 'ExpressionStatement',
- ForStatement: 'ForStatement',
- ForInStatement: 'ForInStatement',
- @@ -774,47 +773,98 @@
- return result;
- }
- - function addExtrasToNode(node, result) {
- + function processExtras(extras, result, previousLineComment) {
- + var i, len, extra;
- - function addExtras(extras, result) {
- - var i, len, extra;
- -
- - for (i = 0, len = extras.length; i < len; i += 1) {
- - // was previous extra a line-comment?
- - if (previousLineComment) {
- - // need to insert a new-line to delimit previous line-comment?
- - if (!startsWithLineTerminator(extras[i].value)) {
- - result.push('\n');
- - }
- + for (i = 0, len = extras.length; i < len; i += 1) {
- + // was previous extra a line-comment?
- + if (previousLineComment) {
- + // need to insert a new-line to delimit previous line-comment?
- + if (!startsWithLineTerminator(extras[i].value)) {
- + result.push('\n');
- }
- + }
- - extra = extras[i];
- + extra = extras[i];
- - if (extra.type === 'Whitespace') {
- - result.push(extra.value);
- - previousLineComment = false;
- - }
- - else if (extra.type === 'LineComment') {
- - result.push((extra.marker || '//') + extra.value);
- - previousLineComment = true;
- - }
- - else if (extra.type === 'MultilineComment') {
- - result.push('/*' + extra.value + '*/');
- - previousLineComment = false;
- - }
- + if (extra.type === 'Whitespace') {
- + result.push(extra.value);
- + previousLineComment = false;
- }
- + else if (extra.type === 'LineComment') {
- + result.push((extra.marker || '//') + extra.value);
- + previousLineComment = true;
- + }
- + else if (extra.type === 'MultilineComment') {
- + result.push('/*' + extra.value + '*/');
- + previousLineComment = false;
- + }
- + }
- - return result;
- + return {
- + result: result,
- + previousLineComment: previousLineComment
- + };
- + }
- +
- + function extrasOrSpace(extras) {
- + var result, ret;
- +
- + if (extras) {
- + ret = processExtras(extras, [], false);
- + result = ret.result;
- +
- + // was last extra a line-comment?
- + if (ret.previousLineComment) {
- + // insert a new-line to delimit previous line-comment
- + result.push('\n');
- + }
- + }
- + else {
- + result = space;
- + }
- +
- + return result;
- + }
- +
- + function spaceIfNoExtras(extras) {
- + if (!extras) {
- + return space;
- }
- - var save, previousLineComment = false;
- + return '';
- + }
- +
- + function addBetweenExtrasToNode(node, result, firstBetweenChild) {
- + var ret;
- +
- + if (node && node.extras && node.extras.between && node.extras.between.length > 0) {
- + ret = processExtras(node.extras.between, result, false);
- + result = ret.result;
- +
- + // was last extra a line-comment?
- + if (ret.previousLineComment) {
- + // need to insert a new-line to delimit previous line-comment?
- + if (!firstBetweenChild || !startsWithLineTerminator(firstBetweenChild || '')) {
- + result.push('\n');
- + }
- + }
- + }
- +
- + return result;
- + }
- +
- + function addOuterExtrasToNode(node, result) {
- + var save, ret, previousLineComment = false;
- if (node && node.extras) {
- if (node.extras.leading && node.extras.leading.length > 0) {
- save = result;
- result = [];
- - result = addExtras(node.extras.leading, result);
- + ret = processExtras(node.extras.leading, result, previousLineComment);
- + result = ret.result;
- + previousLineComment = ret.previousLineComment;
- // was last extra a line-comment?
- if (previousLineComment) {
- @@ -833,7 +883,9 @@
- if (!Array.isArray(result)) {
- result = [result];
- }
- - result = addExtras(node.extras.trailing, result);
- + ret = processExtras(node.extras.trailing, result, previousLineComment);
- + result = ret.result;
- + previousLineComment = ret.previousLineComment;
- // was last extra a line-comment?
- if (previousLineComment) {
- @@ -898,7 +950,7 @@
- function generateIdentifier(node) {
- var ret = toSourceNode(node.name, node);
- if (node.extras) {
- - ret = addExtrasToNode(node,ret);
- + ret = addOuterExtrasToNode(node,ret);
- }
- return ret;
- }
- @@ -913,10 +965,13 @@
- result = [generateIdentifier(node.params[0])];
- } else {
- result = ['('];
- + if (node.extras) {
- + result = addBetweenExtrasToNode(node, result, (node.params[0] ? toSourceNode(generateIdentifier(node.params[0])).toString() : null));
- + }
- for (i = 0, len = node.params.length; i < len; i += 1) {
- result.push(generateIdentifier(node.params[i]));
- if (i + 1 < len) {
- - result.push(',' + space);
- + result.push(',', spaceIfNoExtras(node.params[i + 1].extras && node.params[i + 1].extras.leading));
- }
- }
- result.push(')');
- @@ -927,7 +982,7 @@
- }
- if (node.expression) {
- - result.push(space);
- + result.push(spaceIfNoExtras(node.body.extras && node.body.extras.leading));
- expr = generateExpression(node.body, {
- precedence: Precedence.Assignment,
- allowIn: true,
- @@ -971,15 +1026,6 @@
- }
- switch (type) {
- -
- - case Syntax.EmptyExpression:
- - // 'EmptyExpression' is just a placeholder node for where only
- - // extras are present.
- - // Example: comment or whitespace extras in an otherwise empty
- - // arguments list of a call expression
- - result = [];
- - break;
- -
- case Syntax.SequenceExpression:
- result = [];
- allowIn |= (Precedence.Sequence < precedence);
- @@ -990,7 +1036,7 @@
- allowCall: true
- }));
- if (i + 1 < len) {
- - result.push(',' + space);
- + result.push(',', spaceIfNoExtras(expr.expressions[i + 1].extras && expr.expressions[i + 1].extras.leading));
- }
- }
- result = parenthesize(result, Precedence.Sequence, precedence);
- @@ -1005,7 +1051,9 @@
- allowIn: allowIn,
- allowCall: true
- }),
- - space + expr.operator + space,
- + spaceIfNoExtras(expr.left.extras && expr.left.extras.trailing),
- + expr.operator,
- + spaceIfNoExtras(expr.right.extras && expr.right.extras.leading),
- generateExpression(expr.right, {
- precedence: Precedence.Assignment,
- allowIn: allowIn,
- @@ -1031,13 +1079,17 @@
- allowIn: allowIn,
- allowCall: true
- }),
- - space + '?' + space,
- + spaceIfNoExtras(expr.test.extras && expr.test.extras.trailing),
- + '?',
- + spaceIfNoExtras(expr.consequent.extras && expr.consequent.extras.leading),
- generateExpression(expr.consequent, {
- precedence: Precedence.Assignment,
- allowIn: allowIn,
- allowCall: true
- }),
- - space + ':' + space,
- + spaceIfNoExtras(expr.consequent.extras && expr.consequent.extras.trailing),
- + ':',
- + spaceIfNoExtras(expr.alternate.extras && expr.alternate.extras.leading),
- generateExpression(expr.alternate, {
- precedence: Precedence.Assignment,
- allowIn: allowIn,
- @@ -1064,7 +1116,11 @@
- leftSource = fragment.toString();
- if (leftSource.charAt(leftSource.length - 1) === '/' && isIdentifierPart(expr.operator.charAt(0))) {
- - result = [fragment, noEmptySpace(), expr.operator];
- + result = [fragment];
- + if (!(expr.left.extras && expr.left.extras.trailing)) {
- + result.push(noEmptySpace());
- + }
- + result.push(expr.operator);
- } else {
- result = join(fragment, expr.operator);
- }
- @@ -1078,7 +1134,10 @@
- if (expr.operator === '/' && fragment.toString().charAt(0) === '/' ||
- expr.operator.slice(-1) === '<' && fragment.toString().slice(0, 3) === '!--') {
- // If '/' concats with '/' or `<` concats with `!--`, it is interpreted as comment start
- - result.push(noEmptySpace(), fragment);
- + if (!(expr.right.extras && expr.right.extras.leading)) {
- + result.push(noEmptySpace());
- + }
- + result.push(fragment);
- } else {
- result = join(result, fragment);
- }
- @@ -1107,7 +1166,7 @@
- allowCall: true
- }));
- if (i + 1 < len) {
- - result.push(',' + space);
- + result.push(',', spaceIfNoExtras(expr['arguments'][i + 1].extras && expr['arguments'][i + 1].extras.leading));
- }
- }
- result.push(')');
- @@ -1142,7 +1201,7 @@
- allowCall: true
- }));
- if (i + 1 < len) {
- - result.push(',' + space);
- + result.push(',', spaceIfNoExtras(expr['arguments'][i + 1].extras && expr['arguments'][i + 1].extras.leading));
- }
- }
- result.push(')');
- @@ -1212,10 +1271,13 @@
- rightChar = fragment.toString().charAt(0);
- if (((leftChar === '+' || leftChar === '-') && leftChar === rightChar) || (isIdentifierPart(leftChar) && isIdentifierPart(rightChar))) {
- - result.push(noEmptySpace(), fragment);
- - } else {
- - result.push(fragment);
- + // were leading-extras NOT already included in `fragment`?
- + if (!(expr.argument.extras && expr.argument.extras.leading)) {
- + // make sure there's a necessary space there instead
- + result.push(noEmptySpace());
- + }
- }
- + result.push(fragment);
- }
- }
- result = parenthesize(result, Precedence.Unary, precedence);
- @@ -1273,9 +1335,13 @@
- result = 'function';
- if (expr.id) {
- - result = [result, noEmptySpace(),
- - generateIdentifier(expr.id),
- - generateFunctionBody(expr)];
- + result = [result];
- + // leading-extras NOT already included in identifier?
- + if (!(expr.id.extras && expr.id.extras.leading)) {
- + // make sure there's a necessary space there instead
- + result.push(noEmptySpace());
- + }
- + result.push(generateIdentifier(expr.id), generateFunctionBody(expr));
- } else {
- result = [result + space, generateFunctionBody(expr)];
- }
- @@ -1284,50 +1350,78 @@
- case Syntax.ArrayPattern:
- case Syntax.ArrayExpression:
- + if (expr.extras) {
- + result = [];
- + result.push('[');
- + result = addBetweenExtrasToNode(expr, result);
- + }
- +
- if (!expr.elements.length) {
- - result = '[]';
- + result.push(']');
- break;
- }
- - multiline = expr.elements.length > 1;
- - result = ['[', multiline ? newline : ''];
- - withIndent(function (indent) {
- +
- + if (expr.extras) {
- for (i = 0, len = expr.elements.length; i < len; i += 1) {
- - if (!expr.elements[i]) {
- - if (multiline) {
- - result.push(indent);
- - }
- - if (i + 1 === len) {
- - result.push(',');
- - }
- - } else {
- - result.push(multiline ? indent : '', generateExpression(expr.elements[i], {
- + if (expr.elements[i]) {
- + result.push(generateExpression(expr.elements[i], {
- precedence: Precedence.Assignment,
- allowIn: true,
- allowCall: true
- }));
- }
- +
- if (i + 1 < len) {
- - result.push(',' + (multiline ? newline : space));
- + result.push(',', spaceIfNoExtras(expr.elements[i + 1].extras && expr.elements[i + 1].extras.leading));
- }
- }
- - });
- - if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
- - result.push(newline);
- + result.push(']');
- + } else {
- + multiline = expr.elements.length > 1;
- + result = ['[', multiline ? newline : ''];
- + withIndent(function (indent) {
- + for (i = 0, len = expr.elements.length; i < len; i += 1) {
- + if (!expr.elements[i]) {
- + if (multiline) {
- + result.push(indent);
- + }
- + if (i + 1 === len) {
- + result.push(',');
- + }
- + } else {
- + result.push(multiline ? indent : '', generateExpression(expr.elements[i], {
- + precedence: Precedence.Assignment,
- + allowIn: true,
- + allowCall: true
- + }));
- + }
- + if (i + 1 < len) {
- + result.push(',' + (multiline ? newline : space));
- + }
- + }
- + });
- + if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
- + result.push(newline);
- + }
- + result.push(multiline ? base : '', ']');
- }
- - result.push(multiline ? base : '', ']');
- break;
- case Syntax.Property:
- if (expr.kind === 'get' || expr.kind === 'set') {
- - result = [
- - expr.kind, noEmptySpace(),
- - generateExpression(expr.key, {
- + result = [expr.kind];
- + // leading-extras NOT already included in key?
- + if (!(expr.key.extras && expr.key.extras.leading)) {
- + // make sure there's a necessary space there instead
- + result.push(noEmptySpace());
- + }
- + result.push(generateExpression(expr.key, {
- precedence: Precedence.Sequence,
- allowIn: true,
- allowCall: true
- }),
- generateFunctionBody(expr.value)
- - ];
- + );
- } else {
- if (expr.shorthand) {
- result = generateExpression(expr.key, {
- @@ -1352,7 +1446,8 @@
- allowIn: true,
- allowCall: true
- }),
- - ':' + space,
- + ':',
- + spaceIfNoExtras(expr.value.extras && expr.value.extras.leading),
- generateExpression(expr.value, {
- precedence: Precedence.Assignment,
- allowIn: true,
- @@ -1364,101 +1459,149 @@
- break;
- case Syntax.ObjectExpression:
- - if (!expr.properties.length) {
- - result = '{}';
- - break;
- + if (expr.extras) {
- + result = [];
- + result.push('{');
- + result = addBetweenExtrasToNode(expr, result);
- }
- - multiline = expr.properties.length > 1;
- -
- - withIndent(function () {
- - fragment = generateExpression(expr.properties[0], {
- - precedence: Precedence.Sequence,
- - allowIn: true,
- - allowCall: true,
- - type: Syntax.Property
- - });
- - });
- - if (!multiline) {
- - // issues 4
- - // Do not transform from
- - // dejavu.Class.declare({
- - // method2: function () {}
- - // });
- - // to
- - // dejavu.Class.declare({method2: function () {
- - // }});
- - if (!hasLineTerminator(toSourceNode(fragment).toString())) {
- - result = [ '{', space, fragment, space, '}' ];
- - break;
- - }
- + if (!expr.properties.length) {
- + result.push('}');
- + break;
- }
- - withIndent(function (indent) {
- - result = [ '{', newline, indent, fragment ];
- -
- - if (multiline) {
- - result.push(',' + newline);
- - for (i = 1, len = expr.properties.length; i < len; i += 1) {
- - result.push(indent, generateExpression(expr.properties[i], {
- + if (expr.extras) {
- + for (i = 0, len = expr.properties.length; i < len; i += 1) {
- + if (expr.properties[i]) {
- + result.push(generateExpression(expr.properties[i], {
- precedence: Precedence.Sequence,
- allowIn: true,
- allowCall: true,
- type: Syntax.Property
- }));
- - if (i + 1 < len) {
- - result.push(',' + newline);
- - }
- + }
- +
- + if (i + 1 < len) {
- + result.push(',', spaceIfNoExtras(expr.properties[i + 1].extras && expr.properties[i + 1].extras.leading));
- }
- }
- - });
- + result.push('}');
- + } else {
- + multiline = expr.properties.length > 1;
- - if (!endsWithLineTerminator(toSourceNode(result).toString())) {
- - result.push(newline);
- + withIndent(function () {
- + fragment = generateExpression(expr.properties[0], {
- + precedence: Precedence.Sequence,
- + allowIn: true,
- + allowCall: true,
- + type: Syntax.Property
- + });
- + });
- +
- + if (!multiline) {
- + // issues 4
- + // Do not transform from
- + // dejavu.Class.declare({
- + // method2: function () {}
- + // });
- + // to
- + // dejavu.Class.declare({method2: function () {
- + // }});
- + if (!hasLineTerminator(toSourceNode(fragment).toString())) {
- + result = [ '{', space, fragment, space, '}' ];
- + break;
- + }
- + }
- +
- + withIndent(function (indent) {
- + result = [ '{', newline, indent, fragment ];
- +
- + if (multiline) {
- + result.push(',' + newline);
- + for (i = 1, len = expr.properties.length; i < len; i += 1) {
- + result.push(indent, generateExpression(expr.properties[i], {
- + precedence: Precedence.Sequence,
- + allowIn: true,
- + allowCall: true,
- + type: Syntax.Property
- + }));
- + if (i + 1 < len) {
- + result.push(',' + newline);
- + }
- + }
- + }
- + });
- +
- + if (!endsWithLineTerminator(toSourceNode(result).toString())) {
- + result.push(newline);
- + }
- + result.push(base, '}');
- }
- - result.push(base, '}');
- break;
- case Syntax.ObjectPattern:
- + if (expr.extras) {
- + result = [];
- + result.push('{');
- + result = addBetweenExtrasToNode(expr, result);
- + }
- +
- if (!expr.properties.length) {
- - result = '{}';
- + result.push('}');
- break;
- }
- - multiline = false;
- - if (expr.properties.length === 1) {
- - property = expr.properties[0];
- - if (property.value.type !== Syntax.Identifier) {
- - multiline = true;
- + if (expr.extras) {
- + for (i = 0, len = expr.properties.length; i < len; i += 1) {
- + if (expr.properties[i]) {
- + result.push(generateExpression(expr.properties[i], {
- + precedence: Precedence.Sequence,
- + allowIn: true,
- + allowCall: true
- + }));
- + }
- +
- + if (i + 1 < len) {
- + result.push(',', spaceIfNoExtras(expr.properties[i + 1].extras && expr.properties[i + 1].extras.leading));
- + }
- }
- + result.push('}');
- } else {
- - for (i = 0, len = expr.properties.length; i < len; i += 1) {
- - property = expr.properties[i];
- - if (!property.shorthand) {
- + multiline = false;
- + if (expr.properties.length === 1) {
- + property = expr.properties[0];
- + if (property.value.type !== Syntax.Identifier) {
- multiline = true;
- - break;
- + }
- + } else {
- + for (i = 0, len = expr.properties.length; i < len; i += 1) {
- + property = expr.properties[i];
- + if (!property.shorthand) {
- + multiline = true;
- + break;
- + }
- }
- }
- - }
- - result = ['{', multiline ? newline : '' ];
- + result = ['{', multiline ? newline : '' ];
- - withIndent(function (indent) {
- - for (i = 0, len = expr.properties.length; i < len; i += 1) {
- - result.push(multiline ? indent : '', generateExpression(expr.properties[i], {
- - precedence: Precedence.Sequence,
- - allowIn: true,
- - allowCall: true
- - }));
- - if (i + 1 < len) {
- - result.push(',' + (multiline ? newline : space));
- + withIndent(function (indent) {
- + for (i = 0, len = expr.properties.length; i < len; i += 1) {
- + result.push(multiline ? indent : '', generateExpression(expr.properties[i], {
- + precedence: Precedence.Sequence,
- + allowIn: true,
- + allowCall: true
- + }));
- + if (i + 1 < len) {
- + result.push(',' + (multiline ? newline : space));
- + }
- }
- - }
- - });
- + });
- - if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
- - result.push(newline);
- + if (multiline && !endsWithLineTerminator(toSourceNode(result).toString())) {
- + result.push(newline);
- + }
- + result.push(multiline ? base : '', '}');
- }
- - result.push(multiline ? base : '', '}');
- break;
- case Syntax.ThisExpression:
- @@ -1508,14 +1651,18 @@
- break;
- case Syntax.ComprehensionExpression:
- - result = [
- - '[',
- + result = [];
- + result.push('[');
- + if (expr.extras) {
- + result = addBetweenExtrasToNode(expr, result);
- + }
- + result.push(
- generateExpression(expr.body, {
- precedence: Precedence.Assignment,
- allowIn: true,
- allowCall: true
- })
- - ];
- + );
- if (expr.blocks) {
- for (i = 0, len = expr.blocks.length; i < len; i += 1) {
- @@ -1579,7 +1726,7 @@
- }
- if (expr.extras) {
- - result = addExtrasToNode(expr, result);
- + result = addOuterExtrasToNode(expr, result);
- }
- return toSourceNode(result, expr);
- @@ -1663,7 +1810,7 @@
- case Syntax.CatchClause:
- withIndent(function () {
- result = [
- - 'catch' + space + '(',
- + 'catch', extrasOrSpace(stmt.extras && stmt.extras.between), '(',
- generateExpression(stmt.param, {
- precedence: Precedence.Sequence,
- allowIn: true,
- @@ -1707,9 +1854,9 @@
- allowIn: allowIn,
- allowCall: true
- }),
- - space,
- + spaceIfNoExtras(stmt.extras && stmt.extras.trailing),
- '=',
- - space,
- + spaceIfNoExtras(stmt.extras && stmt.extras.leading),
- generateExpression(stmt.init, {
- precedence: Precedence.Assignment,
- allowIn: allowIn,
- @@ -1754,7 +1901,7 @@
- allowIn: allowIn
- })));
- } else {
- - result.push(',' + space, generateStatement(node, {
- + result.push(',' + spaceIfNoExtras(node.extras && node.extras.leading), generateStatement(node, {
- allowIn: allowIn
- }));
- }
- @@ -1810,7 +1957,7 @@
- case Syntax.SwitchStatement:
- withIndent(function () {
- result = [
- - 'switch' + space + '(',
- + 'switch' + extrasOrSpace(stmt.extras && stmt.extras.between) + '(',
- generateExpression(stmt.discriminant, {
- precedence: Precedence.Sequence,
- allowIn: true,
- @@ -2052,7 +2199,7 @@
- }
- if (stmt.extras) {
- - result = addExtrasToNode(stmt, result);
- + result = addOuterExtrasToNode(stmt, result);
- }
- return toSourceNode(result, stmt);
- @@ -2150,7 +2297,6 @@
- case Syntax.BinaryExpression:
- case Syntax.CallExpression:
- case Syntax.ConditionalExpression:
- - case Syntax.EmptyExpression:
- case Syntax.FunctionExpression:
- case Syntax.Identifier:
- case Syntax.Literal:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement