Skip to content

Commit 4773fc6

Browse files
committed
test(lint): dedupe no-lib-imports analyzer cases
1 parent 4614984 commit 4773fc6

1 file changed

Lines changed: 46 additions & 83 deletions

File tree

packages/app/tests/eslint/no-lib-imports.test.ts

Lines changed: 46 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import tseslint from "typescript-eslint"
44

55
import { noLibImportsRule } from "../../eslint/no-lib-imports.mjs"
66

7+
const defaultFilePath = "src/new-client.ts"
8+
79
const verify = (source: string, filePath: string) => {
810
const linter = new Linter({ configType: "flat" })
911

@@ -29,105 +31,66 @@ const verify = (source: string, filePath: string) => {
2931
)
3032
}
3133

32-
describe("noLibImportsRule", () => {
33-
it("rejects import declarations from lib", () => {
34-
const messages = verify(
35-
"import { listProjects } from \"@effect-template/lib\"\n",
36-
"src/new-client.ts"
37-
)
34+
const line = (source: string): string => `${source}\n`
3835

39-
expect(messages).toHaveLength(1)
40-
expect(messages[0]?.message).toContain("Direct import")
41-
expect(messages[0]?.message).toContain("@effect-template/lib")
42-
})
36+
const lines = (source: ReadonlyArray<string>): string => source.join("\n")
4337

44-
it("rejects type-only import declarations from lib", () => {
45-
const messages = verify(
46-
"import type { TemplateConfig } from \"@effect-template/lib/core/domain\"\n",
47-
"src/new-client.ts"
48-
)
49-
50-
expect(messages).toHaveLength(1)
51-
expect(messages[0]?.message).toContain("@effect-template/lib/core/domain")
52-
})
38+
const expectMessages = (
39+
source: string,
40+
expectedMessages: ReadonlyArray<ReadonlyArray<string>>,
41+
filePath: string = defaultFilePath
42+
) => {
43+
const messages = verify(source, filePath)
5344

54-
it("rejects type import expressions from lib", () => {
55-
const messages = verify(
56-
"type Template = import(\"@effect-template/lib/core/domain\").TemplateConfig\n",
57-
"src/new-client.ts"
58-
)
59-
60-
expect(messages).toHaveLength(1)
61-
expect(messages[0]?.message).toContain("@effect-template/lib/core/domain")
62-
})
63-
64-
it("rejects require calls from lib", () => {
65-
const messages = verify(
66-
"const templateLib = require(\"@effect-template/lib\")\n",
67-
"src/new-client.ts"
68-
)
69-
70-
expect(messages).toHaveLength(1)
71-
expect(messages[0]?.message).toContain("@effect-template/lib")
72-
})
45+
expect(messages).toHaveLength(expectedMessages.length)
46+
for (const [index, expectedMessageParts] of expectedMessages.entries()) {
47+
for (const expectedMessagePart of expectedMessageParts) {
48+
expect(messages[index]?.message).toContain(expectedMessagePart)
49+
}
50+
}
51+
}
7352

74-
it("rejects template literal module calls from lib", () => {
75-
const messages = verify(
76-
[
53+
describe("noLibImportsRule", () => {
54+
const violationCases = [
55+
["rejects import declarations from lib", line("import { listProjects } from \"@effect-template/lib\""), [["Direct import", "@effect-template/lib"]]],
56+
["rejects type-only import declarations from lib", line("import type { TemplateConfig } from \"@effect-template/lib/core/domain\""), [["@effect-template/lib/core/domain"]]],
57+
["rejects type import expressions from lib", line("type Template = import(\"@effect-template/lib/core/domain\").TemplateConfig"), [["@effect-template/lib/core/domain"]]],
58+
["rejects require calls from lib", line("const templateLib = require(\"@effect-template/lib\")"), [["@effect-template/lib"]]],
59+
[
60+
"rejects template literal module calls from lib",
61+
lines([
7762
"const requiredTemplateLib = require(`@effect-template/lib/core/domain`)",
7863
"const importedTemplateLib = await import(`@effect-template/lib/usecases/projects`)"
79-
].join("\n"),
80-
"src/new-client.ts"
81-
)
82-
83-
expect(messages).toHaveLength(2)
84-
expect(messages[0]?.message).toContain("@effect-template/lib/core/domain")
85-
expect(messages[1]?.message).toContain("@effect-template/lib/usecases/projects")
86-
})
87-
88-
it("rejects import equals require from lib", () => {
89-
const messages = verify(
90-
"import templateLib = require(\"@effect-template/lib/core/domain\")\n",
91-
"src/new-client.ts"
92-
)
93-
94-
expect(messages).toHaveLength(1)
95-
expect(messages[0]?.message).toContain("@effect-template/lib/core/domain")
96-
})
97-
98-
it("rejects re-export declarations from lib", () => {
99-
const messages = verify(
100-
[
64+
]),
65+
[["@effect-template/lib/core/domain"], ["@effect-template/lib/usecases/projects"]]
66+
],
67+
["rejects import equals require from lib", line("import templateLib = require(\"@effect-template/lib/core/domain\")"), [["@effect-template/lib/core/domain"]]],
68+
[
69+
"rejects re-export declarations from lib",
70+
lines([
10171
"export { listProjects } from \"@effect-template/lib\"",
10272
"export * from \"@effect-template/lib/core/domain\""
103-
].join("\n"),
104-
"src/new-client.ts"
105-
)
73+
]),
74+
[["@effect-template/lib"], ["@effect-template/lib/core/domain"]]
75+
],
76+
["rejects migrated legacy paths too", line("import { listProjects } from \"@effect-template/lib\""), [["Direct import"]], "src/docker-git/program.ts"]
77+
] as const
10678

107-
expect(messages).toHaveLength(2)
108-
expect(messages[0]?.message).toContain("@effect-template/lib")
109-
expect(messages[1]?.message).toContain("@effect-template/lib/core/domain")
110-
})
79+
for (const [name, source, expectedMessages, filePath] of violationCases) {
80+
it(name, () => {
81+
expectMessages(source, expectedMessages, filePath)
82+
})
83+
}
11184

11285
it("allows non-lib imports", () => {
11386
const messages = verify(
114-
[
87+
lines([
11588
"import { request } from \"./api-client.js\"",
11689
"import type { Command } from \"@lib/core/domain\""
117-
].join("\n"),
118-
"src/new-client.ts"
90+
]),
91+
defaultFilePath
11992
)
12093

12194
expect(messages).toHaveLength(0)
12295
})
123-
124-
it("rejects migrated legacy paths too", () => {
125-
const messages = verify(
126-
"import { listProjects } from \"@effect-template/lib\"\n",
127-
"src/docker-git/program.ts"
128-
)
129-
130-
expect(messages).toHaveLength(1)
131-
expect(messages[0]?.message).toContain("Direct import")
132-
})
13396
})

0 commit comments

Comments
 (0)