js: Add option to disable quotes around strings

This commit is contained in:
Lucien Fiorini 2025-05-29 20:01:51 +02:00 committed by Andrew Kaster
commit 81e84c8273
Notes: github-actions[bot] 2025-05-29 23:34:38 +00:00
3 changed files with 6 additions and 3 deletions

View file

@ -1038,7 +1038,7 @@ ErrorOr<void> print_value(JS::PrintContext& print_context, JS::Value value, Hash
else if (value.is_undefined()) else if (value.is_undefined())
TRY(js_out(print_context, "\033[34;1m")); TRY(js_out(print_context, "\033[34;1m"));
if (value.is_string()) if (value.is_string() && !print_context.disable_string_quotes)
TRY(js_out(print_context, "\"")); TRY(js_out(print_context, "\""));
else if (value.is_negative_zero()) else if (value.is_negative_zero())
TRY(js_out(print_context, "-")); TRY(js_out(print_context, "-"));
@ -1049,7 +1049,7 @@ ErrorOr<void> print_value(JS::PrintContext& print_context, JS::Value value, Hash
else else
TRY(js_out(print_context, "{}", contents)); TRY(js_out(print_context, "{}", contents));
if (value.is_string()) if (value.is_string() && !print_context.disable_string_quotes)
TRY(js_out(print_context, "\"")); TRY(js_out(print_context, "\""));
TRY(js_out(print_context, "\033[0m")); TRY(js_out(print_context, "\033[0m"));
return {}; return {};

View file

@ -17,6 +17,7 @@ struct PrintContext {
JS::VM& vm; JS::VM& vm;
Stream& stream; Stream& stream;
bool strip_ansi { false }; bool strip_ansi { false };
bool disable_string_quotes { false };
}; };
ErrorOr<void> print(JS::Value value, PrintContext&); ErrorOr<void> print(JS::Value value, PrintContext&);

View file

@ -85,6 +85,7 @@ static bool s_dump_ast = false;
static bool s_as_module = false; static bool s_as_module = false;
static bool s_print_last_result = false; static bool s_print_last_result = false;
static bool s_strip_ansi = false; static bool s_strip_ansi = false;
static bool s_disable_string_quotes = false;
static bool s_disable_source_location_hints = false; static bool s_disable_source_location_hints = false;
#if !defined(AK_OS_WINDOWS) #if !defined(AK_OS_WINDOWS)
static RefPtr<Line::Editor> s_editor; static RefPtr<Line::Editor> s_editor;
@ -96,7 +97,7 @@ static int s_exit_code = 0;
static ErrorOr<void> print(JS::Value value, Stream& stream) static ErrorOr<void> print(JS::Value value, Stream& stream)
{ {
JS::PrintContext print_context { .vm = *g_vm, .stream = stream, .strip_ansi = s_strip_ansi }; JS::PrintContext print_context { .vm = *g_vm, .stream = stream, .strip_ansi = s_strip_ansi, .disable_string_quotes = s_disable_string_quotes };
return JS::print(value, print_context); return JS::print(value, print_context);
} }
@ -788,6 +789,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(s_disable_source_location_hints, "Disable source location hints", "disable-source-location-hints", 'h'); args_parser.add_option(s_disable_source_location_hints, "Disable source location hints", "disable-source-location-hints", 'h');
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g'); args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's'); args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
args_parser.add_option(s_disable_string_quotes, "Disable quotes around strings", "disable-string-quotes", {});
args_parser.add_option(disable_debug_printing, "Disable debug output", "disable-debug-output", {}); args_parser.add_option(disable_debug_printing, "Disable debug output", "disable-debug-output", {});
args_parser.add_option(evaluate_script, "Evaluate argument as a script", "evaluate", 'c', "script"); args_parser.add_option(evaluate_script, "Evaluate argument as a script", "evaluate", 'c', "script");
args_parser.add_option(use_test262_global, "Use test262 global ($262)", "use-test262-global", {}); args_parser.add_option(use_test262_global, "Use test262 global ($262)", "use-test262-global", {});