Skip to content

Commit

Permalink
Lint components type (#102)
Browse files Browse the repository at this point in the history
* Extend HooksRule on extension

* Add ComponentAsStructRule

* Fix typo

* Apply swiftformat

* Fix violation description

* Fix rule name
  • Loading branch information
hodovani committed May 30, 2019
1 parent 30a2b9d commit 770e1c4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 3 deletions.
43 changes: 43 additions & 0 deletions Sources/TokamakLint/Rules/ComponentIsStructRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// ComponentIsStructRule.swift
// TokamakLint
//
// Created by Matvii Hodovaniuk on 5/30/19.
//

import SwiftSyntax

struct ComponentIsStructRule: Rule {
static let description = RuleDescription(
type: ComponentIsStructRule.self,
name: "Component as struct",
description: "Components can only be declared as structs"
)

public static func validate(visitor: TokenVisitor) -> [StyleViolation] {
var violations: [StyleViolation] = []

// search usage of componentProtocols
componentProtocols.forEach { prot in
let components = visitor.root.children(with: prot)
components.forEach { component in

// check that components is struct
guard let componentCodeBlockItem = component.firstParent(of: .codeBlockItem) else { return }
guard componentCodeBlockItem.children.first?.text == SyntaxKind.structDecl.rawValue else {
violations.append(StyleViolation(
ruleDescription: ComponentIsStructRule.description,
location: Location(
file: visitor.path,
line: componentCodeBlockItem.range.startRow,
character: componentCodeBlockItem.range.startColumn
)
))
return
}
}
}

return violations
}
}
4 changes: 2 additions & 2 deletions Sources/TokamakLint/Rules/HooksRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct HooksRule: Rule {
!codeBlockItemList.children.contains(hookCodeBlockItem)
else { return }
violations.append(StyleViolation(
ruleDescription: OneRenderFunctionRule.description,
ruleDescription: HooksRule.description,
location: Location(
file: visitor.path,
line: hook.range.startRow,
Expand Down Expand Up @@ -81,7 +81,7 @@ struct HooksRule: Rule {
guard memberCodeBlockItemList.children.contains(safeState) else {
violations.append(
StyleViolation(
ruleDescription: OneRenderFunctionRule.description,
ruleDescription: HooksRule.description,
location: Location(
file: visitor.path,
line: state.range.startRow,
Expand Down
12 changes: 12 additions & 0 deletions Tests/TokamakCLITests/TokamakLintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,16 @@ final class TokamakLintTests: XCTestCase {
XCTAssertEqual(result[i].location.line, line)
}
}

func testComponentIsStructRulePositive() throws {
let path = "\(try srcRoot())/ComponentAsStructPositive.swift"
let result = try ComponentIsStructRule.validate(path: path)
XCTAssertEqual(result.count, 0)
}

func testComponentIsStructRuleNegative() throws {
let path = "\(try srcRoot())/ComponentAsStructNegative.swift"
let result = try ComponentIsStructRule.validate(path: path)
XCTAssertEqual(result.count, 2)
}
}
4 changes: 4 additions & 0 deletions Tokamak.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

/* Begin PBXBuildFile section */
A6BFBFEC229D301000F2B06F /* GetComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BFBFEB229D301000F2B06F /* GetComponents.swift */; };
A6BFBFEE229FFEFF00F2B06F /* ComponentIsStructRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BFBFED229FFEFF00F2B06F /* ComponentIsStructRule.swift */; };
A6E7BC612293D7B20042E787 /* HooksRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E7BC602293D7B20042E787 /* HooksRule.swift */; };
OBJ_325 /* ArgumentList.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_239 /* ArgumentList.swift */; };
OBJ_326 /* ArgumentListManipulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_240 /* ArgumentListManipulator.swift */; };
Expand Down Expand Up @@ -400,6 +401,7 @@

/* Begin PBXFileReference section */
A6BFBFEB229D301000F2B06F /* GetComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetComponents.swift; sourceTree = "<group>"; };
A6BFBFED229FFEFF00F2B06F /* ComponentIsStructRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComponentIsStructRule.swift; sourceTree = "<group>"; };
A6E7BC602293D7B20042E787 /* HooksRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HooksRule.swift; sourceTree = "<group>"; };
OBJ_100 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
OBJ_102 /* BaselineConstraint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaselineConstraint.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -908,6 +910,7 @@
OBJ_150 /* PropsIsEquatableRule.swift */,
OBJ_151 /* RenderGetsHooksRule.swift */,
A6E7BC602293D7B20042E787 /* HooksRule.swift */,
A6BFBFED229FFEFF00F2B06F /* ComponentIsStructRule.swift */,
);
path = Rules;
sourceTree = "<group>";
Expand Down Expand Up @@ -1919,6 +1922,7 @@
OBJ_550 /* Location.swift in Sources */,
OBJ_551 /* Node.swift in Sources */,
OBJ_552 /* RuleDescription.swift in Sources */,
A6BFBFEE229FFEFF00F2B06F /* ComponentIsStructRule.swift in Sources */,
OBJ_553 /* StyleViolation.swift in Sources */,
A6BFBFEC229D301000F2B06F /* GetComponents.swift in Sources */,
OBJ_554 /* TokamakLint.swift in Sources */,
Expand Down
12 changes: 12 additions & 0 deletions ValidationTests/ComponentAsStructNegative.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class BeatlesComponents: CompositeComponent {
let willThereBeAnAnswer = true
let itBe = "Whisper words of wisdom"
}

class LedZeppelinLeaf: PureLeafComponent {
let doIhaveToGo = false
enum WhatMakeMe: String {
case mad = "the letter you wrote me"
case sad = "the news that letters told me"
}
}
16 changes: 16 additions & 0 deletions ValidationTests/ComponentAsStructPositive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct List: PureLeafComponent {
struct Props: Equatable {
let model: [AppRoute]
let onSelect: Handler<CellPath>
}

static func render(props: Props) -> AnyNode {
return ListView<Cells>.node(.init(
Style([
Edges.equal(to: .parent),
]),
model: [props.model],
onSelect: props.onSelect
))
}
}
2 changes: 1 addition & 1 deletion ValidationTests/HooksRuleNegative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct HookedLeafComponent: LeafComponent {
}
}

// don't use Hooks in extesion on non first level
// don't use Hooks in extension on non first level
extension Hooks {
var blah: State<Int> {
if true {
Expand Down

0 comments on commit 770e1c4

Please sign in to comment.