Skip to content

Commit

Permalink
Don't add final empty line if file has no contents
Browse files Browse the repository at this point in the history
Fix prettier#60. Use same logic as prettier's printer-estree.js "Program"
case.
  • Loading branch information
ryanashcraft committed Mar 4, 2018
1 parent d6499b9 commit 8f63f70
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/printer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,14 @@ function genericPrint(path, options, print) {

switch (n.ast_type) {
case "Module": {
return concat([printBody(path, print, true /*isTopLevel*/), hardline]);
const parts = [printBody(path, print, true /*isTopLevel*/)];

// Only force a trailing newline if there were any contents.
if (n.body.length || n.comments) {
parts.push(hardline);
}

return concat(parts);
}

case "AsyncFunctionDef":
Expand Down
21 changes: 21 additions & 0 deletions tests/python_empty/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`empty_file.py 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;

exports[`empty_file.py 2`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;

exports[`single_space.py 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;

exports[`single_space.py 2`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;
Empty file.
2 changes: 2 additions & 0 deletions tests/python_empty/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run_spec(__dirname, ["python"], { pythonVersion: "2" });
run_spec(__dirname, ["python"], { pythonVersion: "3" });
1 change: 1 addition & 0 deletions tests/python_empty/single_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 8f63f70

Please sign in to comment.