-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Npgsql.FSharp.Analyzer v3.18 detect queries within lambda expressions
- Loading branch information
Showing
17 changed files
with
312 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Auto-Generated by FAKE; do not edit | ||
namespace System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyTitleAttribute("Ubik")>] | ||
[<assembly: AssemblyProductAttribute("NpgsqlFSharpAnalyzer")>] | ||
[<assembly: AssemblyVersionAttribute("3.18.0")>] | ||
[<assembly: AssemblyMetadataAttribute("ReleaseDate","2020-09-15T00:00:00.0000000")>] | ||
[<assembly: AssemblyFileVersionAttribute("3.18.0")>] | ||
[<assembly: AssemblyInformationalVersionAttribute("3.18.0")>] | ||
[<assembly: AssemblyMetadataAttribute("ReleaseChannel","release")>] | ||
[<assembly: AssemblyMetadataAttribute("GitHash","8d5412fe3dd28abc45fb45a7d97134d09ec1ce82")>] | ||
do () | ||
|
||
module internal AssemblyVersionInformation = | ||
let [<Literal>] AssemblyTitle = "Ubik" | ||
let [<Literal>] AssemblyProduct = "NpgsqlFSharpAnalyzer" | ||
let [<Literal>] AssemblyVersion = "3.18.0" | ||
let [<Literal>] AssemblyMetadata_ReleaseDate = "2020-09-15T00:00:00.0000000" | ||
let [<Literal>] AssemblyFileVersion = "3.18.0" | ||
let [<Literal>] AssemblyInformationalVersion = "3.18.0" | ||
let [<Literal>] AssemblyMetadata_ReleaseChannel = "release" | ||
let [<Literal>] AssemblyMetadata_GitHash = "8d5412fe3dd28abc45fb45a7d97134d09ec1ce82" |
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,79 @@ | ||
open System | ||
open System.IO | ||
open Npgsql.FSharp.Analyzers.Core | ||
open Spectre.Console | ||
open System.Xml | ||
|
||
let resolveFile (path: string) = | ||
if Path.IsPathRooted path | ||
then path | ||
else Path.GetFullPath (Path.Combine(Environment.CurrentDirectory, path)) | ||
|
||
let getProject (args: string []) = | ||
try | ||
match args with | ||
| [| |] -> | ||
Directory.GetFiles(Environment.CurrentDirectory, "*.fsproj") | ||
|> Array.tryHead | ||
|> Option.map (fun projectPath -> resolveFile projectPath) | ||
|
||
| multipleArgs -> | ||
let firstArg = multipleArgs.[0] | ||
if firstArg.EndsWith(".fsproj") then | ||
Some (resolveFile firstArg) | ||
else | ||
Directory.GetFiles(resolveFile firstArg, "*.fsproj") | ||
|> Array.tryHead | ||
|> Option.map (fun projectPath -> resolveFile projectPath) | ||
with | ||
| error -> None | ||
|
||
[<EntryPoint>] | ||
let main argv = | ||
match getProject argv with | ||
| None -> | ||
printfn "No project file found in the current directory" | ||
1 | ||
|
||
| Some project -> | ||
AnsiConsole.MarkupLine("Analyzing [blue]{0}[/]", project) | ||
|
||
let document = XmlDocument() | ||
document.LoadXml(File.ReadAllText project) | ||
|
||
let fsharpFileNodes = document.GetElementsByTagName("Compile") | ||
let fsharpFiles = [ | ||
for item in 0 .. fsharpFileNodes.Count - 1 -> | ||
let relativePath = fsharpFileNodes.[item].Attributes.["Include"].InnerText | ||
let projectParent = Directory.GetParent project | ||
Path.Combine(projectParent.FullName, relativePath) | ||
] | ||
|
||
for file in fsharpFiles do | ||
AnsiConsole.MarkupLine("Analyzing file [green]{0}[/]", file) | ||
match Project.context file with | ||
| None -> () | ||
| Some context -> | ||
let syntacticBlocks = SyntacticAnalysis.findSqlOperations context | ||
if not syntacticBlocks.IsEmpty then | ||
let messages = | ||
let connectionString = SqlAnalyzer.tryFindConnectionString context.FileName | ||
if isNull connectionString || String.IsNullOrWhiteSpace connectionString then | ||
[ ] | ||
else | ||
match SqlAnalysis.databaseSchema connectionString with | ||
| Result.Error connectionError -> | ||
[ | ||
for block in syntacticBlocks -> | ||
SqlAnalysis.createWarning (sprintf "Error while connecting to the development database using the connection string from environment variable 'NPGSQL_FSHARP' or put the connection string in a file called 'NPGSQL_FSHARP' relative next your project or in your project root. Connection error: %s" connectionError) block.range | ||
] | ||
|
||
| Result.Ok schema -> | ||
syntacticBlocks | ||
|> List.collect (fun block -> SqlAnalysis.analyzeOperation block connectionString schema) | ||
|> List.distinctBy (fun message -> message.Range) | ||
|
||
for message in messages do | ||
AnsiConsole.MarkupLine("Error [red]{0}[/]", message.Message) | ||
|
||
0 |
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,97 @@ | ||
module Project | ||
|
||
open System | ||
open System.IO | ||
open FSharp.Compiler.SourceCodeServices | ||
open FSharp.Compiler.Text | ||
open Npgsql.FSharp.Analyzers.Core | ||
|
||
let checker = | ||
FSharpChecker.Create( | ||
keepAllBackgroundResolutions = true, | ||
keepAssemblyContents = true, | ||
ImplicitlyStartBackgroundWork = true) | ||
|
||
let dumpOpts (opts : FSharpProjectOptions) = | ||
printfn "FSharpProjectOptions.OtherOptions ->" | ||
opts.OtherOptions | ||
|> Array.iter(printfn "%s") | ||
|
||
let loadProject file = | ||
async { | ||
let! source = IO.File.ReadAllTextAsync file |> Async.AwaitTask | ||
let! (opts, error) = checker.GetProjectOptionsFromScript(file, SourceText.ofString source, assumeDotNetFramework = false, useSdkRefs = true, useFsiAuxLib = true, otherFlags = [|"--targetprofile:netstandard" |]) | ||
let newOO = | ||
opts.OtherOptions | ||
|> Array.map(fun i -> | ||
if i.StartsWith("-r:") then | ||
let path = i.Split("-r:", StringSplitOptions.RemoveEmptyEntries).[0] | ||
|
||
sprintf "-r:%s" (IO.FileInfo(path).FullName) | ||
else | ||
i | ||
) | ||
// dumpOpts opts | ||
return file, opts | ||
} |> Async.RunSynchronously | ||
|
||
let typeCheckFile (file,opts) = | ||
let text = File.ReadAllText file | ||
let st = SourceText.ofString text | ||
let (parseRes, checkAnswer) = | ||
checker.ParseAndCheckFileInProject(file, 1, st, opts) | ||
|> Async.RunSynchronously | ||
|
||
match checkAnswer with | ||
| FSharpCheckFileAnswer.Aborted -> | ||
printfn "Checking of file %s aborted because %A" file parseRes.Errors | ||
None | ||
| FSharpCheckFileAnswer.Succeeded(c) -> | ||
Some (file, text, parseRes, c) | ||
|
||
let entityCache = EntityCache() | ||
|
||
let getAllEntities (checkResults: FSharpCheckFileResults) (publicOnly: bool) : AssemblySymbol list = | ||
try | ||
let res = [ | ||
yield! AssemblyContentProvider.getAssemblySignatureContent AssemblyContentType.Full checkResults.PartialAssemblySignature | ||
let ctx = checkResults.ProjectContext | ||
let assembliesByFileName = | ||
ctx.GetReferencedAssemblies() | ||
|> Seq.groupBy (fun asm -> asm.FileName) | ||
|> Seq.map (fun (fileName, asms) -> fileName, List.ofSeq asms) | ||
|> Seq.toList | ||
|> List.rev // if mscorlib.dll is the first then FSC raises exception when we try to | ||
// get Content.Entities from it. | ||
|
||
for fileName, signatures in assembliesByFileName do | ||
let contentType = if publicOnly then Public else Full | ||
let content = AssemblyContentProvider.getAssemblyContent entityCache.Locking contentType fileName signatures | ||
yield! content | ||
] | ||
res | ||
with | ||
| _ -> [] | ||
|
||
let createContext (file, text: string, p: FSharpParseFileResults,c: FSharpCheckFileResults) = | ||
match p.ParseTree with | ||
| Some parseTree -> | ||
let context : SqlAnalyzerContext = { | ||
FileName = file | ||
Content = text.Split([|'\n'|]) | ||
ParseTree = parseTree | ||
Symbols = c.PartialAssemblySignature.Entities |> Seq.toList | ||
} | ||
|
||
Some context | ||
| _ -> | ||
None | ||
|
||
let context proj = | ||
let path = | ||
Path.Combine(Environment.CurrentDirectory, proj) | ||
|> Path.GetFullPath | ||
|
||
loadProject path | ||
|> typeCheckFile | ||
|> Option.bind createContext |
Oops, something went wrong.