-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
util: add inspectTag template literal tag #11130
Conversation
Introduces a simple template literal tag that ensures that each constituent part of the template literal passes through `util.inspect()` using the default options. ```js const inspectTag = require('util').inspectTag; const m = {a: 1}; console.log(`${m}`); // Prints: '[object Object]' console.log(inspectTag`${m}`); // Prints: '{ a : 1 }' ```
|
||
// Without the tag: | ||
console.log(`${obj}`); | ||
// Prints: '[object Object]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we place these before the line of code it's referring to? To me that seems to be the traditional place for such comments. Also the indentation makes it look out of place.
Seems like something good for userland. If core needs it, it could be internal, or in |
I've just wanted something like this recently for |
|
||
assert.strictEqual(`${obj}`, '[object Object]'); | ||
assert.strictEqual(inspectTag`${obj}`, '{ a: 1 }'); | ||
assert.strictEqual(inspectTag`${obj}-${obj}`, '{ a: 1 }-{ a: 1 }'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth add a test when the end is a string(n === keys.length
), and a test when there are no variables in the template.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not an empty string always in the end of a template literal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really like this idea, but I'll defer to other collaborators for comments on code
|
||
// With the tag: | ||
console.log(inspectTag`${obj}`); | ||
// Prints: '{ a : 1 }' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nit: '{ a: 1 }'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind that util.inspect({a: 1})
returns '{ a : 1 }'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> util.inspect({a: 1})
'{ a: 1 }'
It's also this case in the test that comes with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, sorry, don't know where this comes from :/ Could be lack of coffee
added: REPLACEME | ||
--> | ||
|
||
A template literal tag that ensures each replacement in a template string is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: A template literal tag function
Why wouldn't you just use |
Will close given the feedback. |
This seems like an obvious omission to me...? |
@alex-weej is there anything you can't do with |
Really just about ergonomics. Logging is fine, but when you want to 'defer the logging' so to speak by stuffing some stuff in an throw new Error("The thing was broken: " + util.inspect(foo) + ", " + util.inspect(bar) + ", " + util.inspect(baz)); vs. throw new Error(util.inspect`The thing was broken: ${foo}, ${bar}, ${baz}`); What I see a lot of is people being lazy and just relying on some of them being strings, which inevitably leads to confusion when those strings contain unprintables, are empty, or the wrong runtime type entirely! throw new Error("The thing was broken: " + someString);
^ Lossy
^ Confusing, you see this in a production log and it's not that obvious that someone was attempting to log an empty string.
These two are ambiguous. I've definitely debugged situations where the string "undefined" is floating around. Urgh!
Any JS developer who hasn't seen something like this a zillion times is lying haha. Hence, whatever we can do to make it easier to create unambiguous serializations for diagnostics would be a win in my book. Perhaps a way to form a string using the exact same paradigm as the |
|
Introduces a simple template literal tag that ensures that each constituent part of the template literal passes through
util.inspect()
using the default options.This is inspired by recent changes in tests that have tried to convert
console.log('message', a, b)
to something like:`message ${a} ${b}`
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util