Skip to content

Commit 05cea67

Browse files
BridgeARtargos
authored andcommitted
repl: handle stage-3 language features properly
This adds stage-3 language features to acorn so that the REPL is able to parse these features properly. Otherwise these would cause SyntaxErrors. PR-URL: #27400 Fixes: #27391 Fixes: #25835 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 01d632d commit 05cea67

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

lib/internal/repl/utils.js

+46-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
'use strict';
22

33
const acorn = require('internal/deps/acorn/acorn/dist/acorn');
4+
const privateMethods =
5+
require('internal/deps/acorn-plugins/acorn-private-methods/index');
6+
const bigInt = require('internal/deps/acorn-plugins/acorn-bigint/index');
7+
const classFields =
8+
require('internal/deps/acorn-plugins/acorn-class-fields/index');
9+
const numericSeparator =
10+
require('internal/deps/acorn-plugins/acorn-numeric-separator/index');
11+
const staticClassFeatures =
12+
require('internal/deps/acorn-plugins/acorn-static-class-features/index');
413
const { tokTypes: tt, Parser: AcornParser } = acorn;
514

615
// If the error is that we've unexpectedly ended the input,
716
// then let the user try to recover by adding more input.
817
// Note: `e` (the original exception) is not used by the current implementation,
918
// but may be needed in the future.
1019
function isRecoverableError(e, code) {
20+
// For similar reasons as `defaultEval`, wrap expressions starting with a
21+
// curly brace with parenthesis. Note: only the open parenthesis is added
22+
// here as the point is to test for potentially valid but incomplete
23+
// expressions.
24+
if (/^\s*\{/.test(code) && isRecoverableError(e, `(${code}`)) return true;
25+
1126
let recoverable = false;
1227

1328
// Determine if the point of any error raised is at the end of the input.
@@ -26,34 +41,39 @@ function isRecoverableError(e, code) {
2641
// change these messages in the future, this will lead to a test
2742
// failure, indicating that this code needs to be updated.
2843
//
29-
const RecoverableParser = AcornParser.extend((Parser) => {
30-
return class extends Parser {
31-
nextToken() {
32-
super.nextToken();
33-
if (this.type === tt.eof) recoverable = true;
34-
}
35-
raise(pos, message) {
36-
switch (message) {
37-
case 'Unterminated template':
38-
case 'Unterminated comment':
39-
recoverable = true;
40-
break;
44+
const RecoverableParser = AcornParser
45+
.extend(
46+
privateMethods,
47+
bigInt,
48+
classFields,
49+
numericSeparator,
50+
staticClassFeatures,
51+
(Parser) => {
52+
return class extends Parser {
53+
nextToken() {
54+
super.nextToken();
55+
if (this.type === tt.eof)
56+
recoverable = true;
57+
}
58+
raise(pos, message) {
59+
switch (message) {
60+
case 'Unterminated template':
61+
case 'Unterminated comment':
62+
recoverable = true;
63+
break;
4164

42-
case 'Unterminated string constant':
43-
const token = this.input.slice(this.lastTokStart, this.pos);
44-
// See https://www.ecma-international.org/ecma-262/#sec-line-terminators
45-
recoverable = /\\(?:\r\n?|\n|\u2028|\u2029)$/.test(token);
46-
}
47-
super.raise(pos, message);
65+
case 'Unterminated string constant':
66+
const token = this.input.slice(this.lastTokStart, this.pos);
67+
// See https://www.ecma-international.org/ecma-262/#sec-line-terminators
68+
if (/\\(?:\r\n?|\n|\u2028|\u2029)$/.test(token)) {
69+
recoverable = true;
70+
}
71+
}
72+
super.raise(pos, message);
73+
}
74+
};
4875
}
49-
};
50-
});
51-
52-
// For similar reasons as `defaultEval`, wrap expressions starting with a
53-
// curly brace with parenthesis. Note: only the open parenthesis is added
54-
// here as the point is to test for potentially valid but incomplete
55-
// expressions.
56-
if (/^\s*\{/.test(code) && isRecoverableError(e, `(${code}`)) return true;
76+
);
5777

5878
// Try to parse the code with acorn. If the parse fails, ignore the acorn
5979
// error and return the recoverable status.

0 commit comments

Comments
 (0)