Skip to content

Commit

Permalink
Fixed incorrect parsing of consequently declared "@" symbols (#1239)
Browse files Browse the repository at this point in the history
* Fixed bug with incorrect parsing of consequently declared "@" symbols

* renamed `isMacros` to `isPropertyWrapper` for clarity
  • Loading branch information
art-divin authored Dec 17, 2023
1 parent 1502c0f commit db7cc85
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
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

0 comments on commit db7cc85

Please sign in to comment.