-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console, doc, util: console.log() and util.format() are wrongly documented and may be inconsistent #13908
Comments
IMO if the first argument is not a format string, i.e. it could be a string type but with no %-characters (unless you \-escape them), all arguments should be called with |
I am really not sure if this should also be applied for all excessive arguments after a format string. Both ways can be considered somehow inconsistent. |
@vsemozhetbyt What would be the option? To throw an error? |
I think throwing would be needlessly obstructive. Maybe this case should be just documented more clearly. Because if we apply inspect-all-format-not-respective-arguments here, this could be more confusing: > console.log('foo %s', 'str', 'str');
foo str 'str' // instead of current: foo str str |
I think it seems consistent. If you for any case would want to write that string you simply write |
Or However, this should be widely discussed, as these ones are rather heavily used functions across the userland. |
I do not know how this can be worded in good English with not so confusing examples, but this is some dry summation. Various sets of arguments with different behavior and results.
const f = () => {};
const s = 'str';
const o = new Array(3);
console.log('%j %s %j');
console.log('%j %s %j', f);
console.log('%j %s %j', f, s, o);
console.log('%j %s', f, s, o);
console.log('', f, s, o);
console.log(f, s, o);
console.log(); %j %s %j
undefined %s %j
undefined str [null,null,null]
undefined str [ <3 empty items> ]
() => {} str [ <3 empty items> ]
[Function: f] 'str' [ <3 empty items> ]
< '\n' > const f = () => {};
const s = 'str';
const o = new Array(3);
> util.format('%j %s %j');
'%j %s %j'
> util.format('%j %s %j', f);
'undefined %s %j'
> util.format('%j %s %j', f, s, o);
'undefined str [null,null,null]'
> util.format('%j %s', f, s, o);
'undefined str [ <3 empty items> ]'
> util.format('', f, s, o);
' () => {} str [ <3 empty items> ]'
> util.format(f, s, o);
'[Function: f] \'str\' [ <3 empty items> ]'
> util.format();
'' cc @nodejs/documentation, @bnoordhuis, @cjihrig, @evanlucas |
Would it be of interest if I submitted a PR with this proposal realised? |
@nattelog I hope a doc PR would draw some more attention at least) |
@vsemozhetbyt And by doc PR you mean one where I document the util.format() function? |
@vsemozhetbyt I added a small check to this if statement: if (typeof f !== 'string' || !/%\w/.test(f)) To guard against non-format strings. However, it makes many tests fail. See this for instance: === release test-cli-eval ===
Path: parallel/test-cli-eval
assert.js:60
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: '\'start\'\n\'beforeExit\'\n\'exit\'\n' === 'start\nbeforeExit\nexit\n'
at Object.<anonymous> (/Users/nattelog/Projekt/node/test/parallel/test-cli-eval.js:174:10)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
Command: out/Release/node /Users/nattelog/Projekt/node/test/parallel/test-cli-eval.js Since If this change will be implemented, many test cases must be changed as well. |
@nattelog I think the doc for If you like to unify the API behavior, it would be better to raise a separate PR for this. Yes, this change will be a drastic one and many tests will need fixing, so this will be some time-consuming project. And the guard also should be more careful (currently it would erroneously consider strings like So maybe we should get some +1 from CTC members before somebody dares to undertake this fix. |
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. Fixes: nodejs#13908
Rephrased how excessive arguments to the format string are concatenated. Fixes: nodejs#13908
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add clarification to the documentation on util.format() and console.log() regarding how excessive arguments are treated when the first argument is a non-format string compared to when it is not a string at all. PR-URL: #14027 Fixes: #13908 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This is an issue that superseded #6341, Previously I thought this was a pure doc issue, now it seems to me that it is a more complicated problem that needs more discussion.
The both docs fragments (here and here) are wrong:
util.inspect()
is not called on each argument if the first argument is a non-format string. This is true if the first argument is not a string at all (see this path in code). If the first argument is a non-format string,util.inspect()
is called only for arguments whosetypeof
is'object'
or'symbol'
(exceptnull
) — see this path in code.Currently, I've found out that this impacts the output with
String
andFunction
arguments (watch out for quotes in the output for strings and absolutely different output for functions):Maybe there are other diferences.
Possible solutions:
UPD: This fragment is more correct:
As you can see,
util.inspect()
is not called for excessiveString
andFunction
arguments here.However, this doc fragment can also be improved, as functions are objects (maybe
typeof
should be mentioned).The text was updated successfully, but these errors were encountered: