Skip to content

Instantly share code, notes, and snippets.

@SeanROlszewski
Created September 8, 2018 18:31
Show Gist options
  • Select an option

  • Save SeanROlszewski/f86821792416f07600f8d31d8c986fae to your computer and use it in GitHub Desktop.

Select an option

Save SeanROlszewski/f86821792416f07600f8d31d8c986fae to your computer and use it in GitHub Desktop.
let returnStatement = SyntaxFactory.makeBlankReturnStmt()
.withReturnKeyword(
SyntaxFactory.makeReturnKeyword()
.withLeadingTrivia([.carriageReturns(1), .tabs(1)])
.withTrailingTrivia(.spaces(1))
)
.withExpression(SyntaxFactory.makeStringLiteralExpr("example"))
let functionBody = SyntaxFactory.makeBlankCodeBlock()
.withLeftBrace(SyntaxFactory.makeLeftBraceToken())
.withRightBrace(SyntaxFactory.makeRightBraceToken().withLeadingTrivia(.carriageReturns(1)))
.withStatements(SyntaxFactory.makeCodeBlockItemList([
SyntaxFactory.makeCodeBlockItem(item: returnStatement, semicolon: nil)
]))
let functionParameters = SyntaxFactory.makeBlankParameterClause()
.withLeftParen(SyntaxFactory.makeLeftParenToken())
.withRightParen(SyntaxFactory.makeRightParenToken().withTrailingTrivia(.spaces(1)))
let functionDeclaration = SyntaxFactory.makeBlankFunctionDecl()
.withFuncKeyword(SyntaxFactory.makeFuncKeyword().withTrailingTrivia(.spaces(1)))
.withIdentifier(SyntaxFactory.makeIdentifier("myFunctionName"))
.withSignature(SyntaxFactory.makeFunctionSignature(input: functionParameters, throwsOrRethrowsKeyword: nil, output: nil))
.withBody(functionBody)
let functionInvocation = SyntaxFactory.makeBlankFunctionCallExpr()
.withCalledExpression(SyntaxFactory.makeIdentifierExpr(identifier: SyntaxFactory.makeIdentifier("myFunctionName"),
declNameArguments: nil))
.withLeftParen(SyntaxFactory.makeLeftParenToken())
.withRightParen(SyntaxFactory.makeRightParenToken())
print("----------------")
print(functionDeclaration)
print("----------------")
print(functionInvocation)
print("----------------")
/*
----------------
func myFunctionName() {
return "example"
}
----------------
myFunctionName()
----------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment