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

Drop unuseful eval call #8385

Merged
merged 1 commit into from
Nov 19, 2018
Merged

Drop unuseful eval call #8385

merged 1 commit into from
Nov 19, 2018

Conversation

hax
Copy link
Contributor

@hax hax commented Nov 16, 2018

If use CSP (without unsafe-eval), both new Function and eval will be forbidden, so eval part is just dead code.

The only possibility eval part would run was new Function('return this')() return a falsy value, but it never possible as the spec (Is there any old engine violate this? Never heard about that.)

This pr also add new keyword to make the code a little bit explicit.

What kind of change does this PR introduce?

clean unuseful code

Did you add tests for your changes?

no need

Does this PR introduce a breaking change?

no

What needs to be documented once your changes are merged?

no need

If use CSP (without `unsafe-eval`), both `new Function` and `eval` will be forbidden, so `eval` part is just dead code.

The only possibility `eval` part would run was `new Function('return this')()` return a falsy value, but it never possible as the spec (Is there any old engine violate this? Never heard about that.)

This pr also add `new` keyword to make the code a little bit explicit.
@jsf-clabot
Copy link

jsf-clabot commented Nov 16, 2018

CLA assistant check
All committers have signed the CLA.

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This need to be documented (issue in webpack/webpack.js.org will be filed when merged)

@@ -7,7 +7,7 @@ g = (function() {

try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1, eval)("this");
g = g || new Function("return this")();
Copy link
Member

Choose a reason for hiding this comment

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

If both are equal, why not using (1,eval)("this") only instead. That's shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because "eval is evil" 🤪

A better reason:

We use function() {return this;} before, so new Function("return this") is just same code. Actually new Function("return this") is just like function() {"use non-strict"; return this}. If no CSP, we should simply use new Function, if we have "use non-strict" directive, we should simply use function () { ... }

It's possible to only use eval, if that, we should also change function () {...} to eval and it's shorter as you say.

The real bad part of eval is the magic of (1, eval) --- it use rarely used commas expression to get indirect eval which always executed in global context, and direct vs indirect eval is also a unintelligible thing for many js programmers. In fact, I go to here because someone just ask me what (1, eval) means in webpack code! And I myself ask other guys to recall the direct/indirect eval issue (I taught many programmers about that in ES5 era, but I forgot it now!!!)

So I think we'd better use the much simple new Function way. 😆

@hax
Copy link
Contributor Author

hax commented Nov 16, 2018

Have no idea why there is a test fail... incidental timeout?

@webpack-bot
Copy link
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

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

Ok fine. Users seem to trust new Function more than eval (for no real reason).

@sokra sokra closed this Nov 16, 2018
@sokra sokra reopened this Nov 16, 2018
@sokra sokra merged commit 614a6fc into webpack:master Nov 19, 2018
@sokra
Copy link
Member

sokra commented Nov 19, 2018

Thanks

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.

None yet

5 participants