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: "widget-must-have-default-export",
8 meta: {
9 type: "problem",
10 docs: {
11 description: "Widget files must default-export the widget component.",
12 },
13 messages: {
14 missingDefaultExport:
15 "Widget files must have a default export. Default-export the widget component.",
16 },
17 schema: [],
18 },
19 defaultOptions: [],
20 create(context) {
21 let hasDefaultExport = false
22
23 return {
24 ExportDefaultDeclaration() {
25 hasDefaultExport = true
26 },
27 ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) {
28 for (const spec of node.specifiers) {
29 if (
30 spec.exported.type === AST_NODE_TYPES.Identifier &&
31 spec.exported.name === "default"
32 ) {
33 hasDefaultExport = true
34 return
35 }
36 }
37 },
38 "Program:exit"(node: TSESTree.Program) {
39 if (hasDefaultExport) {
40 return
41 }
42 context.report({
43 node,
44 messageId: "missingDefaultExport",
45 })
46 },
47 }
48 },
49 })
50
51 export default rule