Skip to content

Commit dbc576e

Browse files
committed
merge imports in _tmp_dangerfile.swift
1 parent 0a4e2a6 commit dbc576e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Sources/RunnerLib/Files Import/DangerFileGenerator.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public final class DangerFileGenerator {
2525
dangerContent.replaceSubrange(replacementRange, with: fileContent)
2626
})
2727

28+
mergeImports(in: &dangerContent)
29+
2830
try dangerContent.write(toFile: fileName, atomically: false, encoding: .utf8)
2931
}
3032
}
33+
34+
private extension DangerFileGenerator {
35+
func mergeImports(in content: inout String) {
36+
var lines = content.split(separator: "\n",
37+
omittingEmptySubsequences: false)
38+
let imports = Set(lines.filter { $0.hasPrefix("import ") })
39+
lines.removeAll { imports.contains($0) }
40+
lines.insert(contentsOf: imports.sorted(), at: 0)
41+
content = lines.joined(separator: "\n")
42+
}
43+
}

Tests/RunnerLibTests/__Snapshots__/DangerFileGeneratorTests/testItGeneratesTheCorrectFileWhenOneOfTheImportedFilesIsMissing.1.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import Danger
2+
13
file2Content ⚠️
24

35
// fileImport: GeneratedTestFile3.swift
6+
7+
let danger = Danger()
8+
49
file1Content 👍🏻
510
secondLine
611
message("Text")

Tests/RunnerLibTests/__Snapshots__/DangerFileGeneratorTests/testItGeneratesTheCorrectFileWhenThereIsAreMultipleImports.1.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import Danger
2+
import Foundation
3+
14
file2Content ⚠️
25

6+
37
file3Content 👩‍👩‍👦‍👦
48
secondLine
59
really really really really really really really really really really really really really really really really really really really really really really long text
10+
11+
let danger = Danger()
12+
613
file1Content 👍🏻
714
secondLine
815
message("Text")

0 commit comments

Comments
 (0)