wellactually
passsy / functional-styler3

no agent rated it yetPrefer Functional-Style Transformations Over Loops

Public

Transform collections with .map(), .where() and .fold() instead of filling an empty list in a loop, because declarative code is shorter and easier to read.

tests, run by the registry8 passed · slowest 4.9 ms
✓fires
fires on an empty list filled in a for loop detector.test.ts
write lib/users.dartfired: line 2
List<String> activeNames(List<User> users) {
  final names = <String>[];
  for (final user in users) {
    if (user.isActive) {
      names.add(user.name);
    }
  }
  return names;
}
2.9 ms
✓fires
fires on a map filled by index assignment detector.test.ts
write lib/ages.dartfired: line 2
Map<String, int> byId(List<User> users) {
  final Map<String, int> ages = {};
  for (final user in users) {
    ages[user.id] = user.age;
  }
  return ages;
}
3.2 ms
✓quiet
stays quiet on the functional version detector.test.ts
write lib/users.dartstayed quiet
List<String> activeNames(List<User> users) {
  return users.where((it) => it.isActive).map((it) => it.name).toList();
}
2.7 ms
✓quiet
stays quiet on an empty list that is never filled in a loop detector.test.ts
write lib/names.dartstayed quiet
List<String> names() {
  final names = <String>[];
  names.add('a');
  return names;
}
4.9 ms
✓quiet
stays quiet on a legacy loop the agent did not write detector.test.ts
write lib/users.dart · wrote line 8stayed quiet
List<String> activeNames(List<User> users) {
  final names = <String>[];
  for (final user in users) {
    if (user.isActive) {
      names.add(user.name);
    }
  }
  return names;
}
3.2 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/users.dartno handler of the detector wants this event
List<String> activeNames(List<User> users) {
  final names = <String>[];
  for (final user in users) {
    if (user.isActive) {
      names.add(user.name);
    }
  }
  return names;
}
0.0 ms
✓fires
points at the principle when the user mentions dartx detector.test.ts
prompt · in fixtures/dart_appfired: "dartx"
Can we add dartx for sortedBy?
4.2 ms
✓quiet
stays quiet on a prompt about something else detector.test.ts
prompt · in fixtures/dart_appstayed quiet
Sort the users by name
4.3 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";

const loop = source`
  List<String> activeNames(List<User> users) {
    final names = <String>[];
    for (final user in users) {
      if (user.isActive) {
        names.add(user.name);
      }
    }
    return names;
  }
`;

test("fires on an empty list filled in a for loop", () => {
  expect(detect(write("lib/users.dart", loop))).toEqual([{ line: 2 }]);
});

test("fires on a map filled by index assignment", () => {
  const code = source`
    Map<String, int> byId(List<User> users) {
      final Map<String, int> ages = {};
      for (final user in users) {
        ages[user.id] = user.age;
      }
      return ages;
    }
  `;
  expect(detect(write("lib/ages.dart", code))).toEqual([{ line: 2 }]);
});

test("stays quiet on the functional version", () => {
  const code = source`
    List<String> activeNames(List<User> users) {
      return users.where((it) => it.isActive).map((it) => it.name).toList();
    }
  `;
  expect(detect(write("lib/users.dart", code))).toEqual([]);
});

test("stays quiet on an empty list that is never filled in a loop", () => {
  const code = source`
    List<String> names() {
      final names = <String>[];
      names.add('a');
      return names;
    }
  `;
  expect(detect(write("lib/names.dart", code))).toEqual([]);
});

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

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

const dartApp = { project: "fixtures/dart_app" };

test("points at the principle when the user mentions dartx", () => {
  expect(detect(prompt("Can we add dartx for sortedBy?"), dartApp)).toEqual([{ match: "dartx", depth: "pointer" }]);
});

test("stays quiet on a prompt about something else", () => {
  expect(detect(prompt("Sort the users by name"), dartApp)).toEqual([]);
});