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

"colon" violation no longer autocorrected in dictionary literals (since 0.23.1) #2050

Closed
AliSoftware opened this Issue Feb 14, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@AliSoftware
Contributor

AliSoftware commented Feb 14, 2018

New Issue Checklist

Bug Report

swiftlint autocorrect does not correct the colon rule on some cases (especially in dictionary literals?).

I've tested with versions 0.24.0 thru 0.25.0 but the bug is still present, while it was working in 0.23.1

Environment

  • SwiftLint versions: 0.23.1 OK, but bug present with 0.24.0, 0.24.1, 0.24.2 and 0.25.0
  • Installation method used: portable zip (also tested 0.23.1 with homebrew, and 0.25.0 with CocoaPods)
  • Which Xcode version are you using (check xcode-select -p)? 9.2

Sample code to reproduce

Sample swift file

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        let dict = [KeysConstants.CELL_KEYS.CELL_TYPE : FooCellType.editTextCell.rawValue,
                    KeysConstants.CELL_KEYS.CELL_IDENTIFIER_KEY : FooEditTextTableViewCell.cellIdentifier,
                    KeysConstants.CELL_KEYS.CELL_GRAY : false,
                    KeysConstants.CELL_KEYS.VALUE_ERROR : self.isInError,
                    KeysConstants.CELL_KEYS.ESTIMATED_CELL_HEIGHT : FooEditTextTableViewCell.cellHeight] as [String: Any]

        self.dataSource = dict as NSDictionary

    }
}

Configuration File

whitelist_rules:
  - colon

Test script

This script aims to test a given swiftlint version by linting first to show the colon violations, then trying to autocorrect them, then linting again to check if they have indeed been corrected.

#!/bin/sh

bin=$1/swiftlint
echo ">>> Testing with version `$bin version`"

git checkout sample.swift

echo "------ Initial linting ------"
$bin lint --path sample.swift

echo "------ Autocorrecting  ------"
$bin autocorrect --path sample.swift

echo "------  Linting again  ------"
$bin lint --path sample.swift

Note: I made this test script in order to test the bug across multiple versions of SwiftLint. In order for this script to work, I've downloaded various versions of SwiftLint (portable zip) and placed each version in its own folder (each folder named after the corresponding version)

Output for various Swiftlint versions

swiftlint 0.23.1

This version works as expected: 5 violations before autocorrect, all corrected, no more violation after autocorrect.

$ ./test.sh 0.23.1
>>> Testing with version 0.23.1
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
------ Autocorrecting  ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64 Corrected Colon
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54 Corrected Colon
Done correcting 1 files!
------  Linting again  ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
swiftlint 0.24.0

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

$ ./test.sh 0.24.0
>>> Testing with version 0.24.0
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
------ Autocorrecting  ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------  Linting again  ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
swiftlint 0.24.1

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

$ ./test.sh 0.24.1
>>> Testing with version 0.24.1
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
------ Autocorrecting  ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------  Linting again  ------
Loading configuration from '.swiftlint.yml'
Invalid configuration for 'indentation'. Falling back to default.
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
swiftlint 0.24.2

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

$ ./test.sh 0.24.2
>>> Testing with version 0.24.2
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
------ Autocorrecting  ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------  Linting again  ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
swiftlint 0.25.0

This version does not work as expected: 5 violations before autocorrect, none auto-corrected, still 5 violation present after autocorrect.

$ ./test.sh 0.25.0
>>> Testing with version 0.25.0
------ Initial linting ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.
------ Autocorrecting  ------
Loading configuration from '.swiftlint.yml'
Correcting Swift files at path sample.swift
Correcting 'sample.swift' (1/1)
Done correcting 1 files!
------  Linting again  ------
Loading configuration from '.swiftlint.yml'
Linting Swift files at path sample.swift
Linting 'sample.swift' (1/1)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:7:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:8:64: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:9:54: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:10:56: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
/Users/ohalligon/Desktop/swiftlint-bug-test/swiftlint/sample.swift:11:66: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 5 violations, 0 serious in 1 file.

\cc @marcelofabri as discussed on Slack.

@AliSoftware

This comment has been minimized.

Contributor

AliSoftware commented Feb 14, 2018

Additional info: I've also tested with this sample code:

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        let dict = [
        	KeysConstants.CELL_KEYS.CELL_TYPE : FooCellType.editTextCell.rawValue,
            KeysConstants.CELL_KEYS.CELL_IDENTIFIER_KEY :FooEditTextTableViewCell.cellIdentifier,
            KeysConstants.CELL_KEYS.CELL_GRAY:  false,
            KeysConstants.CELL_KEYS.VALUE_ERROR  : self.isInError,
            KeysConstants.CELL_KEYS.ESTIMATED_CELL_HEIGHT:FooEditTextTableViewCell.cellHeight
        ] as [String: Any]

        self.dataSource = dict as NSDictionary

    }
}

And the problem still remains: autocorrected properly in 0.23.1, not in 0.25.0.

@AliSoftware

This comment has been minimized.

Contributor

AliSoftware commented Feb 14, 2018

Interestingly, changing the Dictionary literal into a function call makes autocorrect work in 0.25.0 (but not 0.24.2).

So maybe it's rather an issue with auto-correcting colons in dictionary literals specifically?

i.e. this gets auto-corrected properly in 0.25.0 (and also in 0.23.1, but not in 0.24.2) :

import UIKit

class SwiftLintBugSample: UIViewController {

    fileprivate func updateTableView() {

        foo(
            CELL_TYPE : FooCellType.editTextCell.rawValue,
            CELL_IDENTIFIER_KEY :FooEditTextTableViewCell.cellIdentifier,
            CELL_GRAY:  false,
            VALUE_ERROR  : self.isInError,
            ESTIMATED_CELL_HEIGHT:FooEditTextTableViewCell.cellHeight
        )

        self.dataSource = dict as NSDictionary

    }
}

See also #2007 which probably corrected only part of the regression

@AliSoftware AliSoftware changed the title from "colon" violation no longer autocorrects spaces before colons since 0.23.1 to "colon" violation no longer autocorrected in dictionary literals (since 0.23.1) Feb 14, 2018

@marcelofabri marcelofabri added the bug label Feb 14, 2018

marcelofabri added a commit to marcelofabri/SwiftLint that referenced this issue Feb 15, 2018

@mshibanami

This comment has been minimized.

Contributor

mshibanami commented Feb 18, 2018

Oops I didn't realize this bug. Sorry, and thanks for fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment