Skip to content

Commit c33c57c

Browse files
committed
Remove semantic hashbang error
1 parent 0b4bc29 commit c33c57c

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-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: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,59 @@
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+
attributes: nil,
36+
modifiers: nil,
37+
classKeyword: .keyword(
38+
.class,
39+
leadingTrivia: [
40+
.shebang("#!/usr/bin/swift"),
41+
.newlines(1),
42+
],
43+
trailingTrivia: .space
44+
),
45+
identifier: .identifier(
46+
"Foo",
47+
trailingTrivia: .space
48+
),
49+
genericParameterClause: nil,
50+
inheritanceClause: nil,
51+
genericWhereClause: nil,
52+
members: MemberDeclBlockSyntax(
53+
leftBrace: .leftBraceToken(),
54+
members: MemberDeclListSyntax([]),
55+
rightBrace: .rightBraceToken()
56+
)
57+
)
58+
)
59+
),
60+
semicolon: nil,
61+
errorTokens: nil
62+
)
63+
]),
64+
eofToken: .eof()
65+
)
66+
),
67+
substructureCheckTrivia: true
2968
)
3069
}
31-
3270
}

0 commit comments

Comments
 (0)