Skip to content

Commit ed7e019

Browse files
./Utilities/format.swift
1 parent eeb3c6d commit ed7e019

File tree

6 files changed

+141
-123
lines changed

6 files changed

+141
-123
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let package = Package(
6363
name: "JavaScriptKitMacros",
6464
dependencies: [
6565
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
66-
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
66+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
6767
]
6868
),
6969

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ public struct ImportTS {
355355
"_$\(type.name)_init"
356356
}
357357

358-
private static func thunkName(type: ImportedTypeSkeleton, property: ImportedPropertySkeleton, operation: String)
358+
private static func thunkName(
359+
type: ImportedTypeSkeleton,
360+
property: ImportedPropertySkeleton,
361+
operation: String
362+
)
359363
-> String
360364
{
361365
"_$\(type.name)_\(property.name)_\(operation)"

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,13 @@ public struct ImportedTypeSkeleton: Codable {
655655
public let properties: [ImportedPropertySkeleton]
656656
public let documentation: String?
657657

658-
public init(name: String, constructor: ImportedConstructorSkeleton? = nil, methods: [ImportedFunctionSkeleton], properties: [ImportedPropertySkeleton], documentation: String? = nil) {
658+
public init(
659+
name: String,
660+
constructor: ImportedConstructorSkeleton? = nil,
661+
methods: [ImportedFunctionSkeleton],
662+
properties: [ImportedPropertySkeleton],
663+
documentation: String? = nil
664+
) {
659665
self.name = name
660666
self.constructor = constructor
661667
self.methods = methods

Sources/JavaScriptKit/Macros.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ public enum JSEnumStyle: String {
109109
public macro JS(namespace: String? = nil, enumStyle: JSEnumStyle = .const) = Builtin.ExternalMacro
110110

111111
@attached(accessor)
112-
public macro JSVariable(from: String? = nil, jsName: String? = nil) = #externalMacro(module: "JavaScriptKitMacros", type: "JSImportVariableMacro")
112+
public macro JSVariable(from: String? = nil, jsName: String? = nil) =
113+
#externalMacro(module: "JavaScriptKitMacros", type: "JSImportVariableMacro")
113114

114115
@attached(body)
115-
public macro JSFunction(from: String? = nil, jsName: String? = nil) = #externalMacro(module: "JavaScriptKitMacros", type: "JSImportFunctionMacro")
116+
public macro JSFunction(from: String? = nil, jsName: String? = nil) =
117+
#externalMacro(module: "JavaScriptKitMacros", type: "JSImportFunctionMacro")
116118

117119
@attached(member)
118-
public macro JSClass(from: String? = nil, jsName: String? = nil) = #externalMacro(module: "JavaScriptKitMacros", type: "JSImportClassMacro")
120+
public macro JSClass(from: String? = nil, jsName: String? = nil) =
121+
#externalMacro(module: "JavaScriptKitMacros", type: "JSImportClassMacro")

Sources/JavaScriptKitMacros/JSImportMacro.swift

Lines changed: 117 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -88,147 +88,152 @@ private enum JSImportMacroHelper {
8888
}
8989

9090
extension JSImportFunctionMacro: BodyMacro {
91-
package static func expansion(
92-
of node: AttributeSyntax,
93-
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
94-
in context: some MacroExpansionContext
95-
) throws -> [CodeBlockItemSyntax] {
96-
if let functionDecl = declaration.as(FunctionDeclSyntax.self) {
97-
let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context)
98-
let isStatic = JSImportMacroHelper.isStatic(functionDecl.modifiers)
99-
let isInstanceMember = enclosingTypeName != nil && !isStatic
91+
package static func expansion(
92+
of node: AttributeSyntax,
93+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
94+
in context: some MacroExpansionContext
95+
) throws -> [CodeBlockItemSyntax] {
96+
if let functionDecl = declaration.as(FunctionDeclSyntax.self) {
97+
let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context)
98+
let isStatic = JSImportMacroHelper.isStatic(functionDecl.modifiers)
99+
let isInstanceMember = enclosingTypeName != nil && !isStatic
100+
101+
let name = functionDecl.name.text
102+
let glueName = JSImportMacroHelper.glueName(baseName: name, enclosingTypeName: enclosingTypeName)
103+
104+
var arguments: [String] = []
105+
if isInstanceMember {
106+
arguments.append("self.jsObject")
107+
}
108+
arguments.append(
109+
contentsOf: JSImportMacroHelper.parameterNames(functionDecl.signature.parameterClause.parameters)
110+
)
100111

101-
let name = functionDecl.name.text
102-
let glueName = JSImportMacroHelper.glueName(baseName: name, enclosingTypeName: enclosingTypeName)
112+
let argsJoined = arguments.joined(separator: ", ")
113+
let call = "\(glueName)(\(argsJoined))"
103114

104-
var arguments: [String] = []
105-
if isInstanceMember {
106-
arguments.append("self.jsObject")
115+
let effects = functionDecl.signature.effectSpecifiers
116+
let isAsync = effects?.asyncSpecifier != nil
117+
let isThrows = effects?.throwsClause != nil
118+
let prefix = JSImportMacroHelper.tryAwaitPrefix(isAsync: isAsync, isThrows: isThrows)
119+
120+
let isVoid = JSImportMacroHelper.isVoidReturn(functionDecl.signature.returnClause?.type)
121+
let line = isVoid ? "\(prefix)\(call)" : "return \(prefix)\(call)"
122+
return [CodeBlockItemSyntax(stringLiteral: line)]
107123
}
108-
arguments.append(contentsOf: JSImportMacroHelper.parameterNames(functionDecl.signature.parameterClause.parameters))
109124

110-
let argsJoined = arguments.joined(separator: ", ")
111-
let call = "\(glueName)(\(argsJoined))"
125+
if let initializerDecl = declaration.as(InitializerDeclSyntax.self) {
126+
guard let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context) else {
127+
context.diagnose(
128+
Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedDeclaration)
129+
)
130+
return []
131+
}
112132

113-
let effects = functionDecl.signature.effectSpecifiers
114-
let isAsync = effects?.asyncSpecifier != nil
115-
let isThrows = effects?.throwsClause != nil
116-
let prefix = JSImportMacroHelper.tryAwaitPrefix(isAsync: isAsync, isThrows: isThrows)
133+
let glueName = JSImportMacroHelper.glueName(baseName: "init", enclosingTypeName: enclosingTypeName)
134+
let parameters = initializerDecl.signature.parameterClause.parameters
135+
let arguments = JSImportMacroHelper.parameterNames(parameters)
136+
let call = "\(glueName)(\(arguments.joined(separator: ", ")))"
117137

118-
let isVoid = JSImportMacroHelper.isVoidReturn(functionDecl.signature.returnClause?.type)
119-
let line = isVoid ? "\(prefix)\(call)" : "return \(prefix)\(call)"
120-
return [CodeBlockItemSyntax(stringLiteral: line)]
121-
}
138+
let effects = initializerDecl.signature.effectSpecifiers
139+
let isAsync = effects?.asyncSpecifier != nil
140+
let isThrows = effects?.throwsClause != nil
141+
let prefix = JSImportMacroHelper.tryAwaitPrefix(isAsync: isAsync, isThrows: isThrows)
122142

123-
if let initializerDecl = declaration.as(InitializerDeclSyntax.self) {
124-
guard let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context) else {
125-
context.diagnose(Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedDeclaration))
126-
return []
143+
return [
144+
CodeBlockItemSyntax(stringLiteral: "let jsObject = \(prefix)\(call)"),
145+
CodeBlockItemSyntax(stringLiteral: "self.init(unsafelyWrapping: jsObject)"),
146+
]
127147
}
128148

129-
let glueName = JSImportMacroHelper.glueName(baseName: "init", enclosingTypeName: enclosingTypeName)
130-
let parameters = initializerDecl.signature.parameterClause.parameters
131-
let arguments = JSImportMacroHelper.parameterNames(parameters)
132-
let call = "\(glueName)(\(arguments.joined(separator: ", ")))"
133-
134-
let effects = initializerDecl.signature.effectSpecifiers
135-
let isAsync = effects?.asyncSpecifier != nil
136-
let isThrows = effects?.throwsClause != nil
137-
let prefix = JSImportMacroHelper.tryAwaitPrefix(isAsync: isAsync, isThrows: isThrows)
138-
139-
return [
140-
CodeBlockItemSyntax(stringLiteral: "let jsObject = \(prefix)\(call)"),
141-
CodeBlockItemSyntax(stringLiteral: "self.init(unsafelyWrapping: jsObject)"),
142-
]
149+
context.diagnose(Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedDeclaration))
150+
return []
143151
}
144-
145-
context.diagnose(Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedDeclaration))
146-
return []
147-
}
148152
}
149153

