-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CursorInfo referenced USR code from swiftlint (#605)
From realm/SwiftLint#2818
- Loading branch information
1 parent
73505fd
commit 43fa288
Showing
5 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// CursorInfo+USR.swift | ||
// SourceKittenFramework | ||
// | ||
// Created by Colton Schlosser on 7/21/19. | ||
// Copyright © 2019 SourceKitten. All rights reserved. | ||
// | ||
|
||
import SWXMLHash | ||
|
||
public extension Dictionary where Key == String, Value == SourceKitRepresentable { | ||
var referencedUSRs: [String] { | ||
if let usr = self["key.usr"] as? String, | ||
let kind = self["key.kind"] as? String, | ||
kind.contains("source.lang.swift.ref") { | ||
if let relatedDecls = self["key.related_decls"] as? [[String: SourceKitRepresentable]] { | ||
return [usr] + relatedDecls.compactMap { ($0["key.annotated_decl"] as? String)?.relatedNameUSR } | ||
} else { | ||
return [usr] | ||
} | ||
} | ||
|
||
return [] | ||
} | ||
} | ||
|
||
private extension String { | ||
var relatedNameUSR: String? { | ||
return SWXMLHash.parse(self)["RelatedName"].element?.value(ofAttribute: "usr") | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
Tests/SourceKittenFrameworkTests/CursorInfoParsingTests.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,53 @@ | ||
// | ||
// CursorInfoParsingTests.swift | ||
// SourceKittenFramework | ||
// | ||
// Created by Colton Schlosser on 7/21/19. | ||
// Copyright © 2019 SourceKitten. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
import XCTest | ||
|
||
final class CursorInfoParsingTests: XCTestCase { | ||
|
||
func testNoReferencedUSR() { | ||
let cursorInfo: [String: SourceKitRepresentable] = [ | ||
"key.usr": "s:4main5limitL_Sivp", | ||
"key.kind": "source.lang.swift.decl.var.local" | ||
] | ||
|
||
XCTAssertEqual(cursorInfo.referencedUSRs, []) | ||
} | ||
|
||
func testSingleReferencedUSR() { | ||
let cursorInfo: [String: SourceKitRepresentable] = [ | ||
"key.usr": "s:4main5limitL_Sivp", | ||
"key.kind": "source.lang.swift.ref.var.local" | ||
] | ||
|
||
XCTAssertEqual(cursorInfo.referencedUSRs, ["s:4main5limitL_Sivp"]) | ||
} | ||
|
||
func testRelatedReferencedUSR() { | ||
let cursorInfo: [String: SourceKitRepresentable] = [ | ||
"key.usr": "s:4main5limitL_Sivp", | ||
"key.kind": "source.lang.swift.ref.var.local", | ||
"key.related_decls": [ | ||
["key.annotated_decl": "<RelatedName usr=\"s:4main5limit33_BF49849BA67C991867DA082EF03F5F16LLSivp\">limit</RelatedName>"] | ||
] | ||
] | ||
|
||
XCTAssertEqual(cursorInfo.referencedUSRs, ["s:4main5limitL_Sivp", "s:4main5limit33_BF49849BA67C991867DA082EF03F5F16LLSivp"]) | ||
} | ||
} | ||
|
||
extension CursorInfoParsingTests { | ||
static var allTests: [(String, (CursorInfoParsingTests) -> () throws -> Void)] { | ||
return [ | ||
("testNoReferencedUSR", testNoReferencedUSR), | ||
("testSingleReferencedUSR", testSingleReferencedUSR), | ||
("testRelatedReferencedUSR", testRelatedReferencedUSR) | ||
] | ||
} | ||
} |
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