From e786926de95850235e554624519ef5109cd2dd83 Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Fri, 16 Jun 2017 20:14:58 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/13718 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann Reviewed-By: Luigi Pinca --- lib/internal/url.js | 2 +- lib/readline.js | 8 ++++---- lib/repl.js | 6 +++--- lib/url.js | 2 +- lib/util.js | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 14c22e4ff467b6..7d7c79149a5c4f 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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 diff --git a/lib/readline.js b/lib/readline.js index 60864f40afdbc1..133ef1cf3f6c6e 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -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); } }; @@ -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); } }; @@ -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; @@ -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(); diff --git a/lib/repl.js b/lib/repl.js index 90ca02027a68ce..cf19e6f1999a07 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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 _ @@ -752,7 +752,7 @@ function complete(line, callback) { } else if (match = line.match(requireRE)) { // require('...') 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+/; diff --git a/lib/url.js b/lib/url.js index 6227c8a057a4ce..633d3a40a493bc 100644 --- a/lib/url.js +++ b/lib/url.js @@ -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 = /^\/\/[^@/]+@[^@/]+/; diff --git a/lib/util.js b/lib/util.js index 81e3a42f053a6d..c998888e8e4798 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 { @@ -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'); }