150154
enum JSImportVariableMacro {}
151155

152156
extension JSImportVariableMacro: AccessorMacro {
153-
package static func expansion(
154-
of node: AttributeSyntax,
155-
providingAccessorsOf declaration: some DeclSyntaxProtocol,
156-
in context: some MacroExpansionContext
157-
) throws -> [AccessorDeclSyntax] {
158-
guard let variableDecl = declaration.as(VariableDeclSyntax.self),
159-
let binding = variableDecl.bindings.first,
160-
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)
161-
else {
162-
context.diagnose(Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedVariable))
163-
return []
164-
}
157+
package static func expansion(
158+
of node: AttributeSyntax,
159+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
160+
in context: some MacroExpansionContext
161+
) throws -> [AccessorDeclSyntax] {
162+
guard let variableDecl = declaration.as(VariableDeclSyntax.self),
163+
let binding = variableDecl.bindings.first,
164+
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)
165+
else {
166+
context.diagnose(Diagnostic(node: Syntax(declaration), message: JSImportMacroMessage.unsupportedVariable))
167+
return []
168+
}
165169

166-
let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context)
167-
let isStatic = JSImportMacroHelper.isStatic(variableDecl.modifiers)
168-
let isInstanceMember = enclosingTypeName != nil && !isStatic
169-
170-
let propertyName = identifier.identifier.text
171-
let getterName = JSImportMacroHelper.glueName(
172-
baseName: propertyName,
173-
enclosingTypeName: enclosingTypeName,
174-
operation: "get"
175-
)
176-
let setterName = JSImportMacroHelper.glueName(
177-
baseName: propertyName,
178-
enclosingTypeName: enclosingTypeName,
179-
operation: "set"
180-
)
181-
182-
var getterArgs: [String] = []
183-
if isInstanceMember {
184-
getterArgs.append("self.jsObject")
185-
}
186-
let getterCall: CodeBlockItemSyntax = "return try \(raw: getterName)(\(raw: getterArgs.joined(separator: ", ")))"
187-
188-
let throwsClause = ThrowsClauseSyntax(
189-
throwsSpecifier: .keyword(.throws),
190-
leftParen: .leftParenToken(),
191-
type: IdentifierTypeSyntax(name: .identifier("JSException")),
192-
rightParen: .rightParenToken()
193-
)
194-
195-
var accessors: [AccessorDeclSyntax] = [
196-
AccessorDeclSyntax(
197-
accessorSpecifier: .keyword(.get),
198-
effectSpecifiers: AccessorEffectSpecifiersSyntax(
199-
asyncSpecifier: nil,
200-
throwsClause: throwsClause
201-
),
202-
body: CodeBlockSyntax {
203-
getterCall
204-
}
170+
let enclosingTypeName = JSImportMacroHelper.enclosingTypeName(from: context)
171+
let isStatic = JSImportMacroHelper.isStatic(variableDecl.modifiers)
172+
let isInstanceMember = enclosingTypeName != nil && !isStatic
173+
174+
let propertyName = identifier.identifier.text
175+
let getterName = JSImportMacroHelper.glueName(
176+
baseName: propertyName,
177+
enclosingTypeName: enclosingTypeName,
178+
operation: "get"
179+
)
180+
let setterName = JSImportMacroHelper.glueName(
181+
baseName: propertyName,
182+
enclosingTypeName: enclosingTypeName,
183+
operation: "set"
205184
)
206-
]
207185

208-
let isLet = variableDecl.bindingSpecifier.tokenKind == .keyword(.let)
209-
if !isLet {
210-
var setterArgs: [String] = []
186+
var getterArgs: [String] = []
211187
if isInstanceMember {
212-
setterArgs.append("self.jsObject")
188+
getterArgs.append("self.jsObject")
213189
}
214-
setterArgs.append("newValue")
215-
let setterCall: CodeBlockItemSyntax = "try \(raw: setterName)(\(raw: setterArgs.joined(separator: ", ")))"
216-
accessors.append(
190+
let getterCall: CodeBlockItemSyntax =
191+
"return try \(raw: getterName)(\(raw: getterArgs.joined(separator: ", ")))"
192+
193+
let throwsClause = ThrowsClauseSyntax(
194+
throwsSpecifier: .keyword(.throws),
195+
leftParen: .leftParenToken(),
196+
type: IdentifierTypeSyntax(name: .identifier("JSException")),
197+
rightParen: .rightParenToken()
198+
)
199+
200+
var accessors: [AccessorDeclSyntax] = [
217201
AccessorDeclSyntax(
218-
accessorSpecifier: .keyword(.set),
202+
accessorSpecifier: .keyword(.get),
219203
effectSpecifiers: AccessorEffectSpecifiersSyntax(
220204
asyncSpecifier: nil,
221205
throwsClause: throwsClause
222206
),
223207
body: CodeBlockSyntax {
224-
setterCall
208+
getterCall
225209
}
226210
)
227-
)
228-
}
211+
]
212+
213+
let isLet = variableDecl.bindingSpecifier.tokenKind == .keyword(.let)
214+
if !isLet {
215+
var setterArgs: [String] = []
216+
if isInstanceMember {
217+
setterArgs.append("self.jsObject")
218+
}
219+
setterArgs.append("newValue")
220+
let setterCall: CodeBlockItemSyntax = "try \(raw: setterName)(\(raw: setterArgs.joined(separator: ", ")))"
221+
accessors.append(
222+
AccessorDeclSyntax(
223+
accessorSpecifier: .keyword(.set),
224+
effectSpecifiers: AccessorEffectSpecifiersSyntax(
225+
asyncSpecifier: nil,
226+
throwsClause: throwsClause
227+
),
228+
body: CodeBlockSyntax {
229+
setterCall
230+
}
231+
)
232+
)
233+
}
229234

230-
return accessors
231-
}
235+
return accessors
236+
}
232237
}
233238

234239
enum JSImportClassMacro {}

Sources/JavaScriptKitMacros/JavaScriptKitMacrosPlugin.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import SwiftSyntaxMacros
33

44
@main
55
struct JavaScriptKitMacrosPlugin: CompilerPlugin {
6-
var providingMacros: [Macro.Type] = [
7-
JSImportFunctionMacro.self,
8-
JSImportVariableMacro.self,
9-
JSImportClassMacro.self,
10-
]
6+
var providingMacros: [Macro.Type] = [
7+
JSImportFunctionMacro.self,
8+
JSImportVariableMacro.self,
9+
JSImportClassMacro.self,
10+
]
1111
}

0 commit comments

Comments
 (0)