boa icon indicating copy to clipboard operation
boa copied to clipboard

to_interned_string breaks on object string keys

Open julianbraha opened this issue 5 months ago • 2 comments

See this example javascript code:

export const IconText = {
      extend: 'Flex',

      type: 'checkbox',
      ':checked + div': 'primary',
      display: 'none',
    };

and notice the ':checked + div' field.

After parsing this code, like so:

fn main() {
    let js_str = r#"export const IconText = {
      extend: 'Flex',

      type: 'checkbox',
      ':checked + div': 'primary',
      display: 'none',
    };"#;

    let js_code_bytes = js_str.as_bytes();

    let source = Source::from_bytes(js_code_bytes);

    let mut parser = parser::Parser::new(source);
    let mut interner = Interner::new();
    let tree = parser.parse_module(&mut interner);

    let output = match tree {
        Ok(root) => {
            let module_item_list: &boa_ast::ModuleItemList = module.items();
            let items: &[ModuleItem] = module_item_list.items();
            let item_0 = items[0].clone();

            if let ModuleItem::ExportDeclaration(e) = item_0 {
                if let ExportDeclaration::Declaration(d) = e {
                    let interned_string = d.to_interned_string(interner);
                    dbg!(interned_string);
                }
            }
        },
        Err(e) => panic!(),
    };
}

And after printing out the javascript code using d.to_interned_string(interner);, we see this result:

interned_string = "const IconText = {\n    extend: \"Flex\",\n    type: \"checkbox\",\n    :checked + div: \"primary\",\n    display: \"none\",\n};"

And most importantly, notice that \n :checked + div: no longer has the quotes that were in the input. But as you can see, the rest of the values in the object still have their quotes - just not this key.

julianbraha avatar Sep 02 '24 16:09 julianbraha