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

readline: inform line event about separator used #23929

Conversation

Sayanc93
Copy link

@Sayanc93 Sayanc93 commented Oct 27, 2018

This is an attempt to address #7952
and is a work in progress PR (as of now).
Would love some suggestions and guidance on the changes.
Tests and documentation updates are pending on this.

Any help on why I'm not able to run node test/parallel/test-readline-interface.js?
it fails with Error: Cannot find module 'internal/readline'

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

@nodejs-github-bot nodejs-github-bot added the readline Issues and PRs related to the built-in readline module. label Oct 27, 2018
@devsnek
Copy link
Member

devsnek commented Oct 27, 2018

to run the test use $ tools/test.py -J test/parallel/test-readline-interface.js.

pass separator as a parameter to the callback
alongwith line to inform line event about separator
used to break lines
fixes: nodejs#7952 (comment)
@Sayanc93 Sayanc93 force-pushed the inform-line-event-on-readline-about-endline-input branch from 9887ce2 to 687cdf5 Compare October 27, 2018 23:53
@Trott
Copy link
Member

Trott commented Nov 4, 2018

@nodejs/repl This could use some reviews.

@@ -290,14 +291,14 @@ Interface.prototype.question = function(query, cb) {
};


Interface.prototype._onLine = function(line) {
Interface.prototype._onLine = function(line, separator = '\n') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the default value here? Due to your changes it seems like the value is always set?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR Hey thanks for taking time for the review. I've mostly passed around the appropriate values in the argument in the places I saw this function was being invoked. The idea behind the default value is to not alter its behavior if its called with falsy values by mistake by any call site.

@@ -57,7 +57,8 @@ let StringDecoder;
const kHistorySize = 30;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
const lineEnding = /\r?\n|\r(?!\n)/;
const lineEndingWithSeparator = /(?<=\r?\n|\r(?!\n))/;
const lineEndingSeparatorInclusion = /[\r?\n|\r(?!\n)]$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regular expression seems to be faulty. Wrapping the values in the square brackets means that each character is checked for and the special characters loose their meaning and will be checked for as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the insight, I'll check and update

@@ -642,10 +652,10 @@ Interface.prototype.clearLine = function() {
};


Interface.prototype._line = function() {
Interface.prototype._line = function(separator = '\n') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest not to use a default value here and to be explicit in the call sites. Especially the call site that uses s.split(/\r\n|\n|\r/). In that case it would be best to make sure these are kept as before as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem, I'll revert the change. I was just being cautious about the bugs that might happen later if someone forgets to add the separator.

this._onLine(lineWithoutSeparator, separator);
} else {
this._onLine(_line, '\n');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken, each line has a separator here and the else case will not be reached besides potentially for the very last line. Instead of checking with a regular expression what is used, it would be significantly faster to just check the last two characters of each line. But in that case the line would still require to be sliced and that is extra overhead. It's probably faster to use a loop that checks for some special characters and to manually slice reach line.

@BridgeAR
Copy link
Member

BridgeAR commented Nov 4, 2018

It would be great to add a test with a file that contains mixed line endings.

@Sayanc93
Copy link
Author

Sayanc93 commented Nov 6, 2018

@BridgeAR Thanks for the review, this was a WIP branch, got caught up with other stuff. I'll push the changes with your suggestions soon.

@Trott Trott added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 6, 2018
@Trott
Copy link
Member

Trott commented Nov 21, 2018

Is this still being worked on?

@antsmartian
Copy link
Contributor

Ping @Sayanc93

@zero1five
Copy link
Contributor

That seems pretty good, it please let me know if I can help :) @Sayanc93

@BridgeAR
Copy link
Member

I am closing this due to long inactivity. Please feel free to reopen if this should be continued to work on.

@BridgeAR BridgeAR closed this Dec 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
readline Issues and PRs related to the built-in readline module. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants