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: Handling source maps without sourcesContent #15445

Merged
merged 2 commits into from Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/babel-generator/src/source-map.ts
Expand Up @@ -58,7 +58,7 @@ export default class SourceMap {
setSourceContent(
map,
resolvedSources[i],
this._inputMap.sourcesContent[i],
this._inputMap.sourcesContent?.[i],
Copy link
Member

Choose a reason for hiding this comment

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

Nit: We can skip this full loop if there's no sourcesContent.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this affect sources? At first I skipped the whole loop, but then I'm not sure if sources will be preserved.

Copy link
Member

Choose a reason for hiding this comment

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

Instead of eagerly adding all sources, it'll only adds source as they are marked. It's not important to keep a source that was input if that source is never seen in the map, but setting a sourcesContent forces it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

);
}
}
Expand Down
34 changes: 34 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -880,6 +880,40 @@ describe("generation", function () {
});"
`);
});

it("inputSourceMap without sourcesContent", () => {
const ast = parse("var t = x => x * x;");

expect(
generate(ast, {
sourceMaps: true,
inputSourceMap: {
version: 3,
names: ["t", "x"],
sources: ["source-maps/arrow-function/input.js"],
mappings:
"AAAA,IAAIA,CAAC,GAAG,SAAJA,CAACA,CAAGC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAC;AAAA",
},
}).map,
).toMatchInlineSnapshot(`
Object {
"file": undefined,
"mappings": "AAAA,IAAIA,CAAC,GAAGC,CAAA,IAAAA,CAAA,GAAJA,CAAC",
"names": Array [
"t",
"x",
],
"sourceRoot": undefined,
"sources": Array [
"source-maps/arrow-function/input.js",
],
"sourcesContent": Array [
undefined,
],
"version": 3,
}
`);
});
});

describe("programmatic generation", function () {
Expand Down