passsy / extract-widgetsr4
Extract Widgets Instead of _buildX() Methods
PublicExtract UI sections into public widget classes instead of Widget _buildX() methods, so tests can find them by type and the tree stays easy to query.
tests, run by the registry7 passed · slowest 8.2 ms
- The advice links to blog.angular-university.io, efthymis.com, medium.com, www.patterns.dev
✓fires8.2 ms
fires on a _buildX method the agent writes
write lib/profile_page.dartfired: line 7
class ProfilePage extends StatelessWidget {
Widget build(BuildContext context) {
return Column(children: [_buildHeader(context)]);
}
Widget _buildHeader(BuildContext context) {
return Text(context.l10n.title);
}
}✓quiet3.6 ms
stays quiet on a legacy _buildX method when the agent edits another line
write lib/profile_page.dart · wrote line 8stayed quiet
class ProfilePage extends StatelessWidget {
Widget build(BuildContext context) {
return Column(children: [_buildHeader(context)]);
}
Widget _buildHeader(BuildContext context) {
return Text(context.l10n.title);
}
}✓skips0.0 ms
stays quiet when the agent only reads the file
read lib/profile_page.dartno handler of the detector wants this event
class ProfilePage extends StatelessWidget {
Widget build(BuildContext context) {
return Column(children: [_buildHeader(context)]);
}
Widget _buildHeader(BuildContext context) {
return Text(context.l10n.title);
}
}✓quiet3.7 ms
stays quiet on extracted widgets
write lib/profile_page.dartstayed quiet
class ProfilePage extends StatelessWidget {
Widget build(BuildContext context) {
return Column(children: [Header(title: context.l10n.title)]);
}
}✓skips0.0 ms
stays quiet in tests
write test/profile_page_test.dartno handler of the detector wants this event
Widget _buildApp() => const MaterialApp();✓fires5.9 ms
points at the principle when the user mentions a build method in a Dart project
prompt · in fixtures/dart_appfired: "build method"
Split the build method of the profile page, it is too long✓quiet4.1 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
Split the build method of the profile page, it is too longdetector.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 page = source`
class ProfilePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(children: [_buildHeader(context)]);
}
Widget _buildHeader(BuildContext context) {
return Text(context.l10n.title);
}
}
`;
test("fires on a _buildX method the agent writes", () => {
expect(detect(write("lib/profile_page.dart", page))).toEqual([{ line: 7 }]);
});
test("stays quiet on a legacy _buildX method when the agent edits another line", () => {
expect(detect(edit("lib/profile_page.dart", page, { written: " return Text(context.l10n.title);" }))).toEqual([]);
});
test("stays quiet when the agent only reads the file", () => {
expect(detect(read("lib/profile_page.dart", page))).toEqual([]);
});
test("stays quiet on extracted widgets", () => {
const code = source`
class ProfilePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(children: [Header(title: context.l10n.title)]);
}
}
`;
expect(detect(write("lib/profile_page.dart", code))).toEqual([]);
});
test("stays quiet in tests", () => {
expect(detect(write("test/profile_page_test.dart", "Widget _buildApp() => const MaterialApp();"))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user mentions a build method in a Dart project", () => {
expect(detect(prompt("Split the build method of the profile page, it is too long"), dartApp)).toEqual([{ match: "build method", depth: "pointer" }]);
});
test("stays quiet on the same words outside a Dart project", () => {
expect(detect(prompt("Split the build method of the profile page, it is too long"))).toEqual([]);
});