google / google/closure-compiler

Use NTI type info in conformance violations such as BanUnknownThis

Open
#1,411 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

[Originally titled: Covariant subtype return value "unknown this" incorrect conformance violation]

I'm experimenting with this trick:
https://github.com/google/closure-compiler/wiki/covariant-subtype-values-with-@template

And I'm applying `BanUnknownThis` throughout my codebase:
https://github.com/google/closure-compiler/wiki/JS-Conformance-Framework
https://github.com/google/closure-compiler/blob/0f54fd1f1d51680d61fb40c647373349d536fcbb/src/com/google/javascript/jscomp/ConformanceRules.java#L1132

``` javascript
class X {
/**
* @this {THIS}
* @return {THIS}
* @template THIS
*/
x() {
return this; // WARNING - Violation: unknown this
}
}

class Y extends X {
}

var /** Y */ y = new Y().x();
```

NTI seems to correctly infer 'this' but the conformance violation warning still happens:

https://closure-compiler-debugger.appspot.com/#input0%3Dclass%2520X%2520%257B%250A%2520%2520%252F**%2520%250A%2520%2520%2520*%2520%2540this%2520%257BTHIS%257D%250A%2520%2520%2520*%2520%2540return%2520%257BTHIS%257D%250A%2520%2520%2520*%2520%2540template%2520THIS%250A%2520%2520%2520*%252F%250A%2520%2520x()%2520%257B%250A%2520%2520%2520%2520return%2520this%253B%2520%252F%252F%2520WARNING%2520-%2520Violation%253A%2520unknown%2520this%250A%2520%2520%257D%250A%257D%250A%250Aclass%2520Y%2520extends%2520X%2520%257B%250A%257D%250A%250Avar%2520%252F**%2520Y%2520*%252F%2520y%2520%253D%2520new%2520Y().x()%253B%26input1%26conformanceConfig%3Drequirement%253A%2520%257B%2520type%253A%2520CUSTOM%2520java_class%253A%2520'com.google.javascript.jscomp.ConformanceRules%2524BanUnknownThis'%2520error_message%253A%2520'unknown%2520this'%2520%257D%250A%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26CHECK_TYPES%3D1%26CHECK_TYPES_NEW_INFERENCE%3D1%26LANG_IN_IS_ES6%3D1%26MISSING_PROPERTIES%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1%26TRANSPILE%3D1

Now is it the conformance rule that is broken or something else? Or am I doing it wrong? I don't see how NTI can be getting it right but `BanUnknownThis` still not working. Is `BanUnknownThis` just an example, or is it used in production?

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.