Skip to content

Commit

Permalink
feat: ignore private types and variables as they wouldn't be accessib…
Browse files Browse the repository at this point in the history
…le anyway (#47)
  • Loading branch information
ilyapuchka authored and krzysztofzablocki committed Dec 18, 2016
1 parent 7eef0df commit 2444718
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Sourcery/Parsing/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,18 @@ final class Parser {
///
/// - Parameter types: Types and extensions.
/// - Returns: Just types.
internal func uniqueTypes(_ ParserResult: ParserResult) -> [Type] {
internal func uniqueTypes(_ parserResult: ParserResult) -> [Type] {
var unique = [String: Type]()
let types = ParserResult.types
let typealiases = ParserResult.typealiases
let types = parserResult.types
let typealiases = parserResult.typealiases

//replace extensions for type aliases with original types
types
.filter { $0.isExtension == true }
.forEach { $0.localName = typealiases[$0.name] ?? $0.localName }

types
.filter { $0.isExtension == false }
.filter { $0.isExtension == false}
.forEach { unique[$0.name] = $0 }

types.forEach { type in
Expand Down Expand Up @@ -277,7 +277,11 @@ final class Parser {
}
}

return unique.values.sorted { $0.name < $1.name }
return unique.values.filter {
let isPrivate = $0.accessLevel == .private
if isPrivate && self.verbose { print("Skipping \($0.kind) \($0.name) as it is private") }
return !isPrivate
}.sorted { $0.name < $1.name }
}

}
Expand All @@ -301,6 +305,7 @@ extension Parser {

internal func parseVariable(_ source: [String: SourceKitRepresentable], isStatic: Bool = false) -> Variable? {
guard let (name, _, accesibility) = parseTypeRequirements(source),
accesibility != .private,
let type = source[SwiftDocKey.typeName.rawValue] as? String else { return nil }

var writeAccessibility = AccessLevel.none
Expand Down
26 changes: 26 additions & 0 deletions SourceryTests/ParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class ParserSpec: QuickSpec {
return sut?.parseVariable(src)
}

it("ignores private variables") {
expect(parse("private var name: String")).to(beNil())
}

it("extracts standard property correctly") {
expect(parse("var name: String")).to(equal(Variable(name: "name", type: "String", accessLevel: (read: .internal, write: .internal), isComputed: false)))
}
Expand Down Expand Up @@ -121,6 +125,10 @@ class ParserSpec: QuickSpec {
}

context("given struct") {
it("ignores private structs") {
expect(parse("private struct Foo {}")).to(beEmpty())
}

it("extracts properly") {
expect(parse("struct Foo { }"))
.to(equal([
Expand Down Expand Up @@ -166,6 +174,10 @@ class ParserSpec: QuickSpec {
}

context("given class") {
it("ignores private classes") {
expect(parse("private class Foo {}")).to(beEmpty())
}

it("extracts variables properly") {
expect(parse("class Foo { }; extension Foo { var x: Int }"))
.to(equal([
Expand Down Expand Up @@ -240,6 +252,10 @@ class ParserSpec: QuickSpec {
}

context("given enum") {
it("ignores private enums") {
expect(parse("private enum Foo {}")).to(beEmpty())
}

it("extracts empty enum properly") {
expect(parse("enum Foo { }"))
.to(equal([
Expand Down Expand Up @@ -345,13 +361,23 @@ class ParserSpec: QuickSpec {
}

context("given protocol") {
it("ignores private protocols") {
expect(parse("private protocol Foo {}")).to(beEmpty())
}

it("extracts empty protocol properly") {
expect(parse("protocol Foo { }"))
.to(equal([
Protocol(name: "Foo")
]))
}
}

context("given extension") {
it("ignores extension for private type") {
expect(parse("private struct Foo {}; extension Foo { var x: Int { return 0 } }")).to(beEmpty())
}
}

context("given existing types") {
let existingType = Type(name: "Bar", accessLevel: .internal, isExtension: false, variables: [], inheritedTypes: ["TestProtocol"])
Expand Down

0 comments on commit 2444718

Please sign in to comment.