Skip to content

Commit

Permalink
Don't close string when there is a triple backslash before the double…
Browse files Browse the repository at this point in the history
… quote. Fix #1289. (#1291)
  • Loading branch information
nojaf committed Dec 17, 2020
1 parent 4e28f64 commit fee9f2d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/Fantomas.Tests/SynConstTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,35 @@ let ``preserve underscore in int64, 1120`` () =
formatSourceString false "let x = 60_000L" config
|> should equal "let x = 60_000L
"

[<Test>]
let ``spaces before hash define inside string, 1290`` () =
formatSourceString false "
[<Test>]
let ``defines inside string, escaped quote`` () =
let source = \"
let a = \\\"\\\\\\\"
#if FOO
#if BAR
#endif
#endif
\\\"
\"
getDefines source == []
" config
|> prepend newline
|> should equal "
[<Test>]
let ``defines inside string, escaped quote`` () =
let source = \"
let a = \\\"\\\\\\\"
#if FOO
#if BAR
#endif
#endif
\\\"
\"
getDefines source == []
"
22 changes: 21 additions & 1 deletion src/Fantomas.Tests/TokenParserTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ let private isNewline item =
| _ -> false

let getDefines v =
let _, hashTokens = getDefines v
let normalizedString = String.normalizeNewLine v
let _, hashTokens = getDefines normalizedString
getDefinesWords hashTokens

let tokenize v = tokenize [] [] v
Expand Down Expand Up @@ -546,3 +547,22 @@ let file =
#endif
"
getDefines source == [ "WATCH" ]

[<Test>]
let ``escaped backslash inside escaped string, 1290`` () =
let source = "
[<Test>]
let ``defines inside string, escaped quote`` () =
let source = \"
let a = \\\"\\\\\\\"
#if FOO
#if BAR
#endif
#endif
\\\"
\"
getDefines source == []
"

getDefines source == []
7 changes: 5 additions & 2 deletions src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ let rec private getTokenizedHashes (sourceCode: string): Token list =
{ acc with
NewlineIndexes = idx :: acc.NewlineIndexes }
| InsideString, (DoubleQuoteChar, _, _) ->
match minusOne, minusTwo with
| BackSlashChar, NoBackSlashChar -> acc
let minusThree = sourceCode.[idx - 3]

match minusOne, minusTwo, minusThree with
| BackSlashChar, NoBackSlashChar, _
| BackSlashChar, BackSlashChar, BackSlashChar -> acc
| _ -> { acc with State = Normal }
| InsideString, (DoubleQuoteChar, _, _) -> { acc with State = Normal }
| InsideTripleQuoteString _, (NewlineChar, _, _) ->
Expand Down

0 comments on commit fee9f2d

Please sign in to comment.