google / google/closure-compiler

Static inheritance prevents certain nested types extending each other

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

Description

This code should be perfectly legit

```js
class A {}
A.B = class extends A {}
A.C = class extends A.B {}
```

But the compiler generates a cryptic [error]:
```
input0:3: WARNING - assignment to property C of A.B
found : function (new:A.C, ...?): undefined
required: function (new:A.B.C, ...?): ?
A.C = class extends A.B {}
^^^^^^^^^^^^^^^^^^^^
```

NTI [isn't much better][nti]:
```
input0:3: WARNING - Expected constructor name but found A.B.C.
A.C = class extends A.B {}
^^^^^^^^^^^^^^^^^^^^

input0:3: WARNING - Expected constructor name but found A.C.
A.C = class extends A.B {}
^^^^^^^^^^^^^^^^^^^^
```

This seems to be an issue with the way the compiler transpiles static properties by copying. The transpiled output show

```js
var A = function() {};
A.B = function(var_args) { A.apply(this, arguments); };
$jscomp.inherits(A.B, A);
A.B.C = A.C;
A.B.B = A.B;
// ...
```

and this is clearly an error, since `A.C` hasn't been defined yet.

[error]: https://closure-compiler-debugger.appspot.com/#input0%3Dclass%2520A%2520%257B%257D%250AA.B%2520%253D%2520class%2520extends%2520A%2520%257B%257D%250AA.C%2520%253D%2520class%2520extends%2520A.B%2520%257B%257D%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26MISSING_PROPERTIES%3D1%26TRANSPILE%3D1%26CHECK_TYPES%3D1%26CLOSURE_PASS%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1

[nti]: https://closure-compiler-debugger.appspot.com/#input0%3Dclass%2520A%2520%257B%257D%250AA.B%2520%253D%2520class%2520extends%2520A%2520%257B%257D%250AA.C%2520%253D%2520class%2520extends%2520A.B%2520%257B%257D%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26MISSING_PROPERTIES%3D1%26TRANSPILE%3D1%26CHECK_TYPES%3D1%26CHECK_TYPES_NEW_INFERENCE%3D1%26CLOSURE_PASS%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1

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.