|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { FRAMEWORK_UTILS_SOURCE } from "../../constants" |
|
5
|
|
|
6
|
type MessageIds = "missingPrimaryKey" |
|
7
|
|
|
8
|
const MODEL_IMPORT = "model" |
|
9
|
const DEFINE_METHOD = "define" |
|
10
|
const PRIMARY_KEY_METHOD = "primaryKey" |
|
11
|
|
|
12
|
/** |
|
13
|
* Walks a property-value method chain (e.g. `model.id().primaryKey().nullable()`) |
|
14
|
* looking for a `.primaryKey()` call anywhere in the chain. Descends through |
|
15
|
* call callees and member-expression objects only — returns `false` the moment |
|
16
|
* the chain bottoms out at anything else (identifier, literal, etc.). |
|
17
|
*/ |
|
18
|
const chainHasPrimaryKey = (node: TSESTree.Node): boolean => { |
|
19
|
let current: TSESTree.Node = node |
|
20
|
while (current) { |
|
21
|
if (current.type === AST_NODE_TYPES.CallExpression) { |
|
22
|
const callee = current.callee |
|
23
|
if ( |
|
24
|
callee.type === AST_NODE_TYPES.MemberExpression && |
|
25
|
!callee.computed && |
|
26
|
callee.property.type === AST_NODE_TYPES.Identifier && |
|
27
|
callee.property.name === PRIMARY_KEY_METHOD |
|
28
|
) { |
|
29
|
return true |
|
30
|
} |
|
31
|
current = callee |
|
32
|
} else if (current.type === AST_NODE_TYPES.MemberExpression) { |
|
33
|
current = current.object |
|
34
|
} else { |
|
35
|
return false |
|
36
|
} |
|
37
|
} |
|
38
|
return false |
|
39
|
} |
|
40
|
|
|
41
|
export const rule = createRule<[], MessageIds>({ |
|
42
|
name: "primary-key-required", |
|
43
|
meta: { |
|
44
|
type: "problem", |
|
45
|
docs: { |
|
46
|
description: |
|
47
|
"A data model defined with `model.define` must declare a primary key via `.primaryKey()`.", |
|
48
|
}, |
|
49
|
messages: { |
|
50
|
missingPrimaryKey: |
|
51
|
"Data model {{name}} has no primary key. Add `.primaryKey()` to one of its `id`, `text`, or `number` properties.", |
|
52
|
}, |
|
53
|
schema: [], |
|
54
|
}, |
|
55
|
defaultOptions: [], |
|
56
|
create(context) { |
|
57
|
const modelLocalNames = new Set<string>() |
|
58
|
|
|
59
|
return { |
|
60
|
ImportDeclaration(node) { |
|
61
|
if (node.source.value !== FRAMEWORK_UTILS_SOURCE) { |
|
62
|
return |
|
63
|
} |
|
64
|
for (const specifier of node.specifiers) { |
|
65
|
if ( |
|
66
|
specifier.type === AST_NODE_TYPES.ImportSpecifier && |
|
67
|
specifier.imported.type === AST_NODE_TYPES.Identifier && |
|
68
|
specifier.imported.name === MODEL_IMPORT |
|
69
|
) { |
|
70
|
modelLocalNames.add(specifier.local.name) |
|
71
|
} |
|
72
|
} |
|
73
|
}, |
|
74
|
|
|
75
|
CallExpression(node) { |
|
76
|
if (modelLocalNames.size === 0) { |
|
77
|
return |
|
78
|
} |
|
79
|
const callee = node.callee |
|
80
|
if ( |
|
81
|
callee.type !== AST_NODE_TYPES.MemberExpression || |
|
82
|
callee.computed || |
|
83
|
callee.property.type !== AST_NODE_TYPES.Identifier || |
|
84
|
callee.property.name !== DEFINE_METHOD || |
|
85
|
callee.object.type !== AST_NODE_TYPES.Identifier || |
|
86
|
!modelLocalNames.has(callee.object.name) |
|
87
|
) { |
|
88
|
return |
|
89
|
} |
|
90
|
const schemaArg = node.arguments[1] |
|
91
|
if (!schemaArg || schemaArg.type !== AST_NODE_TYPES.ObjectExpression) { |
|
92
|
return |
|
93
|
} |
|
94
|
|
|
95
|
// A spread could supply the primary-key property — can't tell, so bail |
|
96
|
// to avoid a false positive. |
|
97
|
const hasSpread = schemaArg.properties.some( |
|
98
|
(p) => p.type === AST_NODE_TYPES.SpreadElement |
|
99
|
) |
|
100
|
if (hasSpread) { |
|
101
|
return |
|
102
|
} |
|
103
|
|
|
104
|
const hasPrimaryKey = schemaArg.properties.some( |
|
105
|
(prop) => |
|
106
|
prop.type === AST_NODE_TYPES.Property && |
|
107
|
chainHasPrimaryKey(prop.value) |
|
108
|
) |
|
109
|
if (hasPrimaryKey) { |
|
110
|
return |
|
111
|
} |
|
112
|
|
|
113
|
const firstArg = node.arguments[0] |
|
114
|
const name = |
|
115
|
firstArg && |
|
116
|
firstArg.type === AST_NODE_TYPES.Literal && |
|
117
|
typeof firstArg.value === "string" |
|
118
|
? JSON.stringify(firstArg.value) |
|
119
|
: "defined with `model.define`" |
|
120
|
|
|
121
|
context.report({ |
|
122
|
node: callee.property, |
|
123
|
messageId: "missingPrimaryKey", |
|
124
|
data: { name }, |
|
125
|
}) |
|
126
|
}, |
|
127
|
} |
|
128
|
}, |
|
129
|
}) |
|
130
|
|
|
131
|
export default rule |