Skip to content

Commit

Permalink
- Use ES6 class
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jun 23, 2020
1 parent 5424f3b commit cf9a7b8
Showing 1 changed file with 169 additions and 170 deletions.
339 changes: 169 additions & 170 deletions src/escodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,46 +787,6 @@
return toSourceNodeWhenNeeded(result, expr);
}

function CodeGenerator() {
}

// Helpers.

CodeGenerator.prototype.maybeBlock = function(stmt, flags) {
const that = this;

const noLeadingComment = !extra.comment || !stmt.leadingComments;

if (stmt.type === Syntax.BlockStatement && noLeadingComment) {
return [space, this.generateStatement(stmt, flags)];
}

if (stmt.type === Syntax.EmptyStatement && noLeadingComment) {
return ';';
}

let result;
withIndent(function () {
result = [
newline,
addIndent(that.generateStatement(stmt, flags))
];
});

return result;
};

CodeGenerator.prototype.maybeBlockSuffix = function (stmt, result) {
const ends = endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString());
if (stmt.type === Syntax.BlockStatement && (!extra.comment || !stmt.leadingComments) && !ends) {
return [result, space];
}
if (ends) {
return [result, base];
}
return [result, newline, base];
};

function generateIdentifier(node) {
return toSourceNodeWhenNeeded(node.name, node);
}
Expand All @@ -853,137 +813,208 @@
return prefix;
}

