|
1 | 1 | import { ESLintUtils, TSESTree, ASTUtils } from "@typescript-eslint/utils";
|
2 |
| -import { RuleOptions } from "../ruleOptions.js"; |
3 |
| -import { getQueryValue, stringifyNode } from "../utils.js"; |
| 2 | +import { getQueryValue } from "../utils.js"; |
4 | 3 | import { inferQueryInput, QueryInput } from "../inferQueryInput.js";
|
5 | 4 |
|
6 |
| -export function createTypedInputRule(options: RuleOptions) { |
7 |
| - return ESLintUtils.RuleCreator.withoutDocs({ |
8 |
| - create(context) { |
9 |
| - return { |
10 |
| - 'CallExpression[callee.type=MemberExpression][callee.property.name="prepare"][arguments.length=1]'( |
11 |
| - node: Omit<TSESTree.CallExpression, "arguments" | "callee"> & { |
12 |
| - arguments: [TSESTree.CallExpression["arguments"][0]]; |
13 |
| - callee: TSESTree.MemberExpression; |
14 |
| - }, |
15 |
| - ) { |
16 |
| - const val = getQueryValue( |
17 |
| - node.arguments[0], |
18 |
| - context.sourceCode.getScope(node.arguments[0]), |
19 |
| - ); |
20 |
| - if (typeof val?.value !== "string") { |
21 |
| - return; |
22 |
| - } |
23 |
| - |
24 |
| - const databaseName = stringifyNode(node.callee.object); |
25 |
| - if (!databaseName) { |
26 |
| - return; |
27 |
| - } |
28 |
| - |
29 |
| - const db = options.getDatabase({ |
30 |
| - filename: context.filename, |
31 |
| - name: databaseName, |
32 |
| - }); |
33 |
| - |
34 |
| - const queryInput = inferQueryInput(val.value, db); |
35 |
| - if (queryInput == null) { |
36 |
| - return; |
37 |
| - } |
38 |
| - |
39 |
| - const typeArguments = node.typeArguments; |
40 |
| - const inputParam = typeArguments?.params[0]; |
41 |
| - if (!typeArguments || !inputParam) { |
42 |
| - context.report({ |
43 |
| - node: node, |
44 |
| - messageId: "missingInputType", |
45 |
| - *fix(fixer) { |
46 |
| - if (typeArguments && !inputParam) { |
47 |
| - yield fixer.replaceText( |
48 |
| - typeArguments, |
49 |
| - `<${queryInputToText(queryInput)}>`, |
50 |
| - ); |
51 |
| - } else { |
52 |
| - yield fixer.insertTextAfter( |
53 |
| - node.callee, |
54 |
| - `<${queryInputToText(queryInput)}>`, |
55 |
| - ); |
56 |
| - } |
57 |
| - }, |
58 |
| - }); |
59 |
| - return; |
60 |
| - } |
61 |
| - |
62 |
| - if (isDeclaredTypeCorrect(queryInput, inputParam)) { |
63 |
| - return; |
64 |
| - } |
65 |
| - |
66 |
| - const members = |
67 |
| - inputParam.type === TSESTree.AST_NODE_TYPES.TSTypeLiteral |
68 |
| - ? inputParam.members |
69 |
| - : inputParam.type === TSESTree.AST_NODE_TYPES.TSTupleType |
70 |
| - ? inputParam.elementTypes.find( |
71 |
| - (element) => |
72 |
| - element.type === TSESTree.AST_NODE_TYPES.TSTypeLiteral, |
73 |
| - )?.members |
74 |
| - : null; |
75 |
| - |
76 |
| - const userDeclaredTypes = new Map<string, string>(); |
77 |
| - |
78 |
| - if (members) { |
79 |
| - for (const member of members) { |
80 |
| - if (member.type !== TSESTree.AST_NODE_TYPES.TSPropertySignature) { |
81 |
| - continue; |
82 |
| - } |
83 |
| - |
84 |
| - if (!member.typeAnnotation) { |
85 |
| - continue; |
| 5 | +export const typedInputRule = ESLintUtils.RuleCreator.withoutDocs({ |
| 6 | + create(context) { |
| 7 | + return { |
| 8 | + 'CallExpression[callee.type=MemberExpression][callee.property.name="prepare"][arguments.length=1]'( |
| 9 | + node: Omit<TSESTree.CallExpression, "arguments" | "callee"> & { |
| 10 | + arguments: [TSESTree.CallExpression["arguments"][0]]; |
| 11 | + callee: TSESTree.MemberExpression; |
| 12 | + }, |
| 13 | + ) { |
| 14 | + const val = getQueryValue( |
| 15 | + node.arguments[0], |
| 16 | + context.sourceCode.getScope(node.arguments[0]), |
| 17 | + ); |
| 18 | + if (typeof val?.value !== "string") { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const queryInput = inferQueryInput(val.value); |
| 23 | + if (queryInput == null) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const typeArguments = node.typeArguments; |
| 28 | + const inputParam = typeArguments?.params[0]; |
| 29 | + if (!typeArguments || !inputParam) { |
| 30 | + context.report({ |
| 31 | + node: node, |
| 32 | + messageId: "missingInputType", |
| 33 | + *fix(fixer) { |
| 34 | + if (typeArguments && !inputParam) { |
| 35 | + yield fixer.replaceText( |
| 36 | + typeArguments, |
| 37 | + `<${queryInputToText(queryInput)}>`, |
| 38 | + ); |
| 39 | + } else { |
| 40 | + yield fixer.insertTextAfter( |
| 41 | + node.callee, |
| 42 | + `<${queryInputToText(queryInput)}>`, |
| 43 | + ); |
86 | 44 | }
|
| 45 | + }, |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (isDeclaredTypeCorrect(queryInput, inputParam)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const members = |
| 55 | + inputParam.type === TSESTree.AST_NODE_TYPES.TSTypeLiteral |
| 56 | + ? inputParam.members |
| 57 | + : inputParam.type === TSESTree.AST_NODE_TYPES.TSTupleType |
| 58 | + ? inputParam.elementTypes.find( |
| 59 | + (element) => |
| 60 | + element.type === TSESTree.AST_NODE_TYPES.TSTypeLiteral, |
| 61 | + )?.members |
| 62 | + : null; |
| 63 | + |
| 64 | + const userDeclaredTypes = new Map<string, string>(); |
| 65 | + |
| 66 | + if (members) { |
| 67 | + for (const member of members) { |
| 68 | + if (member.type !== TSESTree.AST_NODE_TYPES.TSPropertySignature) { |
| 69 | + continue; |
| 70 | + } |
87 | 71 |
|
88 |
| - const name = |
89 |
| - member.key.type === TSESTree.AST_NODE_TYPES.Identifier |
90 |
| - ? member.key.name |
91 |
| - : ASTUtils.getStringIfConstant(member.key); |
| 72 | + if (!member.typeAnnotation) { |
| 73 | + continue; |
| 74 | + } |
92 | 75 |
|
93 |
| - if (!name) { |
94 |
| - continue; |
95 |
| - } |
| 76 | + const name = |
| 77 | + member.key.type === TSESTree.AST_NODE_TYPES.Identifier |
| 78 | + ? member.key.name |
| 79 | + : ASTUtils.getStringIfConstant(member.key); |
96 | 80 |
|
97 |
| - const type = context.sourceCode.getText(member.typeAnnotation); |
98 |
| - userDeclaredTypes.set(name, type); |
| 81 | + if (!name) { |
| 82 | + continue; |
99 | 83 | }
|
100 |
| - } |
101 | 84 |
|
102 |
| - context.report({ |
103 |
| - node: inputParam, |
104 |
| - messageId: "incorrectInputType", |
105 |
| - *fix(fixer) { |
106 |
| - yield fixer.replaceText( |
107 |
| - inputParam, |
108 |
| - queryInputToText(queryInput, userDeclaredTypes), |
109 |
| - ); |
110 |
| - }, |
111 |
| - }); |
112 |
| - }, |
113 |
| - }; |
114 |
| - }, |
115 |
| - meta: { |
116 |
| - messages: { |
117 |
| - missingInputType: "Missing input type for query", |
118 |
| - incorrectInputType: "Incorrect input type for query", |
| 85 | + const type = context.sourceCode.getText(member.typeAnnotation); |
| 86 | + userDeclaredTypes.set(name, type); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + context.report({ |
| 91 | + node: inputParam, |
| 92 | + messageId: "incorrectInputType", |
| 93 | + *fix(fixer) { |
| 94 | + yield fixer.replaceText( |
| 95 | + inputParam, |
| 96 | + queryInputToText(queryInput, userDeclaredTypes), |
| 97 | + ); |
| 98 | + }, |
| 99 | + }); |
119 | 100 | },
|
120 |
| - schema: [], |
121 |
| - type: "suggestion", |
122 |
| - fixable: "code", |
| 101 | + }; |
| 102 | + }, |
| 103 | + meta: { |
| 104 | + messages: { |
| 105 | + missingInputType: "Missing input type for query", |
| 106 | + incorrectInputType: "Incorrect input type for query", |
123 | 107 | },
|
124 |
| - defaultOptions: [], |
125 |
| - }); |
126 |
| -} |
| 108 | + schema: [], |
| 109 | + type: "suggestion", |
| 110 | + fixable: "code", |
| 111 | + }, |
| 112 | + defaultOptions: [], |
| 113 | +}); |
127 | 114 |
|
128 | 115 | function queryInputToText(
|
129 | 116 | queryInput: QueryInput,
|
|
0 commit comments