Navigation Menu

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 rendered class names #4674

Merged
merged 3 commits into from Oct 16, 2022
Merged

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Oct 14, 2022

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

This PR preserves the name property of classes even if the class needs to be renamed due to conflicts. The solution is to rewrite both class foo {} and let foo = class {} to e.g. let foo$1 = class foo {} if foo needs to be renamed.

@github-actions
Copy link

github-actions bot commented Oct 14, 2022

Thank you for your contribution! ❤️

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

npm install rollup/rollup#gh-4637-preserve-class-name

or load it into the REPL:
https://rollupjs.org/repl/?pr=4674

@codecov
Copy link

codecov bot commented Oct 14, 2022

Codecov Report

Merging #4674 (d6665ee) into master (11d425b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4674   +/-   ##
=======================================
  Coverage   99.02%   99.02%           
=======================================
  Files         215      215           
  Lines        7552     7567   +15     
  Branches     2092     2096    +4     
=======================================
+ Hits         7478     7493   +15     
  Misses         24       24           
  Partials       50       50           
Impacted Files Coverage Δ
src/ast/nodes/ClassDeclaration.ts 100.00% <100.00%> (ø)
src/ast/nodes/VariableDeclarator.ts 100.00% <100.00%> (ø)
src/ast/scopes/Scope.ts 84.61% <0.00%> (+1.28%) ⬆️

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

@lukastaegert lukastaegert merged commit 46e1eaa into master Oct 16, 2022
@lukastaegert lukastaegert deleted the gh-4637-preserve-class-name branch October 16, 2022 04:42
@rollup-bot
Copy link
Collaborator

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

@hyf0
Copy link

hyf0 commented Feb 24, 2023

Just happen to dig into here.

I notice that function.name seems doesn't apply the strategy. Are there any traps?

I guess

function foo() {
  console.log(foo)
}

is the same as

var foo$1 = function foo() {
  console.log(foo$1)
}

@lukastaegert
Copy link
Member Author

lukastaegert commented Feb 25, 2023

No it is not.

foo("Hello"); // logs "Hello" 
function foo() {
  console.log(foo)
}
foo$1("Hello"); // throws because we cannot call "undefined" 
var foo$1 = function foo() {
  console.log(foo)
}

function has a hoisting magic that can not be replicated by other means.

@hyf0
Copy link

hyf0 commented Feb 25, 2023

I take a look at esbuild's approach. It solves the problem in a non-intuitive(IMO) way.

Anyway, thanks for the detail explaining.

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.

Class names are mangled, breaking runtime behavior that relies on them.
3 participants