Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-identifier properties not quoted #168

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Oct 25, 2023

Ran into a bug where we didn't quote non-identifier object properties like aria-label.

// before
const a = { aria-label: "foo" }

// after
const a = { "aria-label": "foo" }

@@ -520,7 +520,15 @@ impl JsxPrecompile {
match attr {
JSXAttrOrSpread::JSXAttr(jsx_attr) => {
let attr_name = get_attr_name(jsx_attr, is_component);
let prop_name = PropName::Ident(quote_ident!(attr_name.clone()));
let prop_name = if attr_name.contains('-') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this function in dprint. Would it be useful to use here? (is_ident_start() and is_ident_part() are helper methods from swc)

use deno_ast::swc::parser::lexer::util::CharExt;

fn is_text_valid_identifier(string_value: &str) -> bool {
  if string_value.is_empty() {
    return false;
  }
  for (i, c) in string_value.chars().enumerate() {
    if (i == 0 && !c.is_ident_start()) || !c.is_ident_part() {
      return false;
    }
  }
  true
}

Copy link
Member

@dsherret dsherret Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is probably not because jsx attribute names are limited in what they can contain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh nice! I was looking for something like is_Ident_start but didn't find it 👍

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marvinhagemeister marvinhagemeister merged commit a173a74 into main Oct 25, 2023
2 checks passed
@marvinhagemeister marvinhagemeister deleted the fix-quoted-properties branch October 25, 2023 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants