From 0115314cf48756024bf8df160203a6738648cdfe Mon Sep 17 00:00:00 2001 From: Shannon Rothe Date: Thu, 23 Nov 2023 21:30:14 +1100 Subject: [PATCH] feat(ast/semantic): parse jsdoc on `PropertyDefinition` (#1517) This should be enough to handle jsdoc comments on class properties/fields. See #1506 --- crates/oxc_ast/src/ast_kind.rs | 2 +- crates/oxc_semantic/src/jsdoc/builder.rs | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/oxc_ast/src/ast_kind.rs b/crates/oxc_ast/src/ast_kind.rs index 94121cf2e..7ba899c99 100644 --- a/crates/oxc_ast/src/ast_kind.rs +++ b/crates/oxc_ast/src/ast_kind.rs @@ -183,7 +183,7 @@ impl<'a> AstKind<'a> { || matches!(self, Self::Class(class) if class.is_declaration()) || matches!(self, Self::ModuleDeclaration(_) | Self::TSEnumDeclaration(_) | Self::TSModuleDeclaration(_) | Self::VariableDeclaration(_) | Self::TSInterfaceDeclaration(_) - | Self::TSTypeAliasDeclaration(_) | Self::TSImportEqualsDeclaration(_) + | Self::TSTypeAliasDeclaration(_) | Self::TSImportEqualsDeclaration(_) | Self::PropertyDefinition(_) ) } diff --git a/crates/oxc_semantic/src/jsdoc/builder.rs b/crates/oxc_semantic/src/jsdoc/builder.rs index 7b291e13d..505466788 100644 --- a/crates/oxc_semantic/src/jsdoc/builder.rs +++ b/crates/oxc_semantic/src/jsdoc/builder.rs @@ -78,8 +78,9 @@ mod test { allocator: &'a Allocator, source_text: &'a str, symbol: &'a str, + source_type: Option, ) -> Option> { - let source_type = SourceType::default(); + let source_type = source_type.unwrap_or_default(); let ret = Parser::new(allocator, source_text, source_type).parse(); let program = allocator.alloc(ret.program); let semantic = SemanticBuilder::new(source_text, source_type) @@ -92,10 +93,10 @@ mod test { jsdoc.get_by_span(span) } - fn test_jsdoc(source_text: &str, symbol: &str) { + fn test_jsdoc(source_text: &str, symbol: &str, source_type: Option) { let allocator = Allocator::default(); assert!( - get_jsdoc(&allocator, source_text, symbol).is_some(), + get_jsdoc(&allocator, source_text, symbol, source_type).is_some(), "{symbol} not found in {source_text}" ); } @@ -103,7 +104,7 @@ mod test { fn test_jsdoc_not_found(source_text: &str, symbol: &str) { let allocator = Allocator::default(); assert!( - get_jsdoc(&allocator, source_text, symbol).is_none(), + get_jsdoc(&allocator, source_text, symbol, None).is_none(), "{symbol} found in {source_text}" ); } @@ -139,7 +140,17 @@ mod test { function foo() {}", ]; for source_text in source_texts { - test_jsdoc(source_text, "function foo() {}"); + test_jsdoc(source_text, "function foo() {}", None); } } + + #[test] + fn found_on_property_definition() { + let source = "class Foo { + /** jsdoc */ + bar: string; + }"; + let source_type = SourceType::default().with_typescript(true); + test_jsdoc(source, "bar: string;", Some(source_type)); + } }