Skip to content

Commit f4f203e

Browse files
committed
clean up lint and unused code
1 parent 2ac4bcd commit f4f203e

File tree

3 files changed

+55
-50
lines changed

3 files changed

+55
-50
lines changed

src/io.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ class IO {
99
this.stdin = stdin;
1010
this.stdout = stdout;
1111

12-
this.buffer = ''; // used for capturing the current statement
12+
this.buffer = '';
13+
this.multilineBuffer = '';
1314
this.cursor = 0;
1415
this.prefix = '';
1516
this.suffix = '';
16-
this.multilineBuffer = ''; // for buffering the multiline statements
1717

1818
this._paused = false;
1919
this.transformBuffer = transformBuffer;
@@ -135,18 +135,14 @@ class IO {
135135
this.buffer = '';
136136
this.cursor = 0;
137137
this.history.unshift(b);
138-
this.multilineBuffer += b;
139-
// always buffer the line so that when we encounter multi-line
140-
// statements we can execute them as needed.
141-
const code = this.multilineBuffer;
142-
const result = await onLine(code);
143-
// online returns a Symbol when it sees a multi-line statement
144-
if (IO.kNeedsAnotherLine !== result) {
145-
this.stdout.write(`${result}\n`);
138+
const result = await onLine(this.multilineBuffer + b);
139+
if (result === IO.kNeedsAnotherLine) {
140+
this.multilineBuffer += b;
141+
this.setPrefix('... ');
142+
} else {
146143
this.multilineBuffer = '';
144+
this.stdout.write(`${result}\n`);
147145
this.setPrefix('> ');
148-
} else {
149-
this.setPrefix('... ');
150146
}
151147
this.unpause();
152148
} else {

src/recoverable.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22

33
const acorn = require('acorn');
4+
45
const { tokTypes: tt } = acorn;
56

67
// If the error is that we've unexpectedly ended the input,
78
// then let the user try to recover by adding more input.
8-
// Note: `e` (the original exception) is not used by the current implemention,
9-
// but may be needed in the future.
10-
function isRecoverableError(e, code) {
9+
function isRecoverableError(code) {
1110
let recoverable = false;
1211

1312
// Determine if the point of the any error raised is at the end of the input.
@@ -27,39 +26,46 @@ function isRecoverableError(e, code) {
2726
// failure, indicating that this code needs to be updated.
2827
//
2928
acorn.plugins.replRecoverable = (parser) => {
30-
parser.extend('nextToken', (nextToken) => {
31-
return function() {
32-
Reflect.apply(nextToken, this, []);
29+
parser.extend('nextToken', (nextToken) =>
30+
function handleNextToken() {
31+
if (this.type === tt.eof) {
32+
recoverable = true;
33+
}
3334

34-
if (this.type === tt.eof) recoverable = true;
35-
};
36-
});
35+
return Reflect.apply(nextToken, this, []);
36+
});
3737

38-
parser.extend('raise', (raise) => {
39-
return function(pos, message) {
38+
parser.extend('raise', (raise) =>
39+
function handleRaise(pos, message) {
4040
switch (message) {
4141
case 'Unterminated template':
4242
case 'Unterminated comment':
4343
case 'Unexpected end of input':
4444
recoverable = true;
4545
break;
4646

47-
case 'Unterminated string constant':
47+
case 'Unterminated string constant': {
4848
const token = this.input.slice(this.lastTokStart, this.pos);
4949
// see https://www.ecma-international.org/ecma-262/#sec-line-terminators
5050
recoverable = /\\(?:\r\n?|\n|\u2028|\u2029)$/.test(token);
51+
break;
52+
}
53+
54+
default:
55+
break;
5156
}
5257

53-
Reflect.apply(raise, this, [pos, message]);
54-
};
55-
});
58+
return Reflect.apply(raise, this, [pos, message]);
59+
});
5660
};
5761

5862
// For similar reasons as `defaultEval`, wrap expressions starting with a
5963
// curly brace with parenthesis. Note: only the open parenthesis is added
6064
// here as the point is to test for potentially valid but incomplete
6165
// expressions.
62-
if (/^\s*\{/.test(code) && isRecoverableError(e, `(${code}`)) return true;
66+
if (/^\s*\{/.test(code) && isRecoverableError(`(${code}`)) {
67+
return true;
68+
}
6369

6470
// Try to parse the code with acorn. If the parse fails, ignore the acorn
6571
// error and return the recoverable status.
@@ -70,10 +76,9 @@ function isRecoverableError(e, code) {
7076
// but Acorn detected no issue. Presume that additional text won't
7177
// address this issue.
7278
return false;
73-
} catch (e) {
79+
} catch {
7480
return recoverable;
7581
}
7682
}
7783

78-
module.exports = isRecoverableError
79-
84+
module.exports = isRecoverableError;

src/repl.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ Error.prepareStackTrace = (err, frames) => {
1313
const cut = frames.findIndex((f) =>
1414
!f.getFileName() && !f.getFunctionName()) + 1;
1515

16-
return `${err}
17-
at ${frames.slice(0, cut).join('\n at ')}`;
16+
frames = frames.slice(0, cut);
17+
18+
if (frames.length === 0) {
19+
return `${err}`;
20+
}
21+
22+
return `${err}\n at ${frames.join('\n at ')}`;
1823
};
1924

2025
const inspect = (v) => util.inspect(v, { colors: true, showProxy: 2 });
@@ -103,32 +108,31 @@ Prototype REPL - https://github.com/nodejs/repl`,
103108
awaited = true;
104109
}
105110
}
111+
106112
const evaluateResult = await this.eval(line, awaited);
107113

108114
if (evaluateResult.exceptionDetails) {
109-
// lets try for recovering
110-
const result = isRecoverableError(evaluateResult.exceptionDetails.exception, line);
111-
if (result) {
115+
if (isRecoverableError(line)) {
112116
return IO.kNeedsAnotherLine;
113-
} else {
114-
// we tried our best - throw error
115-
await this.callFunctionOn(
116-
(err) => {
117-
global.REPL.lastError = err;
118-
},
119-
evaluateResult.exceptionDetails.exception,
120-
);
121-
return inspect(global.REPL.lastError);
122117
}
123-
} else {
118+
124119
await this.callFunctionOn(
125-
(result) => {
126-
global.REPL.last = result;
120+
(err) => {
121+
global.REPL.lastError = err;
127122
},
128-
evaluateResult.result,
123+
evaluateResult.exceptionDetails.exception,
129124
);
130-
return inspect(global.REPL.last);
125+
126+
return inspect(global.REPL.lastError);
131127
}
128+
129+
await this.callFunctionOn(
130+
(result) => {
131+
global.REPL.last = result;
132+
},
133+
evaluateResult.result,
134+
);
135+
return inspect(global.REPL.last);
132136
}
133137

134138
async onAutocomplete(buffer) {

0 commit comments

Comments
 (0)