Skip to content

Commit 3dc75ca

Browse files
committed
add test for elif statement
1 parent 92c26ff commit 3dc75ca

6 files changed

Lines changed: 42 additions & 17 deletions

File tree

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ ci:
44

55
repos:
66
- repo: https://github.com/jvllmr/pre-commit-cargo
7-
rev: c8489dd
7+
rev: c8489ddc7d198af94135f31c3a70cf708e98f069
88
hooks:
99
- id: cargo-fix
1010
- id: cargo-fmt
11+
- repo: https://github.com/astral-sh/ruff-pre-commit
12+
rev: "v0.8.4"
13+
hooks:
14+
- id: ruff
15+
args:
16+
- --fix
17+
- id: ruff-format
1118

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
"cSpell.words": [
33
"unparse",
44
"Unparser"
5-
]
5+
],
6+
"editor.formatOnSave": true,
7+
"[python]": {
8+
"editor.defaultFormatter": "charliermarsh.ruff"
9+
}
610
}

src/unparser.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Unparser {
121121
Stmt::For(data) => self.unparse_stmt_for(data),
122122
Stmt::AsyncFor(data) => self.unparse_stmt_async_for(data),
123123
Stmt::While(data) => self.unparse_stmt_while(data),
124-
Stmt::If(data) => self.unparse_stmt_if(data),
124+
Stmt::If(data) => self.unparse_stmt_if(data, false),
125125
Stmt::With(data) => self.unparse_stmt_with(data),
126126
Stmt::AsyncWith(data) => self.unparse_stmt_async_with(data),
127127
Stmt::Match(data) => self.unparse_stmt_match(data),
@@ -401,8 +401,13 @@ impl Unparser {
401401
}
402402
}
403403

404-
fn unparse_stmt_if(&mut self, node: &StmtIf<TextRange>) {
405-
self.fill("if ");
404+
fn unparse_stmt_if(&mut self, node: &StmtIf<TextRange>, inner_if: bool) {
405+
if inner_if {
406+
self.fill("elif ");
407+
} else {
408+
self.fill("if ");
409+
}
410+
406411
self.unparse_expr(&node.test);
407412
self.write_str(":");
408413
self.block(|block_self| {
@@ -412,14 +417,7 @@ impl Unparser {
412417
});
413418
match node.orelse.as_slice() {
414419
[Stmt::If(inner_if)] => {
415-
self.fill("elif ");
416-
self.unparse_expr(&inner_if.test);
417-
self.write_str(":");
418-
self.block(|block_self| {
419-
for stmt in &inner_if.body {
420-
block_self.unparse_stmt(stmt);
421-
}
422-
});
420+
self.unparse_stmt_if(inner_if, true);
423421
}
424422
[] => {}
425423
_ => {
@@ -951,8 +949,9 @@ impl Unparser {
951949
}
952950

953951
fn unparse_expr_attribute(&mut self, node: &ExprAttribute<TextRange>) {
952+
self.unparse_expr(&node.value);
954953
self.write_str(".");
955-
self.unparse_expr(&node.value)
954+
self.write_str(&node.attr);
956955
}
957956
fn unparse_expr_subscript(&mut self, node: &ExprSubscript<TextRange>) {
958957
self.unparse_expr(&node.value);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import datetime
2+
3+
4+
def main():
5+
now = datetime.now()
6+
7+
if now.hour < 12:
8+
print("Before noon")
9+
elif now.hour > 12:
10+
print("After noon")
11+
else:
12+
print("How did we get here?")
13+
14+
15+
if __name__ == "__main__":
16+
main()

test_files/simple_f_string.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
world = "World"
32
if __name__ == "__main__":
4-
print(f"Hello {world}!")
3+
print(f"Hello {world}!")

test_files/simple_if_statement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
pass
33

44
if True:
5-
print("Hello!")
5+
print("Hello!")

0 commit comments

Comments
 (0)