Skip to content

Commit

Permalink
fix: Avoid internally generating negative source maps columns (#15719)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jul 5, 2023
1 parent abb5a7c commit 4986833
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 26 deletions.
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
17 changes: 6 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,14 @@ 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;
// TODO: Fix https://github.com/babel/babel/issues/15712 in downstream
target.column = Math.max(pos.column + columnOffset, 0);
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;

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 @@ -498,6 +498,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 @@ -4416,7 +4417,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

0 comments on commit 4986833

Please sign in to comment.