Skip to content

fix(es/transforms/cjs): Reassign exports for the functions #2569

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

Merged
merged 4 commits into from
Oct 29, 2021

Conversation

kwonoj
Copy link
Member

@kwonoj kwonoj commented Oct 28, 2021

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:

// input 
export const someObj = { ... }

Object.defineProperty(...);

// output
Object.defineProperty(test, "errors", {
    get: ()=> someObj 
    ,
    set: (v)=>{
        exports.someObj  = someObj  = v; //current master correctly emits this if someObj is not a function
    }
});

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.

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

I checked it, and I think it would be better to name it exported_bindings instead of exported_vars. Can you rename it? (I checked that it has scoped visibility, so it's fine)

@kwonoj kwonoj changed the title test(es/cjs): add test cases fix(es/cjs): reassign exports for the functions Oct 28, 2021
@kwonoj kwonoj force-pushed the fix-export-scope-var-assign branch from 87be3be to 45a85d1 Compare October 28, 2021 15:15
@kwonoj
Copy link
Member Author

kwonoj commented Oct 28, 2021

name it exported_bindings instead of exported_vars. Can you rename

Sounds good to me, applied changes.

@kwonoj kwonoj force-pushed the fix-export-scope-var-assign branch from 45a85d1 to f41a3e1 Compare October 28, 2021 15:34
@kwonoj
Copy link
Member Author

kwonoj commented Oct 28, 2021

Not sure about Benchmark / Performance regression check failure - peeking log, this looks like not directly related with PR's change?

@kdy1 kdy1 changed the title fix(es/cjs): reassign exports for the functions fix(es/common_js): Reassign exports for the functions Oct 29, 2021
@kdy1 kdy1 changed the title fix(es/common_js): Reassign exports for the functions fix(es/transforms/common_js): Reassign exports for the functions Oct 29, 2021
@kdy1 kdy1 changed the title fix(es/transforms/common_js): Reassign exports for the functions fix(es/transforms/cjs): Reassign exports for the functions Oct 29, 2021
@kdy1 kdy1 merged commit 7cc51be into swc-project:master Oct 29, 2021
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thanks!

@kdy1 kdy1 added this to the v1.2.106 milestone Oct 29, 2021
@kwonoj kwonoj deleted the fix-export-scope-var-assign branch November 6, 2021 07:40
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Object.defineProperty does not allow reassign ref to exported fn
2 participants