-
-
Notifications
You must be signed in to change notification settings - Fork 570
feat(lint): handle jsdoc and tsdoc syntax in nounusedimports #5698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! You mentioned this is your first venture into Rust? Even more impressive!
Great test cases, clear implementation, and limitations well documented 👍
Do you want to add a changeset still?
LazyLock::new(|| Regex::new(r"\{@(link|see)\s*([^}| #\.]+)(?:[^}]+)?\}").unwrap()); | ||
|
||
static JSDOC_TYPE_TAG_REGEX: LazyLock<Regex> = | ||
LazyLock::new(|| Regex::new(r"@(param|returns|type|typedef)\s*\{([^}]+)}").unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the last }
doesn't really need escaping, but let's stay on the safe side :)
LazyLock::new(|| Regex::new(r"@(param|returns|type|typedef)\s*\{([^}]+)}").unwrap()); | |
LazyLock::new(|| Regex::new(r"@(param|returns|type|typedef)\s*\{([^}]+)\}").unwrap()); |
} | ||
|
||
fn load_jsdoc_types_from_node(model: &mut JsDocTypeModel, node: &SyntaxNode<JsLanguage>) { | ||
if let Ok(comment) = JsdocComment::try_from(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the regular expressions need to be able to work across newlines? If not, maybe you can even use JsdocComment::text_is_jsdoc_comment()
only and avoid instantiating the JsdocComment
itself. The regular expressions may just work fine on the raw string, so you would avoid some allocation and unnecesssary work in that case.
CodSpeed Performance ReportMerging #5698 will not alter performanceComparing Summary
|
match event { | ||
WalkEvent::Enter(node) => { | ||
if AnyJsWithTypeReferencingJsDoc::can_cast(node.kind()) { | ||
load_jsdoc_types_from_node(&mut self.jsdoc_types, &node); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
load_jsdoc_types_from_node(&mut self.jsdoc_types, &node); | |
load_jsdoc_types_from_node(&mut self.jsdoc_types, node); |
Summary
#4677
Adds the initial foundation for detecting type usage in JSDoc and TSDoc comments to avoid imports being flagged as unused.
use_hook_at_top_level.rs
1this.field =
initializers in constructor) - Performance impact might be quite high if we handle all assignment expression statements./** @typedef {import('module').Type} Alias */
in such cases instead of adding an import?Test Plan
I added a variety of spec-tests which should show that things are mostly working as expected. Especially for TypeScript things should be well covered for most use-cases. For JSDoc I am a bit more reserved.
Footnotes
https://github.com/biomejs/biome/issues/4677#issuecomment-2813648388 ↩