-
-
Notifications
You must be signed in to change notification settings - Fork 41
Improve nested if printing #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ const softline = docBuilders.softline; | |
const group = docBuilders.group; | ||
const indent = docBuilders.indent; | ||
|
||
const FastPath = require("prettier/src/common/fast-path"); | ||
|
||
function printPythonString(raw, options) { | ||
// `rawContent` is the string exactly like it appeared in the input source | ||
// code, without its enclosing quotes. | ||
|
@@ -139,48 +141,7 @@ function printWithItem(path, print) { | |
} | ||
|
||
function printIf(path, print) { | ||
function printOrElse(path) { | ||
const n = path.getValue(); | ||
|
||
if (n.orelse && n.orelse.length > 0) { | ||
if (n.orelse[0].ast_type === "If") { | ||
return concat(path.map(printElseBody, "orelse")); | ||
} | ||
return concat([ | ||
hardline, | ||
"else:", | ||
concat(path.map(printElseBody, "orelse")) | ||
]); | ||
} | ||
} | ||
|
||
function printElseBody(path) { | ||
const n = path.getValue(); | ||
|
||
const parts = []; | ||
|
||
if (n.ast_type === "If") { | ||
parts.push( | ||
concat([ | ||
hardline, | ||
"elif ", | ||
path.call(print, "test"), | ||
":", | ||
indent(concat([hardline, printBody(path, print)])) | ||
]) | ||
); | ||
} else { | ||
parts.push(indent(concat([hardline, path.call(print)]))); | ||
} | ||
|
||
const orElse = printOrElse(path); | ||
|
||
if (orElse) { | ||
parts.push(orElse); | ||
} | ||
|
||
return concat(parts); | ||
} | ||
let n = path.getValue(); | ||
|
||
const parts = [ | ||
"if ", | ||
|
@@ -189,10 +150,29 @@ function printIf(path, print) { | |
indent(concat([hardline, printBody(path, print)])) | ||
]; | ||
|
||
const orElse = printOrElse(path); | ||
while (n.orelse && n.orelse.length === 1 && n.orelse[0].ast_type === "If") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: wouldn't hurt to have a comment or two in here :) Maybe something along the lines of this? # "Handle the nested `elif`, which are represented as `else: if...` in the AST." Up to you though :) |
||
n = n.orelse[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just something like: parts.push(concat(path.map(printElif, 'orelse'))); and then have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I'm understanding what you mean here |
||
|
||
if (orElse) { | ||
parts.push(orElse); | ||
path = new FastPath(n); | ||
|
||
parts.push( | ||
hardline, | ||
"elif ", | ||
path.call(print, "test"), | ||
":", | ||
indent(concat([hardline, printBody(path, print)])) | ||
); | ||
} | ||
|
||
if (n.orelse && n.orelse.length > 0) { | ||
parts.push(hardline, "else:"); | ||
|
||
for (let i = 0; i < n.orelse.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't you just do parts.push(indent(concat(path.map(print, 'orelse')))); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for some reason it doesn't work, it is like it is missing the if foo:
foo()
elif bar:
bar()
else:baz() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we doing a manual |
||
const x = n.orelse[i]; | ||
path = new FastPath(x); | ||
|
||
parts.push(indent(concat([hardline, path.call(print)]))); | ||
} | ||
} | ||
|
||
return concat(parts); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,11 @@ | |
else: | ||
assert something | ||
another_statement() | ||
|
||
if foo: | ||
foo() | ||
else: | ||
if bar: | ||
bar() | ||
if baz: | ||
baz() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be great to see some tests that have if foo:
foo()
elif bar:
bar()
else:
if baz:
baz()
elif garply:
garply()
else:
qux() Maybe also a simple case like: if a:
a()
else:
if b:
b() My guess is that to make the above cases work (and not, for instance, collapse the end of the second case into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: If you wanted, you could update this function to use the
indentConcat
function, which reduces the number of parens you need :D