Skip to content

Commit

Permalink
Apply fixes of analyzers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 6, 2023
1 parent ba2fc17 commit 855e4f0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
32 changes: 20 additions & 12 deletions src/Fable.Compiler/Globbing.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Fable.Compiler.Globbing

open System
open System.Collections.Generic
open System.IO

Expand Down Expand Up @@ -140,7 +141,7 @@ module Glob =

let globRoot =
// If we dropped "/" from the beginning of the path in the 'Split' call, put it back!
if normPattern.StartsWith("/") then
if normPattern.StartsWith('/') then
"/" + globRoot
else
globRoot
Expand All @@ -162,9 +163,9 @@ module Glob =
// names (as one folder name could be a substring of the other)
let start =
baseDir.TrimEnd([| Path.DirectorySeparatorChar |])
+ string Path.DirectorySeparatorChar
+ string<char> Path.DirectorySeparatorChar
// See https://github.com/fsharp/FAKE/issues/1925
if input.StartsWith start then
if input.StartsWith(start, StringComparison.Ordinal) then
input.Substring start.Length
else
input
Expand All @@ -182,7 +183,10 @@ module Glob =

let baseItems =
let start, rest =
if input.StartsWith "\\\\" && splits.Length >= 4 then
if
input.StartsWith("\\\\", StringComparison.Ordinal)
&& splits.Length >= 4
then
let serverName = splits.[2]
let share = splits.[3]

Expand All @@ -197,21 +201,21 @@ module Glob =
elif
splits.Length >= 2
&& Path.IsPathRooted input
&& input.StartsWith "/"
&& input.StartsWith '/'
then
[ Directory("/") ], splits |> Array.toSeq
else
if Path.IsPathRooted input then
if input.StartsWith "\\" then
if input.StartsWith '\\' then
failwithf
"Please remove the leading '\\' or '/' and replace them with \
'.\\' or './' if you want to use a relative path. Leading \
slashes are considered an absolute path (input was '%s')!"
'.\\' or './' if you want to use a relative path. Leading \
slashes are considered an absolute path (input was '%s')!"
originalInput
else
failwithf
"Unknown globbing input '%s', try to use a \
relative path and report an issue!"
relative path and report an issue!"
originalInput

[], splits |> Array.toSeq
Expand Down Expand Up @@ -412,13 +416,13 @@ module GlobbingPatternExtensions =

