Instantly share code, notes, and snippets.

Embed
What would you like to do?
libsyntax typealias creation
import SwiftSyntax
@greeble(bork) typealias Element = Int
let typeAliasKeyword = SyntaxFactory.makeTypealiasKeyword(leadingTrivia: .spaces(1),
trailingTrivia: .spaces(1))
let elementID = SyntaxFactory.makeIdentifier("Element", leadingTrivia: .zero, trailingTrivia: .spaces(1))
let equal = SyntaxFactory.makeEqualToken(leadingTrivia: Trivia.zero, trailingTrivia: .spaces(1))
let intType = SyntaxFactory.makeTypeIdentifier("Int", leadingTrivia: .zero, trailingTrivia: .zero)
let initializer = SyntaxFactory.makeTypeInitializerClause(equal: equal, value: intType)
let openParen = SyntaxFactory.makeLeftParenToken()
let closeParen = SyntaxFactory.makeRightParenToken()
let balancedTokens = [openParen, SyntaxFactory.makeIdentifier("bork", leadingTrivia: .zero,
trailingTrivia: .zero), closeParen]
let balancedTokenSyntax = SyntaxFactory.makeTokenList(balancedTokens)
let atsign = SyntaxFactory.makeAtSignToken(leadingTrivia: .zero, trailingTrivia: .zero)
let attributeName = SyntaxFactory.makeIdentifier("greeble", leadingTrivia: .zero, trailingTrivia: .zero)
let attribute = SyntaxFactory.makeAttribute(atSignToken: atsign,
attributeName: attributeName,
balancedTokens: balancedTokenSyntax)
let attributes = SyntaxFactory.makeAttributeList([attribute])
let typeAlias = SyntaxFactory.makeTypealiasDecl(attributes: attributes,
accessLevelModifier: nil,
typealiasKeyword: typeAliasKeyword,
identifier: elementID,
genericParameterClause: nil,
initializer: initializer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment