google / google/closure-compiler

[OTI] Warn on too many template parameters

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

Description

[Repro]

The following currently produces no errors whatsoever:

```js
/** @typedef {!Promise} */
var Foo;
/** @enum {!Object} */
var Bar = {BAZ: {a: 'b'}};
/** @typedef {string} */
var Baz;

var /** !Foo */ x = Promise.resolve('xyz');
var /** !Bar */ y = Bar.BAZ;
var /** !Baz */ z = 'abc';
var /** string */ w = 'abc';
var /** !Promise */ q = Promise.resolve(42);
```

Each of these assignment lines should cause some sort of error. The root issue is that the error for `q` is currently off by default, so the first step is to flip that error on by default (we need to fix some usages first). Then we need to ensure that the codepath that checks the number of template parameters (`JSTypeRegistry#createFromTypeNodesInternal` case NAME) runs for non-object types (`w`) and for enum and typedef usages (without adding `null` to the union, since that's currently mixed in). The trickiest warnings are probably `x` and `y`, since the enum case should fill in `` *before* making the new type, thus making `Bar` illegal, while the typedef may want to leave the parameters unfilled so that `Foo` and `Foo` would both work. If we instead fill in `` as the parameter, then at least adding a parameter to `Foo` should still be an error (note: promise is tricky since it has a constructor template parameter that's currently conflated, so we'd need to make sure typedef-ing `Promise` to `Foo` and then writing `Foo` wouldn't try to expand to `Promise`, which may not be an error but should).

[Repro]: https://closure-compiler-debugger.appspot.com/#input0%3D%252F**%2520%2540typedef%2520%257B!Promise%257D%2520*%252F%250Avar%2520Foo%253B%250A%252F**%2520%2540enum%2520%257B!Object%257D%2520*%252F%250Avar%2520Bar%2520%253D%2520%257BBAZ%253A%2520%257Ba%253A%2520'b'%257D%257D%253B%250A%252F**%2520%2540typedef%2520%257Bstring%257D%2520*%252F%250Avar%2520Baz%253B%250A%250Avar%2520%252F**%2520!Foo%253Cnumber%253E%2520*%252F%2520x%2520%253D%2520Promise.resolve('xyz')%253B%250Avar%2520%252F**%2520!Bar%253Cnumber%253E%2520*%252F%2520y%2520%253D%2520Bar.BAZ%253B%250Avar%2520%252F**%2520!Baz%253Cnumber%253E%2520*%252F%2520z%2520%253D%2520'abc'%253B%250Avar%2520%252F**%2520string%253Cnumber%253E%2520*%252F%2520w%2520%253D%2520'abc'%253B%250Avar%2520%252F**%2520!Promise%253Cnumber%252C%2520string%253E%2520*%252F%2520q%2520%253D%2520Promise.resolve(42)%253B%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3Dtrue%26CHECK_SYMBOLS%3Dtrue%26MISSING_PROPERTIES%3Dtrue%26TRANSPILE%3Dtrue%26CHECK_TYPES%3Dtrue%26CLOSURE_PASS%3Dtrue%26PRESERVE_TYPE_ANNOTATIONS%3Dtrue%26PRETTY_PRINT%3Dtrue

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.