google / google/closure-compiler

Optional argument can appear before non-optional parameters

Open
#2,314 7 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

This code produces 2 warnings:
```js
function f(a=1,b) {}
f(undefined, 3);
```

```
JSC_OPTIONAL_ARG_AT_END: optional arguments must be at the end at line 1 character 0
function f(a=1,b) {}
^
JSC_WRONG_ARGUMENT_COUNT: Function f: called with 2 argument(s). Function requires at least 0 argument(s) and no more than 1 argument(s). at line 2 character 0
f(undefined, 3);
^
```

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.

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.