|
| 1 | +import type { TSESTree } from "@typescript-eslint/types" |
| 2 | +import type { RuleModule } from "../types.ts" |
| 3 | +import { createRule } from "../utils/index.ts" |
| 4 | +import { getSourceCode, getFilename } from "../utils/compat.ts" |
| 5 | + |
| 6 | +const PAGES_DIR_PATTERN = /(?:^|[/\\])pages[/\\]/ |
| 7 | + |
| 8 | +const rule: RuleModule = createRule("no-prerender-export-outside-pages", { |
| 9 | + meta: { |
| 10 | + docs: { |
| 11 | + description: "disallow `prerender` export outside of pages/ directory", |
| 12 | + category: "Possible Errors", |
| 13 | + recommended: false, |
| 14 | + }, |
| 15 | + schema: [], |
| 16 | + messages: { |
| 17 | + disallowPrerenderOutsidePages: |
| 18 | + "'prerender' export is only valid inside a pages/ directory.", |
| 19 | + }, |
| 20 | + type: "problem", |
| 21 | + }, |
| 22 | + create(context) { |
| 23 | + const sourceCode = getSourceCode(context) |
| 24 | + if (!sourceCode.parserServices?.isAstro) { |
| 25 | + return {} |
| 26 | + } |
| 27 | + |
| 28 | + const filename = getFilename(context) |
| 29 | + if (PAGES_DIR_PATTERN.test(filename)) { |
| 30 | + return {} |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Verify for export declarations |
| 35 | + */ |
| 36 | + function verifyDeclaration( |
| 37 | + node: TSESTree.ExportNamedDeclaration["declaration"], |
| 38 | + ) { |
| 39 | + if (!node) return |
| 40 | + if ( |
| 41 | + node.type === "VariableDeclaration" && |
| 42 | + node.declarations.some( |
| 43 | + (decl) => |
| 44 | + decl.id.type === "Identifier" && decl.id.name === "prerender", |
| 45 | + ) |
| 46 | + ) { |
| 47 | + context.report({ |
| 48 | + node, |
| 49 | + messageId: "disallowPrerenderOutsidePages", |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + ExportNamedDeclaration(node) { |
| 56 | + if (node.exportKind === "type") return |
| 57 | + verifyDeclaration(node.declaration) |
| 58 | + for (const spec of node.specifiers) { |
| 59 | + if (spec.exportKind === "type") continue |
| 60 | + if ( |
| 61 | + spec.exported.type === "Identifier" && |
| 62 | + spec.exported.name === "prerender" |
| 63 | + ) { |
| 64 | + context.report({ |
| 65 | + node: spec, |
| 66 | + messageId: "disallowPrerenderOutsidePages", |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + } |
| 72 | + }, |
| 73 | +}) |
| 74 | + |
| 75 | +export default rule |
0 commit comments