Skip to content

Commit 7b03d68

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 38253af commit 7b03d68

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

lib/internal/url.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
798798
if (array) {
799799
str = str.replace(/\n/g, '\n ');
800800
} else {
801-
str = str.replace(/(^|\n)/g, '\n ');
801+
str = str.replace(/^|\n/g, '\n ');
802802
}
803803
}
804804
} else {
@@ -810,13 +810,13 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
810810
return str;
811811
}
812812
name = JSON.stringify('' + key);
813-
if (/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/.test(name)) {
813+
if (/^"[a-zA-Z_][a-zA-Z_0-9]*"$/.test(name)) {
814814
name = name.substr(1, name.length - 2);
815815
name = ctx.stylize(name, 'name');
816816
} else {
817817
name = name.replace(/'/g, "\\'")
818818
.replace(/\\"/g, '"')
819-
.replace(/(^"|"$)/g, "'")
819+
.replace(/^"|"$/g, "'")
820820
.replace(/\\\\/g, '\\');
821821
name = ctx.stylize(name, 'string');
822822
}

0 commit comments

Comments
 (0)