-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code organization moving towards a more configurable linter
- Loading branch information
Showing
22 changed files
with
438 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// Linter.swift | ||
// stringray | ||
// | ||
// Created by Geoffrey Foster on 2018-11-19. | ||
// | ||
|
||
import Foundation | ||
import Yams | ||
|
||
struct Linter { | ||
static let fileName = ".stringray.yml" | ||
|
||
private static let rules: [LintRule] = [ | ||
MissingLocalizationLintRule(), | ||
OrphanedLocalizationLintRule(), | ||
MissingPlaceholderLintRule() | ||
] | ||
|
||
let rules: [LintRule] | ||
let reporter: Reporter | ||
|
||
init(rules: [LintRule], reporter: Reporter = ConsoleReporter()) { | ||
self.rules = rules | ||
self.reporter = reporter | ||
} | ||
|
||
init(excluded: Set<String> = []) { | ||
let rules = Linter.rules.filter { | ||
!excluded.contains($0.info.identifier) | ||
} | ||
self.init(rules: rules) | ||
} | ||
|
||
init(path: String = Linter.fileName, rootPath: String? = nil) throws { | ||
let rootURL = URL(fileURLWithPath: rootPath ?? FileManager.default.currentDirectoryPath, isDirectory: true) | ||
let fullPathURL = URL(fileURLWithPath: path, relativeTo: rootURL) | ||
let yamlString = try String(contentsOf: fullPathURL, encoding: .utf8) | ||
let dict = try Yams.load(yaml: yamlString) as? [String: Any] | ||
let excluded = dict?["excluded"] as? [String] ?? [] | ||
self.init(excluded: Set(excluded)) | ||
} | ||
|
||
func run(on table: StringsTable, url: URL) throws -> [LintRuleViolation] { | ||
return try rules.flatMap { | ||
try $0.scan(table: table, url: url) | ||
} | ||
} | ||
|
||
func report(on table: StringsTable, url: URL) throws { | ||
let violations = try run(on: table, url: url) | ||
var outputStream = LinterOutputStream(fileHandle: FileHandle.standardOutput) | ||
reporter.generateReport(for: violations, to: &outputStream) | ||
} | ||
} | ||
|
||
private struct LinterOutputStream: TextOutputStream { | ||
private let fileHandle: FileHandle | ||
|
||
init(fileHandle: FileHandle) { | ||
self.fileHandle = fileHandle | ||
} | ||
|
||
mutating func write(_ string: String) { | ||
guard let data = string.appending("\n").data(using: .utf8) else { return } | ||
fileHandle.write(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.