diff --git a/build.fsx b/build.fsx index 24efbf705a..90796db874 100644 --- a/build.fsx +++ b/build.fsx @@ -158,8 +158,11 @@ pipeline "FormatChanged" { |> Array.choose (fun line -> let line = line.Trim() if - (line.StartsWith("AM") || line.StartsWith("M")) - && (line.EndsWith(".fs") || line.EndsWith(".fsx") || line.EndsWith(".fsi")) + (line.StartsWith("AM", StringComparison.Ordinal) + || line.StartsWith("M", StringComparison.Ordinal)) + && (line.EndsWith(".fs", StringComparison.Ordinal) + || line.EndsWith(".fsx", StringComparison.Ordinal) + || line.EndsWith(".fsi", StringComparison.Ordinal)) then Some(line.Replace("AM ", "").Replace("M ", "")) else @@ -372,7 +375,7 @@ let mkGithubRelease (v: SemanticVersion, d: DateTime, cd: ChangelogData option) None else let output = cmdResult.StandardOutput.Trim() - let lastIdx = output.LastIndexOf("Z") + let lastIdx = output.LastIndexOf("Z", StringComparison.Ordinal) Some(output.Substring(0, lastIdx)) let sections = diff --git a/docs/docs/end-users/GettingStarted.fsx b/docs/docs/end-users/GettingStarted.fsx index e03dff11d1..0126cf2749 100644 --- a/docs/docs/end-users/GettingStarted.fsx +++ b/docs/docs/end-users/GettingStarted.fsx @@ -78,7 +78,7 @@ Consider the following PowerShell script: function Format-Changed(){ $files = git status --porcelain ` - | Where-Object { ($_.StartsWith(" M") -or $_.StartsWith("AM")) ` + | Where-Object { ($_.StartsWith(" M", "Ordinal") -or $_.StartsWith("AM", "Ordinal")) ` -and (Test-FSharpExtension $_) } | ForEach-Object { $_.substring(3) } & "dotnet" "fantomas" $files } diff --git a/src/Fantomas.Client/Contracts.fs b/src/Fantomas.Client/Contracts.fs index 735bb8101e..9f16eb4ffd 100644 --- a/src/Fantomas.Client/Contracts.fs +++ b/src/Fantomas.Client/Contracts.fs @@ -2,7 +2,6 @@ module Fantomas.Client.Contracts open System open System.Collections.Generic -open System.Globalization open System.Threading open System.Threading.Tasks @@ -26,8 +25,7 @@ type FormatDocumentRequest = Config: IReadOnlyDictionary option Cursor: FormatCursorPosition option } - member this.IsSignatureFile = - this.FilePath.EndsWith(".fsi", false, CultureInfo.InvariantCulture) + member this.IsSignatureFile = this.FilePath.EndsWith(".fsi", StringComparison.Ordinal) and FormatCursorPosition = class @@ -49,8 +47,7 @@ type FormatSelectionRequest = Range: FormatSelectionRange } - member this.IsSignatureFile = - this.FilePath.EndsWith(".fsi", false, CultureInfo.InvariantCulture) + member this.IsSignatureFile = this.FilePath.EndsWith(".fsi", StringComparison.Ordinal) and FormatSelectionRange = class diff --git a/src/Fantomas.Core/ASTTransformer.fs b/src/Fantomas.Core/ASTTransformer.fs index 720d38fd8a..1dd91af976 100644 --- a/src/Fantomas.Core/ASTTransformer.fs +++ b/src/Fantomas.Core/ASTTransformer.fs @@ -1,7 +1,7 @@ module internal rec Fantomas.Core.ASTTransformer +open System open System.Collections.Generic -open System.Globalization open System.Text.RegularExpressions open Fantomas.FCS.Text open Fantomas.FCS.Text.Range @@ -486,9 +486,7 @@ let (|SameInfixApps|_|) expr = match expr with | InfixApp(_, operator, _) -> let isRight = - Set.exists - (fun (rOp: string) -> operator.Text.StartsWith(rOp, false, CultureInfo.InvariantCulture)) - rightOperators + Set.exists (fun (rOp: string) -> String.startsWithOrdinal rOp operator.Text) rightOperators let head, xs = if isRight then @@ -1500,11 +1498,9 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = stn (creationAide.TextFromSource (fun () -> - if idx = 0 && not (v.StartsWith("$", false, CultureInfo.InvariantCulture)) then + if idx = 0 && not (String.startsWithOrdinal "$" v) then $"$\"%s{v}{{" - elif - idx = lastIndex && not (v.EndsWith("\"", false, CultureInfo.InvariantCulture)) - then + elif idx = lastIndex && not (String.endsWithOrdinal "\"" v) then $"}}%s{v}\"" else $"}}{v}{{") @@ -1538,10 +1534,7 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = let c3Node = stn (creationAide.TextFromSource (fun () -> c3) mC3) mC3 let dotText = - if - c1Node.Text.EndsWith(".", false, CultureInfo.InvariantCulture) - || c2Node.Text.EndsWith(".", false, CultureInfo.InvariantCulture) - then + if String.endsWithOrdinal "." c1Node.Text || String.endsWithOrdinal "." c2Node.Text then " .. " else ".." @@ -1556,7 +1549,7 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr = let hasSpaces = let rec (|AtomicExpr|_|) e = match e with - | ConstNumberExpr(v, _) when v.StartsWith("-", false, CultureInfo.InvariantCulture) -> None + | ConstNumberExpr(v, _) when String.startsWithOrdinal "-" v -> None | SynExpr.Ident _ | SynExpr.Const(SynConst.Int32 _, _) | SynExpr.IndexRange(expr1 = Some(AtomicExpr _); expr2 = Some(AtomicExpr _)) @@ -1606,10 +1599,7 @@ let mkExprQuote creationAide isRaw e range : ExprQuoteNode = let (|ParenStarSynIdent|_|) = function | IdentTrivia.OriginalNotationWithParen(lpr, originalNotation, rpr) -> - if - originalNotation.Length > 1 - && originalNotation.StartsWith("*", false, CultureInfo.InvariantCulture) - then + if originalNotation.Length > 1 && String.startsWithOrdinal "*" originalNotation then Some(lpr, originalNotation, rpr) else None diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index eeb77ec89a..2da33c60c2 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1,7 +1,6 @@ module internal rec Fantomas.Core.CodePrinter open System -open System.Globalization open Fantomas.Core.Context open Fantomas.Core.SyntaxOak open Microsoft.FSharp.Core.CompilerServices @@ -751,9 +750,7 @@ let genExpr (e: Expr) = match node.Expr with | Expr.Constant _ - | Expr.InterpolatedStringExpr _ when - not (node.Operator.Text.StartsWith("%", false, CultureInfo.InvariantCulture)) - -> + | Expr.InterpolatedStringExpr _ when not (String.startsWithOrdinal "%" node.Operator.Text) -> genSingleTextNode node.Operator +> sepSpace +> genExpr node.Expr | Expr.AppSingleParenArg appNode -> genSingleTextNode node.Operator @@ -1841,7 +1838,7 @@ let genArrayOrList (preferMultilineCramped: bool) (node: ExprArrayOrListNode) = let (|YieldLikeExpr|_|) e = match e with | Expr.Single singleNode -> - if singleNode.Leading.Text.StartsWith("yield", false, CultureInfo.InvariantCulture) then + if String.startsWithOrdinal "yield" singleNode.Leading.Text then Some e else None @@ -2263,7 +2260,7 @@ let colGenericTypeParameters typeParameters = coli sepComma typeParameters (fun idx t -> let leadingSpace = match t with - | Type.Var n when idx = 0 && n.Text.StartsWith("^", false, CultureInfo.InvariantCulture) -> sepSpace + | Type.Var n when idx = 0 && String.startsWithOrdinal "^" n.Text -> sepSpace | _ -> sepNone leadingSpace +> genType t) @@ -2514,10 +2511,7 @@ let genPatLeftMiddleRight (node: PatLeftMiddleRight) = let genTyparDecl (isFirstTypeParam: bool) (td: TyparDeclNode) = genOnelinerAttributes td.Attributes - +> onlyIf - (isFirstTypeParam - && td.TypeParameter.Text.StartsWith("^", false, CultureInfo.InvariantCulture)) - sepSpace + +> onlyIf (isFirstTypeParam && String.startsWithOrdinal "^" td.TypeParameter.Text) sepSpace +> genSingleTextNode td.TypeParameter |> genNode td @@ -3111,7 +3105,7 @@ let genType (t: Type) = let addExtraSpace = match node.Arguments with | [] -> sepNone - | Type.Var node :: _ when node.Text.StartsWith("^", false, CultureInfo.InvariantCulture) -> sepSpace + | Type.Var node :: _ when String.startsWithOrdinal "^" node.Text -> sepSpace | t :: _ -> addSpaceIfSynTypeStaticConstantHasAtSignBeforeString t genType node.Identifier @@ -3213,7 +3207,7 @@ let addSpaceIfSynTypeStaticConstantHasAtSignBeforeString (t: Type) = match t with | Type.StaticConstant sc -> match sc with - | Constant.FromText node -> onlyIf (node.Text.StartsWith("@", false, CultureInfo.InvariantCulture)) sepSpace + | Constant.FromText node -> onlyIf (String.startsWithOrdinal "@" node.Text) sepSpace | _ -> sepNone | _ -> sepNone diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index fa7ee86968..db7dd1100e 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -1,7 +1,6 @@ module internal Fantomas.Core.Context open System -open System.Globalization open Fantomas.FCS.Text open Fantomas.Core open Fantomas.Core.SyntaxOak @@ -536,11 +535,7 @@ let sepSpace (ctx: Context) = (!- " ") ctx else match lastWriteEventOnLastLine ctx with - | Some w when - (w.EndsWith(" ", false, CultureInfo.InvariantCulture) - || w.EndsWith(Environment.NewLine, false, CultureInfo.InvariantCulture)) - -> - ctx + | Some w when (String.endsWithOrdinal " " w || String.endsWithOrdinal Environment.NewLine w) -> ctx | None -> ctx | _ -> (!- " ") ctx @@ -864,7 +859,7 @@ let sepColon (ctx: Context) = defaultExpr ctx else match lastWriteEventOnLastLine ctx with - | Some w when w.EndsWith(" ", false, CultureInfo.InvariantCulture) -> !- ": " ctx + | Some w when String.endsWithOrdinal " " w -> !- ": " ctx | None -> !- ": " ctx | _ -> defaultExpr ctx diff --git a/src/Fantomas.Core/Trivia.fs b/src/Fantomas.Core/Trivia.fs index 1e63f4c3e1..229050708e 100644 --- a/src/Fantomas.Core/Trivia.fs +++ b/src/Fantomas.Core/Trivia.fs @@ -1,6 +1,6 @@ module internal Fantomas.Core.Trivia -open System.Globalization +open System open Fantomas.FCS.Syntax open Fantomas.FCS.SyntaxTrivia open Fantomas.FCS.Text @@ -47,9 +47,9 @@ let internal collectTriviaFromCodeComments let content = let trimmedLine = line.TrimStart(' ', ';') - if index = 0 && trimmedLine.StartsWith("#!", false, CultureInfo.InvariantCulture) then // shebang + if index = 0 && String.startsWithOrdinal "#!" trimmedLine then // shebang CommentOnSingleLine content - else if trimmedLine.StartsWith("//", false, CultureInfo.InvariantCulture) then + else if String.startsWithOrdinal "//" trimmedLine then CommentOnSingleLine content else LineCommentAfterSourceCode content