Skip to content

Commit

Permalink
[Scripts] SwiftLint integration (material-components#1129)
Browse files Browse the repository at this point in the history
* Integrate SwiftLint in the Catalog.

* Update the disabled SwiftLint rules.

* Reference a local swiftlint.yml symlink.

This prevents us from getting warnings about code style violations from the other demos.

* Fix SwiftLint violations in the Catalog and Components.

* Ignore the `third_party` directory when running the linter.

We should not have to deal with warnings in code we may not be able to fix easily.

* Integrate SwiftLint into Shrine.

Additionally, fix a number of warnings introduced.

* Run SwiftLint on Travis.

* Enable many of the previously disabled rules and correct them.

* Fix control statement violations from SwiftLint.
  • Loading branch information
samsymons authored and ajsecord committed Jan 6, 2017
1 parent c6620b9 commit bb561ac
Show file tree
Hide file tree
Showing 41 changed files with 202 additions and 154 deletions.
10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
excluded:
- Carthage
- Pods
- third_party

disabled_rules:
- cyclomatic_complexity

force_cast: warning
force_try: warning
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ env:
- DESTINATION="platform=iOS Simulator,name=iPhone 7,OS=10.2" SDK="iphonesimulator10.2"
before_install:
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
- brew update && brew install swiftlint
# The travis_wait prefix ensures that the build doesn't error out because too much time went past
# without any output being produced.
script:
- set -o pipefail
- xcodebuild -version
- xcodebuild -showsdks
- pod --version
- swiftlint
- scripts/prep_all
- travis_wait scripts/build_all
- travis_wait scripts/test_all
Expand Down
1 change: 1 addition & 0 deletions catalog/.swiftlint.yml
15 changes: 15 additions & 0 deletions catalog/MDCCatalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
664524AF1C6BA62A001ADBF8 /* Sources */,
664524B01C6BA62A001ADBF8 /* Frameworks */,
664524B11C6BA62A001ADBF8 /* Resources */,
5951872D1E1DA83700AC2110 /* SwiftLint */,
72ACDAA295D1F2087113246F /* [CP] Embed Pods Frameworks */,
8164F6F5D8C80F373A698031 /* [CP] Copy Pods Resources */,
64307B471DF74FEF004AE4AC /* Embed App Extensions */,
Expand Down Expand Up @@ -494,6 +495,20 @@
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-MDCInteractionTests/Pods-MDCInteractionTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
5951872D1E1DA83700AC2110 /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = SwiftLint;
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "${SRCROOT}/../scripts/lint.sh";
};
72ACDAA295D1F2087113246F /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
10 changes: 5 additions & 5 deletions catalog/MDCCatalog/MDCCatalogComponentsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
self.node = node

let layout = UICollectionViewFlowLayout()
let sectionInset:CGFloat = spacing
layout.sectionInset = UIEdgeInsetsMake(sectionInset, sectionInset, sectionInset, sectionInset)
let sectionInset: CGFloat = spacing
layout.sectionInset = UIEdgeInsets(top: sectionInset, left: sectionInset, bottom: sectionInset, right: sectionInset)
layout.minimumInteritemSpacing = spacing
layout.minimumLineSpacing = spacing

Expand All @@ -63,7 +63,7 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
forCellWithReuseIdentifier: "MDCCatalogCollectionViewCell")
self.collectionView?.backgroundColor = UIColor(white: 0.9, alpha: 1)

MDCIcons.ic_arrow_backUseNewStyle(true);
MDCIcons.ic_arrow_backUseNewStyle(true)
}

