-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from nerdsupremacist/diagnostics
Diagnostics
- Loading branch information
Showing
11 changed files
with
138 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import Foundation | ||
|
||
struct Warning: CustomStringConvertible { | ||
let location: Location | ||
let descriptionText: String | ||
|
||
var description: String { | ||
return "\(location.locationDescription): warning: \(descriptionText)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Sources/Graphaello/Processing/Pipeline/Diagnostics/Array+WarningDiagnoser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import Foundation | ||
|
||
extension Array: WarningDiagnoser where Element: WarningDiagnoser { | ||
|
||
func diagnose(parsed: Struct<Stage.Parsed>) throws -> [Warning] { | ||
return try flatMap { try $0.diagnose(parsed: parsed) } | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
Sources/Graphaello/Processing/Pipeline/Diagnostics/UnusedWarningDiagnoser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import Foundation | ||
import SwiftSyntax | ||
import SourceKittenFramework | ||
|
||
private let swiftUIViewProtocols: Set<String> = ["View"] | ||
|
||
struct UnusedWarningDiagnoser: WarningDiagnoser { | ||
func diagnose(parsed: Struct<Stage.Parsed>) throws -> [Warning] { | ||
guard !swiftUIViewProtocols.intersection(parsed.inheritedTypes).isEmpty else { | ||
return [] | ||
} | ||
|
||
return try parsed.properties.flatMap { try diagnose(property: $0, from: parsed) } | ||
} | ||
|
||
private func diagnose(property: Property<Stage.Parsed>, from parsed: Struct<Stage.Parsed>) throws -> [Warning] { | ||
guard property.graphqlPath != nil, | ||
property.name != "id" else { return [] } | ||
|
||
let verifier = UsageVerifier(property: property) | ||
let syntax = try parsed.code.syntaxTree() | ||
|
||
verifier.walk(syntax) | ||
|
||
guard !verifier.isUsed else { return [] } | ||
return [Warning(location: property.code.location, | ||
descriptionText: "Unused Property `\(property.name)` belongs to a View and is fetching data from GraphQL. This can be wasteful. Consider using it or removing the property.")] | ||
} | ||
} | ||
|
||
class UsageVerifier: SyntaxVisitor { | ||
let property: Property<Stage.Parsed> | ||
|
||
private(set) var shouldUseSelf = false | ||
private(set) var isUsed = false | ||
|
||
init(property: Property<Stage.Parsed>) { | ||
self.property = property | ||
super.init() | ||
} | ||
|
||
override func visitPost(_ node: IdentifierExprSyntax) { | ||
if node.identifier.text == property.name { | ||
isUsed = true | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Sources/Graphaello/Processing/Pipeline/Diagnostics/WarningDiagnoser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
import Foundation | ||
|
||
protocol WarningDiagnoser { | ||
func diagnose(parsed: Struct<Stage.Parsed>) throws -> [Warning] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters