Skip to content

Commit e786926

Browse files
vsemozhetbytaddaleax
authored andcommitted
readline,repl,url,util: remove needless capturing
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 11f4562 commit e786926

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

lib/internal/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const IteratorPrototype = Object.getPrototypeOf(
5151
);
5252

5353
const unpairedSurrogateRe =
54-
/([^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
54+
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
5555
function toUSVString(val) {
5656
const str = `${val}`;
5757
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are

lib/readline.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ function commonPrefix(strings) {
541541
Interface.prototype._wordLeft = function() {
542542
if (this.cursor > 0) {
543543
var leading = this.line.slice(0, this.cursor);
544-
var match = leading.match(/([^\w\s]+|\w+|)\s*$/);
544+
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
545545
this._moveCursor(-match[0].length);
546546
}
547547
};
@@ -550,7 +550,7 @@ Interface.prototype._wordLeft = function() {
550550
Interface.prototype._wordRight = function() {
551551
if (this.cursor < this.line.length) {
552552
var trailing = this.line.slice(this.cursor);
553-
var match = trailing.match(/^(\s+|\W+|\w+)\s*/);
553+
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
554554
this._moveCursor(match[0].length);
555555
}
556556
};
@@ -577,7 +577,7 @@ Interface.prototype._deleteRight = function() {
577577
Interface.prototype._deleteWordLeft = function() {
578578
if (this.cursor > 0) {
579579
var leading = this.line.slice(0, this.cursor);
580-
var match = leading.match(/([^\w\s]+|\w+|)\s*$/);
580+
var match = leading.match(/(?:[^\w\s]+|\w+|)\s*$/);
581581
leading = leading.slice(0, leading.length - match[0].length);
582582
this.line = leading + this.line.slice(this.cursor, this.line.length);
583583
this.cursor = leading.length;
@@ -589,7 +589,7 @@ Interface.prototype._deleteWordLeft = function() {
589589
Interface.prototype._deleteWordRight = function() {
590590
if (this.cursor < this.line.length) {
591591
var trailing = this.line.slice(this.cursor);
592-
var match = trailing.match(/^(\s+|\W+|\w+)\s*/);
592+
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
593593
this.line = this.line.slice(0, this.cursor) +
594594
trailing.slice(match[0].length);
595595
this._refreshLine();

lib/repl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,9 @@ ArrayStream.prototype.writable = true;
679679
ArrayStream.prototype.resume = function() {};
680680
ArrayStream.prototype.write = function() {};
681681

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

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

lib/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function Url() {
5656

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

lib/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
797797
if (array) {
798798
str = str.replace(/\n/g, '\n ');
799799
} else {
800-
str = str.replace(/(^|\n)/g, '\n ');
800+
str = str.replace(/^|\n/g, '\n ');
801801
}
802802
}
803803
} else {
@@ -809,13 +809,13 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
809809
return str;
810810
}
811811
name = JSON.stringify('' + key);
812-
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
812+
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
813813
name = name.substr(1, name.length - 2);
814814
name = ctx.stylize(name, 'name');
815815
} else {
816816
name = name.replace(/'/g, "\\'")
817817
.replace(/\\"/g, '"')
818-
.replace(/(^"|"$)/g, "'")
818+
.replace(/^"|"$/g, "'")
819819
.replace(/\\\\/g, '\\');
820820
name = ctx.stylize(name, 'string');
821821
}

0 commit comments

Comments
 (0)