fsprojects / fsprojects/FSharpLint

Possible issue with SourceLengthHelper::stripMultilineComments ?

Open Beginner friendly
#869 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
F#
Stars
327
Forks
74
PR merge metrics
No merged PRs in 30d

Description

Hi,

I was having a look at the code around https://github.com/fsprojects/FSharpLint/blob/bd94a097d9cbb48604c8657b8fa108e7cfd8bf12/src/FSharpLint.Core/Rules/Conventions/SourceLength/SourceLengthHelper.fs#L63 as it seems quite allocation heavy, and when looking noticed that it didn't appear to behave correctly when there are multiple block comments present, as it removes the blocks starting from the start of the source string, and each removal changes the offsets of any following comments.

Basically, If I add a new test like

    [<Test>]
    member this.FunctionTooManyLinesWithMultiLineComments() =
        this.Parse($"""
module Program

let dog x =
    (*
    Foo
    Bar
    *)
    (*
    Foo
    Bar
    *)
    %s{generateNewLines (FunctionLength - 8) 4}
    ()""")
        Assert.IsFalse this.ErrorsExist

Then the generated source is

dog x =
    (*
    Foo
    Bar
    *)
    (*
    Foo
    Bar
    *)
    printf System.String.Empty
    printf System.String.Empty
    printf System.String.Empty
    printf System.String.Empty
    printf System.String.Empty

...

And after stripMultilineComments has run it looks like

dog x =
    
    (*
    Foo
    Bar
    *).Empty
    printf System.String.Empty
    printf System.String.Empty
    printf System.String.Empty
    printf System.String.Empty

...

Which doesn't seem right?
Basically I wonder if it should maybe be removing the blocks in reverse order (starting from the one latest in the file), so the offsets for earlier comments don't change?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/FSharpLint.Core/Rules/Conventions/SourceLength/SourceLengthHelper.fs around line 63 and inspect how stripMultilineComments handles multiple comment spans. Add the shown FunctionTooManyLinesWithMultiLineComments test to the existing SourceLength tests and run it; done means both multiline comments are removed without corrupting following source text and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.