|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +const bannedPackageName = "@effect-template/lib" |
| 4 | + |
| 5 | +/** @type {ReadonlyArray<string>} */ |
| 6 | +export const appLegacyLibImportAllowlist = [ |
| 7 | + "src/app/program.ts", |
| 8 | + "src/docker-git/cli/input.ts", |
| 9 | + "src/docker-git/cli/parser-apply.ts", |
| 10 | + "src/docker-git/cli/parser-attach.ts", |
| 11 | + "src/docker-git/cli/parser-auth.ts", |
| 12 | + "src/docker-git/cli/parser-clone.ts", |
| 13 | + "src/docker-git/cli/parser-create.ts", |
| 14 | + "src/docker-git/cli/parser-mcp-playwright.ts", |
| 15 | + "src/docker-git/cli/parser-options.ts", |
| 16 | + "src/docker-git/cli/parser-panes.ts", |
| 17 | + "src/docker-git/cli/parser-scrap.ts", |
| 18 | + "src/docker-git/cli/parser-session-gists.ts", |
| 19 | + "src/docker-git/cli/parser-sessions.ts", |
| 20 | + "src/docker-git/cli/parser-shared.ts", |
| 21 | + "src/docker-git/cli/parser-state.ts", |
| 22 | + "src/docker-git/cli/parser.ts", |
| 23 | + "src/docker-git/cli/read-command.ts", |
| 24 | + "src/docker-git/cli/usage.ts", |
| 25 | + "src/docker-git/menu-actions.ts", |
| 26 | + "src/docker-git/menu-auth-data.ts", |
| 27 | + "src/docker-git/menu-auth-effects.ts", |
| 28 | + "src/docker-git/menu-auth-helpers.ts", |
| 29 | + "src/docker-git/menu-auth-snapshot-builder.ts", |
| 30 | + "src/docker-git/menu-auth.ts", |
| 31 | + "src/docker-git/menu-create.ts", |
| 32 | + "src/docker-git/menu-labeled-env.ts", |
| 33 | + "src/docker-git/menu-menu.ts", |
| 34 | + "src/docker-git/menu-project-auth-data.ts", |
| 35 | + "src/docker-git/menu-project-auth-flows.ts", |
| 36 | + "src/docker-git/menu-project-auth.ts", |
| 37 | + "src/docker-git/menu-render-select.ts", |
| 38 | + "src/docker-git/menu-render.ts", |
| 39 | + "src/docker-git/menu-select-actions.ts", |
| 40 | + "src/docker-git/menu-select-connect.ts", |
| 41 | + "src/docker-git/menu-select-load.ts", |
| 42 | + "src/docker-git/menu-select-order.ts", |
| 43 | + "src/docker-git/menu-select-runtime.ts", |
| 44 | + "src/docker-git/menu-select-view.ts", |
| 45 | + "src/docker-git/menu-startup.ts", |
| 46 | + "src/docker-git/menu-types.ts", |
| 47 | + "src/docker-git/menu.ts", |
| 48 | + "src/docker-git/program.ts", |
| 49 | + "src/docker-git/tmux.ts", |
| 50 | + "tests/docker-git/entrypoint-auth.test.ts", |
| 51 | + "tests/docker-git/fixtures/project-item.ts", |
| 52 | + "tests/docker-git/menu-select-connect.test.ts", |
| 53 | + "tests/docker-git/parser-helpers.ts", |
| 54 | + "tests/docker-git/parser.test.ts" |
| 55 | +] |
| 56 | + |
| 57 | +/** @param {string} value */ |
| 58 | +const normalizePath = (value) => value.replaceAll("\\", "/") |
| 59 | + |
| 60 | +/** @param {string} value */ |
| 61 | +const isDirectLibImport = (value) => |
| 62 | + value === bannedPackageName || value.startsWith(`${bannedPackageName}/`) |
| 63 | + |
| 64 | +/** |
| 65 | + * @param {string} filename |
| 66 | + * @param {ReadonlyArray<string>} allowInFiles |
| 67 | + */ |
| 68 | +const isAllowlistedFile = (filename, allowInFiles) => { |
| 69 | + const normalized = normalizePath(filename) |
| 70 | + return allowInFiles.some((entry) => normalized === entry || normalized.endsWith(`/${entry}`)) |
| 71 | +} |
| 72 | + |
| 73 | +/** @param {(import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined} source */ |
| 74 | +const readSourceText = (source) => |
| 75 | + source && source.type === "Literal" && typeof source.value === "string" |
| 76 | + ? source.value |
| 77 | + : null |
| 78 | + |
| 79 | +/** |
| 80 | + * @param {import("eslint").Rule.RuleContext} context |
| 81 | + * @returns {import("eslint").Rule.RuleListener} |
| 82 | + */ |
| 83 | +const createRuleListener = (context) => { |
| 84 | + const [options = {}] = context.options |
| 85 | + const allowInFiles = Array.isArray(options.allowInFiles) |
| 86 | + ? options.allowInFiles.map( |
| 87 | + /** @param {unknown} value */ (value) => normalizePath(String(value)) |
| 88 | + ) |
| 89 | + : [] |
| 90 | + const filename = typeof context.filename === "string" ? context.filename : "" |
| 91 | + |
| 92 | + if (isAllowlistedFile(filename, allowInFiles)) { |
| 93 | + return {} |
| 94 | + } |
| 95 | + |
| 96 | + /** @param {(import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined} source */ |
| 97 | + const checkSource = (source) => { |
| 98 | + if (source == null) { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + const sourceText = readSourceText(source) |
| 103 | + if (sourceText === null || !isDirectLibImport(sourceText)) { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + context.report({ |
| 108 | + node: source, |
| 109 | + messageId: "noLibImport", |
| 110 | + data: { source: sourceText } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + /** @param {{ readonly source?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined }} node */ |
| 116 | + ExportAllDeclaration(node) { |
| 117 | + checkSource(node.source) |
| 118 | + }, |
| 119 | + /** @param {{ readonly source?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined }} node */ |
| 120 | + ExportNamedDeclaration(node) { |
| 121 | + checkSource(node.source) |
| 122 | + }, |
| 123 | + /** @param {{ readonly source?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined }} node */ |
| 124 | + ImportDeclaration(node) { |
| 125 | + checkSource(node.source) |
| 126 | + }, |
| 127 | + /** @param {{ readonly source?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined }} node */ |
| 128 | + ImportExpression(node) { |
| 129 | + checkSource(node.source) |
| 130 | + }, |
| 131 | + /** @param {{ readonly source?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined, readonly argument?: (import("eslint").JSSyntaxElement & { readonly value?: unknown }) | null | undefined }} node */ |
| 132 | + TSImportType(node) { |
| 133 | + checkSource("source" in node ? node.source : node.argument) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** @type {import("eslint").Rule.RuleModule} */ |
| 139 | +export const noLibImportsRule = { |
| 140 | + meta: { |
| 141 | + type: "problem", |
| 142 | + docs: { |
| 143 | + description: "forbid direct imports from @effect-template/lib inside package/app" |
| 144 | + }, |
| 145 | + schema: [ |
| 146 | + { |
| 147 | + type: "object", |
| 148 | + properties: { |
| 149 | + allowInFiles: { |
| 150 | + type: "array", |
| 151 | + items: { type: "string" } |
| 152 | + } |
| 153 | + }, |
| 154 | + additionalProperties: false |
| 155 | + } |
| 156 | + ], |
| 157 | + messages: { |
| 158 | + noLibImport: |
| 159 | + "Direct import '{{source}}' from @effect-template/lib is forbidden in package/app. Use the API client or a local app adapter instead." |
| 160 | + } |
| 161 | + }, |
| 162 | + create: createRuleListener |
| 163 | +} |
0 commit comments