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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = { | ||
|
@@ -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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
You are right, but the reason for this regression is that we internally added There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 () { | ||
|
@@ -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(); | ||
|
@@ -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, | ||
}; | ||
|
There was a problem hiding this comment.
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.