1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import {
4 hasLinkableInChain,
5 isDefineLinkCall,
6 isLinkableMemberChain,
7 trackDefineLinkImports,
8 } from "../../util/link"
9
10 type MessageIds = "notLinkableProperty"
11
12 function isLinkableMemberChainOrId(node: TSESTree.Node): boolean {
13 // Match `<X>.linkable.<y>` or `<X>.linkable.<y>.id`. The `.id` form is used
14 // when linking to a data model by its id (e.g. linking to draft orders via
15 // `OrderModule.linkable.order.id`).
16 if (isLinkableMemberChain(node)) {
17 return true
18 }
19 if (
20 node.type === "MemberExpression" &&
21 !node.computed &&
22 node.property.type === "Identifier" &&
23 node.property.name === "id" &&
24 isLinkableMemberChain(node.object)
25 ) {
26 return true
27 }
28 return false
29 }
30
31 function hasOwnPropertyNamed(
32 node: TSESTree.ObjectExpression,
33 name: string
34 ): boolean {
35 for (const prop of node.properties) {
36 if (prop.type !== "Property" || prop.computed) {
37 continue
38 }
39 const keyName =
40 prop.key.type === "Identifier"
41 ? prop.key.name
42 : prop.key.type === "Literal" && typeof prop.key.value === "string"
43 ? prop.key.value
44 : null
45 if (keyName === name) {
46 return true
47 }
48 }
49 return false
50 }
51
52 function isValidLinkParticipant(node: TSESTree.Node): boolean {
53 if (isLinkableMemberChain(node)) {
54 return true
55 }
56 if (node.type === "ObjectExpression") {
57 for (const prop of node.properties) {
58 if (prop.type === "SpreadElement") {
59 // Inverse read-only links spread `<X>.linkable.<y>.id` (or similar).
60 if (hasLinkableInChain(prop.argument)) {
61 return true
62 }
63 continue
64 }
65 if (prop.type !== "Property" || prop.computed) {
66 continue
67 }
68 const keyName =
69 prop.key.type === "Identifier"
70 ? prop.key.name
71 : prop.key.type === "Literal" && typeof prop.key.value === "string"
72 ? prop.key.value
73 : null
74 if (keyName !== "linkable") {
75 continue
76 }
77 if (isLinkableMemberChainOrId(prop.value)) {
78 return true
79 }
80 // Read-only link form: `linkable` is an inline object describing the
81 // foreign data model via `serviceName` + `alias` + (optional) `primaryKey`.
82 if (
83 prop.value.type === "ObjectExpression" &&
84 hasOwnPropertyNamed(prop.value, "serviceName")
85 ) {
86 return true
87 }
88 }
89 }
90 return false
91 }
92
93 export const rule = createRule<[], MessageIds>({
94 name: "link-uses-linkable-properties",
95 meta: {
96 type: "problem",
97 docs: {
98 description:
99 "Arguments to `defineLink` must be of the form `Module.linkable.<dataModel>` or an object with `linkable: Module.linkable.<dataModel>`.",
100 },
101 messages: {
102 notLinkableProperty:
103 "Arguments to `defineLink` must reference a module's `linkable` property (e.g. `Module.linkable.dataModel` or `{ linkable: Module.linkable.dataModel }`).",
104 },
105 schema: [],
106 },
107 defaultOptions: [],
108 create(context) {
109 const defineLinkLocalNames = new Set<string>()
110
111 return {
112 ImportDeclaration(node) {
113 trackDefineLinkImports(node, defineLinkLocalNames)
114 },
115
116 CallExpression(node) {
117 if (!isDefineLinkCall(node, defineLinkLocalNames)) {
118 return
119 }
120 // `defineLink(arg1, arg2, options?)` — only the first two args are
121 // link participants. Anything beyond index 1 is configuration.
122 const participants = node.arguments.slice(0, 2)
123 for (const arg of participants) {
124 if (arg.type === "SpreadElement") {
125 continue
126 }
127 if (!isValidLinkParticipant(arg)) {
128 context.report({
129 node: arg,
130 messageId: "notLinkableProperty",
131 })
132 }
133 }
134 },
135 }
136 },
137 })
138
139 export default rule