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 1 commit
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
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
2 changes: 1 addition & 1 deletion packages/babel-generator/src/buffer.ts
Expand Up @@ -480,7 +480,7 @@ export default class Buffer {

if (pos) {
target.line = pos.line + lineOffset;
target.column = pos.column + columnOffset;
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
71 changes: 62 additions & 9 deletions packages/babel-generator/test/index.js
@@ -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
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