Skip to content

Commit

Permalink
Ignore undefined variables if we're inside delete (second part of jsh…
Browse files Browse the repository at this point in the history
…int#25, fixed)
  • Loading branch information
valueof committed Feb 21, 2011
1 parent cd0c7cd commit dd6c894
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 8 additions & 6 deletions jshint.js
Expand Up @@ -4208,11 +4208,13 @@ loop: for (;;) {
// The name is not defined in the function. If we are in the global scope,
// then we have an undefined variable.
//
// Operator typeof does not raise runtime errors even if the base object of
// a reference is null so no need to display warning if we're inside of typeof.
// Operators typeof and delete do not raise runtime errors even if the base
// object of a reference is null so no need to display warning if we're
// inside of typeof or delete.

} else if (funct['(global)']) {
if (anonname != 'typeof' && option.undef && typeof predefined[v] !== 'boolean') {
if (anonname != 'typeof' && anonname != 'delete' &&
option.undef && typeof predefined[v] !== 'boolean') {
warning("'{a}' is not defined.", token, v);
}
note_implied(token);
Expand Down Expand Up @@ -4246,10 +4248,10 @@ loop: for (;;) {
note_implied(token);
} else if (typeof s !== 'object') {

// Operator typeof does not raise runtime errors even if the base object of
// a reference is null so no need to display warning if we're inside of typeof.
// Operators typeof and delete do not raise runtime errors even if the base object of
// a reference is null so no need to display warning if we're inside of typeof or delete.

if (anonname != 'typeof' && option.undef) {
if (anonname != 'typeof' && anonname != 'delete' && option.undef) {
warning("'{a}' is not defined.", token, v);
} else {
funct[v] = true;
Expand Down
9 changes: 7 additions & 2 deletions tests/jshint.js
Expand Up @@ -119,15 +119,20 @@ describe("Operators", function () {
});

/*
* Operator typeof accepts a reference. There is no
* Operators typeof and delete accept a reference. There is no
* runtime error, if the base object of that reference is null.
*/
it("must tolerate references in typeof and delete", function () {
var localTypeof = "(function () { if (typeof NullReference) {} }());",
globalTypeof = "if (typeof NullReference) {}";
globalTypeof = "if (typeof NullReference) {}",
localDelete = "(function () { delete NullReference; }());",
globalDelete = "delete NullReference;";

expect(JSHINT(globalTypeof, { undef: true })).toEqual(true);
expect(JSHINT(localTypeof, { undef: true })).toEqual(true);

expect(JSHINT(localDelete, { undef: true })).toEqual(true);
expect(JSHINT(globalDelete, { undef: true })).toEqual(true);
});
});

Expand Down

0 comments on commit dd6c894

Please sign in to comment.