Skip to content

Commit

Permalink
Fix issue #1322: Occasionally two extra newlines before comment with & .
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 16, 2020
1 parent 5811f3e commit 17a7062
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Changelog

NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.1.1 July 15, 2020
- Fix issue #1322: Occasionally two extra newlines before comment with `-n` & `--fss`.

### 5.1.0 July 14, 2020
- isort now throws an exception if an invalid settings path is given (issue #1174).
- Implemented support for automatic redundant alias removal (issue #1281).
Expand Down
8 changes: 7 additions & 1 deletion isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def sorted_imports(
comments_above = []
new_section_output: List[str] = []
for line in section_output:
if not line:
continue
if line.startswith("#"):
comments_above.append(line)
elif comments_above:
Expand All @@ -132,7 +134,11 @@ def sorted_imports(
for line in new_section_output:
comments = getattr(line, "comments", ())
if comments:
if section_output and config.ensure_newline_before_comments:
if (
config.ensure_newline_before_comments
and section_output
and section_output[-1]
):
section_output.append("")
section_output.extend(comments)
section_output.append(str(line))
Expand Down
26 changes: 26 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,29 @@ def test_isort_doesnt_rewrite_import_with_dot_to_from_import_issue_1280():
""",
show_diff=True,
)


def test_isort_shouldnt_introduce_extra_lines_with_fass_issue_1322():
"""Tests to ensure isort doesn't introduce extra lines when used with fass option.
See: https://github.com/timothycrosley/isort/issues/1322
"""
assert (
isort.code(
"""
import logging
# Comment canary
from foo import bar
import quux
""",
force_sort_within_sections=True,
ensure_newline_before_comments=True,
)
== """
import logging
# Comment canary
from foo import bar
import quux
"""
)

0 comments on commit 17a7062

Please sign in to comment.