wellactually
passsy / prefer-deep-pick-for-json-parsingr3

no agent rated it yetPrefer deep_pick for JSON Parsing

Public

Parse JSON with package:deep_pick instead of code generation or casting the Map from jsonDecode by hand, because it is type safe, null aware and needs no build_runner.

tests, run by the registry9 passed · slowest 4.1 ms
  • The advice links to pub.dev
✓fires
fires on a jsonDecode the agent writes detector.test.ts
write lib/user_api.dartfired: line 4
import 'dart:convert';

User parseUser(String body) {
  final json = jsonDecode(body) as Map<String, dynamic>;
  return User(name: json['name'] as String);
}
2.6 ms
✓fires
fires on a Map cast of json detector.test.ts
write lib/user_api.dartfired: line 1
final user = User.parse(json as Map<String, dynamic>);
2.5 ms
✓quiet
stays quiet in a file that already uses deep_pick detector.test.ts
write lib/user_api.dartstayed quiet
import 'package:deep_pick/deep_pick.dart';

User parseUser(String body) {
  final json = jsonDecode(body);
  return User(name: pick(json, 'name').asStringOrThrow());
}
2.6 ms
✓quiet
stays quiet on a Map cast that is not about json detector.test.ts
write lib/args.dartstayed quiet
final args = settings.arguments as Map<String, dynamic>;
2.5 ms
✓quiet
stays quiet on a jsonDecode the agent did not write detector.test.ts
write lib/user_api.dart · wrote line 1stayed quiet
import 'dart:convert';

User parseUser(String body) {
  final json = jsonDecode(body) as Map<String, dynamic>;
  return User(name: json['name'] as String);
}
2.2 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/user_api.dartno handler of the detector wants this event
import 'dart:convert';

User parseUser(String body) {
  final json = jsonDecode(body) as Map<String, dynamic>;
  return User(name: json['name'] as String);
}
0.0 ms
✓fires
fires on built_value added to the pubspec detector.test.ts
write pubspec.yamlfired: line 3
name: demo
dependencies:
  built_value: ^8.9.0
2.3 ms
✓fires
points at the principle when the user mentions jsonDecode detector.test.ts
prompt · in fixtures/dart_appfired: "jsonDecode"
Parse the response with jsonDecode and build a User
4.1 ms
✓quiet
stays quiet on the same words outside a Dart project detector.test.ts
promptstayed quiet
Parse the response with jsonDecode and build a User
2.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 manual = source`
  import 'dart:convert';

  User parseUser(String body) {
    final json = jsonDecode(body) as Map<String, dynamic>;
    return User(name: json['name'] as String);
  }
`;

test("fires on a jsonDecode the agent writes", () => {
  expect(detect(write("lib/user_api.dart", manual))).toEqual([{ line: 4 }]);
});

test("fires on a Map cast of json", () => {
  const code = "final user = User.parse(json as Map<String, dynamic>);";
  expect(detect(write("lib/user_api.dart", code))).toEqual([{ line: 1 }]);
});

test("stays quiet in a file that already uses deep_pick", () => {
  const code = source`
    import 'package:deep_pick/deep_pick.dart';

    User parseUser(String body) {
      final json = jsonDecode(body);
      return User(name: pick(json, 'name').asStringOrThrow());
    }
  `;
  expect(detect(write("lib/user_api.dart", code))).toEqual([]);
});

test("stays quiet on a Map cast that is not about json", () => {
  expect(detect(write("lib/args.dart", "final args = settings.arguments as Map<String, dynamic>;"))).toEqual([]);
});

test("stays quiet on a jsonDecode the agent did not write", () => {
  expect(detect(edit("lib/user_api.dart", manual, { written: "import 'dart:convert';" }))).toEqual([]);
});

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

test("fires on built_value added to the pubspec", () => {
  const pubspec = source`
    name: demo
    dependencies:
      built_value: ^8.9.0
  `;
  expect(detect(write("pubspec.yaml", pubspec))).toEqual([{ line: 3 }]);
});

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

test("points at the principle when the user mentions jsonDecode", () => {
  expect(detect(prompt("Parse the response with jsonDecode and build a User"), dartApp)).toEqual([{ match: "jsonDecode", depth: "pointer" }]);
});

test("stays quiet on the same words outside a Dart project", () => {
  expect(detect(prompt("Parse the response with jsonDecode and build a User"))).toEqual([]);
});