Skip to content

Commit

Permalink
Verbatim string is ignore for hash directive scan. Fixes fsprojects#1794
Browse files Browse the repository at this point in the history
.
  • Loading branch information
nojaf committed Jul 1, 2021
1 parent 0323229 commit 8fbcdf5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Expand Up @@ -2589,3 +2589,53 @@ type ObjectGraphFormatter(opts: FormatOptions, bindingFlags) =
and arrayValueL depthLim (arr: Array) = ()
"""

[<Test>]
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
"""
18 changes: 18 additions & 0 deletions src/Fantomas.Tests/TokenParserTests.fs
Expand Up @@ -638,3 +638,21 @@ prinfn \"Debug shizzle\"
"

getDefines source == [ "DEBUG" ]

[<Test>]
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" ]
15 changes: 15 additions & 0 deletions src/Fantomas/TokenParser.fs
Expand Up @@ -98,6 +98,7 @@ type SourceCodeState =
| Normal
| InsideString
| InsideTripleQuoteString of startIndex: int
| InsideVerbatimString of startIndex: int
| InsideMultilineComment
| InsideLineComment

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit 8fbcdf5

Please sign in to comment.