Created
October 13, 2022 18:33
-
-
Save baronfel/587163f774390304ea677ee748a9cfaa to your computer and use it in GitHub Desktop.
Minimal FCS source checking
This file contains hidden or 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
#r "FSharp.Compiler.Service" | |
open FSharp.Compiler.EditorServices | |
open FSharp.Compiler.CodeAnalysis | |
open FSharp.Compiler.Text | |
open System.IO | |
let fcsVersion = | |
System | |
.Reflection | |
.Assembly | |
.GetAssembly( | |
typeof<FSharpChecker> | |
) | |
.GetName() | |
.Version | |
printfn "%A" fcsVersion | |
let checker = FSharpChecker.Create() | |
let sourceText code = | |
SourceText.ofString code | |
// get some kind of project options | |
let getProjectOptions (path, sourceText) = | |
checker.GetProjectOptionsFromScript( | |
path, | |
sourceText, | |
assumeDotNetFramework = false, | |
useSdkRefs = true, | |
useFsiAuxLib = true, | |
otherFlags = [| "--targetprofile:netstandard" |] | |
) | |
|> Async.RunSynchronously | |
let parseAndCheckSourcecode (sourceCode: string) = | |
let sourceText = sourceText sourceCode | |
let options, diagnostics = getProjectOptions ("Code.fs", sourceText) | |
let p, c = | |
checker.ParseAndCheckFileInProject("Code.fs", 0, sourceText, options) | |
|> Async.RunSynchronously | |
match c with | |
| FSharpCheckFileAnswer.Aborted -> p, None | |
| FSharpCheckFileAnswer.Succeeded c -> p, Some c | |
let parseResults, checkResults = parseAndCheckSourcecode "let x = 1" | |
printfn "%A" parseResults.Diagnostics | |
printfn "%A" checkResults.Value.Diagnostics |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment