mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Reduce newlines in code gen (#807)
This commit is contained in:
parent
6ffe767252
commit
ee31fa6109
2 changed files with 98 additions and 102 deletions
|
@ -55,18 +55,14 @@ impl SourceGenerator {
|
|||
}
|
||||
|
||||
fn newline(&mut self) -> fmt::Result {
|
||||
if self.initial {
|
||||
self.initial = false;
|
||||
} else {
|
||||
if !self.initial {
|
||||
self.new_lines = std::cmp::max(self.new_lines, 1);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn newlines(&mut self, extra: usize) -> fmt::Result {
|
||||
if self.initial {
|
||||
self.initial = false;
|
||||
} else {
|
||||
if !self.initial {
|
||||
self.new_lines = std::cmp::max(self.new_lines, 1 + extra);
|
||||
}
|
||||
Ok(())
|
||||
|
@ -121,6 +117,7 @@ impl SourceGenerator {
|
|||
self.newline()?;
|
||||
self.p(&" ".repeat(self.indentation))?;
|
||||
$body
|
||||
self.initial = false;
|
||||
}};
|
||||
}
|
||||
|
||||
|
@ -145,12 +142,11 @@ impl SourceGenerator {
|
|||
self.unparse_expr(returns, precedence::EXPR)?;
|
||||
}
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
|
||||
if self.indentation == 0 {
|
||||
self.newlines(2)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::AsyncFunctionDef {
|
||||
name,
|
||||
|
@ -172,11 +168,11 @@ impl SourceGenerator {
|
|||
self.unparse_expr(returns, precedence::EXPR)?;
|
||||
}
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
if self.indentation == 0 {
|
||||
self.newlines(2)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::ClassDef {
|
||||
name,
|
||||
|
@ -209,11 +205,11 @@ impl SourceGenerator {
|
|||
}
|
||||
self.p_if(!first, ")")?;
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
if self.indentation == 0 {
|
||||
self.newlines(2)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::Return { value } => {
|
||||
statement!({
|
||||
|
@ -299,14 +295,14 @@ impl SourceGenerator {
|
|||
self.p(" in ")?;
|
||||
self.unparse_expr(iter, precedence::TEST)?;
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
if !orelse.is_empty() {
|
||||
statement!({
|
||||
self.p("else:")?;
|
||||
self.body(orelse)?;
|
||||
});
|
||||
self.body(orelse)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::AsyncFor {
|
||||
target,
|
||||
|
@ -321,34 +317,35 @@ impl SourceGenerator {
|
|||
self.p(" in ")?;
|
||||
self.unparse_expr(iter, precedence::TEST)?;
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
if !orelse.is_empty() {
|
||||
statement!({
|
||||
self.p("else:")?;
|
||||
self.body(orelse)?;
|
||||
});
|
||||
self.body(orelse)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::While { test, body, orelse } => {
|
||||
statement!({
|
||||
self.p("while ")?;
|
||||
self.unparse_expr(test, precedence::TEST)?;
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
if !orelse.is_empty() {
|
||||
statement!({
|
||||
self.p("else:")?;
|
||||
self.body(orelse)?;
|
||||
});
|
||||
self.body(orelse)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::If { test, body, orelse } => {
|
||||
statement!({
|
||||
self.p("if ")?;
|
||||
self.unparse_expr(test, precedence::TEST)?;
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
|
||||
let mut orelse_: &Vec<Stmt<U>> = orelse;
|
||||
|
@ -359,21 +356,20 @@ impl SourceGenerator {
|
|||
self.p("elif ")?;
|
||||
self.unparse_expr(test, precedence::TEST)?;
|
||||
self.p(":")?;
|
||||
self.body(body)?;
|
||||
});
|
||||
self.body(body)?;
|
||||
orelse_ = orelse;
|
||||
}
|
||||
} else {
|
||||
if !orelse_.is_empty() {
|
||||
statement!({
|
||||
self.p("else:")?;
|
||||
self.body(orelse_)?;
|
||||
});
|
||||
self.body(orelse_)?;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
StmtKind::With { items, body, .. } => {
|
||||
statement!({
|
||||
|
@ -384,8 +380,8 @@ impl SourceGenerator {
|
|||
self.unparse_withitem(item)?;
|
||||
}
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
})
|
||||
}
|
||||
StmtKind::AsyncWith { items, body, .. } => {
|
||||
statement!({
|
||||
|
@ -396,8 +392,8 @@ impl SourceGenerator {
|
|||
self.unparse_withitem(item)?;
|
||||
}
|
||||
self.p(":")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
})
|
||||
}
|
||||
StmtKind::Match { .. } => {}
|
||||
StmtKind::Raise { exc, cause } => {
|
||||
|
@ -421,6 +417,7 @@ impl SourceGenerator {
|
|||
} => {
|
||||
statement!({
|
||||
self.p("try:")?;
|
||||
});
|
||||
self.body(body)?;
|
||||
|
||||
for handler in handlers {
|
||||
|
@ -432,16 +429,15 @@ impl SourceGenerator {
|
|||
if !orelse.is_empty() {
|
||||
statement!({
|
||||
self.p("else:")?;
|
||||
self.body(orelse)?;
|
||||
});
|
||||
self.body(orelse)?;
|
||||
}
|
||||
if !finalbody.is_empty() {
|
||||
statement!({
|
||||
self.p("finally:")?;
|
||||
self.body(finalbody)?;
|
||||
});
|
||||
self.body(finalbody)?;
|
||||
}
|
||||
})
|
||||
}
|
||||
StmtKind::Assert { test, msg } => {
|
||||
statement!({
|
||||
|
|
|
@ -11,7 +11,7 @@ expression: checks
|
|||
column: 52
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType1(TypedDict):\n a: int\n b: str"
|
||||
content: "class MyType1(TypedDict):\n a: int\n b: str"
|
||||
location:
|
||||
row: 4
|
||||
column: 0
|
||||
|
@ -28,7 +28,7 @@ expression: checks
|
|||
column: 50
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType2(TypedDict):\n a: int\n b: str"
|
||||
content: "class MyType2(TypedDict):\n a: int\n b: str"
|
||||
location:
|
||||
row: 7
|
||||
column: 0
|
||||
|
@ -45,7 +45,7 @@ expression: checks
|
|||
column: 44
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType3(TypedDict):\n a: int\n b: str"
|
||||
content: "class MyType3(TypedDict):\n a: int\n b: str"
|
||||
location:
|
||||
row: 10
|
||||
column: 0
|
||||
|
@ -62,7 +62,7 @@ expression: checks
|
|||
column: 30
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType4(TypedDict):\n pass"
|
||||
content: "class MyType4(TypedDict):\n pass"
|
||||
location:
|
||||
row: 13
|
||||
column: 0
|
||||
|
@ -79,7 +79,7 @@ expression: checks
|
|||
column: 46
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType5(TypedDict):\n a: 'hello'"
|
||||
content: "class MyType5(TypedDict):\n a: 'hello'"
|
||||
location:
|
||||
row: 16
|
||||
column: 0
|
||||
|
@ -96,7 +96,7 @@ expression: checks
|
|||
column: 41
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType6(TypedDict):\n a: 'hello'"
|
||||
content: "class MyType6(TypedDict):\n a: 'hello'"
|
||||
location:
|
||||
row: 17
|
||||
column: 0
|
||||
|
@ -113,7 +113,7 @@ expression: checks
|
|||
column: 56
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType7(TypedDict):\n a: NotRequired[dict]"
|
||||
content: "class MyType7(TypedDict):\n a: NotRequired[dict]"
|
||||
location:
|
||||
row: 20
|
||||
column: 0
|
||||
|
@ -130,7 +130,7 @@ expression: checks
|
|||
column: 65
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType8(TypedDict, total=False):\n x: int\n y: int"
|
||||
content: "class MyType8(TypedDict, total=False):\n x: int\n y: int"
|
||||
location:
|
||||
row: 23
|
||||
column: 0
|
||||
|
@ -147,7 +147,7 @@ expression: checks
|
|||
column: 59
|
||||
fix:
|
||||
patch:
|
||||
content: "\nclass MyType10(TypedDict):\n key: Literal['value']"
|
||||
content: "class MyType10(TypedDict):\n key: Literal['value']"
|
||||
location:
|
||||
row: 29
|
||||
column: 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue