passsy / prefer-curly-braces-over-arrowr3
Prefer Curly Braces Over Multi-Line Arrow Lambdas
PublicGive 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
✓fires2.3 ms
fires on an arrow whose body starts on the next line
write lib/names.dartfired: line 1
final result = items.map((item) =>
item.name
.trim()
.toLowerCase());✓fires2.4 ms
fires on a dangling arrow inside a method
write lib/names.dartfired: line 4
class Names {
List<Model> build(List<Item> data) {
return data.map(
(item) =>
Model(id: item.id),
).toList();
}
}✓quiet2.2 ms
stays quiet on single-line arrows and curly bodies
write lib/names.dartstayed quiet
final doubled = numbers.map((n) => n * 2);
final result = items.map((item) {
return item.name.trim();
});✓quiet1.8 ms
stays quiet on switch expression arms
write lib/status.dartstayed quiet
String label(Status status) {
return switch (status) {
Status.active =>
'Active',
Status.done => 'Done',
};
}✓quiet1.9 ms
stays quiet on a dangling arrow the agent did not write
write lib/names.dart · wrote line 3stayed quiet
final result = items.map((item) =>
item.name
.trim()
.toLowerCase());✓skips0.0 ms
stays quiet when the agent only reads the file
read lib/names.dartno handler of the detector wants this event
final result = items.map((item) =>
item.name
.trim()
.toLowerCase());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([]);
});