Skip to content

Commit

Permalink
Update parsed()
Browse files Browse the repository at this point in the history
  • Loading branch information
leaysgur committed Apr 15, 2024
1 parent 6ef12b2 commit 659ecd8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 18 additions & 4 deletions crates/oxc_semantic/src/jsdoc/parser/jsdoc_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'a> JSDocTagTypePart<'a> {
/// Returns the type content without `{` and `}`.
pub fn parsed(&self) -> &'a str {
// +1 for `{`, -1 for `}`
&self.raw[1..self.raw.len() - 1]
self.raw[1..self.raw.len() - 1].trim()
}
}

Expand All @@ -141,7 +141,13 @@ impl<'a> JSDocTagTypeNamePart<'a> {
}

/// Returns the type name itself.
/// `.raw` may be like `[foo = var]`, so extract the name
pub fn parsed(&self) -> &'a str {
if self.raw.starts_with('[') {
let inner = self.raw.trim_start_matches('[').trim_end_matches(']').trim();
return inner.split_once('=').map_or(inner, |(v, _)| v.trim());
}

self.raw
}
}
Expand Down Expand Up @@ -269,8 +275,8 @@ mod test {
("{}", ""),
("{-}", "-"),
("{string}", "string"),
("{ string}", " string"),
("{ bool }", " bool "),
("{ string}", "string"),
("{ bool }", "bool"),
("{{x:1}}", "{x:1}"),
("{[1,2,3]}", "[1,2,3]"),
] {
Expand All @@ -282,7 +288,15 @@ mod test {

#[test]
fn type_name_part_parsed() {
for (actual, expect) in [("foo", "foo"), ("Bar", "Bar"), ("変数", "変数")] {
for (actual, expect) in [
("foo", "foo"),
("Bar", "Bar"),
("変数", "変数"),
("[opt]", "opt"),
("[ opt2 ]", "opt2"),
("[def1 = [ 1 ]]", "def1"),
(r#"[def2 = "foo bar"]"#, "def2"),
] {
// `Span` is not used in this test
let type_name_part = JSDocTagTypeNamePart::new(actual, SPAN);
assert_eq!(type_name_part.parsed(), expect);
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ mod test {
{t2} */",
Some(("t2", "{t2}")),
),
("/** @k3 { t3 } */", Some((" t3 ", "{ t3 }"))),
("/** @k3 { t3 } */", Some(("t3", "{ t3 }"))),
("/** @k4 x{t4}y */", Some(("t4", "{t4}"))),
("/** @k5 {t5}} */", Some(("t5", "{t5}"))),
("/** @k6 */", None),
Expand Down Expand Up @@ -436,19 +436,19 @@ c7 */",
(
r#"/** @property [cfg.n12="default value"] */"#,
None,
Some((r#"[cfg.n12="default value"]"#, r#"[cfg.n12="default value"]"#)),
Some(("cfg.n12", r#"[cfg.n12="default value"]"#)),
("", " "),
),
(
"/** @property {t13} [n = 13] c13 */",
Some(("t13", "{t13}")),
Some(("[n = 13]", "[n = 13]")),
Some(("n", "[n = 13]")),
("c13", " c13 "),
),
(
"/** @param {t14} [n14] - opt */",
Some(("t14", "{t14}")),
Some(("[n14]", "[n14]")),
Some(("n14", "[n14]")),
("- opt", " - opt "),
),
] {
Expand Down

0 comments on commit 659ecd8

Please sign in to comment.