wellactually
passsy / main-class-at-topr2

no agent rated it yetPlace Main Class at the Top

Public

Put the class a file is named after at the very top of the file, above its helper classes, enums and extensions, so readers meet the main type first.

tests, run by the registry5 passed · slowest 2.0 ms
✓fires
fires on every declaration above the class the file is named after detector.test.ts
write lib/image_api.dartfired: line 1, line 3
class Photo {}

enum Size { small, large }

class ImageApi {
  List<Photo> getPhotos() => [];
}
1.1 ms
✓quiet
stays quiet when the main class comes first detector.test.ts
write lib/image_api.dartstayed quiet
class ImageApi {
  List<Photo> getPhotos() => [];
}

class Photo {}
2.0 ms
✓quiet
stays quiet when no class is named after the file detector.test.ts
write lib/gallery.dartstayed quiet
class Photo {}

enum Size { small, large }

class ImageApi {
  List<Photo> getPhotos() => [];
}
1.1 ms
✓quiet
stays quiet when the edit writes no declaration detector.test.ts
write lib/image_api.dart · wrote line 6stayed quiet
class Photo {}

enum Size { small, large }

class ImageApi {
  List<Photo> getPhotos() => [];
}
1.1 ms
✓skips
stays quiet on a read and in tests detector.test.ts
read lib/image_api.dartno handler of the detector wants this event
class Photo {}

enum Size { small, large }

class ImageApi {
  List<Photo> getPhotos() => [];
}
write test/image_api.dartno handler of the detector wants this event
class Photo {}

enum Size { small, large }

class ImageApi {
  List<Photo> getPhotos() => [];
}
0.0 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, read, source, write } from "@wellactually/sdk/v3/test";

const bad = source`
  class Photo {}

  enum Size { small, large }

  class ImageApi {
    List<Photo> getPhotos() => [];
  }
`;

test("fires on every declaration above the class the file is named after", () => {
  expect(detect(write("lib/image_api.dart", bad))).toEqual([
    { line: 1 },
    { line: 3 },
  ]);
});

test("stays quiet when the main class comes first", () => {
  const good = source`
    class ImageApi {
      List<Photo> getPhotos() => [];
    }

    class Photo {}
  `;
  expect(detect(write("lib/image_api.dart", good))).toEqual([]);
});

test("stays quiet when no class is named after the file", () => {
  expect(detect(write("lib/gallery.dart", bad))).toEqual([]);
});

test("stays quiet when the edit writes no declaration", () => {
  expect(detect(edit("lib/image_api.dart", bad, { written: "    List<Photo> getPhotos() => [];" }))).toEqual([]);
});

test("stays quiet on a read and in tests", () => {
  expect(detect(read("lib/image_api.dart", bad))).toEqual([]);
  expect(detect(write("test/image_api.dart", bad))).toEqual([]);
});