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

Preserve name indices for duplicate names #72

Merged
merged 3 commits into from
Aug 22, 2013
Merged

Conversation

evanw
Copy link
Contributor

@evanw evanw commented Jul 30, 2013

ArraySet assumes the "names" array contains unique names, but this is not in the spec and the TypeScript compiler sometimes generates duplicate names. In this case, SourceMapConsumer crashes when it tries to access a name index out of bounds. This change allows duplicates in ArraySet and just returns the index of the first duplicate from indexOf(), which prevents the crash and allows the source maps to be loaded.

Without this fix, ArraySet objects only added the first copy of a name
if the "names" array contained duplicates. This messed up indices into
the names array because the array was shorter than it should be. The
TypeScript compiler sometimes generates source maps with duplicate
names, causing a crash in SourceMapConsumer.
@sokra
Copy link
Contributor

sokra commented Jul 30, 2013

I think it's better to fix this behavior in the TypeScript compiler and change the spec in a way not allowing duplicate entries in names (and sources). This arrays should compress (deduplicate) references to names and sources in the mappings.

@fitzgen
Copy link
Contributor

fitzgen commented Jul 30, 2013

I agree we should change the spec (see my email over there), however we should probably fix this now in a way that we can consume source maps that contain duplicates, but generate source maps that don't.

Haven't looked at the code yet, will get to it soon.

@fitzgen
Copy link
Contributor

fitzgen commented Jul 31, 2013

@evanw we can't modify add to always push the name because we will get way too many duplicates. If a variable referenced 1000 times in a file being minified, and there is a mapping for each time it is referenced, we've now added 1000 items to the names array instead of one. But this is true for every variable that is re-used, not just this particular one. This is going to propagate through to the source maps that we generate, inflating their size. We can't do that.

I think we could add a boolean parameter to ArraySet.fromArray and ArraySet.prototype.add which allows duplicates. Then, SourceMapConsumer would use this new parameter while SourceMapGenerator would remain the same as it is now.

If anyone can think of a better solution, I'm all ears.

For a patch with this functionality to land, I would also like to see tests that:

  • we don't generate source maps with duplicates in the names array,
  • and we don't barf when consuming source maps that have duplicates in the names array

@fitzgen fitzgen merged commit fc0104f into mozilla:master Aug 22, 2013
@fitzgen
Copy link
Contributor

fitzgen commented Aug 22, 2013

@evanw thank you for the pull request! Sorry I missed that you updated it based on my feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants