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

feat: treeshake for optional chaining #4797

Merged
merged 8 commits into from Jan 12, 2023

Conversation

antfu
Copy link
Contributor

@antfu antfu commented Jan 9, 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:

Description

esbuild treeshakes it as well: https://esbuild.egoist.dev/#W1siaW5kZXgudHMiLHsiY29udGVudCI6ImlmICh1bmRlZmluZWQ/LmEpIHtcbiAgY29uc29sZS5sb2coXCJcIik7XG59XG5cbnVuZGVmaW5lZD8uYSgpO1xuIn1dLFsiZXNidWlsZC5jb25maWcuanNvbiIseyJjb250ZW50Ijoie1xuICBcImZvcm1hdFwiOiBcImVzbVwiLFxuICBcImNkblVybFwiOiBcImh0dHBzOi8vY2RuLnNreXBhY2suZGV2XCIsXG4gIFwibWluaWZ5XCI6IHRydWVcbn0ifV0sWyJzdW0udHMiLHsiY29udGVudCI6ImV4cG9ydCBjb25zdCBzdW0gPSAoYTogbnVtYmVyLCBiOiBudW1iZXIpID0+IGEgKyBiIn1dXQ==

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Nice, great work!

@codecov
Copy link

codecov bot commented Jan 10, 2023

Codecov Report

Merging #4797 (3f66268) into master (b26a37f) will decrease coverage by 0.01%.
The diff coverage is 91.66%.

@@            Coverage Diff             @@
##           master    #4797      +/-   ##
==========================================
- Coverage   99.02%   99.01%   -0.02%     
==========================================
  Files         217      218       +1     
  Lines        7780     7792      +12     
  Branches     2161     2166       +5     
==========================================
+ Hits         7704     7715      +11     
- Misses         24       25       +1     
  Partials       52       52              
Impacted Files Coverage Δ
src/ast/nodes/ChainExpression.ts 91.66% <91.66%> (ø)

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


getLiteralValueAtPath(): LiteralValueOrUnknown {
if (this.getObjectValue() == null) return undefined;
return UnknownValue;
Copy link
Member

Choose a reason for hiding this comment

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

This one should be easy to cover in a test, I wonder why you current test does not hit this line

src/ast/nodes/ChainExpression.ts Outdated Show resolved Hide resolved
private objectValue: LiteralValueOrUnknown | typeof unset = unset;

deoptimizeCache(): void {
this.objectValue = UnknownValue;
Copy link
Member

Choose a reason for hiding this comment

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

This one is actually hard to cover in a test and I am fine if it is not covered. To hit this, you have to use a variable further up in the chain that is initialized as undefined but later gets another value assigned, invalidating the object.getLiteralValueAtPath in line 46.
The problem is to find a suitable "later". Sometimes it works to do this in a function but as I said, it is tricky.

src/ast/nodes/ExpressionStatement.ts Outdated Show resolved Hide resolved
@antfu
Copy link
Contributor Author

antfu commented Jan 10, 2023

Updated. Thanks for the guidance! I will leave the coverage deoptimizeCache as I don't exactly understand the context and how. Feel free to push. Thanks!

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Great stuff, thanks!

@lukastaegert lukastaegert merged commit ffc19b0 into rollup:master Jan 12, 2023
@lukastaegert
Copy link
Member

This PR has been released as part of rollup@3.10.0. You can test it via npm install rollup.

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.

None yet

2 participants