Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect parsing of consequently declared "@" symbols #1239

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions SourceryFramework/Sources/Parsing/Utils/AnnotationsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ public struct AnnotationsParser {
var annotations = inlineFrom(line: (lineNumber, column), stop: &stop)
guard !stop else { return annotations }

for line in lines[0..<lineNumber-1].reversed() {
let reversedArray = lines[0..<lineNumber-1].reversed()
for line in reversedArray {
line.annotations.forEach { annotation in
AnnotationsParser.append(key: annotation.key, value: annotation.value, to: &annotations)
}
if line.type != .comment && line.type != .documentationComment && line.type != .macros && line.type != .propertyWrapper {

if line.type != .comment
&& line.type != .documentationComment
&& line.type != .macros
&& line.type != .propertyWrapper
{
break
}
}
Expand Down Expand Up @@ -215,7 +221,7 @@ public struct AnnotationsParser {
var annotations = Annotations()
let isComment = content.hasPrefix("//") || content.hasPrefix("/*") || content.hasPrefix("*")
let isDocumentationComment = content.hasPrefix("///") || content.hasPrefix("/**")
let isPropertyWrapper = content.hasPrefix("@")
let isPropertyWrapper = content.isPropertyWrapper
let isMacros = content.hasPrefix("#")
var type = Line.LineType.other
if isDocumentationComment {
Expand Down Expand Up @@ -424,3 +430,17 @@ public struct AnnotationsParser {
}

}

// Parses string to see if it is a macros or not
private extension String {
/// @objc // true
/// @objc var paosdjapsodji = 1 // false
/// @MyAttribute(some thing) // true
/// @MyAttribute(some thing) var paosdjapsodji = 1 // false
/// @objc let asdasd // false
var isPropertyWrapper: Bool {
guard hasPrefix("@") else { return false }
guard contains(")") || !contains(" ") else { return false }
return true
}
}
71 changes: 71 additions & 0 deletions SourceryTests/Parsing/FileParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,77 @@ class FileParserSpec: QuickSpec {
expect(result.first).to(equal(expectedType))
}

it("extracts annotations which not affect consequently declared properties") {
let annotations: [[String: NSObject]] = [
["extraAnnotation": NSNumber(value: Float(2))],
[:],
[:]
]
let attributes: [AttributeList] = [
["objc": [Attribute(name: "objc")]],
["objc": [Attribute(name: "objc")]],
[:]
]
let expectedVariables = (1...3)
.map { Variable(name: "property\($0)", typeName: TypeName (name: "Int"), attributes: attributes[$0 - 1], annotations: annotations[$0 - 1], definedInTypeName: TypeName (name: "Foo" )) }
let expectedType = Class(name: "Foo", variables: expectedVariables, annotations: [:])
let result = parse(
"""
class Foo {\n
// sourcery: extraAnnotation = 2
@objc var property1: Int
@objc var property2: Int
var property3: Int
}
"""
)
expect(result.first).to(equal(expectedType))
}

it("extracts annotations and attributes which not affect consequently declared properties") {
let annotations: [[String: NSObject]] = [
["extraAnnotation": NSNumber(value: Float(2))],
[:],
[:],
[:],
[:]
]
let attributes: [AttributeList] = [
["objc": [Attribute(name: "objc")]],
["objc": [Attribute(name: "objc")]],
["MyAttribute": [Attribute(name: "MyAttribute")]],
["MyAttribute2": [Attribute(name: "MyAttribute2", arguments: ["0": "Hello" as NSString], description: "@MyAttribute2(Hello there)")]],
[:]
]
let expectedVariables = (1...5)
.map {
Variable(
name: "property\($0)",
typeName: TypeName (name: "Int"),
attributes: attributes[$0 - 1],
annotations: annotations[$0 - 1],
definedInTypeName: TypeName (name: "Foo" )
)
}
let expectedType = Class(name: "Foo", variables: expectedVariables, annotations: [:])
let result = parse(
"""
class Foo {\n
// sourcery: extraAnnotation = 2
@objc
var property1: Int
@objc var property2: Int
@MyAttribute
var property3: Int
@MyAttribute2(Hello there)
var property4: Int
var property5: Int
}
"""
)
expect(result.first).to(equal(expectedType))
}

it("extracts annotations when declaration has an attribute on the preceding line") {
let annotations = ["Annotation": NSNumber(value: true)]

Expand Down