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 <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>
1 parent 11f4562 commit e786926

5 files changed

Lines changed: 12 additions & 12 deletions

File tree

‎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
constunpairedSurrogateRe=
54-
/([^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
54+
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
5555
functiontoUSVString(val){
5656
conststr=`${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
varleading=this.line.slice(0,this.cursor);
544-
varmatch=leading.match(/([^\w\s]+|\w+|)\s*$/);
544+
varmatch=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
vartrailing=this.line.slice(this.cursor);
553-
varmatch=trailing.match(/^(\s+|\W+|\w+)\s*/);
553+
varmatch=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
varleading=this.line.slice(0,this.cursor);
580-
varmatch=leading.match(/([^\w\s]+|\w+|)\s*$/);
580+
varmatch=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
vartrailing=this.line.slice(this.cursor);
592-
varmatch=trailing.match(/^(\s+|\W+|\w+)\s*/);
592+
varmatch=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-
constrequireRE=/\brequire\s*\(['"](([\w@./-]+\/)?([\w@./-]*))/;
682+
constrequireRE=/\brequire\s*\(['"](([\w@./-]+\/)?(?:[\w@./-]*))/;
683683
constsimpleExpressionRE=
684-
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
684+
/(?:[a-zA-Z_$](?:\w|\$)*\.)*[a-zA-Z_$](?:\w|\$)*\.?$/;
685685

686686
functionintFilter(item){
687687
// filters out anything not starting with A-Z, a-z, $ or _
@@ -752,7 +752,7 @@ function complete(line, callback) {
752752
}elseif(match=line.match(requireRE)){
753753
// require('...<Tab>')
754754
constexts=Object.keys(this.context.require.extensions);
755-
varindexRe=newRegExp('^index('+exts.map(regexpEscape).join('|')+
755+
varindexRe=newRegExp('^index(?:'+exts.map(regexpEscape).join('|')+
756756
')$');
757757
varversionedFileNamesRe=/-\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-
constprotocolPattern=/^([a-z0-9.+-]+:)/i;
59+
constprotocolPattern=/^[a-z0-9.+-]+:/i;
6060
constportPattern=/:[0-9]*$/;
6161
consthostPattern=/^\/\/[^@/]+@[^@/]+/;
6262

‎lib/util.js‎

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