I took the output of some compiled TypeScript (also tried with CoffeeScript) and put it into WebStorm. When I do, JSHint complains "'Snake' is already defined" for the inner declaration of the Snake function.
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Animal = (function () {
function Animal(name) {
this.name = name;
}
Animal.prototype.move = function (meters) {
alert(this.name + " moved " + meters + "m.");
};
return Animal;
})();
var Snake = (function (_super) {
__extends(Snake, _super);
function Snake(name) { //Warning registered here
_super.call(this, name);
}
Snake.prototype.move = function () {
alert("Slithering...");
_super.prototype.move.call(this, 5);
};
return Snake;
})(Animal);
I can disable the warning with /*jshint -W004 */
, but it seems like the warning is invalid since we are scoped within a function.
Now the weird part. If I move the __extends
call down to after the function declaration, the error disappears.
function Snake(name) {
_super.call(this, name);
}
__extends(Snake, _super);
I really have 2 questions, but the first is my main question which I will award my answer to.
- Is this warning valid?
- What are the ramifications of moving the
__extends
call down below the function declaration?
It seems like it might be complaining because the inner variable is shadowing an outer variable of the same name which could be potentially confusing.
https://stackoverflow.com/a/17852430/378151
According to this other SO answer, a commit was made in July 2013 around this behavior. Douglas Crockford stated (about JSLint)