File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest/Declarations Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+
15
+ public struct Import : DeclBuildable {
16
+ let moduleName : String
17
+
18
+ public init ( _ moduleName: String ) {
19
+ self . moduleName = moduleName
20
+ }
21
+
22
+ public func buildDecl( format: Format , leadingTrivia: Trivia ) -> DeclSyntax {
23
+ let importToken = Tokens . import. withLeadingTrivia ( leadingTrivia + format. makeIndent ( ) )
24
+ let moduleNameToken = SyntaxFactory . makeIdentifier ( moduleName)
25
+
26
+ return ImportDeclSyntax {
27
+ $0. useImportTok ( importToken)
28
+ $0. addPathComponent ( AccessPathComponentSyntax {
29
+ $0. useName ( moduleNameToken)
30
+ } )
31
+ }
32
+ }
33
+ }
Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ enum Tokens {
22
22
/// `"var "`
23
23
static let `var` = SyntaxFactory . makeVarKeyword ( ) . withTrailingTrivia ( . spaces( 1 ) )
24
24
25
+ /// `"import "`
26
+ static let `import` = SyntaxFactory . makeImportKeyword ( ) . withTrailingTrivia ( . spaces( 1 ) )
27
+
25
28
/// `"struct "`
26
29
static let `struct` = SyntaxFactory . makeStructKeyword ( ) . withTrailingTrivia ( . spaces( 1 ) )
27
30
Original file line number Diff line number Diff line change
1
+ import XCTest
2
+ import SwiftSyntax
3
+
4
+ @testable import SwiftSyntaxBuilder
5
+
6
+ final class ImportTests : XCTestCase {
7
+ func testImport( ) {
8
+ let format = Format ( indentWidth: 2 ) . indented ( )
9
+
10
+ let testCases : [ UInt : ( Import , String ) ] = [
11
+ #line: ( Import ( " SwiftSyntax " ) ,
12
+ " import SwiftSyntax " ) ,
13
+ ]
14
+
15
+ for (line, testCase) in testCases {
16
+ let ( builder, expected) = testCase
17
+ let syntax = builder. buildSyntax ( format: format, leadingTrivia: . zero)
18
+
19
+ var text = " "
20
+ syntax. write ( to: & text)
21
+
22
+ XCTAssertEqual ( text, expected, line: line)
23
+ }
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments