Skip to content

Instantly share code, notes, and snippets.

@baronfel
Created October 13, 2022 18:33
Show Gist options
  • Save baronfel/587163f774390304ea677ee748a9cfaa to your computer and use it in GitHub Desktop.
Save baronfel/587163f774390304ea677ee748a9cfaa to your computer and use it in GitHub Desktop.
Minimal FCS source checking
#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