Move getRender to Node extension (#95)
* Make node final class * Move getRender to Node extension * Apply swiftformat * Regenerate project
This commit is contained in:
parent
8377bf52eb
commit
7d0b41ce4c
|
@ -10,56 +10,58 @@ import SwiftSyntax
|
|||
|
||||
extension Array: Error where Element == StyleViolation {}
|
||||
|
||||
func getRender(from node: Node, at file: String) throws -> Node {
|
||||
let renders = node.children(with: "render").filter {
|
||||
// check if render type is function
|
||||
var memberDeclListItem = $0
|
||||
while memberDeclListItem.text != SyntaxKind.memberDeclListItem.rawValue
|
||||
&& memberDeclListItem.parent != nil {
|
||||
guard let parent = memberDeclListItem.parent else { break }
|
||||
memberDeclListItem = parent
|
||||
extension Node {
|
||||
func getRender(at file: String) throws -> Node {
|
||||
let renders = children(with: "render").filter {
|
||||
// check if render type is function
|
||||
var memberDeclListItem = $0
|
||||
while memberDeclListItem.text != SyntaxKind.memberDeclListItem.rawValue
|
||||
&& memberDeclListItem.parent != nil {
|
||||
guard let parent = memberDeclListItem.parent else { break }
|
||||
memberDeclListItem = parent
|
||||
}
|
||||
guard let functionDecl = memberDeclListItem.children.first,
|
||||
functionDecl.text == SyntaxKind.functionDecl.rawValue else { return false }
|
||||
|
||||
// check if render is static
|
||||
let staticModifier = memberDeclListItem.children(
|
||||
with: SyntaxKind.declModifier.rawValue
|
||||
).filter {
|
||||
guard let child = $0.children.first else { return false }
|
||||
return child.text == "static"
|
||||
}
|
||||
guard staticModifier.first != nil else { return false }
|
||||
|
||||
// check if render is on first layer of component
|
||||
return functionDecl.children.map {
|
||||
$0.text
|
||||
}.contains("render")
|
||||
}
|
||||
guard let functionDecl = memberDeclListItem.children.first,
|
||||
functionDecl.text == SyntaxKind.functionDecl.rawValue else { return false }
|
||||
|
||||
// check if render is static
|
||||
let staticModifier = memberDeclListItem.children(
|
||||
with: SyntaxKind.declModifier.rawValue
|
||||
).filter {
|
||||
guard let child = $0.children.first else { return false }
|
||||
return child.text == "static"
|
||||
}
|
||||
guard staticModifier.first != nil else { return false }
|
||||
|
||||
// check if render is on first layer of component
|
||||
return functionDecl.children.map {
|
||||
$0.text
|
||||
}.contains("render")
|
||||
}
|
||||
|
||||
guard renders.count == 1 else {
|
||||
if renders.count > 1 {
|
||||
throw renders.map {
|
||||
StyleViolation(
|
||||
guard renders.count == 1 else {
|
||||
if renders.count > 1 {
|
||||
throw renders.map {
|
||||
StyleViolation(
|
||||
ruleDescription: OneRenderFunctionRule.description,
|
||||
location: Location(
|
||||
file: file,
|
||||
line: $0.range.startRow,
|
||||
character: $0.range.startColumn
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
throw [StyleViolation(
|
||||
ruleDescription: OneRenderFunctionRule.description,
|
||||
location: Location(
|
||||
file: file,
|
||||
line: $0.range.startRow,
|
||||
character: $0.range.startColumn
|
||||
line: self.range.startRow,
|
||||
character: self.range.startColumn
|
||||
)
|
||||
)
|
||||
)]
|
||||
}
|
||||
} else {
|
||||
throw [StyleViolation(
|
||||
ruleDescription: OneRenderFunctionRule.description,
|
||||
location: Location(
|
||||
file: file,
|
||||
line: node.range.startRow,
|
||||
character: node.range.startColumn
|
||||
)
|
||||
)]
|
||||
}
|
||||
}
|
||||
|
||||
return renders[0]
|
||||
return renders[0]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import Foundation
|
||||
import SwiftSyntax
|
||||
|
||||
public class Node {
|
||||
final class Node {
|
||||
var text: String
|
||||
private(set) var children = [Node]()
|
||||
private(set) weak var parent: Node?
|
||||
|
|
|
@ -16,7 +16,7 @@ struct OneRenderFunctionRule: Rule {
|
|||
|
||||
static func validate(visitor: TokenVisitor) -> [StyleViolation] {
|
||||
do {
|
||||
_ = try getRender(from: visitor.root, at: visitor.path)
|
||||
_ = try visitor.root.getRender(at: visitor.path)
|
||||
} catch let error as [StyleViolation] {
|
||||
return error
|
||||
} catch {
|
||||
|
|
|
@ -16,7 +16,7 @@ struct RenderGetsHooksRule: Rule {
|
|||
|
||||
public static func validate(visitor: TokenVisitor) -> [StyleViolation] {
|
||||
do {
|
||||
let renderFunction = try getRender(from: visitor.root, at: visitor.path)
|
||||
let renderFunction = try visitor.root.getRender(at: visitor.path)
|
||||
guard let codeBlock = renderFunction.firstParent(
|
||||
of: SyntaxKind.codeBlockItem.rawValue
|
||||
) else { return [StyleViolation(
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -6,6 +6,48 @@
|
|||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakAppKit"
|
||||
BuildableName = "TokamakAppKit.framework"
|
||||
BlueprintName = "TokamakAppKit"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakCLI"
|
||||
BuildableName = "TokamakCLI"
|
||||
BlueprintName = "TokamakCLI"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakDemo"
|
||||
BuildableName = "TokamakDemo.framework"
|
||||
BlueprintName = "TokamakDemo"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
|
@ -48,34 +90,6 @@
|
|||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakAppKit"
|
||||
BuildableName = "TokamakAppKit.framework"
|
||||
BlueprintName = "TokamakAppKit"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakCLI"
|
||||
BuildableName = "TokamakCLI"
|
||||
BlueprintName = "TokamakCLI"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
|
@ -90,20 +104,6 @@
|
|||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakDemo"
|
||||
BuildableName = "TokamakDemo.framework"
|
||||
BlueprintName = "TokamakDemo"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
|
@ -149,9 +149,9 @@
|
|||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "Tokamak::TokamakUIKit"
|
||||
BuildableName = "TokamakUIKit.framework"
|
||||
BlueprintName = "TokamakUIKit"
|
||||
BlueprintIdentifier = "Tokamak::TokamakAppKit"
|
||||
BuildableName = "TokamakAppKit.framework"
|
||||
BlueprintName = "TokamakAppKit"
|
||||
ReferencedContainer = "container:Tokamak.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
|
|
Loading…
Reference in New Issue