Skip to content

Commit 55f7bbb

Browse files
BridgeARMylesBorins
authored andcommitted
repl: refactor code for readability
Backport-PR-URL: #19244 PR-URL: #17919 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Weijia Wang <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Lance Ball <[email protected]>
1 parent 6997af7 commit 55f7bbb

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

‎lib/repl.js‎

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,10 @@ function REPLServer(prompt,
320320
// Use stdin and stdout as the default streams if none were given
321321
stream=process;
322322
}
323-
if(stream.stdin&&stream.stdout){
324-
// We're given custom object with 2 streams, or the `process` object
325-
input=stream.stdin;
326-
output=stream.stdout;
327-
}else{
328-
// We're given a duplex readable/writable Stream, like a `net.Socket`
329-
input=stream;
330-
output=stream;
331-
}
323+
// We're given a duplex readable/writable Stream, like a `net.Socket`
324+
// or a custom object with 2 streams, or the `process` object
325+
input=stream.stdin||stream;
326+
output=stream.stdout||stream;
332327
}
333328

334329
self.inputStream=input;
@@ -663,7 +658,7 @@ REPLServer.prototype.createContext = function(){
663658
Object.defineProperty(context,'console',{
664659
configurable: true,
665660
enumerable: true,
666-
get: ()=>_console
661+
value:_console
667662
});
668663

669664
varnames=Object.getOwnPropertyNames(global);
@@ -1035,19 +1030,16 @@ function complete(line, callback){
10351030
break;
10361031
}
10371032
}
1038-
}catch(e){
1039-
// console.log("completion error walking prototype chain:" + e);
1040-
}
1033+
}catch(e){}
10411034
}
10421035

10431036
if(memberGroups.length){
10441037
for(i=0;i<memberGroups.length;i++){
1045-
completionGroups.push(memberGroups[i].map(function(member){
1046-
returnexpr+'.'+member;
1047-
}));
1038+
completionGroups.push(
1039+
memberGroups[i].map((member)=>`${expr}.${member}`));
10481040
}
10491041
if(filter){
1050-
filter=expr+'.'+filter;
1042+
filter=`${expr}.${filter}`;
10511043
}
10521044
}
10531045

@@ -1068,9 +1060,8 @@ function complete(line, callback){
10681060
if(completionGroups.length&&filter){
10691061
varnewCompletionGroups=[];
10701062
for(i=0;i<completionGroups.length;i++){
1071-
group=completionGroups[i].filter(function(elem){
1072-
returnelem.indexOf(filter)===0;
1073-
});
1063+
group=completionGroups[i]
1064+
.filter((elem)=>elem.indexOf(filter)===0);
10741065
if(group.length){
10751066
newCompletionGroups.push(group);
10761067
}
@@ -1400,55 +1391,33 @@ function isCodeRecoverable(code){
14001391

14011392
if(previous==='\\'&&(stringLiteral||isRegExpLiteral)){
14021393
current=null;
1403-
continue;
1404-
}
1405-
1406-
if(stringLiteral){
1394+
}elseif(stringLiteral){
14071395
if(stringLiteral===current){
14081396
stringLiteral=null;
14091397
}
1410-
continue;
1411-
}else{
1412-
if(isRegExpLiteral&&current==='/'){
1413-
isRegExpLiteral=false;
1414-
continue;
1415-
}
1416-
1417-
if(isBlockComment&&previous==='*'&&current==='/'){
1418-
isBlockComment=false;
1419-
continue;
1420-
}
1421-
1422-
if(isSingleComment&&current==='\n'){
1423-
isSingleComment=false;
1424-
continue;
1425-
}
1426-
1427-
if(isBlockComment||isRegExpLiteral||isSingleComment)continue;
1428-
1398+
}elseif(isRegExpLiteral&&current==='/'){
1399+
isRegExpLiteral=false;
1400+
}elseif(isBlockComment&&previous==='*'&&current==='/'){
1401+
isBlockComment=false;
1402+
}elseif(isSingleComment&&current==='\n'){
1403+
isSingleComment=false;
1404+
}elseif(!isBlockComment&&!isRegExpLiteral&&!isSingleComment){
14291405
if(current==='/'&&previous==='/'){
14301406
isSingleComment=true;
1431-
continue;
1432-
}
1433-
1434-
if(previous==='/'){
1407+
}elseif(previous==='/'){
14351408
if(current==='*'){
14361409
isBlockComment=true;
1437-
}elseif(
14381410
// Distinguish between a division operator and the start of a regex
14391411
// by examining the non-whitespace character that precedes the /
1440-
[null,'(','[','{','}',''].includes(prevTokenChar)
1441-
){
1412+
}elseif([null,'(','[','{','}',''].includes(prevTokenChar)){
14421413
isRegExpLiteral=true;
14431414
}
1444-
continue;
1415+
}else{
1416+
if(current.trim())prevTokenChar=current;
1417+
if(current==='\''||current==='"'){
1418+
stringLiteral=current;
1419+
}
14451420
}
1446-
1447-
if(current.trim())prevTokenChar=current;
1448-
}
1449-
1450-
if(current==='\''||current==='"'){
1451-
stringLiteral=current;
14521421
}
14531422
}
14541423

0 commit comments

Comments
(0)