diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_parts.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_parts.rs index e69f273bd70c8..0a89bd7c80987 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_parts.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_parts.rs @@ -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() } } @@ -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 } } @@ -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]"), ] { @@ -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); diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs index 62cbdbfb07d2d..84c5d1c42b735 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs @@ -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), @@ -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 "), ), ] {