Code refactoring for the babel-generator (#5344)
* refactoring code for babel-generator package * removing spaces and refactoring if statement * fixing warnings
This commit is contained in:
parent
39eca84642
commit
6888a2c51b
@ -10,9 +10,7 @@ import type { Format } from "./printer";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Generator extends Printer {
|
class Generator extends Printer {
|
||||||
constructor(ast, opts, code) {
|
constructor(ast, opts = {}, code) {
|
||||||
opts = opts || {};
|
|
||||||
|
|
||||||
const tokens = ast.tokens || [];
|
const tokens = ast.tokens || [];
|
||||||
const format = normalizeOptions(code, opts, tokens);
|
const format = normalizeOptions(code, opts, tokens);
|
||||||
const map = opts.sourceMaps ? new SourceMap(opts, code) : null;
|
const map = opts.sourceMaps ? new SourceMap(opts, code) : null;
|
||||||
|
|||||||
@ -34,12 +34,8 @@ export function NullableTypeAnnotation(node: Object, parent: Object): boolean {
|
|||||||
export { NullableTypeAnnotation as FunctionTypeAnnotation };
|
export { NullableTypeAnnotation as FunctionTypeAnnotation };
|
||||||
|
|
||||||
export function UpdateExpression(node: Object, parent: Object): boolean {
|
export function UpdateExpression(node: Object, parent: Object): boolean {
|
||||||
if (t.isMemberExpression(parent) && parent.object === node) {
|
|
||||||
// (foo++).test()
|
// (foo++).test()
|
||||||
return true;
|
return t.isMemberExpression(parent) && parent.object === node;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ObjectExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
|
export function ObjectExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
|
||||||
@ -51,15 +47,11 @@ export function DoExpression(node: Object, parent: Object, printStack: Array<Obj
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Binary(node: Object, parent: Object): boolean {
|
export function Binary(node: Object, parent: Object): boolean {
|
||||||
if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) {
|
if (
|
||||||
return true;
|
((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) ||
|
||||||
}
|
t.isUnaryLike(parent) ||
|
||||||
|
(t.isMemberExpression(parent) && parent.object === node)
|
||||||
if (t.isUnaryLike(parent)) {
|
) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isMemberExpression(parent) && parent.object === node) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,12 +62,11 @@ export function Binary(node: Object, parent: Object): boolean {
|
|||||||
const nodeOp = node.operator;
|
const nodeOp = node.operator;
|
||||||
const nodePos = PRECEDENCE[nodeOp];
|
const nodePos = PRECEDENCE[nodeOp];
|
||||||
|
|
||||||
if (parentPos > nodePos) {
|
if (
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logical expressions with the same precedence don't need parens.
|
// Logical expressions with the same precedence don't need parens.
|
||||||
if (parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent)) {
|
(parentPos === nodePos && parent.right === node && !t.isLogicalExpression(parent)) ||
|
||||||
|
parentPos > nodePos
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,55 +75,27 @@ export function Binary(node: Object, parent: Object): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function BinaryExpression(node: Object, parent: Object): boolean {
|
export function BinaryExpression(node: Object, parent: Object): boolean {
|
||||||
if (node.operator === "in") {
|
|
||||||
// let i = (1 in []);
|
// let i = (1 in []);
|
||||||
if (t.isVariableDeclarator(parent)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// for ((1 in []);;);
|
// for ((1 in []);;);
|
||||||
if (t.isFor(parent)) {
|
return node.operator === "in" && (t.isVariableDeclarator(parent) || t.isFor(parent));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SequenceExpression(node: Object, parent: Object): boolean {
|
export function SequenceExpression(node: Object, parent: Object): boolean {
|
||||||
if (t.isForStatement(parent)) {
|
|
||||||
|
if (
|
||||||
// Although parentheses wouldn"t hurt around sequence
|
// Although parentheses wouldn"t hurt around sequence
|
||||||
// expressions in the head of for loops, traditional style
|
// expressions in the head of for loops, traditional style
|
||||||
// dictates that e.g. i++, j++ should not be wrapped with
|
// dictates that e.g. i++, j++ should not be wrapped with
|
||||||
// parentheses.
|
// parentheses.
|
||||||
return false;
|
t.isForStatement(parent) ||
|
||||||
}
|
t.isThrowStatement(parent) ||
|
||||||
|
t.isReturnStatement(parent) ||
|
||||||
if (t.isExpressionStatement(parent) && parent.expression === node) {
|
(t.isIfStatement(parent) && parent.test === node) ||
|
||||||
return false;
|
(t.isWhileStatement(parent) && parent.test === node) ||
|
||||||
}
|
(t.isForInStatement(parent) && parent.right === node) ||
|
||||||
|
(t.isSwitchStatement(parent) && parent.discriminant === node) ||
|
||||||
if (t.isReturnStatement(parent)) {
|
(t.isExpressionStatement(parent) && parent.expression === node)
|
||||||
return false;
|
) {
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isThrowStatement(parent)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isSwitchStatement(parent) && parent.discriminant === node) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isWhileStatement(parent) && parent.test === node) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isIfStatement(parent) && parent.test === node) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isForInStatement(parent) && parent.right === node) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,15 +121,9 @@ export function ClassExpression(node: Object, parent: Object, printStack: Array<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function UnaryLike(node: Object, parent: Object): boolean {
|
export function UnaryLike(node: Object, parent: Object): boolean {
|
||||||
if (t.isMemberExpression(parent, { object: node })) {
|
return t.isMemberExpression(parent, { object: node }) ||
|
||||||
return true;
|
t.isCallExpression(parent, { callee: node }) ||
|
||||||
}
|
t.isNewExpression(parent, { callee: node });
|
||||||
|
|
||||||
if (t.isCallExpression(parent, { callee: node }) || t.isNewExpression(parent, { callee: node })) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function FunctionExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
|
export function FunctionExpression(node: Object, parent: Object, printStack: Array<Object>): boolean {
|
||||||
@ -189,19 +146,12 @@ export function ArrowFunctionExpression(node: Object, parent: Object): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ConditionalExpression(node: Object, parent: Object): boolean {
|
export function ConditionalExpression(node: Object, parent: Object): boolean {
|
||||||
if (t.isUnaryLike(parent)) {
|
if (
|
||||||
return true;
|
t.isUnaryLike(parent) ||
|
||||||
}
|
t.isBinary(parent) ||
|
||||||
|
t.isConditionalExpression(parent, { test: node }) ||
|
||||||
if (t.isBinary(parent)) {
|
t.isAwaitExpression(parent)
|
||||||
return true;
|
) {
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isConditionalExpression(parent, { test: node })) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (t.isAwaitExpression(parent)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,28 +177,23 @@ function isFirstInStatement(printStack: Array<Object>, {
|
|||||||
i--;
|
i--;
|
||||||
let parent = printStack[i];
|
let parent = printStack[i];
|
||||||
while (i > 0) {
|
while (i > 0) {
|
||||||
if (t.isExpressionStatement(parent, { expression: node })) {
|
if (
|
||||||
|
t.isExpressionStatement(parent, { expression: node }) ||
|
||||||
|
t.isTaggedTemplateExpression(parent) ||
|
||||||
|
considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node }) ||
|
||||||
|
considerArrow && t.isArrowFunctionExpression(parent, { body: node })
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t.isTaggedTemplateExpression(parent)) {
|
if (
|
||||||
return true;
|
t.isCallExpression(parent, { callee: node }) ||
|
||||||
}
|
|
||||||
|
|
||||||
if (considerDefaultExports && t.isExportDefaultDeclaration(parent, { declaration: node })) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (considerArrow && t.isArrowFunctionExpression(parent, { body: node })) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((t.isCallExpression(parent, { callee: node })) ||
|
|
||||||
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
|
(t.isSequenceExpression(parent) && parent.expressions[0] === node) ||
|
||||||
(t.isMemberExpression(parent, { object: node })) ||
|
t.isMemberExpression(parent, { object: node }) ||
|
||||||
(t.isConditional(parent, { test: node })) ||
|
t.isConditional(parent, { test: node }) ||
|
||||||
(t.isBinary(parent, { left: node })) ||
|
t.isBinary(parent, { left: node }) ||
|
||||||
(t.isAssignmentExpression(parent, { left: node }))) {
|
t.isAssignmentExpression(parent, { left: node })
|
||||||
|
) {
|
||||||
node = parent;
|
node = parent;
|
||||||
i--;
|
i--;
|
||||||
parent = printStack[i];
|
parent = printStack[i];
|
||||||
|
|||||||
@ -332,9 +332,11 @@ export default class Printer {
|
|||||||
this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
||||||
|
|
||||||
let needsParens = n.needsParens(node, parent, this._printStack);
|
let needsParens = n.needsParens(node, parent, this._printStack);
|
||||||
if (this.format.retainFunctionParens &&
|
if (
|
||||||
|
this.format.retainFunctionParens &&
|
||||||
node.type === "FunctionExpression" &&
|
node.type === "FunctionExpression" &&
|
||||||
node.extra && node.extra.parenthesized) {
|
node.extra && node.extra.parenthesized
|
||||||
|
) {
|
||||||
needsParens = true;
|
needsParens = true;
|
||||||
}
|
}
|
||||||
if (needsParens) this.token("(");
|
if (needsParens) this.token("(");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user