Skip to content

Commit 1e63009

Browse files
committed
rework stripMultilineComments to handle one comment being removed changing the offsets of the others
1 parent 500d18d commit 1e63009

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

src/FSharpLint.Core/Rules/Conventions/SourceLength/SourceLengthHelper.fs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ let internal stripMultilineComments (source: string) =
4646
|> Seq.sortBy (function | Begin index -> index | End index -> index)
4747
|> Seq.toList
4848

49-
getTopLevelBalancedPairs markers List.Empty
50-
|> List.fold
51-
(fun (currSource: string) (startIndex, endIndex) ->
52-
let left = currSource.AsSpan(0, startIndex)
53-
let right = currSource.AsSpan(endIndex + multilineCommentMarkerRegexCaptureGroupLength)
54-
String.Concat(left, right))
55-
source
49+
match getTopLevelBalancedPairs markers List.Empty with
50+
| [] -> source
51+
| pairs ->
52+
53+
let builder = Text.StringBuilder(source)
54+
55+
// Remove each comment block in turn
56+
// Keep track of the count of characters removed in order to patch the offsets when removing test changes them
57+
pairs
58+
|> List.fold
59+
(fun (currSource: Text.StringBuilder, removedCharsCount) (startIndex, endIndex) ->
60+
let length = endIndex - startIndex + multilineCommentMarkerRegexCaptureGroupLength
61+
(currSource.Remove(startIndex - removedCharsCount, length), removedCharsCount + length))
62+
(builder, 0)
63+
|> (fst >> _.ToString())
5664

5765
let checkSourceLengthRule (config:Config) range fileContents errorName (skipRanges: array<Range>) =
5866
let error name lineCount actual =

0 commit comments

Comments
 (0)