google / google/closure-compiler

Bad type annotation when documenting an optional parameter value

Open
#3,767 2 comments 0 reactions 0 assignees View on GitHub
good first issue help wanted P4
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

With Closure Compiler v20210106 and the following flags:

```lang-none
--language_in=ECMASCRIPT_NEXT
--compilation_level ADVANCED
--js tmp/wrap.js
```

This works fine:

```javascript
/**
* @param {string} str
* @param {string} [prefix=aa]
* @param {string} [suffix=bb]
*/
function wrap1(str, prefix="aa", suffix="bb") {
return prefix + str + suffix;
}

window.wrap1 = wrap1;
```

However this:

```javascript
/**
* @param {string} str
* @param {string} [prefix=(]
* @param {string} [suffix=)]
*/
function wrap2(str, prefix="(", suffix=")") {
return prefix + str + suffix;
}

window.wrap2 = wrap2;
```

produces the following warnings:

```lang-none
tmp/wrap.js:12:27: WARNING - [JSC_TYPE_PARSE_ERROR] Bad type annotation. missing closing ] See https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler for more information.
12| * @param {string} [prefix=(]
^

tmp/wrap.js:13:27: WARNING - [JSC_TYPE_PARSE_ERROR] Bad type annotation. missing closing ] See https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler for more information.
13| * @param {string} [suffix=)]
^

0 error(s), 2 warning(s), 100.0% typed
```

It looks like the compiler is confused about the sequence of opening and closing "brackets". However in this case both `(` and `)` are the default values for `prefix` and `suffix`. To verify this theory I've created `wrap3` with both `prefix` and `suffix` defaulting to `]` so as to make it look like the sequence is "complete":

```javascript
/**
* @param {string} str
* @param {string} [prefix=]]
* @param {string} [suffix=]]
*/
function wrap3(str, prefix="]", suffix="]") {
return prefix + str + suffix;
}

window.wrap3 = wrap3;
```

The above function generates no warnings or errors.

---

Complete file:

```javascript
/**
* @param {string} str
* @param {string} [prefix=aa]
* @param {string} [suffix=bb]
*/
function wrap1(str, prefix="aa", suffix="bb") {
return prefix + str + suffix;
}

/**
* @param {string} str
* @param {string} [prefix=(]
* @param {string} [suffix=)]
*/
function wrap2(str, prefix="(", suffix=")") {
return prefix + str + suffix;
}

/**
* @param {string} str
* @param {string} [prefix=]]
* @param {string} [suffix=]]
*/
function wrap3(str, prefix="]", suffix="]") {
return prefix + str + suffix;
}

window.wrap1 = wrap1;
window.wrap2 = wrap2;
window.wrap3 = wrap3;
```

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.