Skip to content

linter: avoid constructing an AST to print a string #23780

Description

@camc314

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

Metadata

Metadata

Assignees

Labels

Type

Fields

Priority

None yet

Effort

None yet

Start date

None yet

Target date

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions