Skip to content

Commit

Permalink
Merge pull request #7 from nerdsupremacist/swift-5.2-branch
Browse files Browse the repository at this point in the history
Updating SwiftSyntax to Swift 5.2
  • Loading branch information
nerdsupremacist committed Mar 27, 2020
2 parents 1037d74 + a3a637c commit 9c74459
Show file tree
Hide file tree
Showing 26 changed files with 144 additions and 138 deletions.
27 changes: 2 additions & 25 deletions .swiftpm/xcode/xcshareddata/xcschemes/Graphaello.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1120"
LastUpgradeVersion = "1140"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -52,33 +52,10 @@
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "codegen --project /Users/mathiasquintero/master-thesis/todoapp --apollo derivedData"
argument = "codegen --project /Users/mathias.quintero/master-thesis/graphaello-countries-example --apollo derivedData"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "add --project /Users/mathiasquintero/master-thesis/starwars --apiName StarWars https://swapi-graphql.netlify.com/.netlify/functions/index"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--help"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "init --help"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "codegen --help"
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
<EnvironmentVariable
key = "BUILD_ROOT"
value = "/Users/mathiasquintero/Library/Developer/Xcode/DerivedData/GraphQLTest-bztlvecoifvdbadjotxvkntshwrg/"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
14 changes: 7 additions & 7 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"package": "CLIKit",
"repositoryURL": "https://github.com/nerdsupremacist/CLIKit.git",
"state": {
"branch": "master",
"branch": null,
"revision": "7ae40a74e125c1302598fb90789bd11cb461b963",
"version": null
"version": "0.3.5"
}
},
{
Expand Down Expand Up @@ -59,9 +59,9 @@
"package": "Ogma",
"repositoryURL": "https://github.com/nerdsupremacist/Ogma.git",
"state": {
"branch": "master",
"revision": "9e5a832a757d79b1bb21d910145ba9650229a3c8",
"version": null
"branch": null,
"revision": "541e3c43d68d64296533d28b0b32bfb9fe189a4f",
"version": "0.1.2"
}
},
{
Expand Down Expand Up @@ -114,8 +114,8 @@
"repositoryURL": "https://github.com/apple/swift-syntax.git",
"state": {
"branch": null,
"revision": "3e3eb191fcdbecc6031522660c4ed6ce25282c25",
"version": "0.50100.0"
"revision": "0688b9cfc4c3dd234e4f55f1f056b2affc849873",
"version": "0.50200.0"
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/jpsim/SourceKitten.git", .upToNextMajor(from: "0.27.0")),
.package(url: "https://github.com/nerdsupremacist/CLIKit.git", .branch("master")),
.package(url: "https://github.com/nerdsupremacist/Ogma.git", .branch("master")),
.package(url: "https://github.com/nerdsupremacist/CLIKit.git", .upToNextMajor(from: "0.3.5")),
.package(url: "https://github.com/nerdsupremacist/Ogma.git", .upToNextMajor(from: "0.1.2")),
.package(url: "https://github.com/nerdsupremacist/Stencil.git", .upToNextMajor(from: "0.13.2")),
.package(url: "https://github.com/tuist/xcodeproj.git", .upToNextMajor(from: "7.5.0")),
.package(url: "https://github.com/nicklockwood/SwiftFormat.git", .upToNextMajor(from: "0.42.0")),
.package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "0.50100.0")),
.package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50200.0")),
.package(url: "https://github.com/kylef/PathKit.git", .upToNextMajor(from: "1.0.0")),
],
targets: [
Expand Down
27 changes: 27 additions & 0 deletions Sources/Graphaello/Extensions/ExprSyntax+erasure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import Foundation
import SwiftSyntax

extension ExprSyntax {

func `as`(_: SyntaxEnum.Type) -> SyntaxEnum {
return Syntax(self).as(SyntaxEnum.self)
}

}

extension ExprSyntaxProtocol {

func erased() -> ExprSyntax {
return ExprSyntax(self)
}

}

extension SyntaxProtocol {

func erased() -> Syntax {
return Syntax(self)
}

}
36 changes: 18 additions & 18 deletions Sources/Graphaello/Extensions/ExprSyntax+init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ extension IdentifierExprSyntax {

extension SequenceExprSyntax {

init(lhs: ExprSyntax, rhs: ExprSyntax, binaryOperator: BinaryOperatorExprSyntax) {
init(lhs: ExprSyntaxProtocol, rhs: ExprSyntaxProtocol, binaryOperator: BinaryOperatorExprSyntax) {
self = SequenceExprSyntax { builder in
builder.addElement(lhs)
builder.addElement(binaryOperator)
builder.addElement(rhs)
builder.addElement(lhs.erased())
builder.addElement(binaryOperator.erased())
builder.addElement(rhs.erased())
}
}

}

extension ArrayExprSyntax {

init(expressions: [ExprSyntax]) {
init(expressions: [ExprSyntaxProtocol]) {
let leftSquare = SyntaxFactory.makeLeftSquareBracketToken()
let rightSquare = SyntaxFactory.makeRightSquareBracketToken()
self = ArrayExprSyntax { builder in
Expand Down Expand Up @@ -62,10 +62,10 @@ extension BinaryOperatorExprSyntax {

extension ArrayElementSyntax {

init(expression: ExprSyntax, useTrailingComma: Bool) {
init(expression: ExprSyntaxProtocol, useTrailingComma: Bool) {
let comma = SyntaxFactory.makeCommaToken()
self = ArrayElementSyntax { builder in
builder.useExpression(expression)
builder.useExpression(expression.erased())
if useTrailingComma {
builder.useTrailingComma(comma)
}
Expand All @@ -76,12 +76,12 @@ extension ArrayElementSyntax {

extension MemberAccessExprSyntax {

init(base: ExprSyntax?, name: String) {
init(base: ExprSyntaxProtocol?, name: String) {
let dot = SyntaxFactory.makePeriodToken()
let name = SyntaxFactory.makeIdentifier(name)
self = MemberAccessExprSyntax { builder in
if let base = base {
builder.useBase(base)
builder.useBase(base.erased())
}
builder.useDot(dot)
builder.useName(name)
Expand All @@ -92,30 +92,30 @@ extension MemberAccessExprSyntax {

extension FunctionCallExprSyntax {

init(target: ExprSyntax, arguments: [(String?, ExprSyntax)]) {
init(target: ExprSyntaxProtocol, arguments: [(String?, ExprSyntaxProtocol)]) {
let leftParen = SyntaxFactory.makeLeftParenToken()
let rightParen = SyntaxFactory.makeRightParenToken()

self = FunctionCallExprSyntax { builder in
builder.useCalledExpression(target)
builder.useCalledExpression(target.erased())
builder.useLeftParen(leftParen)
arguments.forEach { argument, isLast in
builder.addArgument(FunctionCallArgumentSyntax(name: argument.0,
value: argument.1,
useTrailingComma: !isLast))
builder.addArgument(TupleExprElementSyntax(name: argument.0,
value: argument.1,
useTrailingComma: !isLast))
}
builder.useRightParen(rightParen)
}
}

}

extension FunctionCallArgumentSyntax {
extension TupleExprElementSyntax {

init(name: String?, value: ExprSyntax, useTrailingComma: Bool) {
init(name: String?, value: ExprSyntaxProtocol, useTrailingComma: Bool) {
let comma = SyntaxFactory.makeCommaToken()

self = FunctionCallArgumentSyntax { builder in
self = TupleExprElementSyntax { builder in
if let name = name {
let label = SyntaxFactory.makeIdentifier(name)
let colon = SyntaxFactory.makeColonToken()
Expand All @@ -124,7 +124,7 @@ extension FunctionCallArgumentSyntax {
builder.useColon(colon)
}

builder.useExpression(value)
builder.useExpression(value.erased())
if useTrailingComma {
builder.useTrailingComma(comma)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension Schema.GraphQLType {
return try inputFields.map { field in
let defaultValue = try transpiler.expression(from: field.defaultValue,
for: field.type,
using: api)
using: api)?.erased()

return InputFieldInitializerArgument(name: field.name,
api: api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ extension GraphQLConnectionQuery {
var queryArgumentAssignments: [QueryArgumentAssignment] {
return query.queryArgumentAssignments.map { assignment in
if assignment.name == "first" {
let expression = SequenceExprSyntax(lhs: IdentifierExprSyntax(identifier: "_pageSize").erased(),
rhs: assignment.expression,
binaryOperator: BinaryOperatorExprSyntax(text: "??"))

return QueryArgumentAssignment(name: assignment.name,
expression: SequenceExprSyntax(lhs: IdentifierExprSyntax(identifier: "_pageSize"),
rhs: assignment.expression,
binaryOperator: BinaryOperatorExprSyntax(text: "??")) )
expression: expression.erased())
}
if assignment.name == "after" {
return QueryArgumentAssignment(name: assignment.name,
expression: IdentifierExprSyntax(identifier: "_cursor"))
expression: IdentifierExprSyntax(identifier: "_cursor").erased())
}
return assignment
}
Expand All @@ -77,10 +79,10 @@ extension GraphQLArgument {
}

if type.isScalar {
return IdentifierExprSyntax(identifier: name.camelized)
return IdentifierExprSyntax(identifier: name.camelized).erased()
} else {
return FunctionCallExprSyntax(target: MemberAccessExprSyntax(base: nil, name: "init"),
arguments: [(nil, IdentifierExprSyntax(identifier: name.camelized))])
return FunctionCallExprSyntax(target: MemberAccessExprSyntax(base: nil, name: "init").erased(),
arguments: [(nil, IdentifierExprSyntax(identifier: name.camelized).erased())]).erased()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ extension SubParser {
parser: @escaping () -> SubParser<BaseMemberAccess, Stage.Parsed.Path>) -> SubParser<MemberAccessExprSyntax, Stage.Parsed.Path> {

return .init { expression in
switch expression.base {
case .some(let base as IdentifierExprSyntax):
guard let base = expression.base else { throw ParseError.expectedBaseForCalls(expression: expression.erased()) }

if let base = base.as(IdentifierExprSyntax.self) {
let access = BaseMemberAccess(base: base.identifier.text, accessedField: expression.name.text)
return try parser().parse(from: access)
case .some(let base):
return try parent.parse(from: base).appending(name: expression.name.text)
default:
throw ParseError.expectedBaseForCalls(expression: expression)
}

return try parent.parse(from: base).appending(name: expression.name.text)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ extension SubParser {

static func annotationFunctionCall(parser: @escaping () -> SubParser<ExprSyntax, Stage.Parsed.Path>) -> SubParser<FunctionCallExprSyntax, Stage.Parsed.Path?> {
return .init { call in
switch call.calledExpression {
case let calledExpression as IdentifierExprSyntax where calledExpression.identifier.text == "GraphQL":
switch call.calledExpression.as(SyntaxEnum.self) {
case .identifierExpr(let calledExpression) where calledExpression.identifier.text == "GraphQL":
break
case let calledExpression as SpecializeExprSyntax:
guard let identifier = calledExpression.expression as? IdentifierExprSyntax,
case .specializeExpr(let calledExpression):
guard let identifier = calledExpression.expression.as(IdentifierExprSyntax.self),
identifier.identifier.text == "GraphQL" else { return nil }

break
Expand All @@ -26,7 +26,7 @@ extension SubParser {
}

guard let expression = Array(call.argumentList).single()?.expression else { return nil }

return try parser().parse(from: expression)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftSyntax

extension SubParser {

static func argumentList(parser: @escaping () -> SubParser<ExprSyntax, Argument>) -> SubParser<FunctionCallArgumentListSyntax, [Field.Argument]> {
static func argumentList(parser: @escaping () -> SubParser<ExprSyntax, Argument>) -> SubParser<TupleExprElementListSyntax, [Field.Argument]> {
return .init { arguments in
let parser = parser()
return try arguments.map { Field.Argument(name: $0.label?.text ?! fatalError(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ extension SubParser {
default defaultCase: @escaping (ExprSyntax) -> Result<Output, Error>) -> SubParser<ExprSyntax, Output> {

return .init { expression, parser in
switch expression {
case let expression as MemberAccessExprSyntax:
switch expression.as(SyntaxEnum.self) {
case .memberAccessExpr(let expression):
return try memberAccess(parser).parse(from: expression)
case let expression as FunctionCallExprSyntax:
case .functionCallExpr(let expression):
return try functionCall(parser).parse(from: expression)
default:
return try defaultCase(expression).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extension SubParser {

static func functionCall(parser: @escaping () -> SubParser<ExprSyntax, Argument.QueryArgument>) -> SubParser<FunctionCallExprSyntax, Argument> {
return .init { expression in
guard let calledMember = expression.calledExpression as? MemberAccessExprSyntax else {
throw ParseError.cannotInstantiateObjectFromExpression(expression, type: Argument.self)
guard let calledMember = expression.calledExpression.as(MemberAccessExprSyntax.self) else {
throw ParseError.cannotInstantiateObjectFromExpression(expression.erased(), type: Argument.self)
}

let argument = Array(expression.argumentList).single()?.expression
Expand All @@ -27,7 +27,7 @@ extension SubParser {
case ("argument", .none):
return .argument(.forced)
default:
throw ParseError.cannotInstantiateObjectFromExpression(calledMember, type: Argument.self)
throw ParseError.cannotInstantiateObjectFromExpression(calledMember.erased(), type: Argument.self)
}
}
}
Expand Down

0 comments on commit 9c74459

Please sign in to comment.