google / google/closure-compiler
Argument in chained promise not checked after truthiness check
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
In a promise chain, the value passed to `then` is not type checked if its truthiness is checked.
Given a function that returns a promise:
`/** @return {!Promise} */ var foo;`
Closure doesn't detect this undefined property usage on that string in a chained `then()` function if we do a truthiness check on the string:
```
Promise.resolve().then(function() {
return foo();
}).then(function(word) {
if (word) // this if statement makes the below line pass
console.log(word.arglebarg); // this should fail, but doesn't
});
```
This only repros when the Promise is returned from another Promise.
Full code example:
```
// compile with:
// java -jar compiler.jar --jscomp_error=missingProperties --checks_only --language_in=ECMASCRIPT6_STRICT --language_out=ECMASCRIPT5_STRICT
/** @return {!Promise} */
function foo() {
return new Promise((resolve, reject) => {
resolve('hello');
});
}
function main() {
Promise.resolve().then(function() {
return foo();
}).then(function(word) {
if (word)
console.log(word.arglebarg);
});
}
```
Version: v20190106
Built on: 2019-01-10 21:49
Contributor guide
Assessment
This issue has not been assessed yet.