|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { ADMIN_SDK_SOURCE } from "../../constants" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
|
|
5
|
type MessageIds = "missingConfigExport" | "configNotDefineWidgetConfig" |
|
6
|
|
|
7
|
const DEFINE_WIDGET_CONFIG = "defineWidgetConfig" |
|
8
|
|
|
9
|
export const rule = createRule<[], MessageIds>({ |
|
10
|
name: "widget-must-export-config", |
|
11
|
meta: { |
|
12
|
type: "problem", |
|
13
|
docs: { |
|
14
|
description: |
|
15
|
"Widget files must export a named `config` from `defineWidgetConfig(...)`.", |
|
16
|
}, |
|
17
|
messages: { |
|
18
|
missingConfigExport: |
|
19
|
"Widget files must have a named export `config` initialized via `defineWidgetConfig(...)`.", |
|
20
|
configNotDefineWidgetConfig: |
|
21
|
"Widget `config` export must be initialized via `defineWidgetConfig(...)`.", |
|
22
|
}, |
|
23
|
schema: [], |
|
24
|
}, |
|
25
|
defaultOptions: [], |
|
26
|
create(context) { |
|
27
|
const defineWidgetConfigLocalNames = new Set<string>() |
|
28
|
let hasConfigExport = false |
|
29
|
let badInitializerNode: TSESTree.Node | null = null |
|
30
|
|
|
31
|
function isDefineWidgetConfigCall(node: TSESTree.Node | null): boolean { |
|
32
|
if (!node || node.type !== AST_NODE_TYPES.CallExpression) { |
|
33
|
return false |
|
34
|
} |
|
35
|
const callee = node.callee |
|
36
|
return ( |
|
37
|
callee.type === AST_NODE_TYPES.Identifier && |
|
38
|
defineWidgetConfigLocalNames.has(callee.name) |
|
39
|
) |
|
40
|
} |
|
41
|
|
|
42
|
function declaratorName(decl: TSESTree.VariableDeclarator): string | null { |
|
43
|
if (decl.id.type === AST_NODE_TYPES.Identifier) { |
|
44
|
return decl.id.name |
|
45
|
} |
|
46
|
return null |
|
47
|
} |
|
48
|
|
|
49
|
return { |
|
50
|
ImportDeclaration(node: TSESTree.ImportDeclaration) { |
|
51
|
if (node.source.value !== ADMIN_SDK_SOURCE) { |
|
52
|
return |
|
53
|
} |
|
54
|
for (const spec of node.specifiers) { |
|
55
|
if ( |
|
56
|
spec.type === AST_NODE_TYPES.ImportSpecifier && |
|
57
|
spec.imported.type === AST_NODE_TYPES.Identifier && |
|
58
|
spec.imported.name === DEFINE_WIDGET_CONFIG |
|
59
|
) { |
|
60
|
defineWidgetConfigLocalNames.add(spec.local.name) |
|
61
|
} |
|
62
|
} |
|
63
|
}, |
|
64
|
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
|
65
|
// export const config = defineWidgetConfig(...) |
|
66
|
if ( |
|
67
|
node.declaration && |
|
68
|
node.declaration.type === AST_NODE_TYPES.VariableDeclaration |
|
69
|
) { |
|
70
|
for (const decl of node.declaration.declarations) { |
|
71
|
if (declaratorName(decl) === "config") { |
|
72
|
hasConfigExport = true |
|
73
|
if (!isDefineWidgetConfigCall(decl.init)) { |
|
74
|
badInitializerNode = decl.init ?? decl |
|
75
|
} |
|
76
|
} |
|
77
|
} |
|
78
|
} |
|
79
|
// export { foo as config }, export { config }, export { config } from "./x" |
|
80
|
for (const spec of node.specifiers) { |
|
81
|
if ( |
|
82
|
spec.exported.type === AST_NODE_TYPES.Identifier && |
|
83
|
spec.exported.name === "config" |
|
84
|
) { |
|
85
|
hasConfigExport = true |
|
86
|
} |
|
87
|
} |
|
88
|
}, |
|
89
|
"Program:exit"(node: TSESTree.Program) { |
|
90
|
if (!hasConfigExport) { |
|
91
|
context.report({ node, messageId: "missingConfigExport" }) |
|
92
|
return |
|
93
|
} |
|
94
|
if (badInitializerNode) { |
|
95
|
context.report({ |
|
96
|
node: badInitializerNode, |
|
97
|
messageId: "configNotDefineWidgetConfig", |
|
98
|
}) |
|
99
|
} |
|
100
|
}, |
|
101
|
} |
|
102
|
}, |
|
103
|
}) |
|
104
|
|
|
105
|
export default rule |