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

Question: Why concatenate Symbols with a string without assigning it to a variable? #462

Closed
lukastaegert opened this issue Jun 10, 2019 · 6 comments

Comments

@lukastaegert
Copy link

When making sure that Rollup is able to handle this shim without unexpected tree-shaking, I became upon this line, which is removed by Rollup:

'' + str; // jscs:ignore disallowImplicitTypeConversion

Is this line actually correct or should it rather be something like str = '' + str?

Otherwise I fail to see what the side-effect should be. If it is a thrown error, I am missing the try-statement. Please tell me if I overlook anything here.

@ljharb
Copy link
Member

ljharb commented Jun 10, 2019

It's correct, because implicit String coercion causes Symbols to throw, and this line is intended to replicate that exception. Removing this line will cause code that shouldn't work, to start working.

@lukastaegert
Copy link
Author

Thanks for explaining. Not sure if we are going to fix this one soon, everything else here is working fine, though. A simple way to prevent tree-shaking could be to put this in a place where the return value is used, e.g.

function parseInt(str, radix) {
  var string = trim(String(typeof str === 'symbol' ? '' + str : str));
  var defaultedRadix = $Number(radix) || (hexRegex.test(string) ? 16 : 10);
  return origParseInt(string, defaultedRadix);
};

I do not suppose this is something you would consider? Tackling this via static analysis from our side will be quite hard, and deoptimizing all string concatenations is a really high price for a runtime error that should not occur in proper code.

@ljharb
Copy link
Member

ljharb commented Jun 10, 2019

How often does a noop string concat occur in code such that you need to ever remove it anywhere? Is that really a common pattern?

@lukastaegert
Copy link
Author

Probably as often as this runtime error—usually due to an error on the developer's side 😜
But yes, we could check for this specific pattern.

@ljharb
Copy link
Member

ljharb commented Jun 10, 2019

I think it's safe to leave that to a linter catching noop statements :-)

@lukastaegert
Copy link
Author

You would be surprised as to how many people do not lint...

Still, catching it now on our side with a specific heuristic: rollup/rollup#2917

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

No branches or pull requests

2 participants