SwiftSyntax without the boilerplate.
This library uses a compound version number, with two parts separated by a dash -
.
X.Y.Z
.For example: 1.0.1-0.50300.0
is the 1.0.1
version compatible with the 0.503000.0
version of SwiftSyntax.
Update your Package.swift
, replacing <version number>
with the version you chose above.
Package(
...,
dependencies: [
...,
.package(
name: "SwiftSyntaxSimplified",
url: "https://github.com/Sunlace/swift-syntax-simplified.git",
.exact("<version number>")
),
],
targets: [
...,
.target(
...,
dependencies: [
...,
.product(name: "SwiftSyntaxSimplified", package: "SwiftSyntaxSimplified"),
]
),
]
)
This library does not replace all the functionality of SwiftSyntax
, so do not remove your existing imports of SwiftSyntax
.
import SwiftSyntax
import SwiftSyntaxSimplified
SwiftSyntax has both typed and type-erased views of all Syntax objects, used throughout its API. With SwiftSyntaxSimplified, you can convert between them on demand.
For example, many of the SyntaxVisitor
APIs pass typed arguments but expect type-erased return values:
override func visit(_ node: IfStmtSyntax) -> StmtSyntax {
return node.withConditions(<conditions>).typeErased
}
Other APIs return type-erased values that must be converted to typed before anything useful can be done:
override func visit(_ node: OptionalChainingExprSyntax) -> ExprSyntax {
switch node.expression.typed {
case let stringLiteral as StringLiteralExprSyntax: <action>
case let functionCall as FunctionCallExprSyntax: <action>
...
}
}
SwiftSyntax defines extremely verbose factory methods for creating syntax objects. With SwiftSyntaxSimplified, you can call the same methods that you would normally call, but with less boilerplate.
This:
SyntaxFactory.Simplified.makeFunctionCallExpr(
calledExpression: <expression>,
arguments: [
SyntaxFactory.Simplified.makeTupleExprElement(
expression:<expression>
),
SyntaxFactory.Simplified.makeTupleExprElement(
label: <token>,
expression: <expression>
),
]
)
Instead of this:
SyntaxFactory.makeFunctionCallExpr(
calledExpression: <expression>,
leftParen: SyntaxFactory.makeLeftParenToken(),
argumentList: SyntaxFactory.makeTupleExprElementList([
SyntaxFactory.makeTupleExprElement(
label: nil,
colon: nil,
expression: <expression>,
trailingComma: SyntaxFactory.makeCommaToken()
),
SyntaxFactory.makeTupleExprElement(
label: <token>,
colon: SyntaxFactory.makeColonToken(),
expression: <expression>,
trailingComma: nil
)
]),
rightParen: SyntaxFactory.makeRightParenToken(),
trailingClosure: nil,
additionalTrailingClosures: nil
)
This library duplicates SyntaxFactory.make<X>(...)
methods as SyntaxFactory.Simplified.make<X>(...)
For each duplicated method, it applies the following interface simplifications, in order. If no simplifications can be made, the method is not duplicated.
Optional<TokenSyntax>
parameter named trailing<X>
that must always be a specific token or nil
, depending on the position of the returned object in a list object (i.e. whether it is the last item in that list)
<X>Syntax
as SyntaxFactory.Simplified.<X>Syntax
, with fields matching the SyntaxFactory.Simplified.make<X>(...)
parameters (including all modifications)SyntaxFactory.Simplified.<X>Syntax
instead of <X>Syntax
.Array<X>
where X
is a type that was duplicated in an above step
Array<X>
with Array<SyntaxFactory.Simplified.X>
TokenSyntax
and must always be a specific token
Optional<TokenSyntax>
and must always be a specific token or nil
:
Optional<TokenSyntax>
parameter type with Bool
include
to the parameter namefalse
TokenSyntax
and must always be one of a specific set of tokens
<X>Token
enum to represent the optionsTokenSyntax
parameter type with <X>Token>
Optional<TokenSyntax>
and must always be one of a specific set of tokens or nil
<X>Token
enum to represent the non-nil
optionsOptional<TokenSyntax>
parameter type with Optional<<X>Token>
Optional
nil
Array
[]
SyntaxFactory.Simplified.make<X>(...)
method which, after modifications, has zero parameters:
SyntaxFactory.Simplified.make<X>(...)
method which, after modifications, has one parameter:
SyntaxFactory.Simplified.make<X>(...)
methodSyntaxFactory.Simplified.make<X>(...)
method to the parameter name<Z>
of type Bool
where true
corresponds to the more verbose optionfalse
Optional<Y>
is nil
:
SyntaxFactory.Simplified.<Z>GroupSyntax
SyntaxFactory.Simplified.make<Z>Group(...)
method with:
Optional
type <Y>
Optional<Y>
with Optional<SyntaxFactory.Simplified.<Z>GroupSyntax>
This library is incomplete, and contributions are welcome! We are actively reviewing PRs, and will merge any that:
If you have ideas for other improvements, please open an issue or a PR. However, consistency across the API is our top concern, so any architectural changes should include equivalent changes to all existing code and, if appropriate, a new major version number.
link |
Stars: 6 |
Last commit: 2 years ago |
Add type erasure (see README.md)
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics