|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
import { findProperty } from "../../util/ast" |
|
4
|
import { |
|
5
|
hasLinkableInChain, |
|
6
|
isDefineLinkCall, |
|
7
|
trackDefineLinkImports, |
|
8
|
} from "../../util/link" |
|
9
|
|
|
10
|
type MessageIds = "missingField" | "missingPrimaryKey" |
|
11
|
|
|
12
|
function isReadOnlyTrue(options: TSESTree.Node): boolean { |
|
13
|
if (options.type !== "ObjectExpression") { |
|
14
|
return false |
|
15
|
} |
|
16
|
const readOnly = findProperty(options, "readOnly") |
|
17
|
if (!readOnly) { |
|
18
|
return false |
|
19
|
} |
|
20
|
return readOnly.value.type === "Literal" && readOnly.value.value === true |
|
21
|
} |
|
22
|
|
|
23
|
function isLinkableIdSpread(node: TSESTree.Node): boolean { |
|
24
|
// Match `...<X>.linkable.<y>.id` — i.e. a MemberExpression ending in `.id` |
|
25
|
// whose object chain contains a non-computed `.linkable` segment. |
|
26
|
if (node.type !== "MemberExpression" || node.computed) { |
|
27
|
return false |
|
28
|
} |
|
29
|
if (node.property.type !== "Identifier" || node.property.name !== "id") { |
|
30
|
return false |
|
31
|
} |
|
32
|
return hasLinkableInChain(node.object) |
|
33
|
} |
|
34
|
|
|
35
|
function hasInverseLinkableSpread(obj: TSESTree.ObjectExpression): boolean { |
|
36
|
for (const prop of obj.properties) { |
|
37
|
if (prop.type === "SpreadElement" && isLinkableIdSpread(prop.argument)) { |
|
38
|
return true |
|
39
|
} |
|
40
|
} |
|
41
|
return false |
|
42
|
} |
|
43
|
|
|
44
|
export const rule = createRule<[], MessageIds>({ |
|
45
|
name: "read-only-link-requires-field", |
|
46
|
meta: { |
|
47
|
type: "problem", |
|
48
|
docs: { |
|
49
|
description: |
|
50
|
"Read-only `defineLink(...)` calls must specify a `field` on the first linkable (and a `primaryKey` on the inverse form).", |
|
51
|
}, |
|
52
|
messages: { |
|
53
|
missingField: |
|
54
|
"Read-only `defineLink(...)` requires the first argument to be an object with a `field` property specifying where the linked record's ID is stored.", |
|
55
|
missingPrimaryKey: |
|
56
|
"Inverse read-only `defineLink(...)` requires the second argument to include a `primaryKey` property override.", |
|
57
|
}, |
|
58
|
schema: [], |
|
59
|
}, |
|
60
|
defaultOptions: [], |
|
61
|
create(context) { |
|
62
|
const defineLinkLocalNames = new Set<string>() |
|
63
|
|
|
64
|
return { |
|
65
|
ImportDeclaration(node) { |
|
66
|
trackDefineLinkImports(node, defineLinkLocalNames) |
|
67
|
}, |
|
68
|
|
|
69
|
CallExpression(node) { |
|
70
|
if (!isDefineLinkCall(node, defineLinkLocalNames)) { |
|
71
|
return |
|
72
|
} |
|
73
|
|
|
74
|
const [arg1, arg2, options] = node.arguments |
|
75
|
if (!options || !arg1 || !arg2) { |
|
76
|
return |
|
77
|
} |
|
78
|
if (!isReadOnlyTrue(options)) { |
|
79
|
return |
|
80
|
} |
|
81
|
|
|
82
|
// First arg must be an ObjectExpression with `field`. |
|
83
|
if (arg1.type !== "ObjectExpression" || !findProperty(arg1, "field")) { |
|
84
|
context.report({ node: arg1, messageId: "missingField" }) |
|
85
|
} |
|
86
|
|
|
87
|
// Inverse form: second arg is an ObjectExpression spreading |
|
88
|
// `<X>.linkable.<y>.id`. It must include a `primaryKey` override, or |
|
89
|
// an `alias` (e.g. linking to draft orders via the `order` linkable, |
|
90
|
// where the foreign key lives on the first linkable's `field` and the |
|
91
|
// spread `.id` is simply re-aliased). |
|
92
|
if ( |
|
93
|
arg2.type === "ObjectExpression" && |
|
94
|
hasInverseLinkableSpread(arg2) && |
|
95
|
!findProperty(arg2, "primaryKey") && |
|
96
|
!findProperty(arg2, "alias") |
|
97
|
) { |
|
98
|
context.report({ node: arg2, messageId: "missingPrimaryKey" }) |
|
99
|
} |
|
100
|
}, |
|
101
|
} |
|
102
|
}, |
|
103
|
}) |
|
104
|
|
|
105
|
export default rule |