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

Regression: undef: false does not work with 'use strict' #2903

Open
mkrogius opened this issue Mar 30, 2016 · 3 comments
Open

Regression: undef: false does not work with 'use strict' #2903

mkrogius opened this issue Mar 30, 2016 · 3 comments

Comments

@mkrogius
Copy link

See previous issue #353

/*jshint undef: false */
'use strict';
a++;

Gives an error: 'a' is not defined.

@jugglinmike
Copy link
Member

Thanks for the report! You have identified an implicit behavior of JSHint that
I personally like but that is technically invalid.

Whenever JSHint encounters a "use strict" directive, it automatically enables
the undef option
:

if (state.isStrict()) {
  state.option.undef = true;
}

I can guess at the motivation here: in strict mode code, assignment to an
unresolvable reference throws a ReferenceError (outside of strict mode, such
assignments silently create global bindings). The author of this code was
likely trying to help users avoid the ReferenceError.

Unfortunately (and as implied by this bug report), we cannot determine which
global references are unresolvable statically.

The reason I like this behavior is that it encourages people to enable "undef",
which in turn forces them to be explicit about global variables. I'll be sad to
see it go, but it seems as though we may have to remove it.

@mkrogius The short-term solution for you is to formally declare all global
variables--either in the script itself, or by explicitly configuring JSHint
with the globals configuration option.

@mkrogius
Copy link
Author

Many thanks for the detailed explanation!

@jugglinmike
Copy link
Member

Sure thing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants