passsy / avoid-if-caser3
Avoid if-case Pattern Matching
PublicAvoid the if-case pattern matching syntax and write a plain type and length check instead, because many developers still find if-case hard to read.
tests, run by the registry7 passed · slowest 5.0 ms
✓fires2.7 ms
fires on an if-case, not on a plain if
write lib/match.dartfired: line 2
void f(Object x) {
if (x case int n) {}
if (x == 1) {}
}✓fires3.4 ms
fires on a pattern written tight against the keyword
write lib/tight.dartfired: line 2
void f(Object x) {
if (x case[1, 2]) {}
}
✓quiet3.5 ms
stays quiet on an if-case that was already there
write lib/match.dart · wrote line 3stayed quiet
void f(Object x) {
if (x case int n) {}
if (x == 1) {}
}read lib/match.dartno handler of the detector wants this event
void f(Object x) {
if (x case int n) {}
if (x == 1) {}
}✓quiet3.2 ms
stays quiet on an identifier beginning with case and on if-case in a comment
write lib/search.dartstayed quiet
bool f(String name, bool caseSensitive) {
if (caseSensitive) {
return true;
}
// if (name case 'x') was here before.
if (name == 'here') {
return false;
}
return false;
}✓quiet3.3 ms
stays quiet on a switch case
write lib/switch.dartstayed quiet
String f(int x) {
switch (x) {
case 1:
return 'one';
}
return 'many';
}✓fires5.0 ms
points at the principle when the user mentions pattern matching in a Dart project
prompt · in fixtures/dart_appfired: "pattern matching"
Could we use pattern matching to unpack the pair?✓quiet3.1 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
Could we use pattern matching to unpack the pair?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 match = source`
void f(Object x) {
if (x case int n) {}
if (x == 1) {}
}
`;
test("fires on an if-case, not on a plain if", () => {
expect(detect(write("lib/match.dart", match))).toEqual([{ line: 2 }]);
});
test("fires on a pattern written tight against the keyword", () => {
expect(detect(write("lib/tight.dart", "void f(Object x) {\n if (x case[1, 2]) {}\n}\n"))).toEqual([{ line: 2 }]);
});
test("stays quiet on an if-case that was already there", () => {
expect(detect(edit("lib/match.dart", match, { written: " if (x == 1) {}" }))).toEqual([]);
expect(detect(read("lib/match.dart", match))).toEqual([]);
});
test("stays quiet on an identifier beginning with case and on if-case in a comment", () => {
const code = source`
bool f(String name, bool caseSensitive) {
if (caseSensitive) {
return true;
}
// if (name case 'x') was here before.
if (name == 'here') {
return false;
}
return false;
}
`;
expect(detect(write("lib/search.dart", code))).toEqual([]);
});
test("stays quiet on a switch case", () => {
const code = source`
String f(int x) {
switch (x) {
case 1:
return 'one';
}
return 'many';
}
`;
expect(detect(write("lib/switch.dart", code))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user mentions pattern matching in a Dart project", () => {
expect(detect(prompt("Could we use pattern matching to unpack the pair?"), dartApp)).toEqual([{ match: "pattern matching", depth: "pointer" }]);
});
test("stays quiet on the same words outside a Dart project", () => {
expect(detect(prompt("Could we use pattern matching to unpack the pair?"))).toEqual([]);
});