Skip to content

Commit

Permalink
readline,repl,url,util: remove needless capturing
Browse files Browse the repository at this point in the history
Use non-capturing grouping or remove capturing completely when:

* capturing is useless per se, e.g. in test() check;
* captured groups are not used afterwards at all;
* some of the later captured groups are not used afterwards.

PR-URL: #13718
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
vsemozhetbyt authored and addaleax committed Jun 21, 2017
1 parent 11f4562 commit e786926
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const IteratorPrototype = Object.getPrototypeOf(
);

const unpairedSurrogateRe =
/([^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
function toUSVString(val) {
const str = `${val}`;
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
Expand Down
8 changes: 4 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ function commonPrefix(strings) {
Interface.prototype._wordLeft = function() {
if (this.cursor > 0) {
var leading = this.line.slice(0, this.cursor);
var match = leading.match(/([^\w\s]+|\w+|)\s*$/);
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
this._moveCursor(-match[0].length);
}
};
Expand All @@ -550,7 +550,7 @@ Interface.prototype._wordLeft = function() {
Interface.prototype._wordRight = function() {
if (this.cursor < this.line.length) {
var trailing = this.line.slice(this.cursor);
var match = trailing.match(/^(\s+|\W+|\w+)\s*/);
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
this._moveCursor(match[0].length);
}
};
Expand All @@ -577,7 +577,7 @@ Interface.prototype._deleteRight = function() {
Interface.prototype._deleteWordLeft = function() {
if (this.cursor > 0) {
var leading = this.line.slice(0, this.cursor);
var match = leading.match(/([^\w\s]+|\w+|)\s*$/);
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
leading = leading.slice(0, leading.length - match[0].length);
this.line = leading + this.line.slice(this.cursor, this.line.length);
this.cursor = leading.length;
Expand All @@ -589,7 +589,7 @@ Interface.prototype._deleteWordLeft = function() {
Interface.prototype._deleteWordRight = function() {
if (this.cursor < this.line.length) {
var trailing = this.line.slice(this.cursor);
var match = trailing.match(/^(\s+|\W+|\w+)\s*/);
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
this.line = this.line.slice(0, this.cursor) +
trailing.slice(match[0].length);
this._refreshLine();
Expand Down
6 changes: 3 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,9 @@ ArrayStream.prototype.writable = true;
ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function() {};

const requireRE = /\brequire\s*\(['"](([\w@./-]+\/)?([\w@./-]*))/;
const requireRE = /\brequire\s*\(['"](([\w@./-]+\/)?(?:[\w@./-]*))/;
const simpleExpressionRE =
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
/(?:[a-zA-Z_$](?:\w|\$)*\.)*[a-zA-Z_$](?:\w|\$)*\.?$/;

function intFilter(item) {
// filters out anything not starting with A-Z, a-z, $ or _
Expand Down Expand Up @@ -752,7 +752,7 @@ function complete(line, callback) {
} else if (match = line.match(requireRE)) {
// require('...<Tab>')
const exts = Object.keys(this.context.require.extensions);
var indexRe = new RegExp('^index(' + exts.map(regexpEscape).join('|') +
var indexRe = new RegExp('^index(?:' + exts.map(regexpEscape).join('|') +
')$');
var versionedFileNamesRe = /-\d+\.\d+/;

Expand Down
2 changes: 1 addition & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function Url() {

// define these here so at least they only have to be
// compiled once on the first module load.
const protocolPattern = /^([a-z0-9.+-]+:)/i;
const protocolPattern = /^[a-z0-9.+-]+:/i;
const portPattern = /:[0-9]*$/;
const hostPattern = /^\/\/[^@/]+@[^@/]+/;

Expand Down
6 changes: 3 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
if (array) {
str = str.replace(/\n/g, '\n ');
} else {
str = str.replace(/(^|\n)/g, '\n ');
str = str.replace(/^|\n/g, '\n ');
}
}
} else {
Expand All @@ -809,13 +809,13 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
return str;
}
name = JSON.stringify('' + key);
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name');
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'")
.replace(/^"|"$/g, "'")
.replace(/\\\\/g, '\\');
name = ctx.stylize(name, 'string');
}
Expand Down

0 comments on commit e786926

Please sign in to comment.