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 {
5 findProperty,
6 getCallArgumentObject,
7 getStringLiteralValue,
8 } from "../../util/ast"
9
10 type MessageIds = "wildcardWithSpecificFields"
11
12 /** Property on the query config object that holds the selected fields. */
13 const FIELDS_PROPERTY = "fields"
14
15 /** The top-level wildcard that selects all of an entity's own fields. */
16 const WILDCARD = "*"
17
18 /** The member method that runs a graph query: `query.graph({ ... })`. */
19 const GRAPH_METHOD = "graph"
20
21 /** The workflow step helper that runs a graph query. */
22 const USE_QUERY_GRAPH_STEP = "useQueryGraphStep"
23
24 /**
25 * True when `call` is a graph query we care about: either `<x>.graph(...)` or
26 * `useQueryGraphStep(...)`.
27 */
28 function isGraphQueryCall(call: TSESTree.CallExpression): boolean {
29 const callee = call.callee
30 if (
31 callee.type === AST_NODE_TYPES.Identifier &&
32 callee.name === USE_QUERY_GRAPH_STEP
33 ) {
34 return true
35 }
36 if (
37 callee.type === AST_NODE_TYPES.MemberExpression &&
38 !callee.computed &&
39 callee.property.type === AST_NODE_TYPES.Identifier &&
40 callee.property.name === GRAPH_METHOD
41 ) {
42 return true
43 }
44 return false
45 }
46
47 export const rule = createRule<[], MessageIds>({
48 name: "no-wildcard-with-specific-fields",
49 meta: {
50 type: "problem",
51 docs: {
52 description:
53 "Disallow combining the `*` wildcard with specific top-level fields in a `query.graph` or `useQueryGraphStep` selection, since `*` drops those field selections.",
54 },
55 messages: {
56 wildcardWithSpecificFields:
57 "The `*` wildcard drops the sibling field selection(s) {{fields}}. `*` selects all of the entity's own fields, so top-level fields not included by `*` (like computed fields such as `total`) are lost. Remove `*` if you need those specific fields.",
58 },
59 schema: [],
60 },
61 defaultOptions: [],
62 create(context) {
63 return {
64 CallExpression(node) {
65 if (!isGraphQueryCall(node)) {
66 return
67 }
68
69 const config = getCallArgumentObject(node)
70 if (!config) {
71 return
72 }
73
74 const fieldsProp = findProperty(config, FIELDS_PROPERTY)
75 if (
76 !fieldsProp ||
77 fieldsProp.value.type !== AST_NODE_TYPES.ArrayExpression
78 ) {
79 return
80 }
81
82 const elements = fieldsProp.value.elements
83
84 let wildcardNode: TSESTree.Node | null = null
85 const specificFields: string[] = []
86
87 for (const element of elements) {
88 const value = getStringLiteralValue(element)
89 if (value === null) {
90 continue
91 }
92 if (value === WILDCARD) {
93 // Anchor the report on the first `*` encountered.
94 wildcardNode ??= element as TSESTree.Node
95 continue
96 }
97 // Relation selections (e.g. `items.*`, `items.title`) are scoped to a
98 // relation and are kept even when `*` is present, so they don't
99 // conflict. Only plain top-level fields are dropped by `*`.
100 if (!value.includes(".")) {
101 specificFields.push(value)
102 }
103 }
104
105 if (!wildcardNode || specificFields.length === 0) {
106 return
107 }
108
109 context.report({
110 node: wildcardNode,
111 messageId: "wildcardWithSpecificFields",
112 data: {
113 fields: specificFields.map((f) => `\`"${f}"\``).join(", "),
114 },
115 })
116 },
117 }
118 },
119 })
120
121 export default rule