diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index 76511191a4..541c968b3c 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -2589,3 +2589,53 @@ type ObjectGraphFormatter(opts: FormatOptions, bindingFlags) = and arrayValueL depthLim (arr: Array) = () """ + +[] +let ``verbatim string is ignore for hash directive scan, 1794`` () = + formatSourceString + false + """ +let ProgramFilesX86 = + match wow64, globalArch with + | "AMD64", "AMD64" + | null, "AMD64" + | "x86", "AMD64" -> Environment.GetEnvironmentVariable "ProgramFiles(x86)" + | _ -> Environment.GetEnvironmentVariable "ProgramFiles" + |> fun detected -> if detected = null then @"C:\Program Files (x86)\" else detected + +let isUnix = +#if NETSTANDARD1_6 || NETSTANDARD2_0 + System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform( + System.Runtime.InteropServices.OSPlatform.Linux) || + System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform( + System.Runtime.InteropServices.OSPlatform.OSX) +#else + int Environment.OSVersion.Platform |> fun p -> (p = 4) || (p = 6) || (p = 128) +#endif +""" + config + |> prepend newline + |> should + equal + """ +let ProgramFilesX86 = + match wow64, globalArch with + | "AMD64", "AMD64" + | null, "AMD64" + | "x86", "AMD64" -> Environment.GetEnvironmentVariable "ProgramFiles(x86)" + | _ -> Environment.GetEnvironmentVariable "ProgramFiles" + |> fun detected -> + if detected = null then + @"C:\Program Files (x86)\" + else + detected + +let isUnix = +#if NETSTANDARD1_6 || NETSTANDARD2_0 + System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux) + || System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX) +#else + int Environment.OSVersion.Platform + |> fun p -> (p = 4) || (p = 6) || (p = 128) +#endif +""" diff --git a/src/Fantomas.Tests/TokenParserTests.fs b/src/Fantomas.Tests/TokenParserTests.fs index 97b4452e9f..7737e1f1f3 100644 --- a/src/Fantomas.Tests/TokenParserTests.fs +++ b/src/Fantomas.Tests/TokenParserTests.fs @@ -638,3 +638,21 @@ prinfn \"Debug shizzle\" " getDefines source == [ "DEBUG" ] + +[] +let ``backslash in verbatim string`` () = + let source = + " +let ProgramFilesX86 = + if detected = null then @\"C:\Program Files (x86)\\\" else detected + +let isUnix = +#if NETSTANDARD1_6 || NETSTANDARD2_0 + meh +#else + foo +#endif + " + + getDefines source + == [ "NETSTANDARD1_6"; "NETSTANDARD2_0" ] diff --git a/src/Fantomas/TokenParser.fs b/src/Fantomas/TokenParser.fs index 48078e3548..4491b88d5f 100644 --- a/src/Fantomas/TokenParser.fs +++ b/src/Fantomas/TokenParser.fs @@ -98,6 +98,7 @@ type SourceCodeState = | Normal | InsideString | InsideTripleQuoteString of startIndex: int + | InsideVerbatimString of startIndex: int | InsideMultilineComment | InsideLineComment @@ -131,6 +132,7 @@ let rec private getTokenizedHashes (sourceCode: string) : Token list = let (|NoBackSlashChar|_|) = differsFromChar '\\' let (|CloseParenChar|_|) = equalsChar ')' let (|ForwardSlashChar|_|) = equalsChar '/' + let (|AtChar|_|) = equalsChar '@' let (|LineCommentStart|_|) v = match v with @@ -253,6 +255,9 @@ let rec private getTokenizedHashes (sourceCode: string) : Token list = | Normal, TripleQuoteChars -> { acc with State = InsideTripleQuoteString(idx) } + | Normal, (AtChar, DoubleQuoteChar, _) -> + { acc with + State = InsideVerbatimString idx } | Normal, (DoubleQuoteChar, _, _) -> { acc with State = InsideString } | Normal, (OpenParenChar, AsteriskChar, NoCloseParenChar) when (sourceLength > 3) -> { acc with @@ -272,6 +277,9 @@ let rec private getTokenizedHashes (sourceCode: string) : Token list = | Normal, TripleQuoteChars -> { acc with State = InsideTripleQuoteString idx } + | Normal, (AtChar, DoubleQuoteChar, _) -> + { acc with + State = InsideVerbatimString idx } | Normal, (DoubleQuoteChar, _, _) -> { acc with State = InsideString } | Normal, (OpenParenChar, AsteriskChar, NoCloseParenChar) -> { acc with @@ -305,6 +313,13 @@ let rec private getTokenizedHashes (sourceCode: string) : Token list = | NoBackSlashChar -> { acc with State = Normal } | _ -> acc | _ -> acc + | InsideVerbatimString _, (DoubleQuoteChar, DoubleQuoteChar, _) -> acc + | InsideVerbatimString startIndex, (DoubleQuoteChar, _, _) -> + if idx = startIndex + 1 then + // Still at the start of the verbatim string @" + acc + else + { acc with State = Normal } | InsideMultilineComment, (NewlineChar, _, _) -> { acc with NewlineIndexes = idx :: acc.NewlineIndexes }