All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rules.jshint.W051.html Maven / Gradle / Ivy

Go to download

Consume reports generated by jshint for code quality. Also consume reports for code duplication (either simian or cpd). Consumes the unit/integration tests reports (generated by Jasmin) coverage report (lcov generated by Istanbul). The information generated by reports are added in Sonar

There is a newer version: 2.1.0
Show newest version

    

History

This warning has existed in two forms in JSLint, JSHint and ESLint. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint the warning given is "Only properties should be deleted"

  • In JSHint and ESLint the warning has always been "Variables should not be deleted"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Only properties should be deleted" error, and the alternative "Variables should not be deleted" error, is thrown when JSLint, JSHint or ESLint encounters the delete operator followed by a single identifier. In the following example we declare a variable x and then attempt to delete it:

x
 
1
var x = 10;
2
delete x;
3
JSLint found 1 errorVersion 2015-09-23
Line 1:Expected '.' and instead saw 'x'.

Why do I get this error?

This error is raised to highlight code that probably doesn't work as you expect it to. It can also indicate a fatal syntax error. The delete operator will only delete properties of objects. It cannot "delete" variables or anything else. Here's a valid use of the delete operator. Notice how this time there are no JSLint errors:

5
 
1
var x = {
2
    prop: 10
3
};
4
delete x.prop;
5
JSHint found no errorsVersion 2.9.0

The ECMAScript 5 specification details the behaviour of the delete operator (ES5 §11.4.1). When the operand is a reference to an object property this is what happens:

If IsPropertyReference(ref) is true, then
    Return the result of calling the [[Delete]] internal method on ToObject(GetBase(ref)) providing GetReferencedName(ref) and IsStrictReference(ref) as the arguments.

But when the operand is a reference to an Environment Record binding (something that is not an object property), the runtime will attempt to delete it (and fail) unless the code is running in strict mode. In that case a syntax error is thrown:

Else, ref is a Reference to an Environment Record binding, so
    If IsStrictReference(ref) is true, throw a SyntaxError exception.
    ...[attempt to delete]...

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W051. This means you can tell JSHint to not issue this warning with the /*jshint -W051 */ directive.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy