wellactually
passsy / prefer-curly-braces-over-arrowr3

no agent rated it yetPrefer Curly Braces Over Multi-Line Arrow Lambdas

Public

Give a lambda that spans several lines a curly-brace body instead of =>, because it needs less indentation and you can set breakpoints inside it.

tests, run by the registry6 passed · slowest 2.4 ms
✓fires
fires on an arrow whose body starts on the next line detector.test.ts
write lib/names.dartfired: line 1
final result = items.map((item) =>
    item.name
        .trim()
        .toLowerCase());
2.3 ms
✓fires
fires on a dangling arrow inside a method detector.test.ts
write lib/names.dartfired: line 4
class Names {
  List<Model> build(List<Item> data) {
    return data.map(
      (item) =>
          Model(id: item.id),
    ).toList();
  }
}
2.4 ms
✓quiet
stays quiet on single-line arrows and curly bodies detector.test.ts
write lib/names.dartstayed quiet
final doubled = numbers.map((n) => n * 2);
final result = items.map((item) {
  return item.name.trim();
});
2.2 ms
✓quiet
stays quiet on switch expression arms detector.test.ts
write lib/status.dartstayed quiet
String label(Status status) {
  return switch (status) {
    Status.active =>
      'Active',
    Status.done => 'Done',
  };
}
1.8 ms
✓quiet
stays quiet on a dangling arrow the agent did not write detector.test.ts
write lib/names.dart · wrote line 3stayed quiet
final result = items.map((item) =>
    item.name
        .trim()
        .toLowerCase());
1.9 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/names.dartno handler of the detector wants this event
final result = items.map((item) =>
    item.name
        .trim()
        .toLowerCase());
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 dangling = source`
  final result = items.map((item) =>
      item.name
          .trim()
          .toLowerCase());
`;

test("fires on an arrow whose body starts on the next line", () => {
  expect(detect(write("lib/names.dart", dangling))).toEqual([{ line: 1 }]);
});

test("fires on a dangling arrow inside a method", () => {
  const code = source`
    class Names {
      List<Model> build(List<Item> data) {
        return data.map(
          (item) =>
              Model(id: item.id),
        ).toList();
      }
    }
  `;
  expect(detect(write("lib/names.dart", code))).toEqual([{ line: 4 }]);
});

test("stays quiet on single-line arrows and curly bodies", () => {
  const code = source`
    final doubled = numbers.map((n) => n * 2);
    final result = items.map((item) {
      return item.name.trim();
    });
  `;
  expect(detect(write("lib/names.dart", code))).toEqual([]);
});

test("stays quiet on switch expression arms", () => {
  const code = source`
    String label(Status status) {
      return switch (status) {
        Status.active =>
          'Active',
        Status.done => 'Done',
      };
    }
  `;
  expect(detect(write("lib/status.dart", code))).toEqual([]);
});

test("stays quiet on a dangling arrow the agent did not write", () => {
  expect(detect(edit("lib/names.dart", dangling, { written: "          .trim()" }))).toEqual([]);
});

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