Skip to content

Commit 8583c03

Browse files
authored
fix: parsing required attribute (krzysztofzablocki#287)
1 parent bbfb9bd commit 8583c03

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fixed creating cache folder when cache is disabled
2323
- Fixed parsing multiple enum cases annotations
2424
- Fixed parsing inline annotations when there is an access level or attribute
25+
- Fixed parsing `required` attribute
2526

2627
### Internal changes
2728

Sourcery/Parsing/FileParser.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,13 @@ extension FileParser {
696696
attributesValue.map({ $0.values }).joined()
697697
.flatMap(Attribute.Identifier.init(identifier:))
698698
.forEach {
699-
let attributeRange = prefix.range(of: $0.description, options: .backwards)
700-
699+
var attributeRange = prefix.range(of: $0.description, options: .backwards)
701700
// we expect all attributes to be prefixed with `@`
702-
// but `convenience` attribute does not need it...
703-
if $0 == Attribute.Identifier.convenience {
701+
// but `convenience` and `required` attribute does not need it...
702+
if $0 == Attribute.Identifier.convenience || $0 == Attribute.Identifier.required {
704703
prefix = prefix.replacingCharacters(in: attributeRange, with: "@\($0)") as NSString
704+
attributeRange.length += 1
705+
attributeRange.location -= 1
705706
}
706707
ranges.append(attributeRange)
707708
}

SourceryRuntime/Sources/Attribute.swift

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public class Attribute: NSObject, AutoCoding, AutoEquatable, AutoDiffable, AutoJ
8686
switch self {
8787
case .convenience:
8888
return "convenience"
89+
case .required:
90+
return "required"
8991
default:
9092
return "@\(name)"
9193
}

SourceryTests/Parsing/FileParser + AttributesSpec.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ class FileParserAttributesSpec: QuickSpec {
5858
"objc": Attribute(name: "objc", arguments: ["some": NSNumber(value: true)], description: "@objc(some)")
5959
]))
6060

61-
expect(parse("class Foo { @nonobjc convenience init() {} }").first?.initializers.first?.attributes).to(equal([
61+
expect(parse("class Foo { @nonobjc convenience required init() {} }").first?.initializers.first?.attributes).to(equal([
6262
"nonobjc": Attribute(name: "nonobjc"),
63-
"convenience": Attribute(name: "convenience", description: "convenience")
63+
"convenience": Attribute(name: "convenience", description: "convenience"),
64+
"required": Attribute(name: "required", description: "required")
6465
]))
6566
}
6667

0 commit comments

Comments
 (0)