|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "missingDefaultExport" |
|
5
|
|
|
6
|
export const rule = createRule<[], MessageIds>({ |
|
7
|
name: "subscriber-default-export-required", |
|
8
|
meta: { |
|
9
|
type: "problem", |
|
10
|
docs: { |
|
11
|
description: |
|
12
|
"Subscriber files must default-export the subscriber function.", |
|
13
|
}, |
|
14
|
messages: { |
|
15
|
missingDefaultExport: |
|
16
|
"Subscriber files must have a default export. Default-export the subscriber handler function.", |
|
17
|
}, |
|
18
|
schema: [], |
|
19
|
}, |
|
20
|
defaultOptions: [], |
|
21
|
create(context) { |
|
22
|
let hasDefaultExport = false |
|
23
|
|
|
24
|
return { |
|
25
|
ExportDefaultDeclaration() { |
|
26
|
hasDefaultExport = true |
|
27
|
}, |
|
28
|
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
|
29
|
for (const spec of node.specifiers) { |
|
30
|
if ( |
|
31
|
spec.exported.type === AST_NODE_TYPES.Identifier && |
|
32
|
spec.exported.name === "default" |
|
33
|
) { |
|
34
|
hasDefaultExport = true |
|
35
|
return |
|
36
|
} |
|
37
|
} |
|
38
|
}, |
|
39
|
"Program:exit"(node: TSESTree.Program) { |
|
40
|
if (hasDefaultExport) { |
|
41
|
return |
|
42
|
} |
|
43
|
context.report({ |
|
44
|
node, |
|
45
|
messageId: "missingDefaultExport", |
|
46
|
}) |
|
47
|
}, |
|
48
|
} |
|
49
|
}, |
|
50
|
}) |
|
51
|
|
|
52
|
export default rule |