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

[Swift APIView] Fix issue with read-only properties #3961

Merged
merged 1 commit into from
Aug 17, 2022
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
5 changes: 5 additions & 0 deletions src/swift/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release History

## Version 0.1.3 (Unreleased)
- Support for getter/setter blocks.
- Fixed issue where read-only properties would appear as
read/write due to not displaying `{ get }` syntax.

## Version 0.1.2 (Unreleased)
- Fixed issue where empty extension blocks were displayed.
- Temporarily will allow duplicate IDs. This will result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIView;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -311,7 +311,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIView;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class VariableModel: Tokenizable, Commentable, AccessLevelProtocol {
case let .initializerList(initializerList):
initializers = initializerList.compactMap { InitializerItemModel(from: $0) }
case let .codeBlock(ident, typeAnno, _):
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil)]
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil, hasGetter: true)]
case let .getterSetterKeywordBlock(ident, typeAnno, _):
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil)]
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil, hasGetter: true, hasSetter: true)]
case let .getterSetterBlock(ident, typeAnno, _):
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil)]
initializers = [InitializerItemModel(name: ident.textDescription, typeModel: TypeAnnotationModel(from: typeAnno), defaultValue: nil, hasGetter: true, hasSetter: true)]
case let .willSetDidSetBlock(ident, typeAnno, expression, _):
// the willSetDidSet block is irrelevant from an API perspective
// so we ignore it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ struct InitializerItemModel: Tokenizable {
let name: String
let typeModel: TypeAnnotationModel?
let defaultValue: String?
let hasGetter: Bool
let hasSetter: Bool

init(from source: PatternInitializer) {
name = source.name!
typeModel = TypeAnnotationModel(from: source.typeModel)
defaultValue = source.defaultValue
hasGetter = false
hasSetter = false
}

init(name: String, typeModel: TypeAnnotationModel?, defaultValue: String?) {
init(name: String, typeModel: TypeAnnotationModel?, defaultValue: String?, hasGetter: Bool = false, hasSetter: Bool = false) {
self.name = name
self.typeModel = typeModel
self.defaultValue = defaultValue
self.hasGetter = hasGetter
self.hasSetter = hasSetter
}

func tokenize(apiview a: APIViewModel) {
Expand All @@ -55,5 +61,17 @@ struct InitializerItemModel: Tokenizable {
a.punctuation("=", prefixSpace: true, postfixSpace: true)
a.literal(defaultValue)
}

if hasGetter && hasSetter {
a.punctuation("{", prefixSpace: true, postfixSpace: true)
a.keyword("get")
a.punctuation(",", postfixSpace: true)
a.keyword("set")
a.punctuation("}", prefixSpace: true, postfixSpace: true)
} else if hasGetter {
a.punctuation("{", prefixSpace: true, postfixSpace: true)
a.keyword("get")
a.punctuation("}", prefixSpace: true, postfixSpace: true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 0.1.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIViewCore;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -685,7 +685,7 @@
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 0.1.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIViewCore;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
16 changes: 15 additions & 1 deletion src/swift/SwiftAPIViewTests/Sources/PropertiesTestFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation

public class PropertiesTestStruct {

var totalSteps: Int = 0 {
public var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")

Expand All @@ -40,5 +40,19 @@ public class PropertiesTestStruct {
}
}
}

public var someReadOnly: String {
return "test"
}

public var someReadWrite: String {
get {
return "test"
}

set {
self = newValue
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIViewTests;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -403,7 +403,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.microsoft.SwiftAPIViewTests;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down