Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve source map generation #14967

Merged
merged 11 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

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

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

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

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

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

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

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

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

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

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

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

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

36 changes: 17 additions & 19 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -373,7 +373,7 @@ export default class Buffer {

// Since this is called extremely often, we re-use the same _sourcePosition
// object for the whole lifetime of the buffer.
this._normalizePosition(prop, loc, this._sourcePosition);
this._normalizePosition(prop, loc, this._sourcePosition, 0, 0);
}

sourceWithOffset(
Expand All @@ -384,21 +384,13 @@ export default class Buffer {
): void {
if (!loc) return;

const pos = loc[prop];

const sourcePosition = this._sourcePosition;

sourcePosition.identifierName =
(prop === "start" && loc.identifierName) || undefined;
if (pos) {
sourcePosition.line = pos.line + lineOffset;
sourcePosition.column = pos.column + columnOffset;
sourcePosition.filename = loc.filename;
} else {
sourcePosition.line = null;
sourcePosition.column = null;
sourcePosition.filename = null;
}
this._normalizePosition(
prop,
loc,
this._sourcePosition,
lineOffset,
columnOffset,
);
}

/**
Expand All @@ -413,14 +405,20 @@ export default class Buffer {
cb();
}

_normalizePosition(prop: "start" | "end", loc: Loc, targetObj: SourcePos) {
_normalizePosition(
prop: "start" | "end",
loc: Loc,
targetObj: SourcePos,
lineOffset: number,
columnOffset: number,
) {
const pos = loc[prop];

targetObj.identifierName =
(prop === "start" && loc.identifierName) || undefined;
if (pos) {
targetObj.line = pos.line;
targetObj.column = pos.column;
targetObj.line = pos.line + lineOffset;
targetObj.column = pos.column + columnOffset;
targetObj.filename = loc.filename;
} else {
targetObj.line = null;
Expand Down
10 changes: 3 additions & 7 deletions packages/babel-generator/src/generators/base.ts
Expand Up @@ -36,15 +36,11 @@ export function BlockStatement(this: Printer, node: t.BlockStatement) {
this.printSequence(node.body, node, { indent: true });
this.removeTrailingNewline();

this.sourceWithOffset("end", node.loc, 0, -1);

if (!this.endsWith(charCodes.lineFeed)) this.newline();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case for this? This should work as an input.


this.rightBrace();
} else {
this.sourceWithOffset("end", node.loc, 0, -1);
this.token("}");
}

this.sourceWithOffset("end", node.loc, 0, -1);
this.rightBrace();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check if we need the same fix for ClassBody, StaticBlock and ModuleExpression?

}

export function Directive(this: Printer, node: t.Directive) {
Expand Down