Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leading_whitespace and trailing_newline don't play nicely on empty files #3854

Closed
2 tasks done
amomchilov opened this issue Feb 12, 2022 · 0 comments · Fixed by #3885
Closed
2 tasks done

leading_whitespace and trailing_newline don't play nicely on empty files #3854

amomchilov opened this issue Feb 12, 2022 · 0 comments · Fixed by #3885
Assignees
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@amomchilov
Copy link

New Issue Checklist

Describe the bug

trailing_newline requires that a file ends with a single extra new-line character, even for empty files. Once this happens, leading_white space gets violated, because (as far as it sees it), the file now starts with a redundant new line.

To preempt the question of "Why do you have an empty file in the first place?": I just have a little empty playground I keep around in my project as a quick little scratch space, to use similar to the REPL, but with support for packages from my workspace.

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint
Linting Swift files in current working directory
/path/dev playground.playground/Contents.swift:1:1: warning: Leading Whitespace Violation: File shouldn't start with whitespace: currently starts with 1 whitespace characters (leading_whitespace)
Done linting! Found 1 violation, 0 serious in 243 files.

Then when you try to fix it:

$ swiftlint --fix
Correcting Swift files in current working directory
/path/dev playground.playground/Contents.swift:1:1 Corrected Leading Whitespace
/path/dev playground.playground/Contents.swift:1:1 Corrected Trailing Newline

It actually hits a fixed point. Rerunning it over and over again doesn't cause any net changes to to the file.

Environment

  • SwiftLint version: 0.46.2
  • Installation method used: Homebrew
  • Paste your configuration file:
excluded:
  - vendor
  - "**/.build"
disabled_rules:
# TODO – I eventually want to have these all fixed
  # I'm okay with force-casting for a lot of this stuff, because C interop often requires it.
  # Nonetheless, it would be good to add in an "unwrap or die" operator (https://github.com/apple/swift-evolution/blob/master/proposals/0217-bangbang.md),
  # to require the author to add an explanation as to why force-casting is safe in each instance.
  - force_cast
  - force_try
  - todo # Lol, I have a TODO to fix the TODOs
# These rules are disabled because I like to manually line things up where it adds clarity
  - colon
  - comma
# Other
  # I would only like to enforce this rule for non-test files, but it doesn't look like
  # exclusion patterns on a per-rule basis: https://github.com/realm/SwiftLint/issues/2355
  - function_body_length
  # I think short variable names are perfectly acceptable in the right contexts
  - identifier_name
  # Meh, deep nesting is useful sometimes
  - nesting
# Disabling these rules so I can apply auto-corrections for them 1 by 1, so that it's easier to review the changes.
  - unused_closure_parameter
  - unused_optional_binding
  - unused_setter_value
  - vertical_parameter_alignment
  - vertical_whitespace
  - void_return

trailing_comma:
  mandatory_comma: true

type_name:
  allowed_symbols: ["_"] # _ can be useful in Test/Spec type names.
  excluded: ["ID"]
  • Are you using nested configurations? Nope
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 13.2.1 Build version 13C100
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
echo "\n" | swiftlint lint --no-cache --use-stdin --enable-all-rules
<nopath>:1:1: warning: Leading Whitespace Violation: File shouldn't start with whitespace: currently starts with 2 whitespace characters (leading_whitespace)
<nopath>:2:1: warning: Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)
<nopath>:2:1: warning: Vertical Whitespace Violation: Limit vertical whitespace to a single empty line. Currently 2. (vertical_whitespace)
Done linting! Found 3 violations, 0 serious in 1 file.
@jpsim jpsim added the enhancement Ideas for improvements of existing features and rules. label Mar 9, 2022
@jpsim jpsim self-assigned this Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants