Skip to content

Commit

Permalink
Merge pull request #21 from p-x9/feature/support-literal
Browse files Browse the repository at this point in the history
Modified to omit type specification when using literal
  • Loading branch information
p-x9 authored Jan 15, 2024
2 parents bbc74e8 + 4a08fb8 commit 4640b44
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ let package = Package(
name: "AliasTests",
dependencies: [
"Alias",
"AliasPlugin",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
Expand Down
35 changes: 32 additions & 3 deletions Sources/AliasPlugin/AliasMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,37 @@ extension AliasMacro {
}

guard let binding = varDecl.bindings.first,
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,
binding.typeAnnotation != nil else {
context.diagnose(AliasMacroDiagnostic.specifyTypeExplicitly.diagnose(at: varDecl))
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier else {
context.diagnose(AliasMacroDiagnostic.unsupportedDeclaration.diagnose(at: varDecl))
return []
}

var typeAnnotation: TypeAnnotationSyntax?
if let specifiedType = binding.typeAnnotation {
typeAnnotation = specifiedType
} else if let defaultValue = binding.initializer?.value {
var type: TypeSyntax?
switch defaultValue {
case _ where defaultValue.is(StringLiteralExprSyntax.self):
type = "Swift.String"
case _ where defaultValue.is(IntegerLiteralExprSyntax.self):
type = "Swift.Int"
case _ where defaultValue.is(FloatLiteralExprSyntax.self):
type = "Swift.Double"
case _ where defaultValue.is(BooleanLiteralExprSyntax.self):
type = "Swift.Bool"
default: break
}
if let type {
typeAnnotation = .init(
type: type,
trailingTrivia: .space
)
}
}

guard let typeAnnotation else {
context.diagnose(AliasMacroDiagnostic.specifyTypeExplicitly.diagnose(at: binding))
return []
}

Expand All @@ -147,6 +175,7 @@ extension AliasMacro {
binding
.with(\.pattern, .init(IdentifierPatternSyntax(identifier: .identifier(arguments.alias))))
.with(\.initializer, nil)
.with(\.typeAnnotation, typeAnnotation)
.with(\.accessorBlock, .init(accessors:
.accessors(
AccessorDeclListSyntax {
Expand Down
31 changes: 27 additions & 4 deletions Tests/AliasTests/AliasTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ final class AliasTests: XCTestCase {
)
}

func testVariableAliasWithLiteral() throws {
assertMacroExpansion(
"""
@Alias("newText", access: .inherit)
fileprivate var string = "text"
""",
expandedSource:
"""
fileprivate var string = "text"
fileprivate var newText: Swift.String {
set {
string = newValue
}
get {
string
}
}
""",
macros: macros
)
}

func testFunctionAlias() throws {
assertMacroExpansion(
"""
Expand Down Expand Up @@ -342,19 +365,19 @@ final class AliasTests: XCTestCase {
assertMacroExpansion(
"""
@Alias("newText", access: .inherit)
fileprivate var string = "text"
fileprivate var string = String()
""",
expandedSource:
"""
fileprivate var string = "text"
fileprivate var string = String()
""",
diagnostics: [
DiagnosticSpec(
message: AliasMacroDiagnostic
.specifyTypeExplicitly
.message,
line: 1,
column: 1
line: 2,
column: 17
)
],
macros: macros
Expand Down

0 comments on commit 4640b44

Please sign in to comment.