Skip to content

Bad type annotation when documenting an optional parameter value #3767

Open
@customcommander

Description

@customcommander

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;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions