passsy / copy-with-nullable-fieldsr3
copyWith With Nullable Fields
PublicWrite copyWith as a typed function getter backed by a sentinel default, so callers can set nullable fields back to null without losing type safety.
detector.tsisolate · reads files, writes none · no network · 100 ms · 32 MBEdit
import { mentioned, onPrompt, onWrite } from "@wellactually/sdk/v3";
import { DART, isDartProject } from "dart-kit";
onPrompt({
*detect(event) {
if (isDartProject(event)) {
yield* mentioned(event, ["copyWith"]);
}
},
});
onWrite({
globs: DART,
*detect(event) {
// A copyWith beside a nullable field, in a file that has not applied the
// `defaultArgument` sentinel yet. Without a nullable field the naive shape
// works fine; with the sentinel the pattern is already applied.
const hasCopyWith = event.text.includes("copyWith");
const hasSentinel = event.text.includes("defaultArgument");
const hasNullableField = /\w+\?\s+\w+;/.test(event.text);
if (!hasCopyWith || hasSentinel || !hasNullableField) {
return;
}
for (const written of event.written) {
// The naive copyWith betrays itself with `?? this.`: an omitted argument
// and a passed `null` collapse into the same branch, so nullable fields
// can never be cleared. Fires when the agent writes that fallback.
if (written.text.includes("?? this.")) {
yield { line: written.line };
}
}
},
});
fixtures/dart_app/pubspec.yamlEdit
namedemo
package.jsonEdit
{
"name": "copy-with-nullable-fields",
"private": true,
"dependencies": {
"dart-kit": "*"
},
"tags": [
"dart"
]
}