Skip to content

Commit 6948083

Browse files
committed
Remove semantic hashbang error
1 parent 28cc1b0 commit 6948083

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

Tests/SwiftParserTest/Assertions.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ func AssertParse(
443443
_ markedSource: String,
444444
substructure expectedSubstructure: Syntax? = nil,
445445
substructureAfterMarker: String = "START",
446+
substructureCheckTrivia: Bool = false,
446447
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
447448
applyFixIts: [String]? = nil,
448449
fixedSource expectedFixedSource: String? = nil,
@@ -454,6 +455,7 @@ func AssertParse(
454455
{ SourceFileSyntax.parse(from: &$0) },
455456
substructure: expectedSubstructure,
456457
substructureAfterMarker: substructureAfterMarker,
458+
substructureCheckTrivia: substructureCheckTrivia,
457459
diagnostics: expectedDiagnostics,
458460
applyFixIts: applyFixIts,
459461
fixedSource: expectedFixedSource,
@@ -470,6 +472,7 @@ func AssertParse<S: SyntaxProtocol>(
470472
_ parse: (inout Parser) -> S,
471473
substructure expectedSubstructure: Syntax? = nil,
472474
substructureAfterMarker: String = "START",
475+
substructureCheckTrivia: Bool = false,
473476
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
474477
applyFixIts: [String]? = nil,
475478
fixedSource expectedFixedSource: String? = nil,
@@ -484,6 +487,7 @@ func AssertParse<S: SyntaxProtocol>(
484487
},
485488
substructure: expectedSubstructure,
486489
substructureAfterMarker: substructureAfterMarker,
490+
substructureCheckTrivia: substructureCheckTrivia,
487491
diagnostics: expectedDiagnostics,
488492
applyFixIts: applyFixIts,
489493
fixedSource: expectedFixedSource,
@@ -515,6 +519,7 @@ func AssertParse<S: SyntaxProtocol>(
515519
_ parse: (String) -> S,
516520
substructure expectedSubstructure: Syntax? = nil,
517521
substructureAfterMarker: String = "START",
522+
substructureCheckTrivia: Bool = false,
518523
diagnostics expectedDiagnostics: [DiagnosticSpec] = [],
519524
applyFixIts: [String]? = nil,
520525
fixedSource expectedFixedSource: String? = nil,
@@ -545,7 +550,7 @@ func AssertParse<S: SyntaxProtocol>(
545550
if let expectedSubstructure = expectedSubstructure {
546551
let subtreeMatcher = SubtreeMatcher(Syntax(tree), markers: markerLocations)
547552
do {
548-
try subtreeMatcher.assertSameStructure(afterMarker: substructureAfterMarker, Syntax(expectedSubstructure), file: file, line: line)
553+
try subtreeMatcher.assertSameStructure(afterMarker: substructureAfterMarker, Syntax(expectedSubstructure), includeTrivia: substructureCheckTrivia, file: file, line: line)
549554
} catch {
550555
XCTFail("Matching for a subtree failed with error: \(error)", file: file, line: line)
551556
}

Tests/SwiftParserTest/translated/HashbangLibraryTests.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,47 @@
1212

1313
// This test file has been translated from swift/test/Parse/hashbang_library.swift
1414

15+
import SwiftSyntax
16+
1517
import XCTest
1618

1719
final class HashbangLibraryTests: XCTestCase {
1820
func testHashbangLibrary1() {
21+
// Check that we diagnose and skip the hashbang at the beginning of the file
22+
// when compiling in library mode.
1923
AssertParse(
2024
"""
21-
#!/usr/bin/swift
25+
#!/usr/bin/swift
2226
class Foo {}
23-
// Check that we diagnose and skip the hashbang at the beginning of the file
24-
// when compiling in library mode.
2527
""",
26-
diagnostics: [
27-
// TODO: Old parser expected error on line 1: hashbang line is allowed only in the main file
28-
]
28+
substructure: Syntax(
29+
SourceFileSyntax(
30+
statements: CodeBlockItemListSyntax([
31+
CodeBlockItemSyntax(
32+
item: .decl(
33+
DeclSyntax(
34+
ClassDeclSyntax(
35+
classKeyword: .keyword(
36+
.class,
37+
leadingTrivia: [
38+
.shebang("#!/usr/bin/swift"),
39+
.newlines(1),
40+
],
41+
trailingTrivia: .space
42+
),
43+
identifier: .identifier("Foo", trailingTrivia: .space),
44+
members: MemberDeclBlockSyntax(
45+
members: MemberDeclListSyntax([])
46+
)
47+
)
48+
)
49+
)
50+
)
51+
]),
52+
eofToken: .eof()
53+
)
54+
),
55+
substructureCheckTrivia: true
2956
)
3057
}
31-
3258
}

0 commit comments

Comments
 (0)