Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Oct 15, 2022
1 parent 802a9b3 commit c43f06e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 40 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 46 additions & 8 deletions packages/babel-generator/src/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type SourceMap from "./source-map";
import * as charcodes from "charcodes";
import { originalPositionFor } from "@jridgewell/trace-mapping";

export type Pos = {
line: number;
Expand All @@ -16,13 +17,15 @@ type SourcePos = {
identifierName: string | undefined;
filename: string | undefined;
};
type InternalSourcePos = SourcePos & { identifierNamePos: Pos };

type QueueItem = {
char: number;
repeat: number;
line: number | undefined;
column: number | undefined;
identifierName: undefined; // Not used, it always undefined.
identifierNamePos: undefined; // Not used, it always undefined.
filename: string | undefined;
};

Expand All @@ -45,8 +48,9 @@ export default class Buffer {
line: 1,
column: 0,
};
_sourcePosition: SourcePos = {
_sourcePosition: InternalSourcePos = {
identifierName: undefined,
identifierNamePos: undefined,
line: undefined,
column: undefined,
filename: undefined,
Expand All @@ -62,6 +66,7 @@ export default class Buffer {
line: undefined,
column: undefined,
identifierName: undefined,
identifierNamePos: undefined,
filename: "",
});
}
Expand Down Expand Up @@ -194,7 +199,11 @@ export default class Buffer {
this._queueCursor = 0;
}

_appendChar(char: number, repeat: number, sourcePos: SourcePos): void {
_appendChar(
char: number,
repeat: number,
sourcePos: InternalSourcePos,
): void {
this._last = char;

this._str +=
Expand All @@ -207,6 +216,7 @@ export default class Buffer {
sourcePos.line,
sourcePos.column,
sourcePos.identifierName,
sourcePos.identifierNamePos,
sourcePos.filename,
);
this._position.column += repeat;
Expand All @@ -216,9 +226,14 @@ export default class Buffer {
}

sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
}

_append(str: string, sourcePos: SourcePos, maybeNewline: boolean): void {
_append(
str: string,
sourcePos: InternalSourcePos,
maybeNewline: boolean,
): void {
const len = str.length;
const position = this._position;

Expand All @@ -238,10 +253,25 @@ export default class Buffer {
return;
}

const { column, identifierName, filename } = sourcePos;
let { column, identifierName, identifierNamePos, filename } = sourcePos;
let line = sourcePos.line;

if (identifierName) sourcePos.identifierName = undefined;
if (identifierName != null || identifierNamePos != null) {
sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
}

// Fast path for multi-line
if (maybeNewline && identifierNamePos && this._map?._inputMap) {
const name = originalPositionFor(
this._map._inputMap,
identifierNamePos,
).name;
if (name) {
identifierName = name;
}
identifierNamePos = undefined;
}

// Search for newline chars. We search only for `\n`, since both `\r` and
// `\r\n` are normalized to `\n` during parse. We exclude `\u2028` and
Expand All @@ -253,7 +283,7 @@ export default class Buffer {
// If the string starts with a newline char, then adding a mark is redundant.
// This catches both "no newlines" and "newline after several chars".
if (i !== 0) {
this._mark(line, column, identifierName, filename);
this._mark(line, column, identifierName, identifierNamePos, filename);
}

// Now, find each reamining newline char in the string.
Expand All @@ -265,7 +295,7 @@ export default class Buffer {
// We mark the start of each line, which happens directly after this newline char
// unless this is the last char.
if (last < len) {
this._mark(++line, 0, identifierName, filename);
this._mark(++line, 0, identifierName, identifierNamePos, filename);
}
i = str.indexOf("\n", last);
}
Expand All @@ -276,9 +306,17 @@ export default class Buffer {
line: number | undefined,
column: number | undefined,
identifierName: string | undefined,
identifierNamePos: Pos | undefined,
filename: string | undefined,
): void {
this._map?.mark(this._position, line, column, identifierName, filename);
this._map?.mark(
this._position,
line,
column,
identifierName,
identifierNamePos,
filename,
);
}

removeTrailingNewline(): void {
Expand Down
35 changes: 21 additions & 14 deletions packages/babel-generator/src/generators/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export function _params(
) {
this.print(node.typeParameters, node);

const name = _getFuncIdName.call(this, idNode, parentNode);
if (name) {
this.sourceIdentifierName(name);
const nameInfo = _getFuncIdName.call(this, idNode, parentNode);
if (nameInfo) {
this.sourceIdentifierName(nameInfo.name, nameInfo.pos);
}

this.token("(");
Expand Down Expand Up @@ -223,8 +223,8 @@ function hasTypesOrComments(
);
}

function _getFuncIdName(this: Printer, nodeId: t.Node, parent: t.Node) {
let id = nodeId;
function _getFuncIdName(this: Printer, idNode: t.Node, parent: t.Node) {
let id = idNode;

if (!id && parent) {
const parentType = parent.type;
Expand All @@ -241,20 +241,27 @@ function _getFuncIdName(this: Printer, nodeId: t.Node, parent: t.Node) {
}
}

let name;
let nameInfo;
if (id) {
if (id.type === "Identifier") {
name =
this.getIdentifierName(id.loc?.start) ||
// @ts-expect-error Undocumented property identifierName
id.loc?.identifierName ||
id.name;
nameInfo = {
pos: id.loc?.start,
name:
// @ts-expect-error Undocumented property identifierName
id.loc?.identifierName || id.name,
};
} else if (id.type === "PrivateName") {
name = this.getIdentifierName(id.id.loc?.start) || id.id.name;
nameInfo = {
pos: id.id.loc?.start,
name: id.id.name,
};
} else if (id.type === "StringLiteral") {
name = this.getIdentifierName(id.loc?.start) || id.value;
nameInfo = {
pos: id.loc?.start,
name: id.value,
};
}
}

return name;
return nameInfo;
}
12 changes: 4 additions & 8 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,10 @@ class Printer {
this._buf.withSource(prop, loc, cb);
}

sourceIdentifierName(identifierName: string): void {
this._buf._sourcePosition.identifierName = identifierName;
}

getIdentifierName(pos: Pos): string | null {
if (this._inputMap && pos) {
return originalPositionFor(this._inputMap, pos).name;
}
sourceIdentifierName(identifierName: string, pos?: Pos): void {
const sourcePosition = this._buf._sourcePosition;
sourcePosition.identifierNamePos = pos;
sourcePosition.identifierName = identifierName;
}

_space(): void {
Expand Down
24 changes: 15 additions & 9 deletions packages/babel-generator/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class SourceMap {
}
}

if (typeof code === "string") {
if (typeof code === "string" && !opts.inputSourceMap) {
setSourceContent(map, this._sourceFileName, code);
} else if (typeof code === "object") {
for (const sourceFileName of Object.keys(code)) {
Expand Down Expand Up @@ -102,6 +102,7 @@ export default class SourceMap {
line: number,
column: number,
identifierName?: string | null,
identifierNamePos?: { line: number; column: number },
filename?: string | null,
) {
this._rawMappings = undefined;
Expand All @@ -111,17 +112,24 @@ export default class SourceMap {

if (line != null) {
if (this._inputMap) {
if (identifierNamePos) {
const name = originalPositionFor(this._inputMap, {
line,
column,
}).name;
if (name) {
identifierName = name;
}
}

const originalMapping = originalPositionFor(this._inputMap, {
line,
column,
});

source =
originalMapping.source ||
filename?.replace(/\\/g, "/") ||
this._sourceFileName;
source = originalMapping.source;

if (originalMapping.line != null) {
if (source && originalMapping.line != null) {
original = {
line: originalMapping.line,
column: originalMapping.column,
Expand All @@ -132,8 +140,6 @@ export default class SourceMap {
}
} else {
source = filename?.replace(/\\/g, "/") || this._sourceFileName;
}
if (!original) {
original = {
line: line,
column: column,
Expand All @@ -144,7 +150,7 @@ export default class SourceMap {
maybeAddMapping(this._map, {
name: identifierName,
generated,
source: line == null ? undefined : source,
source: original == null ? undefined : source,
original: original,
});
}
Expand Down

0 comments on commit c43f06e

Please sign in to comment.