convenience init(node: CBCNode) {
Expand All @@ -87,7 +87,7 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
titleLabel.textColor = UIColor(white: 0.46, alpha: 1)
titleLabel.font = MDCTypography.titleFont()
titleLabel.sizeToFit()
if (inset + titleLabel.frame.size.width > containerView.frame.size.width) {
if inset + titleLabel.frame.size.width > containerView.frame.size.width {
titleLabel.font = MDCTypography.body2Font()
}

Expand Down Expand Up @@ -216,7 +216,7 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let pad = CGFloat(1)
var cellWidth = (self.view.frame.size.width - 3 * pad) / 2
if (self.view.frame.size.width > self.view.frame.size.height) {
if self.view.frame.size.width > self.view.frame.size.height {
cellWidth = (self.view.frame.size.width - 4 * pad) / 3
}
return CGSize(width: cellWidth, height: cellWidth * 0.825)
Expand Down
2 changes: 1 addition & 1 deletion catalog/MDCCatalog/MDCCatalogTileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import UIKit
class MDCCatalogTileView: UIView {

fileprivate var componentNameString = "Misc"
var componentName:String {
var componentName: String {
get {
return componentNameString
}
Expand Down
6 changes: 3 additions & 3 deletions catalog/MDCCatalog/MDCCatalogWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MDCCatalogWindow: MDCOverlayWindow {
for touch in touches {
switch touch.phase {
case .began:
if (enabled) {
if enabled {
beginDisplayingTouch(touch)
}
continue
Expand All @@ -44,7 +44,7 @@ class MDCCatalogWindow: MDCOverlayWindow {
case .stationary:
continue
case .ended:
if (touch.tapCount == 3) {
if touch.tapCount == 3 {
enabled = !enabled
}
fallthrough
Expand Down Expand Up @@ -77,7 +77,7 @@ class MDCCatalogWindow: MDCOverlayWindow {
animations: {
view?.alpha = 0
},
completion: { finished in
completion: { _ in
view?.removeFromSuperview()
})
}
Expand Down
28 changes: 14 additions & 14 deletions catalog/MDCCatalog/MDCNodeListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class MDCNodeListViewController: CBCNodeListViewController {
// MARK: UITableViewDataSource

override func numberOfSections(in tableView: UITableView) -> Int {
if (node.children.count == 1) {
if node.children.count == 1 {
return 1
}
return sectionNames.count
Expand Down Expand Up @@ -209,16 +209,16 @@ class MDCNodeListViewController: CBCNodeListViewController {
multiplier: 1.0,
constant: additionalExamplesSectionHeight).isActive = true

if (section == 0) {
if section == 0 {
let textView = UITextView()
textView.text = componentDescription
textView.font = MDCTypography.captionFont()
textView.alpha = MDCTypography.captionFontOpacity()

if (UIApplication.shared.userInterfaceLayoutDirection == .leftToRight) {
textView.contentInset = UIEdgeInsetsMake(-8, -5, -8, 5)
if UIApplication.shared.userInterfaceLayoutDirection == .leftToRight {
textView.contentInset = UIEdgeInsets(top: -8, left: -5, bottom: -8, right: 5)
} else {
textView.contentInset = UIEdgeInsetsMake(-8, 5, -8, -5)
textView.contentInset = UIEdgeInsets(top: -8, left: 5, bottom: -8, right: -5)
}

textView.isEditable = false
Expand All @@ -240,12 +240,12 @@ class MDCNodeListViewController: CBCNodeListViewController {
let horizontalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "H:|-(20)-[textView]-(20)-|",
options: [], metrics: nil,
views: ["textView" : textView])
views: ["textView": textView])
let verticalConstraints =
NSLayoutConstraint.constraints(withVisualFormat: "V:|-(40)-[textView(==h)]",
options: [],
metrics: ["h" : textViewHeight],
views: ["textView" : textView])
metrics: ["h": textViewHeight],
views: ["textView": textView])
sectionView.addConstraints(horizontalConstraints)
sectionView.addConstraints(verticalConstraints)
}
Expand All @@ -256,14 +256,14 @@ class MDCNodeListViewController: CBCNodeListViewController {

override func tableView(_ tableView: UITableView,
heightForHeaderInSection section: Int) -> CGFloat {
if (section == Section.description.rawValue) {
if section == Section.description.rawValue {
return descriptionSectionHeight
}
return additionalExamplesSectionHeight
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == Section.description.rawValue) {
if section == Section.description.rawValue {
return 1
}
return node.children.count - 1
Expand All @@ -272,13 +272,13 @@ class MDCNodeListViewController: CBCNodeListViewController {
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "NodeViewTableViewDemoCell")
if ((cell == nil)) {
if cell == nil {
cell = NodeViewTableViewDemoCell.init(style: .subtitle,
reuseIdentifier: "NodeViewTableViewDemoCell")
}

var subtitleText: String?
if (indexPath.section == Section.description.rawValue) {
if indexPath.section == Section.description.rawValue {
subtitleText = node.children[indexPath.row].exampleViewControllerName()
cell!.textLabel!.text = "Demo"
cell!.textLabel!.textColor = UIColor(red: 0.01, green: 0.66, blue: 0.96, alpha: 1)
Expand Down Expand Up @@ -307,7 +307,7 @@ class MDCNodeListViewController: CBCNodeListViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var node = self.node.children[0]
if (indexPath.section == Section.additionalExamples.rawValue) {
if indexPath.section == Section.additionalExamples.rawValue {
node = self.node.children[indexPath.row + 1]
}

Expand All @@ -330,7 +330,7 @@ class MDCNodeListViewController: CBCNodeListViewController {

let textColor = UIColor(white: 0, alpha: 0.8)
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSForegroundColorAttributeName:textColor],
[NSForegroundColorAttributeName: textColor],
for: UIControlState())

var contentFrame = container.contentViewController.view.frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AppBarDelegateForwardingExample: UITableViewController {

self.appBar.navigationBar.tintColor = UIColor.white
appBar.navigationBar.titleTextAttributes =
[ NSForegroundColorAttributeName : UIColor.white ]
[ NSForegroundColorAttributeName: UIColor.white ]

self.addChildViewController(appBar.headerViewController)

Expand Down Expand Up @@ -124,11 +124,11 @@ extension AppBarDelegateForwardingExample {
}

extension AppBarDelegateForwardingExample {
override var childViewControllerForStatusBarHidden : UIViewController? {
override var childViewControllerForStatusBarHidden: UIViewController? {
return appBar.headerViewController
}

override var childViewControllerForStatusBarStyle : UIViewController? {
override var childViewControllerForStatusBarStyle: UIViewController? {
return appBar.headerViewController
}

Expand Down
4 changes: 2 additions & 2 deletions components/AppBar/examples/AppBarImageryExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AppBarImagerySwiftExample: UITableViewController {
// implement -preferredStatusBarStyle.
headerView.tintColor = UIColor.white
appBar.navigationBar.titleTextAttributes =
[ NSForegroundColorAttributeName : UIColor.white ]
[ NSForegroundColorAttributeName: UIColor.white ]

// Allow the header to show more of the image.
headerView.maximumHeight = 200
Expand All @@ -56,7 +56,7 @@ class AppBarImagerySwiftExample: UITableViewController {
appBar.addSubviewsToParent()
}

override var preferredStatusBarStyle : UIStatusBarStyle {
override var preferredStatusBarStyle: UIStatusBarStyle {
// Ensure that our status bar is white.
return .lightContent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ class AppBarInterfaceBuilderSwiftExample: UIViewController, UIScrollViewDelegate
func commonAppBarInterfaceBuilderSwiftExampleSetup() {
appBar.navigationBar.tintColor = UIColor.white
appBar.navigationBar.titleTextAttributes =
[ NSForegroundColorAttributeName : UIColor.white ]
[ NSForegroundColorAttributeName: UIColor.white ]

addChildViewController(appBar.headerViewController)
let headerColor = UIColor(red: 0.01, green: 0.67, blue: 0.96, alpha: 1.0)
appBar.headerViewController.headerView.backgroundColor = headerColor
}


override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -56,7 +55,7 @@ class AppBarInterfaceBuilderSwiftExample: UIViewController, UIScrollViewDelegate
appBar.addSubviewsToParent()
}

override var preferredStatusBarStyle : UIStatusBarStyle {
override var preferredStatusBarStyle: UIStatusBarStyle {
// Ensure that our status bar is white.
return .lightContent
}
Expand Down
8 changes: 4 additions & 4 deletions components/AppBar/examples/AppBarTypicalUseExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AppBarTypicalUseSwiftExample: UITableViewController {
appBar.headerViewController.headerView.backgroundColor = color
appBar.navigationBar.tintColor = UIColor.white
appBar.navigationBar.titleTextAttributes =
[ NSForegroundColorAttributeName : UIColor.white ]
[ NSForegroundColorAttributeName: UIColor.white ]
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -61,20 +61,20 @@ class AppBarTypicalUseSwiftExample: UITableViewController {

self.tableView.layoutMargins = UIEdgeInsets.zero
self.tableView.separatorInset = UIEdgeInsets.zero

self.navigationItem.rightBarButtonItem =
UIBarButtonItem(title: "Right", style: .done, target: nil, action: nil)
}

// Optional step: If you allow the header view to hide the status bar you must implement this
// method and return the headerViewController.
override var childViewControllerForStatusBarHidden : UIViewController? {
override var childViewControllerForStatusBarHidden: UIViewController? {
return appBar.headerViewController
}

// Optional step: The Header View Controller does basic inspection of the header view's background
// color to identify whether the status bar should be light or dark-themed.
override var childViewControllerForStatusBarStyle : UIViewController? {
override var childViewControllerForStatusBarStyle: UIViewController? {
return appBar.headerViewController
}

Expand Down
2 changes: 1 addition & 1 deletion components/AppBar/tests/unit/AppBarEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AppBarEncodingTests: XCTestCase {
appBar.navigationBar.backgroundColor = UIColor.black
appBar.navigationBar.tintColor = UIColor.white
appBar.navigationBar.title = "Title"
appBar.navigationBar.titleTextAttributes = [ NSForegroundColorAttributeName : UIColor.white ]
appBar.navigationBar.titleTextAttributes = [ NSForegroundColorAttributeName: UIColor.white ]
appBar.navigationBar.titleAlignment = .leading

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class ButtonBarTypicalUseSwiftExample: UIViewController {

// MDCButtonBar's sizeThatFits gives a "best-fit" size of the provided items.
let size = buttonBar.sizeThatFits(self.view.bounds.size)
let x = (self.view.bounds.size.width - size.width) / 2;
let y = self.view.bounds.size.height / 2 - size.height;
let x = (self.view.bounds.size.width - size.width) / 2
let y = self.view.bounds.size.height / 2 - size.height
buttonBar.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
buttonBar.autoresizingMask =
[.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
Expand Down Expand Up @@ -97,6 +97,6 @@ extension ButtonBarTypicalUseSwiftExample {

func itemTitleTextAttributes () -> [String:AnyObject] {
let textColor = UIColor(white: 0, alpha: 0.8)
return [NSForegroundColorAttributeName:textColor]
return [NSForegroundColorAttributeName: textColor]
}
}
6 changes: 3 additions & 3 deletions components/ButtonBar/tests/unit/ButtonBarIssue370Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import MaterialComponents
class ButtonBarIssue370Tests: XCTestCase {

var buttonBar: MDCButtonBar!
let globalAttributes = [NSForegroundColorAttributeName:UIColor.blue]
let directAttributes = [NSForegroundColorAttributeName:UIColor.blue]
let fontAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 12)]
let globalAttributes = [NSForegroundColorAttributeName: UIColor.blue]
let directAttributes = [NSForegroundColorAttributeName: UIColor.blue]
let fontAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 12)]

override func setUp() {
buttonBar = MDCButtonBar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Foundation
import MaterialComponents

class ButtonsSimpleExampleSwiftViewController: UIViewController {

let floatingButtonPlusDimension = CGFloat(24)

override func viewDidLoad() {
Expand Down Expand Up @@ -48,8 +48,8 @@ class ButtonsSimpleExampleSwiftViewController: UIViewController {
floatingButton.translatesAutoresizingMaskIntoConstraints = false
floatingButton.addTarget(self, action: #selector(tap), for: .touchUpInside)

let plusShapeLayer = ButtonsTypicalUseSupplemental.createPlusShapeLayer(floatingButton);
floatingButton.layer.addSublayer(plusShapeLayer);
let plusShapeLayer = ButtonsTypicalUseSupplemental.createPlusShapeLayer(floatingButton)
floatingButton.layer.addSublayer(plusShapeLayer)

self.view.addSubview(floatingButton)

Expand Down
Loading

0 comments on commit bb561ac

Please sign in to comment.