Swiftlint: 'operator_usage_whitespace' rule conflicts with Image Literal

Created on 22 Dec 2016  路  7Comments  路  Source: realm/SwiftLint

When using image literal, It's should not triggered

bug

Most helpful comment

It's fixed. Thanks.

All 7 comments

How are you triggering this rule with an image literal?

Anywhere in code.

imageView.image = #imageLiteral(resourceName: "image")   // Triggered

button.setImage(#imageLiteral(resourceName: "image"), for: .normal)   // Triggered
button.setImage(#imageLiteral(resourceName: "image") , for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image"), for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image") , for: .normal)   // Triggered

let imageView = UIImageView(image: #imageLiteral(resourceName: "image"))   // Triggered
let imageView = UIImageView(image: #imageLiteral(resourceName: "image") )   // Triggered

let images = [ #imageLiteral(resourceName: "image-1"), #imageLiteral(resourceName: "image-2") ]   // Triggered

I'm sorry I can't reproduce this with the code you've shared.

$ swiftlint version
0.14.0
$ cat .swiftlint.yml
whitelist_rules:
  - operator_usage_whitespace
$ cat imageliteral.swift
imageView.image = #imageLiteral(resourceName: "image")   // Triggered

button.setImage(#imageLiteral(resourceName: "image"), for: .normal)   // Triggered
button.setImage(#imageLiteral(resourceName: "image") , for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image"), for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image") , for: .normal)   // Triggered

let imageView = UIImageView(image: #imageLiteral(resourceName: "image"))   // Triggered
let imageView = UIImageView(image: #imageLiteral(resourceName: "image") )   // Triggered

let images = [ #imageLiteral(resourceName: "image-1"), #imageLiteral(resourceName: "image-2") ]   // Triggered

let a  = 0 // I added this to show the rule is working...
$ swiftlint lint --path imageliteral.swift
Loading configuration from '.swiftlint.yml'
Linting Swift files at path imageliteral.swift
Linting 'imageliteral.swift' (1/1)
imageliteral.swift:13:6: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
Done linting! Found 1 violation, 0 serious in 1 file.

My bad.
The images name I used was just meant for this example.
But the real images name I used was something like this "icon-bar-save", "icon-share", "image-logo-small".
And because of "-" in the images name that made it triggered.
Maybe we should consider ignoring the operator in image name ?

$ swiftlint version
0.14.0

$ cat .swiftlint.yml
whitelist_rules:
  - operator_usage_whitespace

$ cat test.swift 
imageView.image = #imageLiteral(resourceName: "image-1")   // Triggered

button.setImage(#imageLiteral(resourceName: "image-1"), for: .normal)   // Triggered
button.setImage(#imageLiteral(resourceName: "image-1") , for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image-1"), for: .normal)   // Triggered
button.setImage( #imageLiteral(resourceName: "image-1") , for: .normal)   // Triggered

let imageView = UIImageView(image: #imageLiteral(resourceName: "image-1"))   // Triggered
let imageView = UIImageView(image: #imageLiteral(resourceName: "image-1") )   // Triggered

let images = [ #imageLiteral(resourceName: "image-1"), #imageLiteral(resourceName: "image-2") ]   // Triggered

$ swiftlint lint --path test.swift
Loading configuration from '.swiftlint.yml'
Linting Swift files at path test.swift
Linting 'test.swift' (1/1)
test.swift:1:53: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:3:51: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:4:51: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:5:52: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:6:52: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:8:70: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:9:70: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:11:50: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
test.swift:11:90: warning: Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used. (operator_usage_whitespace)
Done linting! Found 9 violations, 0 serious in 1 file.

@mylifeasdog Thanks so much for detailing it, I was able to reproduce it. I believe https://github.com/realm/SwiftLint/pull/1030 fixes this issue, could you please verify?

It's fixed. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Den-Ree picture Den-Ree  路  3Comments

rajohns08 picture rajohns08  路  3Comments

bourquep picture bourquep  路  3Comments

jszumski picture jszumski  路  3Comments

ziryanov picture ziryanov  路  3Comments