|
| 1 | +use crate::JsRuleAction; |
| 2 | +use crate::services::typed::Typed; |
| 3 | +use biome_analyze::{ |
| 4 | + FixKind, Rule, RuleDiagnostic, RuleDomain, context::RuleContext, declare_lint_rule, |
| 5 | +}; |
| 6 | +use biome_console::markup; |
| 7 | +use biome_js_factory::make; |
| 8 | +use biome_js_syntax::{JsVariableDeclaration, JsVariableDeclarator, JsVariableDeclaratorList, T}; |
| 9 | +use biome_rowan::{AstNode, BatchMutationExt}; |
| 10 | +use biome_rule_options::use_disposables::UseDisposablesOptions; |
| 11 | + |
| 12 | +declare_lint_rule! { |
| 13 | + /// Detects a disposable object assigned to a variable without using or await using syntax. |
| 14 | + /// |
| 15 | + /// Disposable objects, which implements Disposable or AsyncDisposable interface, are intended |
| 16 | + /// to dispose after use. Not disposing them can lead some resource or memory leak depending on |
| 17 | + /// the implementation. |
| 18 | + /// |
| 19 | + /// ## Examples |
| 20 | + /// |
| 21 | + /// ### Invalid |
| 22 | + /// |
| 23 | + /// ```js,expect_diagnostic |
| 24 | + /// function createDisposable(): Disposable { |
| 25 | + /// return { |
| 26 | + /// [Symbol.dispose]() { |
| 27 | + /// // do something |
| 28 | + /// }, |
| 29 | + /// }; |
| 30 | + /// } |
| 31 | + /// |
| 32 | + /// const disposable = createDisposable(); |
| 33 | + /// ``` |
| 34 | + /// |
| 35 | + /// ```js,expect_diagnostic |
| 36 | + /// class MyClass implements AsyncDisposable { |
| 37 | + /// async [Symbol.asyncDispose]() { |
| 38 | + /// // do something |
| 39 | + /// }, |
| 40 | + /// } |
| 41 | + /// |
| 42 | + /// const instance = new MyClass(); |
| 43 | + /// ``` |
| 44 | + /// |
| 45 | + /// ### Valid |
| 46 | + /// |
| 47 | + /// ```js |
| 48 | + /// function createDisposable(): Disposable { |
| 49 | + /// return { |
| 50 | + /// [Symbol.dispose]() { |
| 51 | + /// // do something |
| 52 | + /// }, |
| 53 | + /// }; |
| 54 | + /// } |
| 55 | + /// |
| 56 | + /// using disposable = createDisposable(); |
| 57 | + /// ``` |
| 58 | + /// |
| 59 | + /// ```js |
| 60 | + /// class MyClass implements AsyncDisposable { |
| 61 | + /// async [Symbol.asyncDispose]() { |
| 62 | + /// // do something |
| 63 | + /// }, |
| 64 | + /// } |
| 65 | + /// |
| 66 | + /// await using instance = new MyClass(); |
| 67 | + /// ``` |
| 68 | + /// |
| 69 | + pub UseDisposables { |
| 70 | + version: "next", |
| 71 | + name: "useDisposables", |
| 72 | + language: "js", |
| 73 | + recommended: false, |
| 74 | + fix_kind: FixKind::Unsafe, |
| 75 | + domains: &[RuleDomain::Project], |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Rule for UseDisposables { |
| 80 | + type Query = Typed<JsVariableDeclarator>; |
| 81 | + type State = DisposableKind; |
| 82 | + type Signals = Option<Self::State>; |
| 83 | + type Options = UseDisposablesOptions; |
| 84 | + |
| 85 | + fn run(ctx: &RuleContext<Self>) -> Self::Signals { |
| 86 | + let decl = ctx.query(); |
| 87 | + let initializer = decl.initializer()?; |
| 88 | + let expression = initializer.expression().ok()?; |
| 89 | + let ty = ctx.type_of_expression(&expression); |
| 90 | + |
| 91 | + // Lookup the parent declaration which possibly has `await` and/or `using` tokens. |
| 92 | + let parent = decl |
| 93 | + .parent::<JsVariableDeclaratorList>()? |
| 94 | + .parent::<JsVariableDeclaration>()?; |
| 95 | + |
| 96 | + let is_disposed = parent.kind().ok()?.kind() == T![using]; |
| 97 | + if ty.is_disposable() && !is_disposed { |
| 98 | + return Some(DisposableKind::Disposable); |
| 99 | + } |
| 100 | + |
| 101 | + let is_async_disposed = is_disposed && parent.await_token().is_some(); |
| 102 | + if ty.is_async_disposable() && !is_async_disposed { |
| 103 | + return Some(DisposableKind::AsyncDisposable); |
| 104 | + } |
| 105 | + |
| 106 | + None |
| 107 | + } |
| 108 | + |
| 109 | + fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 110 | + let node = ctx.query(); |
| 111 | + |
| 112 | + Some( |
| 113 | + RuleDiagnostic::new( |
| 114 | + rule_category!(), |
| 115 | + node.range(), |
| 116 | + markup! { "Disposable object is assigned here but never disposed." }, |
| 117 | + ) |
| 118 | + .note(match state { |
| 119 | + DisposableKind::Disposable => markup! { |
| 120 | + "The object implements the "<Emphasis>"Disposable"</Emphasis>"interface, which is intended to be disposed after use with "<Emphasis>"using"</Emphasis>" syntax." |
| 121 | + }, |
| 122 | + DisposableKind::AsyncDisposable => markup! { |
| 123 | + "The object implements the "<Emphasis>"AsyncDisposable"</Emphasis>"interface, which is intended to be disposed after use with "<Emphasis>"await using"</Emphasis>" syntax." |
| 124 | + }, |
| 125 | + }) |
| 126 | + .note(markup! { |
| 127 | + "Not disposing the object properly can lead some resource or memory leak." |
| 128 | + }) |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsRuleAction> { |
| 133 | + let mut mutation = ctx.root().begin(); |
| 134 | + |
| 135 | + let decl = ctx |
| 136 | + .query() |
| 137 | + .parent::<JsVariableDeclaratorList>()? |
| 138 | + .parent::<JsVariableDeclaration>()?; |
| 139 | + |
| 140 | + let mut new_decl = decl |
| 141 | + .clone() |
| 142 | + .with_kind_token(make::token_with_trailing_space(T![using])); |
| 143 | + |
| 144 | + if let DisposableKind::AsyncDisposable = state { |
| 145 | + new_decl = new_decl.with_await_token(Some(make::token_with_trailing_space(T![await]))); |
| 146 | + } |
| 147 | + |
| 148 | + mutation.replace_node(decl, new_decl); |
| 149 | + |
| 150 | + Some(JsRuleAction::new( |
| 151 | + ctx.metadata().action_category(ctx.category(), ctx.group()), |
| 152 | + ctx.metadata().applicability(), |
| 153 | + markup! { "Add the "<Emphasis>"using"</Emphasis>" keyword to dispose the object when leaving the scope." }, |
| 154 | + mutation, |
| 155 | + )) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +pub enum DisposableKind { |
| 160 | + Disposable, |
| 161 | + AsyncDisposable, |
| 162 | +} |
0 commit comments