31

How can we fix the "Trailing Whitespace Violation" warnings caused by swiftlint in my iOS project all in one go? I don't want to manually correct each one of them. Moreover, I don't want to disable these warnings so you can skip that suggestion.

I have been trying Find And Replace option but I am not getting a correct keyword to sort this out.

Image :

enter image description here

JJIqbal
  • 630
  • 1
  • 8
  • 23
Malika Arora
  • 439
  • 1
  • 4
  • 6
  • 12
    There’s a setting in Xcode to automatically remove trailing white space. – rmaddy Jul 16 '19 at 04:17
  • Something you might want to consider is switching from a reporting scanner such as SwiftLint to something that can correct issues instead of just reporting on them. I'd suggest looking at SwiftFormat. – drekka Jul 16 '19 at 04:50
  • @rmaddy, can you please tell me the path to enable this setting in Xcode? – Malika Arora Jul 17 '19 at 03:14
  • 14
    Xcode -> Preferences -> Text Editing -> Editing -> Automatically trim trailing whitespace. And Including whitespace-only lines. – rmaddy Jul 17 '19 at 03:17
  • 2
    @rmaddy but it won't correct the already written lines of code but will be beneficial for future prospects. Isn't it? – Malika Arora Jul 17 '19 at 07:38
  • trailing_whitespace has a parameter ignores_empty_lines that enables ignoring empty lines. It didn't take all that long to remove them but it's up to you. – Michael Salmon Aug 20 '19 at 08:03
  • 1
    For existing code if I cut and paste it fixes this violation. But if I auto format(cmd-A ctrl i) this does not work which is a bit of a pain as I like to do this a lot! – Andrew Stoddart Mar 05 '22 at 21:37

5 Answers5

48

swiftlint has an autocorrect option that will fix some issues for you, so when I get trailing whitespace warnings I run swiftlint from the command line in my project

$ swiftlint autocorrect

Update

As of version 0.43 autocorrect has been deprecated and replaced with --fix so now the command is

$ swiftlint --fix

autocorrect is still available for the time being, for more info see the change log

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
35

Check the 2 checkboxes in While editing section

enter image description here

JMGM
  • 642
  • 6
  • 7
11

to fix existing (remove trailing whitespace from) whitespace-only lines, after turning on the preference include-whitespace-only-lines as rmaddy suggests, one can select all, copy the entire file, and then paste, replacing all text with the same text. Xcode will apply the preference on the pasted code.

David Tristram
  • 189
  • 1
  • 3
  • For existing code if I cut and paste it fixes this violation. But if I auto format the whole code file (cmd-A ctrl i) this does not work, which is a bit of a pain as I like to do this a lot! (Xcode 13.2.1) – Andrew Stoddart Mar 05 '22 at 21:38
4

if you want disable only trailing_whitespace then,

  1. launch Terminal app and move to your project folder.
  2. execute the command $ ls -a to see all the hidden files. you will see there is .swiftlint.yml file
  3. open the file in your editor. $ vi .swiftlint.yml (if you installed Sublime Text in your mac, then use this; $ subl .swiftlint.yml)

Lastly, add trailing_whitespace in disabled_rules.

disabled_rules: # rule identifiers to exclude from running
  # - colon
  - comma
  # - control_statement
  - variable_name
  - force_cast
  - large_tuple
  - notification_center_detachment
  - nesting
  - function_parameter_count
  - trailing_whitespace # <---------
opt_in_rules: # some rules are only opt-in
  - empty_count

...

However, I recommend to use autocorrect rather than adding trailing_whitespace in the setting.

Joey
  • 2,912
  • 2
  • 27
  • 32
1

In your build phase, I would recommend the following:

export PATH="$PATH:/opt/homebrew/bin"
if which swiftlint >/dev/null; then
  swiftlint --fix && swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

The swiftlint --fix is the main component.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85