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

fix: Avoid internally generating negative source maps columns #15719

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/babel-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"devDependencies": {
"@babel/helper-fixtures": "workspace:^",
"@babel/parser": "workspace:^",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@types/jsesc": "^2.5.0",
"charcodes": "^0.2.0"
},
Expand Down
16 changes: 5 additions & 11 deletions packages/babel-generator/src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,17 @@ 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, 0, 0);
this._normalizePosition(prop, loc, 0);
}

sourceWithOffset(
prop: "start" | "end",
loc: Loc | undefined,
lineOffset: number,
columnOffset: number,
): void {
if (!this._map) return;

this._normalizePosition(prop, loc, lineOffset, columnOffset);
this._normalizePosition(prop, loc, columnOffset);
}

/**
Expand All @@ -469,18 +468,13 @@ export default class Buffer {
cb();
}

_normalizePosition(
prop: "start" | "end",
loc: Loc,
lineOffset: number,
columnOffset: number,
) {
_normalizePosition(prop: "start" | "end", loc: Loc, columnOffset: number) {
const pos = loc[prop];
const target = this._sourcePosition;

if (pos) {
target.line = pos.line + lineOffset;
target.column = pos.column + columnOffset;
target.line = pos.line;
target.column = Math.max(pos.column + columnOffset, 0);
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 add a note here to #15712? So that we can investigate this issue in the future.

target.filename = loc.filename;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/src/generators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function ObjectExpression(this: Printer, node: t.ObjectExpression) {
this.space();
}

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

this.token("}");
}
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ class Printer {
if (this.format.minified) {
this._buf.removeLastSemicolon();
}
this.sourceWithOffset("end", node.loc, 0, -1);
this.sourceWithOffset("end", node.loc, -1);
this.token("}");
}

rightParens(node: t.Node): void {
this.sourceWithOffset("end", node.loc, 0, -1);
this.sourceWithOffset("end", node.loc, -1);
this.token(")");
}

Expand Down Expand Up @@ -365,14 +365,13 @@ class Printer {
sourceWithOffset(
prop: "start" | "end",
loc: Loc | undefined,
lineOffset: number,
columnOffset: number,
): void {
if (!loc) return;

this._catchUp(prop, loc);

this._buf.sourceWithOffset(prop, loc, lineOffset, columnOffset);
this._buf.sourceWithOffset(prop, loc, columnOffset);
}

withSource(
Expand Down
71 changes: 62 additions & 9 deletions packages/babel-generator/test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { parse } from "@babel/parser";
import { parse, parseExpression } from "@babel/parser";
import * as t from "@babel/types";
import fs from "fs";
import path from "path";
import fixtures from "@babel/helper-fixtures";
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
import { fileURLToPath } from "url";
import { commonJS } from "$repo-utils";
import { encode } from "@jridgewell/sourcemap-codec";

import _generate, { CodeGenerator } from "../lib/index.js";
const generate = _generate.default || _generate;

const { __dirname } = commonJS(import.meta.url);

describe("generation", function () {
it("multiple sources", function () {
const sources = {
Expand Down Expand Up @@ -914,6 +917,61 @@ describe("generation", function () {
}
`);
});

it("should not throw when loc.column === 0 with inputSourceMap", () => {
const ast = parseExpression("a(\n)");

ast.loc.end.column = 0;
Copy link
Contributor

@JLHwung JLHwung Jun 23, 2023

Choose a reason for hiding this comment

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

Can we construct a test without AST location mutations? If not it may point to a downstream issue and we can provide a better error message.

The current _normalizePosition implementation will also throw if any external tool sets ast.loc.end.line to -1. If we decide to mute such errors, we should also handle the .line info.

Copy link
Member Author

Choose a reason for hiding this comment

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

This happened in a complex Angular project, and I thought it was a downstream bug, but since it's a regression, I took this approach.

The current _normalizePosition implementation will also throw if any external tool sets ast.loc.end.line to -1. If we decide to mute such errors, we should also handle the .line info.

You are right, but the reason for this regression is that we internally added columnOffset resulting in a possible -1, and lineOffset is not currently actually used, I'm not sure about doing the same.
Maybe we can remove lineOffset?

Choose a reason for hiding this comment

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

Anything new here ? It would be awesome to have this fix :)


expect(
generate(ast, {
sourceMaps: true,
inputSourceMap: {
version: 3,
names: [],
sources: ["input.js"],
// [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ]
mappings: encode([[0, 0, 1, 0]]),
},
}).rawMappings,
).toMatchInlineSnapshot(`
Array [
Object {
"generated": Object {
"column": 0,
"line": 1,
},
"name": "a",
"original": Object {
"column": 0,
"line": 1,
},
"source": "input.js",
},
Object {
"generated": Object {
"column": 1,
"line": 1,
},
"name": undefined,
"original": Object {
"column": 0,
"line": 1,
},
"source": "input.js",
},
Object {
"generated": Object {
"column": 2,
"line": 1,
},
"name": undefined,
"original": undefined,
"source": undefined,
},
]
`);
});
});

describe("programmatic generation", function () {
Expand Down Expand Up @@ -1435,9 +1493,7 @@ describe("CodeGenerator", function () {
});
});

const suites = (fixtures.default || fixtures)(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
);
const suites = (fixtures.default || fixtures)(path.join(__dirname, "fixtures"));

afterEach(() => {
jest.restoreAllMocks();
Expand Down Expand Up @@ -1467,10 +1523,7 @@ suites.forEach(function (testSuite) {
};
const actualAst = parse(actualCode, parserOpts);
const options = {
sourceFileName: path.relative(
path.dirname(fileURLToPath(import.meta.url)),
actual.loc,
),
sourceFileName: path.relative(__dirname, actual.loc),
...task.options,
sourceMaps: task.sourceMap ? true : task.options.sourceMaps,
};
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ __metadata:
"@babel/parser": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/gen-mapping": ^0.3.2
"@jridgewell/sourcemap-codec": ^1.4.15
"@jridgewell/trace-mapping": ^0.3.17
"@types/jsesc": ^2.5.0
charcodes: ^0.2.0
Expand Down Expand Up @@ -4377,7 +4378,7 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/sourcemap-codec@npm:^1.4.10, @jridgewell/sourcemap-codec@npm:^1.4.13":
"@jridgewell/sourcemap-codec@npm:^1.4.10, @jridgewell/sourcemap-codec@npm:^1.4.13, @jridgewell/sourcemap-codec@npm:^1.4.15":
version: 1.4.15
resolution: "@jridgewell/sourcemap-codec@npm:1.4.15"
checksum: b881c7e503db3fc7f3c1f35a1dd2655a188cc51a3612d76efc8a6eb74728bef5606e6758ee77423e564092b4a518aba569bbb21c9bac5ab7a35b0c6ae7e344c8
Expand Down