aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/js_parser.zig15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 9efdf4c14..0ea126be8 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -2335,6 +2335,7 @@ pub const Parser = struct {
fn _parse(self: *Parser, comptime ParserType: type) !js_ast.Result {
var p: ParserType = undefined;
+ const orig_error_count = self.log.errors;
try ParserType.init(self.allocator, self.log, self.source, self.define, self.lexer, self.options, &p);
p.should_fold_numeric_constants = self.options.features.should_fold_numeric_constants;
defer p.lexer.deinit();
@@ -2364,6 +2365,15 @@ pub const Parser = struct {
// June 4: "Rest of this took: 8003000"
const stmts = try p.parseStmtsUpTo(js_lexer.T.t_end_of_file, &opts);
+ // Halt parsing right here if there were any errors
+ // This fixes various conditions that would cause crashes due to the AST being in an invalid state while visiting
+ // In a number of situations, we continue to parsing despite errors so that we can report more errors to the user
+ // Example where NOT halting causes a crash: A TS enum with a number literal as a member name
+ // https://discord.com/channels/876711213126520882/876711213126520885/1039325382488371280
+ if (self.log.errors > orig_error_count) {
+ return error.SyntaxError;
+ }
+
try p.prepareForVisitPass();
// ESM is always strict mode. I don't think we need this.
@@ -2426,6 +2436,11 @@ pub const Parser = struct {
}
}
+ // If there were errors while visiting, also halt here
+ if (self.log.errors > orig_error_count) {
+ return error.SyntaxError;
+ }
+
const uses_dirname = p.symbols.items[p.dirname_ref.innerIndex()].use_count_estimate > 0;
const uses_filename = p.symbols.items[p.filename_ref.innerIndex()].use_count_estimate > 0;