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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/transpiling/jsx_precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 👍

PropName::Str(Str {
span: DUMMY_SP,
raw: None,
value: attr_name.clone().into(),
})
} else {
PropName::Ident(quote_ident!(attr_name.clone()))
};

// Case: <Foo required />
let Some(attr_value) = &jsx_attr.value else {
Expand Down Expand Up @@ -1181,6 +1189,11 @@ const a = _jsxssr($$_tpl_1);"#,
.as_str(),
);

let quoted = if mapping.1.contains('-') {
format!("\"{}\"", &mapping.1)
} else {
mapping.1.clone()
};
// should still be normalized if HTML element cannot
// be serialized
test_transform(
Expand All @@ -1189,7 +1202,7 @@ const a = _jsxssr($$_tpl_1);"#,
.as_str(),
format!(
"{}\nconst a = _jsx(\"label\", {{\n {}: \"foo\",\n ...foo\n}});",
"import { jsx as _jsx } from \"react/jsx-runtime\";", &mapping.1
"import { jsx as _jsx } from \"react/jsx-runtime\";", quoted
)
.as_str(),
);
Expand Down Expand Up @@ -1286,6 +1299,19 @@ const a = _jsx("div", {
);
}

#[test]
fn non_identiifer_attr_test() {
test_transform(
JsxPrecompile::default(),
r#"const a = <Foo aria-label="bar" {...props} />;"#,
r#"import { jsx as _jsx } from "react/jsx-runtime";
const a = _jsx(Foo, {
"aria-label": "bar",
...props
});"#,
);
}

#[test]
fn dangerously_html_test() {
test_transform(
Expand Down
Loading