parseInt() incorrect number of arguments
See original GitHub issue// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
console.log(parseInt("123"));
results in a warning
JSC_WRONG_ARGUMENT_COUNT: Function parseInt: called with 1 argument(s). Function requires at least 2 argument(s) and no more than 2 argument(s).
which is not correct. The radix argument for parseInt
is optional and can be omitted.
Searching through why Closure wants to have two arguments there leads to Google-specific convention at
It is a bit unfortunate that coding conventions from a specific team/company overrule published standards.
But no biggie, following the advice and adding undefined
, i.e.
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
console.log(parseInt("123",undefined));
results in no warning, but produces
console.log(parseInt("123",void 0));
which is suboptimal with the , void 0
bloating up the code size. (also the code could have been optimized to just console.log(123)
, but that’s beyond the interest of this issue)
Tried to use my own --externs file to fix this problem, and tried
/**
* @param {string} str
* @param {number=} radix
* @return {number}
* @suppress {duplicate}
*/
var parseInt = function(str, radix) {};
but that gave trouble with
WARNING - [JSC_TYPE_MISMATCH] initializing variable
found : function(string, number=): number
required: function(*, (number|undefined)): number
var parseInt = function(str, radix) {};
^^^^^^^^^^^^^^^^^^^^^^^
Then tried with
/**
* @param {string} str
* @param {number=} radix
* @return {number}
* @suppress {duplicate}
*/
var parseInt = /** function(*, (number|undefined)): number */ (function(str, radix) {});
and with
var parseInt = /** function(*, number=): number */ (function(str, radix) {});
but neither of them want to operate since externs.zip/es3.js
already contains an earlier definition.
How can I make Closure be optimal sized and warnings-free when it sees code like parseInt("123")
?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top GitHub Comments
Thanks! Tried with
and
The four first versions gave an error
The last one did not, but it still prints the warning
Tried reading through https://github.com/google/closure-compiler/wiki/@suppress-annotations but was not able to figure it out 😕 I wonder what is the right syntax here?
Thanks! I reissued that test, and it does actually work. I suspect I botched something up when trying the first time.