oxc/crates/oxc_semantic/src/jsdoc/parser/utils.rs
Yuji Sugiura 1391e4a86b
refactor(semantic/jsdoc): Misc fixes for JSDoc related things (#2531)
Sorry for the rather large size of PR. 😓 But essentially, not changed so
much.

#### 1. Reorganize directories and namings

```
src/jsdoc
├── builder.rs 👈🏻 for SemanticBuilder
├── finder.rs  👈🏻 `semantic.jsdoc()`
├── mod.rs
└── parser
   ├── jsdoc.rs     👈🏻 `JSDoc` struct which has `comment` and `tags`
   ├── jsdoc_tag.rs 👈🏻 `JSDocTag` struct
   ├── mod.rs
   ├── parse.rs     👈🏻 parsing logic by `JSDocParser`
   └── utils.rs
```

Now `mod.rs` has only export things.

#### 2. Introduce `JSDocTagKind::Unknown(name)`

We need to keep their name as-is to check valid tag names are used.(e.g.
`jsdoc/check-tag-names` rule)

#### 3. Support multiline description

- Comment for JSDoc
- Comment for each JSDocTag

```js
/**
 * @foo this comment continues
 * here but leading * should be ignored!!
 */
```

- - -

Please correct me if I am doing something wrong... 🐰
2024-02-29 17:28:14 +08:00

58 lines
962 B
Rust

pub fn trim_multiline_comment(s: &str) -> String {
s.trim()
.split('\n')
.map(|line| line.trim().trim_start_matches('*').trim())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join("\n")
}
#[cfg(test)]
mod test {
use super::trim_multiline_comment;
#[test]
fn trim_multiline_jsdoc_comments() {
for (actual, expect) in [
("hello", "hello"),
(
"
trim
", "trim",
),
(
"
* asterisk
",
"asterisk",
),
(
"
* * li
* * li
",
"* li\n* li",
),
(
"
* list
* list
",
"list\nlist",
),
(
"
1
2
3
",
"1\n2\n3",
),
] {
assert_eq!(trim_multiline_comment(actual), expect);
}
}
}