Skip to content

Commit 11dda69

Browse files
committed
repl: refactor code for readability
PR-URL: nodejs#17919 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Lance Ball <lball@redhat.com>
1 parent 80988f9 commit 11dda69

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

lib/repl.js

+26-57
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,10 @@ function REPLServer(prompt,
412412
// Use stdin and stdout as the default streams if none were given
413413
stream = process;
414414
}
415-
if (stream.stdin && stream.stdout) {
416-
// We're given custom object with 2 streams, or the `process` object
417-
input = stream.stdin;
418-
output = stream.stdout;
419-
} else {
420-
// We're given a duplex readable/writable Stream, like a `net.Socket`
421-
input = stream;
422-
output = stream;
423-
}
415+
// We're given a duplex readable/writable Stream, like a `net.Socket`
416+
// or a custom object with 2 streams, or the `process` object
417+
input = stream.stdin || stream;
418+
output = stream.stdout || stream;
424419
}
425420

426421
self.inputStream = input;
@@ -770,7 +765,7 @@ REPLServer.prototype.createContext = function() {
770765
Object.defineProperty(context, 'console', {
771766
configurable: true,
772767
enumerable: true,
773-
get: () => _console
768+
value: _console
774769
});
775770

776771
var names = Object.getOwnPropertyNames(global);
@@ -1129,19 +1124,16 @@ function complete(line, callback) {
11291124
break;
11301125
}
11311126
}
1132-
} catch (e) {
1133-
// console.log("completion error walking prototype chain:" + e);
1134-
}
1127+
} catch (e) {}
11351128
}
11361129

11371130
if (memberGroups.length) {
11381131
for (i = 0; i < memberGroups.length; i++) {
1139-
completionGroups.push(memberGroups[i].map(function(member) {
1140-
return expr + '.' + member;
1141-
}));
1132+
completionGroups.push(
1133+
memberGroups[i].map((member) => `${expr}.${member}`));
11421134
}
11431135
if (filter) {
1144-
filter = expr + '.' + filter;
1136+
filter = `${expr}.${filter}`;
11451137
}
11461138
}
11471139

@@ -1162,9 +1154,8 @@ function complete(line, callback) {
11621154
if (completionGroups.length && filter) {
11631155
var newCompletionGroups = [];
11641156
for (i = 0; i < completionGroups.length; i++) {
1165-
group = completionGroups[i].filter(function(elem) {
1166-
return elem.indexOf(filter) === 0;
1167-
});
1157+
group = completionGroups[i]
1158+
.filter((elem) => elem.indexOf(filter) === 0);
11681159
if (group.length) {
11691160
newCompletionGroups.push(group);
11701161
}
@@ -1493,55 +1484,33 @@ function isCodeRecoverable(code) {
14931484

14941485
if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
14951486
current = null;
1496-
continue;
1497-
}
1498-
1499-
if (stringLiteral) {
1487+
} else if (stringLiteral) {
15001488
if (stringLiteral === current) {
15011489
stringLiteral = null;
15021490
}
1503-
continue;
1504-
} else {
1505-
if (isRegExpLiteral && current === '/') {
1506-
isRegExpLiteral = false;
1507-
continue;
1508-
}
1509-
1510-
if (isBlockComment && previous === '*' && current === '/') {
1511-
isBlockComment = false;
1512-
continue;
1513-
}
1514-
1515-
if (isSingleComment && current === '\n') {
1516-
isSingleComment = false;
1517-
continue;
1518-
}
1519-
1520-
if (isBlockComment || isRegExpLiteral || isSingleComment) continue;
1521-
1491+
} else if (isRegExpLiteral && current === '/') {
1492+
isRegExpLiteral = false;
1493+
} else if (isBlockComment && previous === '*' && current === '/') {
1494+
isBlockComment = false;
1495+
} else if (isSingleComment && current === '\n') {
1496+
isSingleComment = false;
1497+
} else if (!isBlockComment && !isRegExpLiteral && !isSingleComment) {
15221498
if (current === '/' && previous === '/') {
15231499
isSingleComment = true;
1524-
continue;
1525-
}
1526-
1527-
if (previous === '/') {
1500+
} else if (previous === '/') {
15281501
if (current === '*') {
15291502
isBlockComment = true;
1530-
} else if (
15311503
// Distinguish between a division operator and the start of a regex
15321504
// by examining the non-whitespace character that precedes the /
1533-
[null, '(', '[', '{', '}', ';'].includes(prevTokenChar)
1534-
) {
1505+
} else if ([null, '(', '[', '{', '}', ';'].includes(prevTokenChar)) {
15351506
isRegExpLiteral = true;
15361507
}
1537-
continue;
1508+
} else {
1509+
if (current.trim()) prevTokenChar = current;
1510+
if (current === '\'' || current === '"') {
1511+
stringLiteral = current;
1512+
}
15381513
}
1539-
1540-
if (current.trim()) prevTokenChar = current;
1541-
}
1542-
1543-
if (current === '\'' || current === '"') {
1544-
stringLiteral = current;
15451514
}
15461515
}
15471516

0 commit comments

Comments
 (0)