Created
June 16, 2018 01:32
-
-
Save abl/189e53263a35383f7d9d8915694a479d to your computer and use it in GitHub Desktop.
ErrorOnForceUnwrap.swift
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
// Copyright 2018 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
import Foundation | |
import SwiftSyntax | |
public class DiagnoseForceUnwraps: SyntaxVisitor { | |
public let engine: DiagnosticEngine | |
public let file: URL | |
public init(file: URL, diagnosticEngine: DiagnosticEngine) { | |
self.file = file | |
self.engine = diagnosticEngine | |
} | |
override public func visit(_ node: ForcedValueExprSyntax) { | |
let location = node.exclamationMark.startLocation(in: file) | |
engine.diagnose(.noForceUnwrap, location: location) { | |
let expressionRange = node.expression.sourceRange(in: self.file) | |
$0.highlight(expressionRange) | |
} | |
super.visit(node.expression) | |
} | |
} | |
extension Diagnostic.Message { | |
public static let noForceUnwrap = | |
Diagnostic.Message(.error, "force unwrapping is not allowed") | |
} | |
func main() throws { | |
let engine = DiagnosticEngine() | |
engine.addConsumer(PrintingDiagnosticConsumer()) | |
let testURL = URL(fileURLWithPath: CommandLine.arguments[1]) | |
let testFile = try SourceFileSyntax.parse(testURL) | |
let diagnoseForceUnwraps = DiagnoseForceUnwraps( | |
file: testURL, | |
diagnosticEngine: engine | |
) | |
diagnoseForceUnwraps.visit(testFile) | |
} | |
do { | |
try main() | |
} catch { | |
print("error: \(error)") | |
exit(-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment