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

adding eval inside a function puts all variables into global through #27

Closed
ilyavolodin opened this issue Nov 13, 2013 · 3 comments
Closed

Comments

@ilyavolodin
Copy link

The following code:

function evilEval(stuffToEval)
{
    var ultimateAnswer;

    ultimateAnswer = 42;

    alert(stuffToEval);
}

Will put one variable into through array - alert. However, if you change alert to eval like so:

function evilEval(stuffToEval)
{
    var ultimateAnswer;

    ultimateAnswer = 42;

    eval(stuffToEval);
}

There are now 3 variables in through. eval, ultimateAnswer and stuffToEval.

This was found and reported here: eslint/eslint#376

@Constellation
Copy link
Member

When eval appears, escope mark the scope dynamic. This is because eval can introduce new variable: eval("var i = 20"). So escope takes very conservative analysis on this pattern.

If you would like to ignore eval detection, you can specify options.ignoreEval

@Constellation
Copy link
Member

And if you would like to ignore all dynamic scope (with and eval scopes), you can specify options.optimistic.

@ilyavolodin
Copy link
Author

Ahh.. thanks for the explanation. One question though. Wouldn't it make sense to leave variables that were declared outside of eval as static? They are already declared, eval might change their value and add new variables, but it will not be able to change the fact that they are declared.

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