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

lint: wrap long lines that include RegExp when possible #14586

Closed
8 tasks done
refack opened this issue Aug 1, 2017 · 9 comments
Closed
8 tasks done

lint: wrap long lines that include RegExp when possible #14586

refack opened this issue Aug 1, 2017 · 9 comments
Labels
test Issues and PRs related to the tests.

Comments

@refack refack added good first issue Issues that are suitable for first-time contributors. test Issues and PRs related to the tests. labels Aug 1, 2017
@thelostone-mc
Copy link
Contributor

Calling dibs if this hasn't been taken yet ^_^

@thelostone-mc
Copy link
Contributor

@refack Would this be good way to go about it ? Or did you have a better solution in mind for
https://github.com/nodejs/node/blob/master/test/parallel/test-whatwg-url-properties.js#L47

/TypeError: Cannot set property origin of \[object URL\] which has only a getter$/

to

new RegExp([
   'TypeError:',
   'Cannot set property origin of \\[object URL\\] which has only a getter$'
].join(' '))

@refack
Copy link
Contributor Author

refack commented Aug 2, 2017

to

new RegExp([
   'TypeError:',
   'Cannot set property origin of \\[object URL\\] which has only a getter$'
].join(' '))

The main motivation is the keep thing as readable as possible.
So... The general idea is not to use new RegExp() unless there are actual variables involved.
But the case you pointed out could be "mitigated" by wrapping the other parameters
from:

assert.throws(() => url.origin = 'http://foo.bar.com:22',
              /TypeError: Cannot set property origin of \[object URL\] which has only a getter$/);
assert.throws(
  () => url.origin = 'http://foo.bar.com:22',
  /TypeError: Cannot set property origin of \[object URL\] which has only a getter$/
);

We do end up with a line that 85 chars long, but it's still better than before.

But in this specific case since assert.throws also accepts a string as is second argument, and does a simple string compare, which is semantically the same as an anchored RegExp (/^xxx$/), you could compose a string literal

@refack refack added the wip Issues and PRs that are still a work in progress. label Aug 2, 2017
@vsemozhetbyt
Copy link
Contributor

@refack We do not advise strings as error arguments here: https://nodejs.org/api/assert.html#assert_assert_throws_block_error_message (see notes in the chapter end).

@Trott
Copy link
Member

Trott commented Aug 2, 2017

But in this specific case since assert.throws also accepts a string as is second argument, and does a simple string compare,

That is wrong (although, to be clear, the API is at fault, not you). A string as the second argument will be the message printed if the code does not throw. Avoid it entirely because everyone makes this mistake. It is an unfortunate legacy issue in the API.

@Trott
Copy link
Member

Trott commented Aug 2, 2017

IMO https://github.com/nodejs/node/blob/master/test/parallel/test-whatwg-url-properties.js#L47 could be left as is and removed from the list. If you want to marginally improve it, then the indentation modification option suggested by @refack is the way to go:

assert.throws(
  () => url.origin = 'http://foo.bar.com:22',
  /TypeError: Cannot set property origin of \[object URL\] which has only a getter$/
);

You could also consider replacing the regexp with a function, but going that route solely to avoid some line-wrapping seems misguided to me.

@refack
Copy link
Contributor Author

refack commented Aug 2, 2017

(mixed up assert.throws and common.expectsError. P.S. expectsError is not suited here since it's meant to assert Errors that have a code field.)

IMHO wrapping will be the best, and also if we're touching this I believe the RegExp should be anchored for start of line (/^xxx) as well (if the test still passes).

@thelostone-mc
Copy link
Contributor

@refack
Doesn't make these into constants as it's a custom message & further wrapping isn't possible :

Scnearios where the string can be made into a constant but that would make matters worse as defining the constant would hog more characters than the current state :

And when the text is already a constant and exceeds the limit, can't do anything about it, can we?

Yes I'm aware this looks like a long list of complaints but I'm just voicing out my thoughts.
and yup anchoring the start of the RegExp with ^ is definitely happening ^_^

@refack
Copy link
Contributor Author

refack commented Aug 3, 2017

@adityaanandmc for the first two and last two, IMHO simply wrapping will look nicer:

const expected = /^TypeError: "buffer" argument must be a string, Buffer, TypedArray, or DataView$/;
...
    {
      client: client_unix,
      send: '(function(a, a, b) { "use strict"; return a + b + c; })()',
      expect: /\bSyntaxError: Duplicate parameter name not allowed in this context/
    },

to:

const expected =
  /^TypeError: "buffer" argument must be a string, Buffer, TypedArray, or DataView$/;
...
    {
      client: client_unix,
      send: '(function(a, a, b) { "use strict"; return a + b + c; })()',
      expect:
       /\bSyntaxError: Duplicate parameter name not allowed in this context/
    },

Same trick can be done for https://github.com/nodejs/node/blob/master/test/parallel/test-v8-serdes.js#L143 even just before the block scope to save two indentation chars (but add a comment why it's there).

This issue turns out to be an exercise in creative typography 😉

@refack refack added mentor-available and removed good first issue Issues that are suitable for first-time contributors. labels Aug 3, 2017
thelostone-mc added a commit to thelostone-mc/node that referenced this issue Aug 4, 2017
Format commit wrapping lines containing RegEx and exceeding 80
chars.

Fixes: nodejs#14586
@refack refack closed this as completed in ad664ea Aug 7, 2017
@refack refack removed wip Issues and PRs that are still a work in progress. mentor-available labels Aug 7, 2017
addaleax pushed a commit that referenced this issue Aug 10, 2017
Format commit wrapping lines containing RegEx and exceeding 80
chars.

PR-URL: #14607
Fixes: #14586
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants