diff options
author | 2023-04-15 02:32:11 -0700 | |
---|---|---|
committer | 2023-04-15 02:32:11 -0700 | |
commit | 530f5ef82cfe5620a3cc5ce71c4a0fabe16b6593 (patch) | |
tree | 7988be085944ba96ad78fb21ae9e9634b85af0f6 | |
parent | 9e5efe61a38a43181cff0a9887bc947fc5717110 (diff) | |
download | bun-530f5ef82cfe5620a3cc5ce71c4a0fabe16b6593.tar.gz bun-530f5ef82cfe5620a3cc5ce71c4a0fabe16b6593.tar.zst bun-530f5ef82cfe5620a3cc5ce71c4a0fabe16b6593.zip |
Add error for assigning to a constant
-rw-r--r-- | src/js_parser.zig | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 3ce9549d9..be62d36c2 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -14558,14 +14558,36 @@ fn NewParser_( e_.must_keep_due_to_with_stmt = result.is_inside_with_scope; e_.ref = result.ref; - // TODO: fix the underyling cause here - // The problem seems to be that result.ref.innerIndex() is not always set. - // Handle assigning to a constant - // if (in.assign_target != .none and p.symbols.items[result.ref.innerIndex()].kind == .cconst) { - // const r = js_lexer.rangeOfIdentifier(p.source, expr.loc); - // p.log.addRangeErrorFmt(p.source, r, p.allocator, "Cannot assign to {s} because it is a constant", .{name}) catch unreachable; - // } + if (in.assign_target != .none and p.symbols.items[result.ref.innerIndex()].kind == .cconst) { + const r = js_lexer.rangeOfIdentifier(p.source, expr.loc); + var notes = p.allocator.alloc(logger.Data, 1) catch unreachable; + notes[0] = logger.Data{ + .text = std.fmt.allocPrint(p.allocator, "The symbol \"{s}\" was declared a constant here:", .{name}) catch unreachable, + .location = logger.Location.init_or_nil(p.source, js_lexer.rangeOfIdentifier(p.source, result.declare_loc.?)), + }; + + const is_error = p.const_values.contains(result.ref) or p.options.bundle; + switch (is_error) { + true => p.log.addRangeErrorFmtWithNotes( + p.source, + r, + p.allocator, + notes, + "Cannot assign to \"{s}\" because it is a constant", + .{name}, + ) catch unreachable, + + false => p.log.addRangeErrorFmtWithNotes( + p.source, + r, + p.allocator, + notes, + "This assignment will throw because \"{s}\" is a constant", + .{name}, + ) catch unreachable, + } + } var original_name: ?string = null; |