fix(es/transforms/cjs): Reassign exports for the functions #2569
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR attempts to close #2549. As referenced issue(swc-project/swc-node#610) states it was not limited to
Object.defineProperty
, instead all of the function declarations are not assigned into exports. This makes reassigning outside of module scope does not work.Per suggestion (#2549 (comment)) , I started looking emissions but actually it was realated to scope. I noticed this by checking test cases where exporting object actually creates exports assignment correctly:
It is due to scope includes var declaration (https://github.com/swc-project/swc/blob/master/ecmascript/transforms/module/src/common_js.rs#L210) but doesn't do it for the functions. PR attempt to do the same thing by putting fn declaration into scope as well.
One thing I'm a bit hesitant is this change uses
exported_vars
. While this doesn't seem to create any regressions and looks like fixes issue correctly, it may not be a good idea to mix usage of exiting property for the variable declarations.