CodeGenerator.prototype.generatePattern = function (node, precedence, flags) {
if (node.type === Syntax.Identifier) {
return generateIdentifier(node);
class CodeGenerator {
// Helpers.

maybeBlock (stmt, flags) {
const that = this;

const noLeadingComment = !extra.comment || !stmt.leadingComments;

if (stmt.type === Syntax.BlockStatement && noLeadingComment) {
return [space, this.generateStatement(stmt, flags)];
}

if (stmt.type === Syntax.EmptyStatement && noLeadingComment) {
return ';';
}

let result;
withIndent(function () {
result = [
newline,
addIndent(that.generateStatement(stmt, flags))
];
});

return result;
}
return this.generateExpression(node, precedence, flags);
};

CodeGenerator.prototype.generateFunctionParams = function (node) {
let result;
let hasDefault = false;
maybeBlockSuffix (stmt, result) {
const ends = endsWithLineTerminator(toSourceNodeWhenNeeded(result).toString());
if (stmt.type === Syntax.BlockStatement && (!extra.comment || !stmt.leadingComments) && !ends) {
return [result, space];
}
if (ends) {
return [result, base];
}
return [result, newline, base];
}

if (node.type === Syntax.ArrowFunctionExpression &&
!node.rest && (!node.defaults || node.defaults.length === 0) &&
node.params.length === 1 && node.params[0].type === Syntax.Identifier) {
// arg => { } case
result = [generateAsyncPrefix(node, true), generateIdentifier(node.params[0])];
} else {
result = node.type === Syntax.ArrowFunctionExpression ? [generateAsyncPrefix(node, false)] : [];
result.push('(');
if (node.defaults) {
hasDefault = true;
generatePattern (node, precedence, flags) {
if (node.type === Syntax.Identifier) {
return generateIdentifier(node);
}
for (let i = 0, iz = node.params.length; i < iz; ++i) {
if (hasDefault && node.defaults[i]) {
// Handle default values.
result.push(this.generateAssignment(node.params[i], node.defaults[i], '=', Precedence.Assignment, E_TTT));
} else {
result.push(this.generatePattern(node.params[i], Precedence.Assignment, E_TTT));
return this.generateExpression(node, precedence, flags);
}

generateFunctionParams (node) {
let result;
let hasDefault = false;

if (node.type === Syntax.ArrowFunctionExpression &&
!node.rest && (!node.defaults || node.defaults.length === 0) &&
node.params.length === 1 && node.params[0].type === Syntax.Identifier) {
// arg => { } case
result = [generateAsyncPrefix(node, true), generateIdentifier(node.params[0])];
} else {
result = node.type === Syntax.ArrowFunctionExpression ? [generateAsyncPrefix(node, false)] : [];
result.push('(');
if (node.defaults) {
hasDefault = true;
}
if (i + 1 < iz) {
result.push(`,${ space}`);
for (let i = 0, iz = node.params.length; i < iz; ++i) {
if (hasDefault && node.defaults[i]) {
// Handle default values.
result.push(this.generateAssignment(node.params[i], node.defaults[i], '=', Precedence.Assignment, E_TTT));
} else {
result.push(this.generatePattern(node.params[i], Precedence.Assignment, E_TTT));
}
if (i + 1 < iz) {
result.push(`,${ space}`);
}
}
}

if (node.rest) {
if (node.params.length) {
result.push(`,${ space}`);
if (node.rest) {
if (node.params.length) {
result.push(`,${ space}`);
}
result.push('...');
result.push(generateIdentifier(node.rest));
}
result.push('...');
result.push(generateIdentifier(node.rest));

result.push(')');
}

result.push(')');
return result;
}

return result;
};
generateFunctionBody (node) {
const result = this.generateFunctionParams(node);

CodeGenerator.prototype.generateFunctionBody = function (node) {
const result = this.generateFunctionParams(node);
if (node.type === Syntax.ArrowFunctionExpression) {
result.push(space);
result.push('=>');
}

if (node.type === Syntax.ArrowFunctionExpression) {
result.push(space);
result.push('=>');
if (node.expression) {
result.push(space);
let expr = this.generateExpression(node.body, Precedence.Assignment, E_TTT);
if (expr.toString().charAt(0) === '{') {
expr = ['(', expr, ')'];
}
result.push(expr);
} else {
result.push(this.maybeBlock(node.body, S_TTFF));
}

return result;
}

if (node.expression) {
result.push(space);
let expr = this.generateExpression(node.body, Precedence.Assignment, E_TTT);
if (expr.toString().charAt(0) === '{') {
expr = ['(', expr, ')'];
}
result.push(expr);
} else {
result.push(this.maybeBlock(node.body, S_TTFF));
generateIterationForStatement (operator, stmt, flags) {
const that = this;
let result = [`for${ stmt.await ? `${noEmptySpace() }await` : '' }${space }(`];
withIndent(function () {
if (stmt.left.type === Syntax.VariableDeclaration) {
withIndent(function () {
result.push(stmt.left.kind + noEmptySpace());
result.push(that.generateStatement(stmt.left.declarations[0], S_FFFF));
});
} else {
result.push(that.generateExpression(stmt.left, Precedence.Call, E_TTT));
}

result = join(result, operator);
result = [join(
result,
that.generateExpression(stmt.right, Precedence.Assignment, E_TTT)
), ')'];
});
result.push(this.maybeBlock(stmt.body, flags));
return result;
}

return result;
};
generatePropertyKey (expr, computed) {
const result = [];

CodeGenerator.prototype.generateIterationForStatement = function (operator, stmt, flags) {
const that = this;
let result = [`for${ stmt.await ? `${noEmptySpace() }await` : '' }${space }(`];
withIndent(function () {
if (stmt.left.type === Syntax.VariableDeclaration) {
withIndent(function () {
result.push(stmt.left.kind + noEmptySpace());
result.push(that.generateStatement(stmt.left.declarations[0], S_FFFF));
});
} else {
result.push(that.generateExpression(stmt.left, Precedence.Call, E_TTT));
if (computed) {
result.push('[');
}

result = join(result, operator);
result = [join(
result,
that.generateExpression(stmt.right, Precedence.Assignment, E_TTT)
), ')'];
});
result.push(this.maybeBlock(stmt.body, flags));
return result;
};
result.push(this.generateExpression(expr, Precedence.Sequence, E_TTT));

CodeGenerator.prototype.generatePropertyKey = function (expr, computed) {
const result = [];
if (computed) {
result.push(']');
}

if (computed) {
result.push('[');
return result;
}

result.push(this.generateExpression(expr, Precedence.Assignment, E_TTT));
generateAssignment (left, right, operator, precedence, flags) {
if (Precedence.Assignment < precedence) {
flags |= F_ALLOW_IN;
}

if (computed) {
result.push(']');
return parenthesize(
[
this.generateExpression(left, Precedence.Call, flags),
space + operator + space,
this.generateExpression(right, Precedence.Assignment, flags)
],
Precedence.Assignment,
precedence
);
}

return result;
};
semicolon (flags) {
if (!semicolons && flags & F_SEMICOLON_OPT) {
return '';
}
return ';';
}

generateExpression (expr, precedence, flags) {
const type = expr.type || Syntax.Property;

if (extra.verbatim && Object.hasOwnProperty.call(expr, extra.verbatim)) {
return generateVerbatim(expr, precedence);
}

let result = this[type](expr, precedence, flags);

CodeGenerator.prototype.generateAssignment = function (left, right, operator, precedence, flags) {
if (Precedence.Assignment < precedence) {
flags |= F_ALLOW_IN;
if (extra.comment) {
result = addComments(expr, result);
}
return toSourceNodeWhenNeeded(result, expr);
}

return parenthesize(
[
this.generateExpression(left, Precedence.Call, flags),
space + operator + space,
this.generateExpression(right, Precedence.Assignment, flags)
],
Precedence.Assignment,
precedence
);
};
generateStatement (stmt, flags) {
let result = this[stmt.type](stmt, flags);

// Attach comments

if (extra.comment) {
result = addComments(stmt, result);
}

CodeGenerator.prototype.semicolon = function (flags) {
if (!semicolons && flags & F_SEMICOLON_OPT) {
return '';
const fragment = toSourceNodeWhenNeeded(result).toString();
if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') {
result = sourceMap ? toSourceNodeWhenNeeded(result).replaceRight(/\s+$/, '') : fragment.replace(/\s+$/, '');
}

return toSourceNodeWhenNeeded(result, stmt);
}
return ';';
};
}

// Statements.

Expand Down Expand Up @@ -2420,38 +2451,6 @@

merge(CodeGenerator.prototype, CodeGenerator.Expression);

CodeGenerator.prototype.generateExpression = function (expr, precedence, flags) {
const type = expr.type || Syntax.Property;

if (extra.verbatim && Object.hasOwnProperty.call(expr, extra.verbatim)) {
return generateVerbatim(expr, precedence);
}

let result = this[type](expr, precedence, flags);

if (extra.comment) {
result = addComments(expr, result);
}
return toSourceNodeWhenNeeded(result, expr);
};

CodeGenerator.prototype.generateStatement = function (stmt, flags) {
let result = this[stmt.type](stmt, flags);

// Attach comments

if (extra.comment) {
result = addComments(stmt, result);
}

const fragment = toSourceNodeWhenNeeded(result).toString();
if (stmt.type === Syntax.Program && !safeConcatenation && newline === '' && fragment.charAt(fragment.length - 1) === '\n') {
result = sourceMap ? toSourceNodeWhenNeeded(result).replaceRight(/\s+$/, '') : fragment.replace(/\s+$/, '');
}

return toSourceNodeWhenNeeded(result, stmt);
};

function generateInternal(node) {
const codegen = new CodeGenerator();
if (isStatement(node)) {
Expand Down

0 comments on commit cf9a7b8

Please sign in to comment.