google / google/closure-compiler

Do not warn unreachable code for variable declarations

Open
#2,240 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

From https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var :

> var hoisting
> Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.

If there is a return statement in a function, variable declarations written after should not warn for unreachable code.
I did not intentionnally write a statement like that, it's from typescript transpilation:

```ts
export class A
{
public a: A;
private v( ...params: string[] )
{

}
public test()
{
return this.a.v( ...[] );
}
}
```
Is transpiled in:
```js
var A = (function () {
function A() {
}
A.prototype.v = function () {
var params = [];
for (var _i = 0; _i < arguments.length; _i++) {
params[_i] = arguments[_i];
}
};
A.prototype.test = function () {
return (_a = this.a).v.apply(_a, []);
var _a;
};
return A;
}());
exports.A = A;
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.