|
| 1 | +import SwiftParser |
| 2 | +import SwiftSyntax |
| 3 | +import Testing |
| 4 | + |
| 5 | +@testable import BridgeJSCore |
| 6 | + |
| 7 | +@Suite struct DiagnosticsTests { |
| 8 | + /// Returns the first parameter's type node from a function in the source (the first `@JS func`-like decl), for pinpointing diagnostics. |
| 9 | + private func firstParameterTypeNode(source: String) -> TypeSyntax? { |
| 10 | + let tree = Parser.parse(source: source) |
| 11 | + for stmt in tree.statements { |
| 12 | + if let funcDecl = stmt.item.as(FunctionDeclSyntax.self), |
| 13 | + let firstParam = funcDecl.signature.parameterClause.parameters.first |
| 14 | + { |
| 15 | + return firstParam.type |
| 16 | + } |
| 17 | + } |
| 18 | + return nil |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + func diagnosticIncludesLocationSourceAndHint() throws { |
| 23 | + let source = "@JS func foo(_ bar: A<X>) {}\n" |
| 24 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 25 | + let diagnostic = DiagnosticError( |
| 26 | + node: typeNode, |
| 27 | + message: "Unsupported type 'A<X>'.", |
| 28 | + hint: "Only primitive types and types defined in the same module are allowed" |
| 29 | + ) |
| 30 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 31 | + let expectedPrefix = """ |
| 32 | + <stdin>:1:21: error: Unsupported type 'A<X>'. |
| 33 | + 1 | @JS func foo(_ bar: A<X>) {} |
| 34 | + | `- error: Unsupported type 'A<X>'. |
| 35 | + 2 | |
| 36 | + """.trimmingCharacters(in: .whitespacesAndNewlines) |
| 37 | + #expect(description.hasPrefix(expectedPrefix)) |
| 38 | + #expect(description.contains("Hint: Only primitive types and types defined in the same module are allowed")) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + func diagnosticOmitsHintWhenNotProvided() throws { |
| 43 | + let source = "@JS static func foo() {}\n" |
| 44 | + let tree = Parser.parse(source: source) |
| 45 | + let diagnostic = DiagnosticError( |
| 46 | + node: tree, |
| 47 | + message: "Top-level functions cannot be static", |
| 48 | + hint: nil |
| 49 | + ) |
| 50 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 51 | + let expectedPrefix = """ |
| 52 | + <stdin>:1:1: error: Top-level functions cannot be static |
| 53 | + 1 | @JS static func foo() {} |
| 54 | + | `- error: Top-level functions cannot be static |
| 55 | + 2 | |
| 56 | + """.trimmingCharacters(in: .whitespacesAndNewlines) |
| 57 | + #expect(description.hasPrefix(expectedPrefix)) |
| 58 | + #expect(!description.contains("Hint:")) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + func diagnosticUsesGivenFileNameNotStdin() throws { |
| 63 | + let source = "@JS func foo(_ bar: A<X>) {}\n" |
| 64 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 65 | + let diagnostic = DiagnosticError( |
| 66 | + node: typeNode, |
| 67 | + message: "Unsupported type 'A<X>'.", |
| 68 | + hint: nil |
| 69 | + ) |
| 70 | + let description = diagnostic.formattedDescription(fileName: "Sources/Foo.swift", colorize: false) |
| 71 | + #expect(description.hasPrefix("Sources/Foo.swift:1:21: error: Unsupported type 'A<X>'.")) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + func diagnosticWithColorizeTrueIncludesANSISequences() throws { |
| 76 | + let source = "@JS func foo(_ bar: A<X>) {}\n" |
| 77 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 78 | + let diagnostic = DiagnosticError( |
| 79 | + node: typeNode, |
| 80 | + message: "Unsupported type 'A<X>'.", |
| 81 | + hint: nil |
| 82 | + ) |
| 83 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: true) |
| 84 | + let esc = "\u{001B}" |
| 85 | + let boldRed = "\(esc)[1;31m" |
| 86 | + let boldDefault = "\(esc)[1;39m" |
| 87 | + let reset = "\(esc)[0;0m" |
| 88 | + let cyan = "\(esc)[0;36m" |
| 89 | + let underline = "\(esc)[4;39m" |
| 90 | + let expected = |
| 91 | + "<stdin>:1:21: \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n" |
| 92 | + + "\(cyan) 1\(reset) | @JS func foo(_ bar: \(underline)A<X>\(reset)) {}\n" |
| 93 | + + " | `- \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n" |
| 94 | + + "\(cyan) 2\(reset) | " |
| 95 | + #expect(description == expected) |
| 96 | + } |
| 97 | + |
| 98 | + // MARK: - Context source lines |
| 99 | + |
| 100 | + @Test |
| 101 | + func showsOnePreviousLineWhenErrorNotOnFirstLine() throws { |
| 102 | + let source = """ |
| 103 | + preamble |
| 104 | + @JS func foo(_ bar: A<X>) {} |
| 105 | + """ |
| 106 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 107 | + let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil) |
| 108 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 109 | + #expect(description.contains(" 1 | preamble")) |
| 110 | + #expect(description.contains(" 2 | @JS func foo(_ bar: A<X>) {}")) |
| 111 | + #expect(description.contains("<stdin>:2:")) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + func showsThreePreviousLinesWhenAvailable() throws { |
| 116 | + let source = """ |
| 117 | + first |
| 118 | + second |
| 119 | + third |
| 120 | + @JS func foo(_ bar: A<X>) {} |
| 121 | + """ |
| 122 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 123 | + let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil) |
| 124 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 125 | + #expect(description.contains(" 1 | first")) |
| 126 | + #expect(description.contains(" 2 | second")) |
| 127 | + #expect(description.contains(" 3 | third")) |
| 128 | + #expect(description.contains(" 4 | @JS func foo(_ bar: A<X>) {}")) |
| 129 | + #expect(description.contains("<stdin>:4:")) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + func capsContextAtThreePreviousLines() throws { |
| 134 | + let source = """ |
| 135 | + line0 |
| 136 | + line1 |
| 137 | + line2 |
| 138 | + line3 |
| 139 | + @JS func foo(_ bar: A<X>) {} |
| 140 | + """ |
| 141 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 142 | + let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil) |
| 143 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 144 | + #expect(!description.contains(" 1 | line0")) |
| 145 | + #expect(description.contains(" 2 | line1")) |
| 146 | + #expect(description.contains(" 3 | line2")) |
| 147 | + #expect(description.contains(" 4 | line3")) |
| 148 | + #expect(description.contains(" 5 | @JS func foo(_ bar: A<X>) {}")) |
| 149 | + #expect(description.contains("<stdin>:5:")) |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + func includesNextLineAfterErrorLine() throws { |
| 154 | + let source = """ |
| 155 | + @JS func foo( |
| 156 | + _ bar: A<X> |
| 157 | + ) {} |
| 158 | + """ |
| 159 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 160 | + let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil) |
| 161 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 162 | + #expect(description.contains(" 1 | @JS func foo(")) |
| 163 | + #expect(description.contains(" 2 | _ bar: A<X>")) |
| 164 | + #expect(description.contains(" 3 | ) {}")) |
| 165 | + #expect(description.contains("<stdin>:2:")) |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + func omitsNextLineWhenErrorIsOnLastLine() throws { |
| 170 | + let source = """ |
| 171 | + preamble |
| 172 | + @JS func foo(_ bar: A<X>) |
| 173 | + """ |
| 174 | + let typeNode = try #require(firstParameterTypeNode(source: source)) |
| 175 | + let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil) |
| 176 | + let description = diagnostic.formattedDescription(fileName: "-", colorize: false) |
| 177 | + #expect(description.contains(" 2 | @JS func foo(_ bar: A<X>)")) |
| 178 | + #expect(description.contains("<stdin>:2:")) |
| 179 | + // No line 3 in source, so output must not show a " 3 |" context line after the pointer |
| 180 | + #expect(!description.contains(" 3 |")) |
| 181 | + } |
| 182 | +} |
0 commit comments