I noticed a pattern in linter where it creates an Allocator, creates an AstBuilder, uses that to generate a StringLiteral, and then passes the StringLiteral to Codegen to print it - by looks of things, it's doing all that just to escape a string. Surely there's a simpler way? Maybe oxc_codegen could just expose the function which escapes strings, independent of all the AST stuff?
Originally posted by @overlookmotel in #23730 (comment)
rg "expression_string_literal" crates/oxc_linter
crates/oxc_linter/src/rules/react/jsx_curly_brace_presence.rs
635: fix.print_expression(&ast_builder.expression_string_literal(
665: replace.print_expression(&ast_builder.expression_string_literal(
705: replace.print_expression(&ast_builder.expression_string_literal(
crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs
97: codegen.print_expression(&ast.expression_string_literal(
crates/oxc_linter/src/rules/unicorn/prefer_keyboard_event_key.rs
423: codegen.print_expression(&ast.expression_string_literal(
crates/oxc_linter/src/rules/unicorn/prefer_dom_node_dataset.rs
274: codegen.print_expression(&ast.expression_string_literal(SPAN, ast.str(text), None));
crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs
127: content.print_expression(&ast.expression_string_literal(SPAN, ast.str(&argument), None));
See above, but tldr is there's a bunch of places above where we are crating an allocator, and constructing a string literal just to print it (handle quote escaping I assume)
We should probably expose a new Print_string method on codegen to allow us to print the strings without contructing the AST
I noticed a pattern in linter where it creates an
Allocator, creates anAstBuilder, uses that to generate aStringLiteral, and then passes theStringLiteraltoCodegento print it - by looks of things, it's doing all that just to escape a string. Surely there's a simpler way? Maybeoxc_codegencould just expose the function which escapes strings, independent of all the AST stuff?Originally posted by @overlookmotel in #23730 (comment)
See above, but tldr is there's a bunch of places above where we are crating an allocator, and constructing a string literal just to print it (handle quote escaping I assume)
We should probably expose a new
Print_stringmethod on codegen to allow us to print the strings without contructing the AST