mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Support decorators in source code generator (#2081)
This commit is contained in:
parent
d81620397e
commit
c1cb4796f8
1 changed files with 27 additions and 4 deletions
|
@ -142,11 +142,18 @@ impl<'a> Generator<'a> {
|
||||||
args,
|
args,
|
||||||
body,
|
body,
|
||||||
returns,
|
returns,
|
||||||
|
decorator_list,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
// TODO(charlie): Handle decorators.
|
|
||||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||||
statement!({
|
statement!({
|
||||||
|
for decorator in decorator_list {
|
||||||
|
statement!({
|
||||||
|
self.p("@");
|
||||||
|
self.unparse_expr(decorator, precedence::EXPR);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.newline();
|
||||||
self.p("def ");
|
self.p("def ");
|
||||||
self.p(name);
|
self.p(name);
|
||||||
self.p("(");
|
self.p("(");
|
||||||
|
@ -168,11 +175,17 @@ impl<'a> Generator<'a> {
|
||||||
args,
|
args,
|
||||||
body,
|
body,
|
||||||
returns,
|
returns,
|
||||||
|
decorator_list,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
// TODO(charlie): Handle decorators.
|
|
||||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||||
statement!({
|
statement!({
|
||||||
|
for decorator in decorator_list {
|
||||||
|
statement!({
|
||||||
|
self.unparse_expr(decorator, precedence::EXPR);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.newline();
|
||||||
self.p("async def ");
|
self.p("async def ");
|
||||||
self.p(name);
|
self.p(name);
|
||||||
self.p("(");
|
self.p("(");
|
||||||
|
@ -194,11 +207,17 @@ impl<'a> Generator<'a> {
|
||||||
bases,
|
bases,
|
||||||
keywords,
|
keywords,
|
||||||
body,
|
body,
|
||||||
|
decorator_list,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
// TODO(charlie): Handle decorators.
|
|
||||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||||
statement!({
|
statement!({
|
||||||
|
for decorator in decorator_list {
|
||||||
|
statement!({
|
||||||
|
self.unparse_expr(decorator, precedence::EXPR);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.newline();
|
||||||
self.p("class ");
|
self.p("class ");
|
||||||
self.p(name);
|
self.p(name);
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
|
@ -1140,7 +1159,11 @@ mod tests {
|
||||||
r#"def call(*popenargs, timeout=None, **kwargs):
|
r#"def call(*popenargs, timeout=None, **kwargs):
|
||||||
pass"#
|
pass"#
|
||||||
);
|
);
|
||||||
|
assert_round_trip!(
|
||||||
|
r#"@functools.lru_cache(maxsize=None)
|
||||||
|
def f(x: int, y: int) -> int:
|
||||||
|
return x + y"#
|
||||||
|
);
|
||||||
assert_eq!(round_trip(r#"x = (1, 2, 3)"#), r#"x = 1, 2, 3"#);
|
assert_eq!(round_trip(r#"x = (1, 2, 3)"#), r#"x = 1, 2, 3"#);
|
||||||
assert_eq!(round_trip(r#"-(1) + ~(2) + +(3)"#), r#"-1 + ~2 + +3"#);
|
assert_eq!(round_trip(r#"-(1) + ~(2) + +(3)"#), r#"-1 + ~2 + +3"#);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue