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

Make bundle a computed property #109

Merged
merged 1 commit into from
Jul 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension SourceFile.StringExtension {
// MARK: Properites

var storedProperties: [Property] {
[keyProperty, argumentsProperty, tableProperty, bundleProperty]
[keyProperty, argumentsProperty, tableProperty]
}

let keyProperty = Property(name: "key", type: .identifier(.StaticString))
Expand All @@ -44,7 +44,7 @@ extension SourceFile.StringExtension {

let tableProperty = Property(name: "table", type: OptionalTypeSyntax(wrappedType: .identifier(.String)))

let bundleProperty = Property(name: "bundle", type: .identifier(.BundleDescription))
let bundleProperty = Property(name: "bundle", type: .identifier(.Bundle))

let defaultValueProperty = Property(
name: "defaultValue",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ extension LocalizedStringResourceInitializerSnippet: Snippet {
LabeledExprSyntax(
label: "bundle",
expression: FunctionCallExprSyntax(
calledExpression: MemberAccessExprSyntax(name: "from"),
calledExpression: MemberAccessExprSyntax(name: "atURL"),
leftParen: .leftParenToken(),
rightParen: .rightParenToken()
) {
LabeledExprSyntax(
label: "description",
expression: MemberAccessExprSyntax(variableToken, stringsTable.bundleProperty.name)
expression: MemberAccessExprSyntax(variableToken, stringsTable.bundleProperty.name, "bundleURL")
)
}
)
Expand Down
16 changes: 0 additions & 16 deletions Sources/StringGenerator/Snippets/SourceFileSnippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ struct SourceFileSnippet: Snippet {
StringInitializerSnippet(stringsTable: sourceFile.stringExtension.stringsTableStruct)
}

ExtensionSnippet(extending: .type(.Bundle)) {
ConvertBundleDescriptionMethodSnippet.toFoundationBundle(
from: sourceFile.stringExtension.stringsTableStruct.bundleDescriptionEnum
)
}

ExtensionSnippet(
availability: .wwdc2022,
accessLevel: .private,
extending: [.type(.LocalizedStringResource), .type(.BundleDescription)]
) {
ConvertBundleDescriptionMethodSnippet.toLocalizedStringResourceBundleDescription(
from: sourceFile.stringExtension.stringsTableStruct.bundleDescriptionEnum
)
}

ExtensionSnippet(
availability: .wwdc2022,
extending: .type(.LocalizedStringResource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,6 @@ struct StringInitializerSnippet: Snippet {
}
)
) {
// let bundle: Bundle = .from(description: localizable.bundle) ?? .main
VariableDeclSyntax(bindingSpecifier: .keyword(.let)) {
PatternBindingSyntax(
pattern: IdentifierPatternSyntax(identifier: "bundle"),
typeAnnotation: TypeAnnotationSyntax(
type: IdentifierTypeSyntax(name: .type(.Bundle))
),
initializer: InitializerClauseSyntax(
value: InfixOperatorExprSyntax(
leftOperand: FunctionCallExprSyntax(
callee: MemberAccessExprSyntax(name: "from")
) {
LabeledExprSyntax(
label: "description",
expression: MemberAccessExprSyntax(variableToken, stringsTable.bundleProperty.name)
)
},
operator: BinaryOperatorExprSyntax(operator: .binaryOperator("??")),
rightOperand: MemberAccessExprSyntax(name: "main")
)
)
)
}
// let key = String(describing: localizable.key)
VariableDeclSyntax(bindingSpecifier: .keyword(.let)) {
PatternBindingSyntax(
Expand Down Expand Up @@ -78,7 +55,7 @@ struct StringInitializerSnippet: Snippet {
LabeledExprSyntax(
label: "format",
expression: FunctionCallExprSyntax(
callee: MemberAccessExprSyntax("bundle", "localizedString")
callee: MemberAccessExprSyntax(variableToken, stringsTable.bundleProperty.name, "localizedString")
) {
// forKey: key,
LabeledExprSyntax(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import SwiftSyntax
import SwiftSyntaxBuilder

struct StringStringsTableBundleComputedPropertySnippet: Snippet {
let stringsTable: SourceFile.StringExtension.StringsTableStruct

var syntax: some DeclSyntaxProtocol {
// var bundle: Bundle { ... }
VariableDeclSyntax(
bindingSpecifier: .keyword(.var)
) {
PatternBindingSyntax(
pattern: IdentifierPatternSyntax(identifier: stringsTable.bundleProperty.name),
typeAnnotation: typeAnnotation,
accessorBlock: AccessorBlockSyntax(accessors: .getter(body))
)
}
}

var typeAnnotation: TypeAnnotationSyntax {
TypeAnnotationSyntax(
type: stringsTable.bundleProperty.type
)
}

@CodeBlockItemListBuilder
var body: CodeBlockItemListSyntax {
// #if SWIFT_PACKAGE
// .module
// #else
// Bundle(for: BundleLocator.self)
// #endif
IfConfigDeclSyntax(
reference: "SWIFT_PACKAGE",
elements: .statements([
CodeBlockItemSyntax(item: .expr(ExprSyntax(".module")))
]),
else: .statements([
CodeBlockItemSyntax(item: .expr(ExprSyntax("Bundle(for: BundleLocator.self)")))
])
)
}
}

This file was deleted.

This file was deleted.

Loading