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 overwriting existing sourcesContent in sourcemaps #4567

Merged
merged 3 commits into from
Jan 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ function SourceMap(options) {
add(source, gen_line, gen_col, orig_line, orig_col, name);
} : add,
setSourceContent: sources_content ? function(source, content) {
sources_content[source] = content;
if (!sources_content[source]) {
sources_content[source] = content;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Admittedly an odd case, but this will still overwrite an existing blank source file − please consider the following instead:

if (!(source in sources_content)) sources_content[source] = content;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! For running on very old Node/browsers, would if (!HOP(sources_content, source)) be better? Or doesn't matter anymore and prefer readability at this point?

Copy link
Collaborator

Choose a reason for hiding this comment

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

sources_content is initiated to Object.create(null), so in theory you won't get any inherited properties, i.e. in this particular case source in sources_content is equivalent to HOP(sources_content, source) on all platforms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I missed the Object.create(null), thought this was a caller-provided map, you're right! I've changed the check to source in sources_content.

} : noop,
toString: function() {
return JSON.stringify({
Expand Down