Skip to content

Commit

Permalink
Update swift-syntax version
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenghaiWang committed Dec 17, 2023
1 parent 24b9f14 commit 67284ce
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 128 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "165fc6d22394c1168ff76ab5d951245971ef07e5",
"version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-06-05-a"
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0-swift-5.9-DEVELOPMENT-SNAPSHOT-2023-04-25-b"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
.package(url: "https://github.com/ShenghaiWang/SwiftKeychain.git", from: "0.2.0")
],
Expand Down
22 changes: 11 additions & 11 deletions Sources/Macros/Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,34 +125,34 @@ public struct Access: AccessorMacro {
}
}

private extension TupleExprElementSyntax {
private extension LabeledExprSyntax {
var type: String? {
expression.as(MemberAccessExprSyntax.self)?.name.text
?? expression.as(FunctionCallExprSyntax.self)?.calledExpression.as(MemberAccessExprSyntax.self)?.name.text
expression.as(MemberAccessExprSyntax.self)?.declName.baseName.text
?? expression.as(FunctionCallExprSyntax.self)?.calledExpression.as(MemberAccessExprSyntax.self)?.declName.baseName.text
}
}

private extension TupleExprElementSyntax {
private extension LabeledExprSyntax {
var userDefaults: ExprSyntax {
if expression.is(MemberAccessExprSyntax.self) {
return "UserDefaults.standard"
}
if let memeberAceess = expression.as(FunctionCallExprSyntax.self)?.argumentList.first?
.as(TupleExprElementSyntax.self)?.expression.as(MemberAccessExprSyntax.self) {
return "UserDefaults.\(raw: memeberAceess.name.text)"
if let memeberAceess = expression.as(FunctionCallExprSyntax.self)?.arguments.first?
.as(LabeledExprSyntax.self)?.expression.as(MemberAccessExprSyntax.self) {
return "UserDefaults.\(raw: memeberAceess.declName.baseName.text)"
} else {
return expression.as(FunctionCallExprSyntax.self)?.argumentList.first?.expression ?? "UserDefaults.standard"
return expression.as(FunctionCallExprSyntax.self)?.arguments.first?.expression ?? "UserDefaults.standard"
}
}

var object: ExprSyntax? {
expression.as(FunctionCallExprSyntax.self)?.argumentList.first?.as(TupleExprElementSyntax.self)?.expression
expression.as(FunctionCallExprSyntax.self)?.arguments.first?.as(LabeledExprSyntax.self)?.expression
}
}

private extension SimpleTypeIdentifierSyntax {
private extension IdentifierTypeSyntax {
var type: SyntaxProtocol? {
genericArgumentClause?.arguments.first?.as(GenericArgumentSyntax.self)?.argumentType.as(OptionalTypeSyntax.self)?.wrappedType
genericArgumentClause?.arguments.first?.as(GenericArgumentSyntax.self)?.argument.as(OptionalTypeSyntax.self)?.wrappedType
?? genericArgumentClause?.arguments.first?.as(GenericArgumentSyntax.self)
}
}
18 changes: 9 additions & 9 deletions Sources/Macros/AddAssociatedValueVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public struct AddAssociatedValueVariable: MemberMacro {
}
return try members.flatMap { list-> [DeclSyntax] in
try list.compactMap { element -> DeclSyntax? in
guard let associatedValue = element.associatedValue else { return nil }
guard let associatedValue = element.parameterClause else { return nil }
let typeValue: String
if associatedValue.parameterList.isOneSimpleTypeIdentifierSyntax {
typeValue = "\(associatedValue.parameterList.first ?? "")"
if associatedValue.parameters.isOneSimpleTypeIdentifierSyntax {
typeValue = "\(associatedValue.parameters.first ?? "")"
} else {
typeValue = "\(associatedValue)"
}
let varSyntax = try VariableDeclSyntax("\(declaration.modifiers ?? ModifierListSyntax())var \(element.identifier)Value: \(raw: typeValue)?") {
let varSyntax = try VariableDeclSyntax("\(declaration.modifiers )var \(element.name)Value: \(raw: typeValue)?") {
try IfExprSyntax(
"if case let .\(element.identifier)(\(raw: associatedValue.parameterList.toVariableNames)) = self",
"if case let .\(element.name)(\(raw: associatedValue.parameters.toVariableNames)) = self",
bodyBuilder: {
if associatedValue.parameterList.count == 1 {
StmtSyntax("return \(raw: associatedValue.parameterList.toVariableNames)")
if associatedValue.parameters.count == 1 {
StmtSyntax("return \(raw: associatedValue.parameters.toVariableNames)")
} else {
StmtSyntax("return (\(raw: associatedValue.parameterList.toVariableNames))")
StmtSyntax("return (\(raw: associatedValue.parameters.toVariableNames))")
}
})
StmtSyntax(#"return nil"#)
Expand All @@ -44,6 +44,6 @@ extension EnumCaseParameterListSyntax {
}

var isOneSimpleTypeIdentifierSyntax: Bool {
count == 1 && (first?.type.is(SimpleTypeIdentifierSyntax.self) ?? false)
count == 1 && (first?.type.is(IdentifierTypeSyntax.self) ?? false)
}
}
44 changes: 20 additions & 24 deletions Sources/Macros/AddInit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public struct AddInit: MemberMacro {
let (parameters, body) = initBodyAndParams(for: declaration)
let bodyExpr: ExprSyntax = "\(raw: body.joined(separator: "\n"))"
var parametersLiteral = "init(\(parameters.joined(separator: ", ")))"
if let modifiers = declaration.modifiers {
parametersLiteral = "\(modifiers)\(parametersLiteral)"
}
let initDecl = try InitializerDeclSyntax(PartialSyntaxNodeString(stringLiteral: parametersLiteral),
parametersLiteral = "\(declaration.modifiers)\(parametersLiteral)"
let initDecl = try InitializerDeclSyntax(SyntaxNodeString(stringLiteral: parametersLiteral),
bodyBuilder: { bodyExpr })
var result = [DeclSyntax(initDecl)]
if node.argument(for: "withMock")?.as(BooleanLiteralExprSyntax.self)?.booleanLiteral.tokenKind.keyword == .true {
if node.argument(for: "withMock")?.as(BooleanLiteralExprSyntax.self)?.literal.tokenKind.keyword == .true {
let randomValue = node.argument(for: "randomMockValue")?.as(BooleanLiteralExprSyntax.self)?
.booleanLiteral.tokenKind.keyword != .false
.literal.tokenKind.keyword != .false
result.append(mock(basedOn: declaration, randomValue: randomValue))
}
return result
Expand All @@ -45,16 +43,16 @@ public struct AddInit: MemberMacro {
parameter += " = nil"
}
parameters.append(parameter)
body.append("self.\(identifier) = \(identifier)")
body.append(" self.\(identifier) = \(identifier)")
}
}
return (params: parameters, body: body)
}

private static func mock(basedOn declaration: DeclGroupSyntax, randomValue: Bool) -> DeclSyntax {
let identifier = (declaration as? StructDeclSyntax)?.identifier.text
?? (declaration as? ClassDeclSyntax)?.identifier.text
?? (declaration as? ActorDeclSyntax)?.identifier.text ?? ""
let identifier = (declaration as? StructDeclSyntax)?.name.text
?? (declaration as? ClassDeclSyntax)?.name.text
?? (declaration as? ActorDeclSyntax)?.name.text ?? ""
let parameters = declaration.memberBlock.members.compactMap { member -> String? in
guard let patternBinding = member.decl.as(VariableDeclSyntax.self)?.bindings
.as(PatternBindingListSyntax.self)?.first?.as(PatternBindingSyntax.self),
Expand All @@ -66,23 +64,21 @@ public struct AddInit: MemberMacro {
return "\(identifier): \(mockValue)"
}
var varDelcaration: DeclSyntax = "static let mock = \(raw: identifier)(\(raw: parameters.joined(separator: ", ")))"
if let modifiers = declaration.modifiers {
varDelcaration = "\(modifiers)varDelcaration"
}
varDelcaration = "\(declaration.modifiers)\(varDelcaration)"
varDelcaration = "#if DEBUG\n\(varDelcaration)\n#endif"
return varDelcaration
}
}

extension AttributeSyntax {
func argument(for label: String) -> ExprSyntax? {
argument?.as(TupleExprElementListSyntax.self)?.filter({ $0.label?.text == label }).first?.expression
arguments?.as(LabeledExprListSyntax.self)?.filter({ $0.label?.text == label }).first?.expression
}
}

extension SimpleTypeIdentifierSyntax {
extension IdentifierTypeSyntax {
func mockValue(randomValue: Bool) -> String? {
guard let type = self.as(SimpleTypeIdentifierSyntax.self)?.name.text else { return nil }
guard let type = self.as(IdentifierTypeSyntax.self)?.name.text else { return nil }
if let fun = mockFunctions[type] {
return fun(randomValue)
} else if name.text == "Void" {
Expand All @@ -107,30 +103,30 @@ extension OptionalTypeSyntax {

extension FunctionTypeSyntax {
func mockValue(randomValue: Bool) -> String? {
let args = repeatElement("_", count: max(arguments.count, 1)).joined(separator: ", ")
let returnValue = output.returnType.mockValue(randomValue: randomValue) ?? ""
let args = repeatElement("_", count: max(parameters.count, 1)).joined(separator: ", ")
let returnValue = returnClause.type.mockValue(randomValue: randomValue) ?? ""
return "{ \(args) in return \(returnValue) }"
}
}

extension TypeSyntax {
func mockValue(randomValue: Bool) -> String? {
if let mockValue = self.as(SimpleTypeIdentifierSyntax.self)?.mockValue(randomValue: randomValue) {
if let mockValue = self.as(IdentifierTypeSyntax.self)?.mockValue(randomValue: randomValue) {
return mockValue
} else if let type = self.as(DictionaryTypeSyntax.self) {
let mockKeyValue = type.keyType.mockValue(randomValue: randomValue) ?? ""
let mockValueValue = type.valueType.mockValue(randomValue: randomValue) ?? "nil"
let mockKeyValue = type.key.mockValue(randomValue: randomValue) ?? ""
let mockValueValue = type.value.mockValue(randomValue: randomValue) ?? "nil"
return "[\(mockKeyValue): \(mockValueValue)]"
} else if let mockValue = self.as(FunctionTypeSyntax.self)?.mockValue(randomValue: randomValue) {
return mockValue
} else if let mockValue = self.as(TupleTypeSyntax.self)?.elements.first?.type
.as(FunctionTypeSyntax.self)?.mockValue(randomValue: randomValue) {
return mockValue
} else if let type = self.as(ArrayTypeSyntax.self)?.elementType {
} else if let type = self.as(ArrayTypeSyntax.self)?.element {
return "[" + (type.mockValue(randomValue: randomValue) ?? "nil") + "]"
} else if let type = self.as(SimpleTypeIdentifierSyntax.self),
} else if let type = self.as(IdentifierTypeSyntax.self),
type.name.text == "Set",
let genericType = type.genericArgumentClause?.arguments.first?.argumentType {
let genericType = type.genericArgumentClause?.arguments.first?.argument {
return "[" + (genericType.mockValue(randomValue: randomValue) ?? "nil") + "]"
}
return nil
Expand Down
13 changes: 7 additions & 6 deletions Sources/Macros/AddPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public struct AddPublisher: PeerMacro {
providingPeersOf declaration: Declaration,
in context: Context) throws -> [DeclSyntax] {
guard let variableDecl = declaration.as(VariableDeclSyntax.self),
let modifiers = variableDecl.modifiers,
modifiers.map({ $0.name.text }).contains("private") else {
variableDecl.modifiers.map({ $0.name.text }).contains("private") else {
throw MacroDiagnostics.errorMacroUsage(message: "Please make the subject private and use the automated generated publisher variable outsite of this type")
}
guard let binding = variableDecl.bindings.first,
Expand All @@ -32,12 +31,14 @@ public struct AddPublisher: PeerMacro {

extension PatternBindingListSyntax.Element {
var genericArgumentClause: GenericArgumentClauseSyntax? {
initializer?.value.as(FunctionCallExprSyntax.self)?.calledExpression.as(SpecializeExprSyntax.self)?.genericArgumentClause
?? typeAnnotation?.type.as(SimpleTypeIdentifierSyntax.self)?.genericArgumentClause
initializer?.value.as(FunctionCallExprSyntax.self)?
.calledExpression.as(GenericSpecializationExprSyntax.self)?
.genericArgumentClause
?? typeAnnotation?.type.as(IdentifierTypeSyntax.self)?.genericArgumentClause
}

var typeName: String? {
initializer?.value.as(FunctionCallExprSyntax.self)?.calledExpression.as(SpecializeExprSyntax.self)?.expression.as(IdentifierExprSyntax.self)?.identifier.text
?? typeAnnotation?.type.as(SimpleTypeIdentifierSyntax.self)?.name.text
initializer?.value.as(FunctionCallExprSyntax.self)?.calledExpression.as(GenericSpecializationExprSyntax.self)?.expression.as(DeclReferenceExprSyntax.self)?.baseName.text
?? typeAnnotation?.type.as(IdentifierTypeSyntax.self)?.name.text
}
}
20 changes: 10 additions & 10 deletions Sources/Macros/FormatDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public struct FormatDate: ExpressionMacro {

let formatter: DeclSyntax = "let formatter = DateFormatter()"
let formatterStatement = CodeBlockItemSyntax(item: .decl(formatter))
var statementList = CodeBlockItemListSyntax(arrayLiteral: formatterStatement)
node.argumentList.filter { $0.label != nil }.forEach { tupleExprElementSyntax in
if let parameter = tupleExprElementSyntax.label?.text,
!tupleExprElementSyntax.expression.is(NilLiteralExprSyntax.self) {
let stmt: StmtSyntax = "formatter.\(raw: parameter) = \(tupleExprElementSyntax.expression)"
let codeblock = CodeBlockItemSyntax(item: .stmt(stmt))
statementList = statementList.appending(codeblock)
let arguments = node.argumentList.filter { $0.label != nil }
.compactMap { tupleExprElementSyntax in
if let parameter = tupleExprElementSyntax.label?.text,
!tupleExprElementSyntax.expression.is(NilLiteralExprSyntax.self) {
let stmt: StmtSyntax = "\n formatter.\(raw: parameter) = \(tupleExprElementSyntax.expression)"
return CodeBlockItemSyntax(item: .stmt(stmt))
}
return nil
}
}
let returnValue: ExprSyntax = "return formatter.string(from: \(date.expression))"
let returnValue: ExprSyntax = "\n return formatter.string(from: \(date.expression))"
let returnblock = CodeBlockItemSyntax(item: .expr(returnValue))
statementList = statementList.appending(returnblock)
let statementList = CodeBlockItemListSyntax([formatterStatement] + arguments + [returnblock])
let closure = ClosureExprSyntax(statements: statementList)
let function = FunctionCallExprSyntax(callee: closure)
return ExprSyntax(function)
Expand Down
32 changes: 10 additions & 22 deletions Sources/Macros/FormatDateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,31 @@ public struct FormatDateComponents: ExpressionMacro {
return ExprSyntax(function)
}

private static func parseFromToFunction(for argumentList: TupleExprElementListSyntax,
private static func parseFromToFunction(for argumentList: LabeledExprListSyntax,
from fromDate: ExprSyntax,
to toDate: ExprSyntax) -> CodeBlockItemListSyntax {
let formatter: DeclSyntax = "let formatter = DateComponentsFormatter()"
let formatterStatement = CodeBlockItemSyntax(item: .decl(formatter))
var statementList = CodeBlockItemListSyntax(arrayLiteral: formatterStatement)
argumentList.dropFirst(2).forEach { tupleExprElementSyntax in
if let codeblock = codeblock(for: tupleExprElementSyntax) {
statementList = statementList.appending(codeblock)
}
}
let returnValue: ExprSyntax = "return formatter.string(from: \(fromDate), to: \(toDate))"
let arguments = argumentList.dropFirst(2).compactMap { codeblock(for: $0) }
let returnValue: ExprSyntax = "\n return formatter.string(from: \(fromDate), to: \(toDate))"
let returnblock = CodeBlockItemSyntax(item: .expr(returnValue))
statementList = statementList.appending(returnblock)
return statementList
return CodeBlockItemListSyntax([formatterStatement] + arguments + [returnblock])
}

private static func parseFromFunction(for argumentList: TupleExprElementListSyntax,
private static func parseFromFunction(for argumentList: LabeledExprListSyntax,
from fromInterval: ExprSyntax) -> CodeBlockItemListSyntax {
let formatter: DeclSyntax = "let formatter = DateComponentsFormatter()"
let formatterStatement = CodeBlockItemSyntax(item: .decl(formatter))
var statementList = CodeBlockItemListSyntax(arrayLiteral: formatterStatement)
argumentList.dropFirst(1).forEach { tupleExprElementSyntax in
if let codeblock = codeblock(for: tupleExprElementSyntax) {
statementList = statementList.appending(codeblock)
}
}
let returnValue: ExprSyntax = "return formatter.string(from: \(fromInterval))"
let arguments = argumentList.dropFirst(1).compactMap { codeblock(for: $0) }
let returnValue: ExprSyntax = "\n return formatter.string(from: \(fromInterval))"
let returnblock = CodeBlockItemSyntax(item: .expr(returnValue))
statementList = statementList.appending(returnblock)
return statementList
return CodeBlockItemListSyntax([formatterStatement] + arguments + [returnblock])
}

private static func codeblock(for tupleExprElementSyntax: TupleExprElementSyntax) -> CodeBlockItemSyntax? {
private static func codeblock(for tupleExprElementSyntax: LabeledExprSyntax) -> CodeBlockItemSyntax? {
if let parameter = tupleExprElementSyntax.label?.text,
!tupleExprElementSyntax.expression.is(NilLiteralExprSyntax.self) {
let stmt: StmtSyntax = "formatter.\(raw: parameter) = \(tupleExprElementSyntax.expression)"
let stmt: StmtSyntax = "\n formatter.\(raw: parameter) = \(tupleExprElementSyntax.expression)"
let codeblock = CodeBlockItemSyntax(item: .stmt(stmt))
return codeblock
}
Expand Down
Loading

0 comments on commit 67284ce

Please sign in to comment.