diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index f1949477c37..831a6e6ef6b 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -458,6 +458,11 @@ static Optional expression_identifier(Expression const& expression) return literal.value().to_string_without_side_effects().to_byte_string(); } + if (expression.is_string_literal()) { + auto const& literal = static_cast(expression); + return ByteString::formatted("'{}'", literal.value()); + } + if (expression.is_member_expression()) { auto const& member_expression = static_cast(expression); StringBuilder builder; diff --git a/Userland/Libraries/LibJS/Tests/null-or-undefined-access.js b/Userland/Libraries/LibJS/Tests/null-or-undefined-access.js index 939a9bd33fe..fd51cc15219 100644 --- a/Userland/Libraries/LibJS/Tests/null-or-undefined-access.js +++ b/Userland/Libraries/LibJS/Tests/null-or-undefined-access.js @@ -37,6 +37,20 @@ test("null/undefined object key", () => { TypeError, `Cannot access property "baz" on ${value} object "foo.bar"` ); + + expect(() => { + foo["bar"].baz; + }).toThrowWithMessage( + TypeError, + `Cannot access property "baz" on ${value} object "foo['bar']"` + ); + + expect(() => { + foo["bar"].baz = 1; + }).toThrowWithMessage( + TypeError, + `Cannot access property "baz" on ${value} object "foo['bar']"` + ); }); });