Skip to content

Commit

Permalink
Move SyntaxParser to Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yume190 committed Sep 18, 2023
1 parent 9bb2e5d commit 2726d6c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let package = Package(
"Rainbow",
"PathKit",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SKClient", package: "TypeFill"),
]
),
Expand All @@ -66,7 +66,7 @@ let package = Package(
name: "LeakDetectTests",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SKClient", package: "TypeFill"),
"LeakDetectKit",
"HumanString",
Expand Down
24 changes: 9 additions & 15 deletions Sources/LeakDetectKit/Escaping/EscapingDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import SwiftSyntax
import SwiftSyntaxParser
import SwiftParser

public enum EscapingDetector {
public static func detectWithTypeAlias(code: String) -> Bool {
Expand All @@ -17,14 +17,10 @@ public enum EscapingDetector {

public static func detect(code: String) -> Bool {
let target: String = code
do {
let source: SourceFileSyntax = try SyntaxParser.parse(source: target)
let visitor = TypeEscapeVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor.isEscape
} catch {
return false
}
let source: SourceFileSyntax = Parser.parse(source: target)
let visitor = TypeEscapeVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor.isEscape
}

public static func detect(type: TypeSyntax) -> Bool {
Expand All @@ -36,12 +32,10 @@ public enum EscapingDetector {
private static func _detect(code: String) throws -> FunctionParameterListEscapeVisitor? {
let target: String = code

if let source: SourceFileSyntax = try? SyntaxParser.parse(source: target) {
let visitor = FunctionParameterListEscapeVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor
}
return nil
let source: SourceFileSyntax = Parser.parse(source: target)
let visitor = FunctionParameterListEscapeVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor
}

public static func detect(code: String, index: Int) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
import SwiftSyntax
import SwiftSyntaxParser
import SwiftParser

/// typealias C = (@escaping (@escaping (Int) -> ()) -> (), Int) -> ()
/// typealias C = (((@escaping (Int) -> ()) -> ()), Int) -> ()
Expand Down Expand Up @@ -58,9 +58,8 @@ final class FunctionParameterListEscapeVisitor: SyntaxVisitor {
}
let target = "typealias A = \(node.description)"

if let source = try? SyntaxParser.parse(source: target) {
walk(source)
}
let source = Parser.parse(source: target)
walk(source)

return .skipChildren
}
Expand Down
1 change: 0 additions & 1 deletion Sources/LeakDetectKit/Escaping/TypeEscapeVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Foundation
import SwiftSyntax
import SwiftSyntaxParser

/// typealias A = @escaping () -> Void
/// typealias B = (() -> Void)
Expand Down
14 changes: 5 additions & 9 deletions Sources/LeakDetectKit/Escaping/WeakDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//

import Foundation
import SwiftParser
import SwiftSyntax
import SwiftSyntaxParser

public enum WeakDetector {
/// input:
Expand All @@ -16,14 +16,10 @@ public enum WeakDetector {
/// var c: Int
public static func detect(code: String) -> Bool {
let target: String = code
do {
let source: SourceFileSyntax = try SyntaxParser.parse(source: target)
let visitor = WeakVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor.isHaveWeak || visitor.isHaveUnowned
} catch {
return false
}
let source: SourceFileSyntax = Parser.parse(source: target)
let visitor = WeakVisitor(viewMode: .sourceAccurate)
visitor.walk(source)
return visitor.isHaveWeak || visitor.isHaveUnowned
}
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/LeakDetectKit/Pipeline/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import PathKit
import Rainbow
import SKClient
import SourceKittenFramework
import SwiftSyntaxParser
import SwiftParser

public struct Pipeline {
public let filePath: String
Expand All @@ -23,14 +23,14 @@ public struct Pipeline {
let path = Path(filePath)
let code = try path.read(.utf8)

try self.init(filePath, code, arguments)
self.init(filePath, code, arguments)
}

public init(_ filePath: String, _ code: String, _ arguments: [String]) throws {
public init(_ filePath: String, _ code: String, _ arguments: [String]) {
self.filePath = filePath
self.code = code
self.arguments = arguments
(client, rewriter) = try Pipeline.stage1(filePath, self.code, arguments)
(client, rewriter) = Pipeline.stage1(filePath, self.code, arguments)
visitors = Visitors(client, rewriter)
}

Expand All @@ -39,8 +39,8 @@ public struct Pipeline {
_ filePath: String,
_ code: String,
_ arguments: [String]
) throws -> (SKClient, CaptureListRewriter) {
let source = try SyntaxParser.parse(source: code)
) -> (SKClient, CaptureListRewriter) {
let source = Parser.parse(source: code)
let rewriter = CaptureListRewriter()
let newSource = rewriter.visit(source)
let newCode = newSource.description
Expand Down
3 changes: 1 addition & 2 deletions Tests/LeakDetectTests/Capture/LeakTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
@testable import LeakDetectKit
@testable import SKClient
import SwiftSyntax
import SwiftSyntaxParser
import XCTest

/// Type: class, struct, enum, protocol, extension(class, struct), ...
Expand All @@ -24,7 +23,7 @@ class _LeakTests: XCTestCase {

static func detect(_ code: String, _ sdk: SDK = .macosx) throws -> (Pipeline, [LeakResult]) {
let path = "code: /temp.swift"
let pipeline = try Pipeline(
let pipeline = Pipeline(
path,
code,
sdk.args + Self._load + [path]
Expand Down
10 changes: 5 additions & 5 deletions Tests/LeakDetectTests/Concept/CaptureListRewriteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
@testable import LeakDetectKit
import SwiftSyntaxParser
import SwiftParser
import XCTest

final class CaptureListRewriteTests: _LeakTests {
Expand All @@ -28,7 +28,7 @@ final class CaptureListRewriteTests: _LeakTests {
}
}
"""
let root = try SyntaxParser.parse(source: code)
let root = Parser.parse(source: code)

let rewriter = CaptureListRewriter()
let newCode = rewriter.visit(root).description
Expand All @@ -53,7 +53,7 @@ final class CaptureListRewriteTests: _LeakTests {
}
}
"""
let root = try SyntaxParser.parse(source: code)
let root = Parser.parse(source: code)

let rewriter = CaptureListRewriter()
let newCode = rewriter.visit(root).description
Expand All @@ -78,7 +78,7 @@ final class CaptureListRewriteTests: _LeakTests {
}
}
"""
let root = try SyntaxParser.parse(source: code)
let root = Parser.parse(source: code)

let rewriter = CaptureListRewriter()
let newCode = rewriter.visit(root).description
Expand All @@ -96,7 +96,7 @@ extension CaptureListRewriteTests {
}
"""

let source = try SyntaxParser.parse(source: code)
let source = Parser.parse(source: code)
let rewriter = CaptureListRewriter()
_ = rewriter.visit(source)

Expand Down

0 comments on commit 2726d6c

Please sign in to comment.