let included =
this.Includes
|> Seq.exists (fun fileInclude ->
|> List.exists (fun fileInclude ->
Glob.isMatch (fullDir fileInclude) fullPath
)

let excluded =
this.Excludes
|> Seq.exists (fun fileExclude ->
|> List.exists (fun fileExclude ->
Glob.isMatch (fullDir fileExclude) fullPath
)

Expand Down Expand Up @@ -465,7 +469,11 @@ module GlobbingPattern =
|> Seq.filter (fun d ->
directoryIncludes
|> Seq.exists (fun p ->
d.StartsWith(p + string Path.DirectorySeparatorChar) && p <> d
d.StartsWith(
p + string<char> Path.DirectorySeparatorChar,
StringComparison.Ordinal
)
&& p <> d
)
|> not
)
Expand Down
48 changes: 32 additions & 16 deletions src/Fable.Compiler/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ type CrackerResponse =
}

let isSystemPackage (pkgName: string) =
pkgName.StartsWith("System.")
|| pkgName.StartsWith("Microsoft.")
|| pkgName.StartsWith("runtime.")
pkgName.StartsWith("System.", StringComparison.Ordinal)
|| pkgName.StartsWith("Microsoft.", StringComparison.Ordinal)
|| pkgName.StartsWith("runtime.", StringComparison.Ordinal)
|| pkgName = "NETStandard.Library"
|| pkgName = "FSharp.Core"
|| pkgName = "Fable.Core"
Expand Down Expand Up @@ -296,7 +296,9 @@ let tryGetFablePackage (opts: CrackerOptions) (dllPath: string) =
let fsprojPath =
match Map.tryFind pkgId opts.Replace with
| Some replaced ->
if replaced.EndsWith(".fsproj") then
if
replaced.EndsWith(".fsproj", StringComparison.Ordinal)
then
replaced
else
tryFileWithPattern replaced "*.fsproj"
Expand Down Expand Up @@ -469,18 +471,24 @@ let private extractUsefulOptionsAndSources
(line: string)
(accSources: string list, accOptions: string list)
=
if line.StartsWith("-") then
if line.StartsWith('-') then
// "--warnaserror" // Disable for now to prevent unexpected errors, see #2288
if line.StartsWith("--langversion:") && isMainProj then
if
line.StartsWith("--langversion:", StringComparison.Ordinal)
&& isMainProj
then
let v = line.Substring("--langversion:".Length).ToLowerInvariant()

if v = "preview" then
accSources, line :: accOptions
else
accSources, accOptions
elif line.StartsWith("--nowarn") || line.StartsWith("--warnon") then
elif
line.StartsWith("--nowarn", StringComparison.Ordinal)
|| line.StartsWith("--warnon", StringComparison.Ordinal)
then
accSources, line :: accOptions
elif line.StartsWith("--define:") then
elif line.StartsWith("--define:", StringComparison.Ordinal) then
// When parsing the project as .csproj there will be multiple defines in the same line,
// but the F# compiler seems to accept only one per line
let defines =
Expand Down Expand Up @@ -534,7 +542,7 @@ let getCrackedMainFsproj
let sourceFiles, otherOpts =
(projOpts, ([], []))
||> Array.foldBack (fun line (src, otherOpts) ->
if line.StartsWith("-r:") then
if line.StartsWith("-r:", StringComparison.Ordinal) then
let line = Path.normalizePath (line[3..])
let dllName = getDllName line
dllRefs.Add(dllName, line)
Expand Down Expand Up @@ -664,7 +672,12 @@ let getProjectOptionsFromProjectFile =
r.CompilerArguments
|> Array.map (fun line ->
if reg.IsMatch(line) then
if line.StartsWith("/reference") then
if
line.StartsWith(
"/reference",
StringComparison.Ordinal
)
then
"-r" + line.Substring(10)
else
"--" + line.Substring(1)
Expand Down Expand Up @@ -709,9 +722,12 @@ let getProjectOptionsFromProjectFile =

let projOpts =
compilerArgs
|> Array.skipWhile (fun line -> not (line.StartsWith("-")))
|> Array.skipWhile (fun line -> not (line.StartsWith('-')))
|> Array.map (fun f ->
if f.EndsWith(".fs") || f.EndsWith(".fsi") then
if
f.EndsWith(".fs", StringComparison.Ordinal)
|| f.EndsWith(".fsi", StringComparison.Ordinal)
then
if Path.IsPathRooted f then
f
else
Expand Down Expand Up @@ -811,7 +827,7 @@ let retryGetCrackedProjects opts =
// Replace the .fsproj extension with .fableproj for files in fable_modules
// We do this to avoid conflicts with other F# tooling that scan for .fsproj files
let changeFsprojToFableproj (path: string) =
if path.EndsWith(".fsproj") then
if path.EndsWith(".fsproj", StringComparison.Ordinal) then
IO.Path.ChangeExtension(path, Naming.fableProjExt)
else
path
Expand Down Expand Up @@ -858,7 +874,7 @@ let getFableLibraryPath (opts: CrackerOptions) =
| Python, Some Py.Naming.sitePackages ->
"fable-library-py", "fable-library"
| _, Some path ->
if path.StartsWith("./") then
if path.StartsWith("./", StringComparison.Ordinal) then
"", Path.normalizeFullPath path
elif IO.Path.IsPathRooted(path) then
"", Path.normalizePath path
Expand Down Expand Up @@ -969,7 +985,7 @@ let loadPrecompiledInfo (opts: CrackerOptions) otherOptions sourceFiles =
// (e.g. fable_modules/Fable.Promise.2.1.0/Promise.fs) so we assume they're the same wherever they are
// TODO: Check if this holds true also for Python which may not include the version number in the path
let normalizePath (path: string) =
let i = path.IndexOf(Naming.fableModules)
let i = path.IndexOf(Naming.fableModules, StringComparison.Ordinal)

if i >= 0 then
path[i..]
Expand Down Expand Up @@ -1238,7 +1254,7 @@ let getFullProjectOpts (opts: CrackerOptions) =

if
ignoredRefs.Contains(name)
|| (name.StartsWith("System.")
|| (name.StartsWith("System.", StringComparison.Ordinal)
&& not (coreRefs.Contains(name)))
then
None
Expand Down
5 changes: 3 additions & 2 deletions src/Fable.Compiler/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ module Imports =
path.Replace("../", "").Replace("./", "").Replace(":", "")

let isRelativePath (path: string) =
path.StartsWith("./") || path.StartsWith("../")
path.StartsWith("./", StringComparison.Ordinal)
|| path.StartsWith("../", StringComparison.Ordinal)

let isAbsolutePath (path: string) =
path.StartsWith('/') || path.IndexOf(':') = 1
Expand Down Expand Up @@ -735,7 +736,7 @@ module Imports =
importPath

if isAbsolutePath importPath then
if importPath.EndsWith(".fs") then
if importPath.EndsWith(".fs", StringComparison.Ordinal) then
getTargetRelativePath
pathResolver
importPath
Expand Down

0 comments on commit 855e4f0

Please sign in to comment.