Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@TrickyPi
Copy link
Member

@TrickyPi TrickyPi commented Feb 6, 2023

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
resolves #4764

Description

This PR only considers the assignment to the properties of this in the class constructor, and does not work for the following case.

class Main {
	constructor() {
		this.setA();
	}
        setA(){
                this.a = 1;
        }
}

@netlify
Copy link

netlify bot commented Feb 6, 2023

Deploy Preview for rollupjs ready!

Name Link
🔨 Latest commit 6ef4859
🔍 Latest deploy log https://app.netlify.com/sites/rollupjs/deploys/63e4b766270676000803ae14
😎 Deploy Preview https://deploy-preview-4842--rollupjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@TrickyPi TrickyPi force-pushed the fix/this-in-constructor branch 2 times, most recently from fac234e to 5bbd347 Compare February 6, 2023 13:58
@codecov
Copy link

codecov bot commented Feb 6, 2023

Codecov Report

Merging #4842 (6ef4859) into master (e0d9681) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4842   +/-   ##
=======================================
  Coverage   98.99%   98.99%           
=======================================
  Files         219      219           
  Lines        7883     7887    +4     
  Branches     2184     2184           
=======================================
+ Hits         7804     7808    +4     
  Misses         26       26           
  Partials       53       53           
Impacted Files Coverage Δ
src/ast/ExecutionContext.ts 100.00% <ø> (ø)
src/ast/nodes/ArrowFunctionExpression.ts 94.73% <ø> (ø)
src/ast/nodes/shared/FunctionNode.ts 97.05% <100.00%> (+0.18%) ⬆️
src/ast/variables/ThisVariable.ts 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@TrickyPi TrickyPi force-pushed the fix/this-in-constructor branch from 514b757 to 4333e4e Compare February 6, 2023 15:10
@github-actions
Copy link

github-actions bot commented Feb 8, 2023

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install TrickyPi/rollup#fix/this-in-constructor

or load it into the REPL:
https://deploy-preview-4842--rollupjs.netlify.app/repl/?pr=4842

@lukastaegert
Copy link
Member

I think this is a good idea, but there are some edge cases this will not handle correctly. E.g. when the constructor first defines a setter:

https://deploy-preview-4842--rollupjs.netlify.app/repl/?pr=4842&shareable=JTdCJTIyZXhhbXBsZSUyMiUzQW51bGwlMkMlMjJtb2R1bGVzJTIyJTNBJTVCJTdCJTIyY29kZSUyMiUzQSUyMmNsYXNzJTIwRm9vJTIwJTdCJTVDbiUyMCUyMCUyMCUyMGNvbnN0cnVjdG9yKCklMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMlMkMlMjAnb3B0aW9uMSclMkMlMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2V0JTIwKHZhbHVlKSUyMCU3QiU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjb25zb2xlLmxvZygnZWZmZWN0JyklM0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdEJTVDbiUyMCUyMCUyMCUyMCUyMCUyMCU3RCklM0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwdGhpcy5vcHRpb24xJTIwJTNEJTIwMTIzJTNCJTVDbiUyMCUyMCUyMCUyMCU3RCU1Q24lN0QlNUNuJTVDbmNvbnN0JTIwdW51c2VkQ29uc3QlMjAlM0QlMjBuZXclMjBGb28oKSUzQiU1Q25jb25zdCUyMHVzZWRDb25zdCUyMCUzRCUyMG5ldyUyMEZvbygpJTNCJTVDbiU1Q25jb25zb2xlLmxvZyh1c2VkQ29uc3QpJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlMkMlMjJuYW1lJTIyJTNBJTIybWFpbi5qcyUyMiU3RCU1RCUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJvdXRwdXQlMjIlM0ElN0IlMjJmb3JtYXQlMjIlM0ElMjJlcyUyMiU3RCUyQyUyMnRyZWVzaGFrZSUyMiUzQXRydWUlN0QlN0Q=

For functions, this should be handled the the replacedVariableInits logic but apparently, functions have the same problem as classes

https://rollupjs.org/repl/?version=3.7.5&shareable=JTdCJTIyZXhhbXBsZSUyMiUzQW51bGwlMkMlMjJtb2R1bGVzJTIyJTNBJTVCJTdCJTIybmFtZSUyMiUzQSUyMm1haW4uanMlMjIlMkMlMjJjb2RlJTIyJTNBJTIyZnVuY3Rpb24lMjBGb28oKSUyMCU3QiU1Q24lMjAlMjB0aGlzLm9wdGlvbjElMjAlM0QlMjAxMjMlM0IlNUNuJTdEJTVDbiU1Q25jb25zdCUyMHVudXNlZENvbnN0JTIwJTNEJTIwbmV3JTIwRm9vKCklM0IlNUNuY29uc3QlMjB1c2VkQ29uc3QlMjAlM0QlMjBuZXclMjBGb28oKSUzQiU1Q24lNUNuY29uc29sZS5sb2codXNlZENvbnN0KSUzQiU1Q24lMjIlMkMlMjJpc0VudHJ5JTIyJTNBdHJ1ZSU3RCU1RCUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlN0QlN0Q=

@TrickyPi
Copy link
Member Author

TrickyPi commented Feb 8, 2023

Thanks for your review! There are many things that need to be done to complete this pr, it's a bit complicated 😢.

@lukastaegert lukastaegert force-pushed the fix/this-in-constructor branch from 9b673a3 to 9e01b3a Compare February 9, 2023 05:46
@lukastaegert
Copy link
Member

I looked at little more into this and pushed a fix to your branch that solves all issues. Even the example with the setter is working, though that was actually broken on master as well.
The idea is that the FunctionNode holds a "constructedEntity" that is re-used and represents the object created when the class or function is called with "new". Whenever "this" is reassigned, it will be tracked on that entity so that e.g. the definition of a setter is not overlooked. Using your new context flag, we ignore mutations to that variable that are not really side effects when checking if the constructor call should be included.
One nice thing is that this handles both class constructors and functions together.

@lukastaegert lukastaegert force-pushed the fix/this-in-constructor branch from 9e01b3a to 2023d74 Compare February 9, 2023 06:05
@TrickyPi
Copy link
Member Author

TrickyPi commented Feb 9, 2023

You are awesome! I tweaked the test description a bit.

@lukastaegert lukastaegert merged commit 5d81532 into rollup:master Feb 10, 2023
@rollup-bot
Copy link
Collaborator

This PR has been released as part of [email protected]. You can test it via npm install rollup.

@TrickyPi TrickyPi deleted the fix/this-in-constructor branch April 23, 2023 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Treeshake doesn't work for class with this reference

3 participants