Open alexeagle opened 7 years ago
ISTR that we intentionally made the decision to be more restrictive than what the spec allows. If anyone remembers the discussion on that and could summarize it in a wiki page that would be great.
@MatrixFrog advised it could be suppressed with @suppress {functionParams}
but this is not the case:
input0:4: WARNING - Parse error. unknown @suppress parameter: functionParams
* @suppress {functionParams}
^
It is legal ES6 for a parameter with a default value to be followed by a required one, it should not be considered optional.
@alexeagle are you proposing that both parameters be treated as required? What's the point of using a default value then? WDYT about treating all trailing parameters after a parameter with a default value as optional?
Not sure if this is his case but for example in a Redux application a reducer is typically defined as
function myReducer(state = new AppState(), action)
and Redux calls it as myReducer(undefined, someAction)
which results in new AppState()
being passed to the function body.
In this case, state is a required parameter whose type includes undefined. That's fine.
When the compiler sees an unannotated function such as function f(a=1,b) {}
, it needs to decide whether a
is required or optional. I think that in most cases the user wants it to be optional, so we would give spurious warnings when it was meant to be required. If we considered it to be required, there would be more spurious warnings when f is called without any arguments. It's a tradeoff. If the user wants something that is not the default compiler assumption, they can just manually annotate their code.
But I do agree that the current situation of early warning is somewhat unsatisfactory, and we should just consider the trailing parameters optional and not warn.
One possibility is that the default assumption applies when there are no @param
jsdoc. But when there are, it would take precedence, and the lack of =
in the param type would override this behaviour.
Right.
This code produces 2 warnings:
https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Afunction%2520f(a%253D1%252Cb)%2520%257B%257D%250Af(undefined%252C%25203)%253B
And it cannot be suppressed with
@suppress {functionParams}
.It is legal ES6 for a parameter with a default value to be followed by a required one, it should not be considered optional.