google / google/closure-compiler

NullPointerException in calculated default values

Open
#3,336 4 comments 2 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Trying to compile following code:
```javascript
class AnotherClass {
constructor(id) {
this.id = id;
}
isWhatever() {
return true;
}
}
class Bug2 {
constructor(id) {
this.id = id;
}
async doStuff(doStuff = new AnotherClass(this.id).isWhatever()) {
console.warn(doStuff);
}
}
console.warn(new Bug2(1).doStuff());
```
inside a webpack module with settings
`java -jar closure-compiler-v20190325.jar --language_in ECMASCRIPT_2017 --language_out ECMASCRIPT5 --compilation_level ADVANCED_OPTIMIZATIONS --jscomp_off=* --js=dist/closure-bug.js --js_output_file=dist/closure-bug.min.js`
results in error
> java.lang.NullPointerException: NAME $jscomp$async$this 109 [length: 4] [source_file: dist/closure-bug.js]
> at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:895)
> at com.google.javascript.jscomp.RemoveUnusedCode.getVarForNameNode(RemoveUnusedCode.java:726)
> at com.google.javascript.jscomp.RemoveUnusedCode.traverseNameNode(RemoveUnusedCode.java:564)
> at com.google.javascript.jscomp.RemoveUnusedCode.traverseNode(RemoveUnusedCode.java:427)
> at com.google.javascript.jscomp.RemoveUnusedCode.traverseGetProp(RemoveUnusedCode.java:469) <...>

In case it's important, the code is generated by TypeScript 3.4.3 with settings
```json
{
"compilerOptions": {
"target": "es2017",
"module": "es2015"
}
}
```
and Webpack 4.30.0 production profile

Full code in attachment. Just the *.js file is enough, .ts is provided merely for information.

There are different attempts, some of them work, some create error.

One case ("SometimesWorks") is particularly weird because it compiles by itself, but creates error if compiled with other working examples.
Other working examples compile fine in any combination, and buggy examples create error in any case.

[code.zip](https://github.com/google/closure-compiler/files/3089445/code.zip)

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.