fix(semantic): Should return nearest JSDoc (#2490)

Follow up #2437 ( 🙇🏻 )
This commit is contained in:
Yuji Sugiura 2024-02-24 21:35:57 +09:00 committed by GitHub
parent ef5713d600
commit 04f4621ed8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View file

@ -258,6 +258,36 @@ mod test {
}
}
#[test]
fn get_all_by_span_order() {
let allocator = Allocator::default();
let source_text = r"
/**c0*/
function foo() {}
/**c1*/
/* noop */
/**c2*/
// noop
/**c3*/
const x = () => {};
";
let symbol = "const x = () => {};";
let jsdocs = get_jsdoc(&allocator, source_text, symbol, None);
assert!(jsdocs.is_some());
let jsdocs = jsdocs.unwrap();
assert_eq!(jsdocs.len(), 3);
// Should be [farthest, ..., nearest]
let mut iter = jsdocs.iter();
let c1 = iter.next().unwrap();
assert!(c1.comment.contains("c1"));
let _c2 = iter.next().unwrap();
let c3 = iter.next().unwrap();
assert!(c3.comment.contains("c3"));
}
#[test]
fn get_all_jsdoc() {
let allocator = Allocator::default();

View file

@ -39,7 +39,8 @@ impl<'a> JSDoc<'a> {
};
// If flagged, at least 1 JSDoc is attached
jsdocs.first().cloned()
// If multiple JSDocs are attached, return the last = nearest
jsdocs.last().cloned()
}
pub fn get_all_by_node<'b>(&'b self, node: &AstNode<'a>) -> Option<Vec<JSDocComment<'a>>> {