AnyLint icon indicating copy to clipboard operation
AnyLint copied to clipboard

Multiline function parameters

Open meteora8888 opened this issue 2 years ago • 1 comments

I am was working on this autocorrect for long single line function headers so it automatically corrects them into multiple lines but since it was too complex i broke it into two separate autocorrects. First to break paramters into a singular line away from the func ( ) -> x surounding it and a second one that cycles over the match and break the last parameters onto a separate line every cycle. However eventhough it matches correctly as soon as I apply autocorrection to the second rule nothing ever changes. Please help me I am very confused because event if I switch out the replacement for litterally any matching group it ignored the autocorrect

`let longMultilineFunctionDefMatchingExample = "static func defaultAlert(verylongStringToPutToMultipleLineThatExceeds120charactersForSureSinceItIsSoVeryLongOhMyGod: Sting) {\n}" let longMultilineFunctionDefNonMatchingExample1 = "\tstatic func defaultAlert(title: String, title: String, title: String, message: String) {\n}" let longMultilineFunctionDefBeforeAutocorrect = "\tstatic func defaultAlert(verylongStringToPutToMultipleLineThatExceeds120charactersForSureSinceItIsSoVeryLongOhMyGod: String){\n}" let longMultilineFunctionDefAfterAutocorrect = "\tstatic func defaultAlert(\n\t\tverylongStringToPutToMultipleLineThatExceeds120charactersForSureSinceItIsSoVeryLongOhMyGod: String\n\t){\n}" try Lint.checkFileContents( checkInfo: "Long multiparameter function: Parameters of functions above 120 character should be split into multiple lines", regex: #"^(?=.{121,})(?<SPACING>[ \t])(?<HEADER>[a-zA-Z@ ]func[a-zA-Z<> ]()(?<PARAMETERS>.)(?<HEADEND>).(?:->){0,1}.{)$"#, matchingExamples: [longMultilineFunctionDefMatchingExample], nonMatchingExamples: [longMultilineFunctionDefNonMatchingExample1], includeFilters: [#".*.swift"#], excludeFilters: excludeFilters, autoCorrectReplacement: "$SPACING$HEADER\n$SPACING\t$PARAMETERS\n$SPACING$HEADEND", autoCorrectExamples: [ ["before": longMultilineFunctionDefBeforeAutocorrect, "after": longMultilineFunctionDefAfterAutocorrect] ] )

try Lint.checkFileContents(
    checkInfo: "Multiline function definition: Parameters of functions above 120 character should be split into multiple lines",
    regex: #"^(?<SPACING>[ \t]*)(?<HEADER>(?:[a-zA-Z@ ]*)func.*\()\n^[ \t]*(?<PARAMETERSTOKEEP>(?:.*\: .*,(?!\n))+) (?<PARAMETERSTOFORMAT>(?:\s*.*\: .*,{0,1}(?=\n))*)\n^(?<HEADEND>[ \t]*\).*(?:->){0,1}.*\{(?=\n))$"#,
    matchingExamples: ["func makePendingEventsSections(\nfor pendingEvents: [Event], photos: [RolePhoto], statusOverviews: [PresenceOverviewResponse]\n) -> [Section] {\n}"],
    nonMatchingExamples: ["\tstatic func defaultAlert(\ntitle: String) {"],
    includeFilters: [#".*\.swift"#],
    excludeFilters: excludeFilters,
    autoCorrectReplacement: "$SPACING$HEADER\n$SPACING\t$PARAMETERSTOKEEP\n$SPACING\t$PARAMETERSTOFORMAT\n$SPACING$HEADEND",
    autoCorrectExamples: [
        ["before": "static func defaultAlert(\n\ttitle: String, title: String, title: String, message: String, hi hi: no, in controller: (UIViewController\n) -> Void) {\n}",
         "after": "static func defaultAlert(\n\ttitle: String, title: String, title: String, message: String, hi hi: no,\n\tin controller: (UIViewController\n) -> Void) {\n}"]
    ],
    repeatIfAutoCorrected: true
)`

Expected Behavior

single line function parameters break into separate lines each

Actual Behavior

first rule works having all parameters on one line but second one is ignored (eventhough it matches)

Steps to Reproduce the Problem

  1. Run anylint with the stated autocorrect
  2. use this code as an example class func from( html: String?, font: (regular: UIFont, bold: UIFont), color: UIColor, documentAttributes: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil ) -> NSAttributedString? { return from(html: html, font: font, size: font.regular.pointSize, color: color, documentAttributes: documentAttributes) }

Specifications

  • Version: flinesoft/anylint/anylint 0.8.2
  • Platform: iOS
  • IDE Version: Xcode 12.5.1

meteora8888 avatar Aug 27 '21 21:08 meteora8888

Hey @meteora8888, sorry for getting back to you this late, I didn't notice the email last year when you first wrote this and I'm just seeing it.

I've copied and tried out your check, the regex seems to trigger in the tests but not in real files, that's really weird. I don't see anything you're doing wrong, I will have to further investigate what's going on internally in AnyLint to know what the issue is, but looks like a bug in AnyLint, thanks for reporting. 👍

Jeehut avatar Apr 26 '22 12:04 Jeehut