wellactually
passsy / top-level-private-functionsr3

no agent rated it yetPrivate Helpers Are Top-Level Functions

Public

Write a private helper that does not touch instance state as a top-level function in the file, not a static member, because library privacy already scopes it.

tests, run by the registry6 passed · slowest 2.7 ms
  • The advice links to wellactually.dev
✓fires
fires on a private static helper detector.test.ts
write lib/invoice_renderer.dartfired: line 7
/// Renders invoices.
class InvoiceRenderer {
  String render(List<String> lines) {
    return _formatLines(lines).join(', ');
  }

  static List<String> _formatLines(List<String> lines) {
    return lines;
  }
}
2.2 ms
✓quiet
stays quiet on a top-level private helper detector.test.ts
write lib/invoice_renderer.dartstayed quiet
/// Renders invoices.
class InvoiceRenderer {
  String render(List<String> lines) {
    return _formatLines(lines).join(', ');
  }
}

List<String> _formatLines(List<String> lines) {
  return lines;
}
2.7 ms
✓quiet
stays quiet on a static const and a public static method detector.test.ts
write lib/timeouts.dartstayed quiet
class Timeouts {
  static const _zero = Duration.zero;
  static Timeouts parse(String raw) {
    return Timeouts();
  }
}
2.0 ms
✓skips
stays quiet in test files detector.test.ts
write test/helpers_test.dartno handler of the detector wants this event
class TestHelpers {
  static String _wrap(String value) {
    return value;
  }
}

void main() {}
0.0 ms
✓quiet
stays quiet on a static helper the agent did not write detector.test.ts
write lib/invoice_renderer.dart · wrote line 8stayed quiet
/// Renders invoices.
class InvoiceRenderer {
  String render(List<String> lines) {
    return _formatLines(lines).join(', ');
  }

  static List<String> _formatLines(List<String> lines) {
    return lines;
  }
}
1.8 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/invoice_renderer.dartno handler of the detector wants this event
/// Renders invoices.
class InvoiceRenderer {
  String render(List<String> lines) {
    return _formatLines(lines).join(', ');
  }

  static List<String> _formatLines(List<String> lines) {
    return lines;
  }
}
0.0 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, read, source, write } from "@wellactually/sdk/v3/test";

const renderer = source`
  /// Renders invoices.
  class InvoiceRenderer {
    String render(List<String> lines) {
      return _formatLines(lines).join(', ');
    }

    static List<String> _formatLines(List<String> lines) {
      return lines;
    }
  }
`;

test("fires on a private static helper", () => {
  expect(detect(write("lib/invoice_renderer.dart", renderer))).toEqual([{ line: 7 }]);
});

test("stays quiet on a top-level private helper", () => {
  const code = source`
    /// Renders invoices.
    class InvoiceRenderer {
      String render(List<String> lines) {
        return _formatLines(lines).join(', ');
      }
    }

    List<String> _formatLines(List<String> lines) {
      return lines;
    }
  `;
  expect(detect(write("lib/invoice_renderer.dart", code))).toEqual([]);
});

test("stays quiet on a static const and a public static method", () => {
  const code = source`
    class Timeouts {
      static const _zero = Duration.zero;
      static Timeouts parse(String raw) {
        return Timeouts();
      }
    }
  `;
  expect(detect(write("lib/timeouts.dart", code))).toEqual([]);
});

test("stays quiet in test files", () => {
  const code = source`
    class TestHelpers {
      static String _wrap(String value) {
        return value;
      }
    }

    void main() {}
  `;
  expect(detect(write("test/helpers_test.dart", code))).toEqual([]);
});

test("stays quiet on a static helper the agent did not write", () => {
  expect(detect(edit("lib/invoice_renderer.dart", renderer, { written: "    return lines;" }))).toEqual([]);
});

test("stays quiet when the agent only reads the file", () => {
  expect(detect(read("lib/invoice_renderer.dart", renderer))).toEqual([]);
});