MCPcopy Create free account
hub / github.com/astral-sh/ruff / unparse_stmt

Method unparse_stmt

crates/ruff_python_codegen/src/generator.rs:248–740  ·  view source on GitHub ↗
(&mut self, ast: &Stmt)

Source from the content-addressed store, hash-verified

246 }
247
248 pub(crate) fn unparse_stmt(&mut self, ast: &Stmt) {
249 macro_rules! statement {
250 ($body:block) => {{
251 self.newline();
252 self.p(&self.indent.deref().repeat(self.indent_depth));
253 $body
254 self.initial = false;
255 }};
256 }
257
258 match ast {
259 Stmt::FunctionDef(ast::StmtFunctionDef {
260 is_async,
261 name,
262 parameters,
263 body,
264 returns,
265 decorator_list,
266 type_params,
267 ..
268 }) => {
269 self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
270 for decorator in decorator_list {
271 statement!({
272 self.p("@");
273 self.unparse_expr(&decorator.expression, precedence::MAX);
274 });
275 }
276 statement!({
277 if *is_async {
278 self.p("async ");
279 }
280 self.p("def ");
281 self.p_id(name);
282 if let Some(type_params) = type_params {
283 self.unparse_type_params(type_params);
284 }
285 self.p("(");
286 self.unparse_parameters(parameters);
287 self.p(")");
288 if let Some(returns) = returns {
289 self.p(" -> ");
290 self.unparse_expr(returns, precedence::MAX);
291 }
292 self.p(":");
293 });
294 self.body(body);
295 if self.indent_depth == 0 {
296 self.newlines(2);
297 }
298 }
299 Stmt::ClassDef(ast::StmtClassDef {
300 name,
301 arguments,
302 body,
303 decorator_list,
304 type_params,
305 range: _,

Callers 4

stmtMethod · 0.80
bodyMethod · 0.80
unparse_suiteMethod · 0.80
jupyter_round_tripFunction · 0.80

Calls 5

newlinesMethod · 0.80
bodyMethod · 0.45
is_emptyMethod · 0.45
saturating_addMethod · 0.45
saturating_subMethod · 0.45

Tested by

no test coverage detected