Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jul 8, 2022
1 parent 9fd8a4c commit a6a50b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 52 deletions.
18 changes: 9 additions & 9 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -18,7 +18,7 @@ type SourcePos = {
filename: string | undefined;
};

type queueItem = {
type QueueItem = {
char: number;
repeat: number;
line: number | undefined;
Expand Down Expand Up @@ -48,20 +48,20 @@ export default class Buffer {
_str = "";
_appendCount = 0;
_last = 0;
_queue: queueItem[] = [];
_queue: QueueItem[] = [];
_queueCursor = 0;

_position = {
line: 1,
column: 0,
};
_sourcePosition = SourcePos();
_disallowedPop: SourcePos & { ok: boolean } = {
_disallowedPop: SourcePos & { objectReusable: boolean } = {
identifierName: undefined,
line: undefined,
column: undefined,
filename: undefined,
ok: false,
objectReusable: true, // To avoid deleting and re-creating objects, we reuse existing objects when they are not needed anymore.
};

_allocQueue() {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class Buffer {
this._queueCursor++;
}

_popQueue(): queueItem {
_popQueue(): QueueItem {
if (this._queueCursor === 0) {
throw new Error("Cannot pop from empty queue");
}
Expand Down Expand Up @@ -200,7 +200,7 @@ export default class Buffer {
const queueCursor = this._queueCursor;
const queue = this._queue;
for (let i = 0; i < queueCursor; i++) {
const item: queueItem = queue[i];
const item: QueueItem = queue[i];
this._appendChar(item.char, item.repeat, item);
}
this._queueCursor = 0;
Expand Down Expand Up @@ -416,7 +416,7 @@ export default class Buffer {

if (
// Verify if reactivating this specific position has been disallowed.
!this._disallowedPop.ok ||
this._disallowedPop.objectReusable ||
this._disallowedPop.line !== originalLine ||
this._disallowedPop.column !== originalColumn ||
this._disallowedPop.filename !== originalFilename
Expand All @@ -425,7 +425,7 @@ export default class Buffer {
this._sourcePosition.column = originalColumn;
this._sourcePosition.filename = originalFilename;
this._sourcePosition.identifierName = originalIdentifierName;
this._disallowedPop.ok = false;
this._disallowedPop.objectReusable = true;
}
}

Expand All @@ -441,7 +441,7 @@ export default class Buffer {

this._normalizePosition(prop, loc, disallowedPop);

disallowedPop.ok = true;
disallowedPop.objectReusable = false;
}

_normalizePosition(prop: "start" | "end", loc: Loc, targetObj: SourcePos) {
Expand Down
16 changes: 3 additions & 13 deletions packages/babel-generator/src/node/index.ts
Expand Up @@ -9,6 +9,8 @@ import {
} from "@babel/types";
import type * as t from "@babel/types";

import type { WhitespaceFlag } from "./whitespace";

export type NodeHandlers<R> = {
[K in string]?: (
node: K extends t.Node["type"] ? Extract<t.Node, { type: K }> : t.Node,
Expand Down Expand Up @@ -56,7 +58,6 @@ function expandAliases<R>(obj: NodeHandlers<R>) {
// into concrete types so that the 'find' call below can be as fast as possible.
const expandedParens = expandAliases(parens);
const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
//const expandedWhitespaceList = expandAliases(whitespace.list);

function find<R>(
obj: NodeHandlers<R>,
Expand All @@ -79,7 +80,7 @@ function isOrHasCallExpression(node: t.Node): boolean {
export function needsWhitespace(
node: t.Node,
parent: t.Node,
type: number,
type: WhitespaceFlag,
): boolean {
if (!node) return false;

Expand All @@ -93,17 +94,6 @@ export function needsWhitespace(
return (flag & type) !== 0;
}

// A bug that existed before, commenting out it will cause the test to fail.

// const items = find(expandedWhitespaceList, node, parent);
// if (items) {
// for (let i = 0; i < items.length; i++) {
// if (needsWhitespace(items[i], node, type)) {
// return true;
// }
// }
// }

return false;
}

Expand Down
32 changes: 2 additions & 30 deletions packages/babel-generator/src/node/whitespace.ts
Expand Up @@ -25,6 +25,8 @@ const enum WhitespaceFlag {
after = 1 << 1,
}

export type { WhitespaceFlag };

function crawlInternal(
node: t.Node,
state: { hasCall: boolean; hasFunction: boolean; hasHelper: boolean },
Expand Down Expand Up @@ -254,36 +256,6 @@ nodes.ObjectTypeInternalSlot = function (
}
};

/**
* Returns lists from node types that need whitespace.
*/

export const list: NodeHandlers<t.Node[]> = {
/**
* Return VariableDeclaration declarations init properties.
*/

VariableDeclaration(node: t.VariableDeclaration) {
return node.declarations.map(decl => decl.init);
},

/**
* Return VariableDeclaration elements.
*/

ArrayExpression(node: t.ArrayExpression) {
return node.elements;
},

/**
* Return VariableDeclaration properties.
*/

ObjectExpression(node: t.ObjectExpression) {
return node.properties;
},
};

/**
* Add whitespace tests for nodes and their aliases.
*/
Expand Down

0 comments on commit a6a50b3

Please sign in to comment.