google/closure-compiler

Bad type annotation when documenting an optional parameter value

Open

#3767 aperta il 7 feb 2021

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Java (1197 fork)batch import
P4good first issuehelp wanted

Metriche repository

Star
 (7176 star)
Metriche merge PR
 (Merge medio 12g 15h) (1 PR mergiata in 30 g)

Descrizione

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;

Guida contributor