google/closure-compiler

Bad type annotation when documenting an optional parameter value

Open

#3,767 opened on Feb 7, 2021

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Java (7,176 stars) (1,197 forks)batch import
P4good first issuehelp wanted

Description

With Closure Compiler v20210106 and the following flags:

--language_in=ECMASCRIPT_NEXT
--compilation_level ADVANCED
--js tmp/wrap.js 

This works fine:

/**
 * @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:

/**
 * @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:

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":

/**
 * @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:

/**
 * @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

Bad type annotation when documenting an optional parameter value · google/closure-compiler#3767 | Good First Issue