Skip to content
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, test: improve inspect escaping #21624

Closed
wants to merge 4 commits into from

Conversation

Projects
None yet
7 participants
@BridgeAR
Copy link
Member

commented Jul 2, 2018

For me it is always hard to read strings that use escaped quotes. This PR reduces the amount of quotes that have to be escaped significantly be just using either double quotes or backticks instead, if appropriate. That should improve the readability of these strings a lot.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

BridgeAR added some commits Jul 2, 2018

util: do not escape single quotes if not necessary
Right now util.inspect will always escape single quotes. That is not
necessary though in case the string that will be escaped does not
contain double quotes. In that case the string can simply be wrapped
in double quotes instead.
util: reduce .inspect quote escaping
This reduces the amount of quotes util.inspect escapes by falling
back to backticks in case a string contains single and double quotes.
That makes sure only very few strings have to escape quotes at all.
Thus it increases the readability of these strings.
test: improve inspect readability
The string escaping is hard to read. This changes all those escaped
strings to use double quotes instead so no escaping is necessary.

@BridgeAR BridgeAR requested a review from nodejs/tsc Jul 2, 2018

@ChALkeR
Copy link
Member

left a comment

> console.log(util.inspect('${xx}\'"'))
`${xx}'"`
undefined
> `${xx}'"`
ReferenceError: xx is not defined

If $ is present backticks should be avoided as well.
Also, please add a test for that.

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 4, 2018

@ChALkeR done

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 4, 2018

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 9, 2018

Ping @ChALkeR
@nodejs/tsc PTAL

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 13, 2018

@nodejs/tsc PTAL.

@ChALkeR PTAL, I addressed your comment :)

@BridgeAR BridgeAR requested a review from nodejs/tsc Jul 13, 2018

@BridgeAR BridgeAR added the tsc-review label Jul 13, 2018

Fixed.

@ChALkeR

This comment has been minimized.

Copy link
Member

commented Jul 13, 2018

@BridgeAR Sorry for the delay, totally missed this. 😞
I dismissed my stale review.

@targos

This comment has been minimized.

Copy link
Member

commented Jul 14, 2018

I'm OK with the change

@Trott

This comment has been minimized.

Copy link
Member

commented Jul 15, 2018

I'm neutral on this one. I see that there is benefit. And I see that there is maintenance cost. And I wonder whether or not it's an issue that people may be surprised. ("Why did I get a template string one time, a single-quoted string the other time, and a double-quoted string the last time? What is going on?!") Honestly have no idea if that will ever come up and if it's a problem or not.

Leaving a note so that it doesn't feel like the TSC ping is being ignored.

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 16, 2018

@targos is the "OK" a LG? In that case I would go ahead and land this.

@Trott I believe that is relatively clear from the output itself: if the string contains a single quote it is clear that it can only be wrapped into double quotes or back ticks or the single quote would have to be escaped. Do you really think there is room for confusion? I actually wonder if people will even notice the difference besides being able to read the output without knowing that it was escaped before.

@mcollina
Copy link
Member

left a comment

LGTM

@targos

This comment has been minimized.

Copy link
Member

commented Jul 16, 2018

@BridgeAR consider it a rubber-stamp LG. I didn't review the code.

@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 16, 2018

BridgeAR added a commit to BridgeAR/node that referenced this pull request Jul 16, 2018

util: do not escape single quotes if not necessary
Right now util.inspect will always escape single quotes. That is not
necessary though in case the string that will be escaped does not
contain double quotes. In that case the string can simply be wrapped
in double quotes instead.
If the string contains single and double quotes and it does not
contain `${` as part of the string, backticks will be used instead.
That makes sure only very few strings have to escape quotes at all.
Thus it increases the readability of these strings.

PR-URL: nodejs#21624
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>

BridgeAR added a commit to BridgeAR/node that referenced this pull request Jul 16, 2018

test: improve inspect readability
The string escaping is hard to read. This changes all those escaped
strings to use double quotes instead so no escaping is necessary.

PR-URL: nodejs#21624
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@BridgeAR

This comment has been minimized.

Copy link
Member Author

commented Jul 16, 2018

Landed in b3e93a9, 9c50199 🎉

@BridgeAR BridgeAR closed this Jul 16, 2018

targos added a commit that referenced this pull request Jul 31, 2018

test: improve inspect readability
The string escaping is hard to read. This changes all those escaped
strings to use double quotes instead so no escaping is necessary.

PR-URL: #21624
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>

@targos targos referenced this pull request Jul 31, 2018

Merged

v10.8.0 proposal #22040

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.