passsy / avoid-code-generationr4
Avoid Code Generation
PublicAvoid build_runner and code generation in Dart; write the code once instead, and do not suggest freezed unless the project already depends on it.
tests, run by the registry7 passed · slowest 9.8 ms
✓fires2.6 ms
fires on a build_runner dependency the agent writes
write pubspec.yamlfired: line 6
namedemo
dependencies
flutter
sdkflutter
dev_dependencies
build_runner^2.4.0✓quiet2.9 ms
stays quiet on a dependency that was already there
write pubspec.yaml · wrote line 1stayed quiet
namedemo
dependencies
flutter
sdkflutter
dev_dependencies
build_runner^2.4.0✓quiet3.0 ms
stays quiet on a commented out dependency
write pubspec.yamlstayed quiet
namedemo
dev_dependencies
# build_runner: ^2.4.0
✓fires2.8 ms
fires on a codegen annotation and part directive in Dart
write lib/user.dartfired: line 3, line 5
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
()
class User {
final String name;
User(this.name);
}✓skips0.0 ms
stays quiet in generated code and on reads
write lib/user.g.dartno handler of the detector wants this event
(name: 'x')
final x = 1;read lib/user.dartno handler of the detector wants this event
part 'user.g.dart';✓fires6.5 ms
points at the principle when the user asks for freezed in a Dart project
prompt · in fixtures/dart_appfired: "freezed"
Can we use freezed for the models?✓quiet9.8 ms
stays quiet on a prompt about something else
prompt · in fixtures/dart_appstayed quiet
Add a name field to the User modeldetector.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 pubspec = source`
name: demo
dependencies:
flutter:
sdk: flutter
dev_dependencies:
build_runner: ^2.4.0
`;
test("fires on a build_runner dependency the agent writes", () => {
expect(detect(write("pubspec.yaml", pubspec))).toEqual([{ line: 6 }]);
});
test("stays quiet on a dependency that was already there", () => {
expect(detect(edit("pubspec.yaml", pubspec, { written: "name: demo" }))).toEqual([]);
});
test("stays quiet on a commented out dependency", () => {
expect(detect(write("pubspec.yaml", "name: demo\ndev_dependencies:\n # build_runner: ^2.4.0\n"))).toEqual([]);
});
test("fires on a codegen annotation and part directive in Dart", () => {
const code = source`
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
final String name;
User(this.name);
}
`;
expect(detect(write("lib/user.dart", code))).toEqual([
{ line: 3 },
{ line: 5 },
]);
});
test("stays quiet in generated code and on reads", () => {
expect(detect(write("lib/user.g.dart", "@JsonKey(name: 'x')\nfinal x = 1;"))).toEqual([]);
expect(detect(read("lib/user.dart", "part 'user.g.dart';"))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user asks for freezed in a Dart project", () => {
expect(detect(prompt("Can we use freezed for the models?"), dartApp)).toEqual([{ match: "freezed", depth: "pointer" }]);
});
test("stays quiet on a prompt about something else", () => {
expect(detect(prompt("Add a name field to the User model"), dartApp)).toEqual([]);
});