repl: refactor code for readability
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
80988f9322
commit
11dda69a33
71
lib/repl.js
71
lib/repl.js
@ -412,15 +412,10 @@ function REPLServer(prompt,
|
|||||||
// Use stdin and stdout as the default streams if none were given
|
// Use stdin and stdout as the default streams if none were given
|
||||||
stream = process;
|
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`
|
// We're given a duplex readable/writable Stream, like a `net.Socket`
|
||||||
input = stream;
|
// or a custom object with 2 streams, or the `process` object
|
||||||
output = stream;
|
input = stream.stdin || stream;
|
||||||
}
|
output = stream.stdout || stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.inputStream = input;
|
self.inputStream = input;
|
||||||
@ -770,7 +765,7 @@ REPLServer.prototype.createContext = function() {
|
|||||||
Object.defineProperty(context, 'console', {
|
Object.defineProperty(context, 'console', {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
get: () => _console
|
value: _console
|
||||||
});
|
});
|
||||||
|
|
||||||
var names = Object.getOwnPropertyNames(global);
|
var names = Object.getOwnPropertyNames(global);
|
||||||
@ -1129,19 +1124,16 @@ function complete(line, callback) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {}
|
||||||
// console.log("completion error walking prototype chain:" + e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memberGroups.length) {
|
if (memberGroups.length) {
|
||||||
for (i = 0; i < memberGroups.length; i++) {
|
for (i = 0; i < memberGroups.length; i++) {
|
||||||
completionGroups.push(memberGroups[i].map(function(member) {
|
completionGroups.push(
|
||||||
return expr + '.' + member;
|
memberGroups[i].map((member) => `${expr}.${member}`));
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
if (filter) {
|
if (filter) {
|
||||||
filter = expr + '.' + filter;
|
filter = `${expr}.${filter}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1162,9 +1154,8 @@ function complete(line, callback) {
|
|||||||
if (completionGroups.length && filter) {
|
if (completionGroups.length && filter) {
|
||||||
var newCompletionGroups = [];
|
var newCompletionGroups = [];
|
||||||
for (i = 0; i < completionGroups.length; i++) {
|
for (i = 0; i < completionGroups.length; i++) {
|
||||||
group = completionGroups[i].filter(function(elem) {
|
group = completionGroups[i]
|
||||||
return elem.indexOf(filter) === 0;
|
.filter((elem) => elem.indexOf(filter) === 0);
|
||||||
});
|
|
||||||
if (group.length) {
|
if (group.length) {
|
||||||
newCompletionGroups.push(group);
|
newCompletionGroups.push(group);
|
||||||
}
|
}
|
||||||
@ -1493,57 +1484,35 @@ function isCodeRecoverable(code) {
|
|||||||
|
|
||||||
if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
|
if (previous === '\\' && (stringLiteral || isRegExpLiteral)) {
|
||||||
current = null;
|
current = null;
|
||||||
continue;
|
} else if (stringLiteral) {
|
||||||
}
|
|
||||||
|
|
||||||
if (stringLiteral) {
|
|
||||||
if (stringLiteral === current) {
|
if (stringLiteral === current) {
|
||||||
stringLiteral = null;
|
stringLiteral = null;
|
||||||
}
|
}
|
||||||
continue;
|
} else if (isRegExpLiteral && current === '/') {
|
||||||
} else {
|
|
||||||
if (isRegExpLiteral && current === '/') {
|
|
||||||
isRegExpLiteral = false;
|
isRegExpLiteral = false;
|
||||||
continue;
|
} else if (isBlockComment && previous === '*' && current === '/') {
|
||||||
}
|
|
||||||
|
|
||||||
if (isBlockComment && previous === '*' && current === '/') {
|
|
||||||
isBlockComment = false;
|
isBlockComment = false;
|
||||||
continue;
|
} else if (isSingleComment && current === '\n') {
|
||||||
}
|
|
||||||
|
|
||||||
if (isSingleComment && current === '\n') {
|
|
||||||
isSingleComment = false;
|
isSingleComment = false;
|
||||||
continue;
|
} else if (!isBlockComment && !isRegExpLiteral && !isSingleComment) {
|
||||||
}
|
|
||||||
|
|
||||||
if (isBlockComment || isRegExpLiteral || isSingleComment) continue;
|
|
||||||
|
|
||||||
if (current === '/' && previous === '/') {
|
if (current === '/' && previous === '/') {
|
||||||
isSingleComment = true;
|
isSingleComment = true;
|
||||||
continue;
|
} else if (previous === '/') {
|
||||||
}
|
|
||||||
|
|
||||||
if (previous === '/') {
|
|
||||||
if (current === '*') {
|
if (current === '*') {
|
||||||
isBlockComment = true;
|
isBlockComment = true;
|
||||||
} else if (
|
|
||||||
// Distinguish between a division operator and the start of a regex
|
// Distinguish between a division operator and the start of a regex
|
||||||
// by examining the non-whitespace character that precedes the /
|
// by examining the non-whitespace character that precedes the /
|
||||||
[null, '(', '[', '{', '}', ';'].includes(prevTokenChar)
|
} else if ([null, '(', '[', '{', '}', ';'].includes(prevTokenChar)) {
|
||||||
) {
|
|
||||||
isRegExpLiteral = true;
|
isRegExpLiteral = true;
|
||||||
}
|
}
|
||||||
continue;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
if (current.trim()) prevTokenChar = current;
|
if (current.trim()) prevTokenChar = current;
|
||||||
}
|
|
||||||
|
|
||||||
if (current === '\'' || current === '"') {
|
if (current === '\'' || current === '"') {
|
||||||
stringLiteral = current;
|
stringLiteral = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return stringLiteral ? lastChar === '\\' : isBlockComment;
|
return stringLiteral ? lastChar === '\\' : isBlockComment;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user