Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
repl: refactor code for readability
PR-URL: #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>
  • Loading branch information
BridgeAR committed Mar 21, 2018
commit 6086e718035d7806c70cb769f5a2c6ab0483641d
83 changes: 26 additions & 57 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,10 @@ function REPLServer(prompt,
// Use stdin and stdout as the default streams if none were given
stream = process;
}
if (stream.stdin && stream.stdout) {
// We're given custom object with 2 streams, or the `process` object
input = stream.stdin;
output = stream.stdout;
} else {
// We're given a duplex readable/writable Stream, like a `net.Socket`
input = stream;
output = stream;
}
// We're given a duplex readable/writable Stream, like a `net.Socket`
// or a custom object with 2 streams, or the `process` object
input = stream.stdin || stream;
output = stream.stdout || stream;
}

self.inputStream = input;
Expand Down Expand Up @@ -663,7 +658,7 @@ REPLServer.prototype.createContext = function() {
Object.defineProperty(context, 'console', {
configurable: true,
enumerable: true,
get: () => _console
value: _console
});

var names = Object.getOwnPropertyNames(global);
Expand Down Expand Up @@ -1035,19 +1030,16 @@ function complete(line, callback) {
break;
}
}
} catch (e) {
// console.log("completion error walking prototype chain:" + e);
}
} catch (e) {}
}

if (memberGroups.length) {
for (i = 0; i < memberGroups.length; i++) {
completionGroups.push(memberGroups[i].map(function(member) {
return expr + '.' + member;
}));
completionGroups.push(
memberGroups[i].map((member) => `${expr}.${member}`));
}
if (filter) {
filter = expr + '.' + filter;
filter = `${expr}.${filter}`;
}
}

Expand All @@ -1068,9 +1060,8 @@ function complete(line, callback) {
if (completionGroups.length && filter) {
var newCompletionGroups = [];
for (i = 0; i < completionGroups.length; i++) {
group = completionGroups[i].filter(function(elem) {
return elem.indexOf(filter) === 0;
});
group = completionGroups[i]
.filter((elem) => elem.indexOf(filter) === 0);
if (group.length) {
newCompletionGroups.push(group);
}
Expand Down Expand Up @@ -1400,55 +1391,33 @@ function isCodeRecoverable(code) {

if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
current = null;
continue;
}

if (stringLiteral) {
} else if (stringLiteral) {
if (stringLiteral === current) {
stringLiteral = null;
}
continue;
} else {
if (isRegExpLiteral && current === '/') {
isRegExpLiteral = false;
continue;
}

if (isBlockComment && previous === '*' && current === '/') {
isBlockComment = false;
continue;
}

if (isSingleComment && current === '\n') {
isSingleComment = false;
continue;
}

if (isBlockComment || isRegExpLiteral || isSingleComment) continue;

} else if (isRegExpLiteral && current === '/') {
isRegExpLiteral = false;
} else if (isBlockComment && previous === '*' && current === '/') {
isBlockComment = false;
} else if (isSingleComment && current === '\n') {
isSingleComment = false;
} else if (!isBlockComment && !isRegExpLiteral && !isSingleComment) {
if (current === '/' && previous === '/') {
isSingleComment = true;
continue;
}

if (previous === '/') {
} else if (previous === '/') {
if (current === '*') {
isBlockComment = true;
} else if (
// Distinguish between a division operator and the start of a regex
// by examining the non-whitespace character that precedes the /
[null, '(', '[', '{', '}', ';'].includes(prevTokenChar)
) {
} else if ([null, '(', '[', '{', '}', ';'].includes(prevTokenChar)) {
isRegExpLiteral = true;
}
continue;
} else {
if (current.trim()) prevTokenChar = current;
if (current === '\'' || current === '"') {
stringLiteral = current;
}
}

if (current.trim()) prevTokenChar = current;
}

if (current === '\'' || current === '"') {
stringLiteral = current;
}
}

Expand Down