forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargument-trivia.cjs
More file actions
206 lines (180 loc) · 7.34 KB
/
argument-trivia.cjs
File metadata and controls
206 lines (180 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const { AST_NODE_TYPES, ESLintUtils } = require("@typescript-eslint/utils");
const { createRule } = require("./utils.cjs");
const ts = require("typescript");
/**
* @import { TSESTree } from "@typescript-eslint/utils"
* @typedef {TSESTree.CallExpression | TSESTree.NewExpression} CallOrNewExpression
*/
void 0;
const unset = Symbol();
/**
* @template T
* @param {() => T} fn
* @returns {() => T}
*/
function memoize(fn) {
/** @type {T | typeof unset} */
let value = unset;
return () => {
if (value === unset) {
value = fn();
}
return value;
};
}
module.exports = createRule({
name: "argument-trivia",
meta: {
docs: {
description: ``,
},
messages: {
argumentTriviaArgumentError: `Tag argument with parameter name`,
argumentTriviaArgumentSpaceError: `There should be 1 space between an argument and its comment`,
argumentTriviaArgumentNameError: `Argument name "{{ got }}" does not match expected name "{{ want }}"`,
},
schema: [],
type: "problem",
fixable: "code",
},
defaultOptions: [],
create(context) {
const sourceCode = context.sourceCode;
const sourceCodeText = sourceCode.getText();
/** @type {(name: string) => boolean} */
const isSetOrAssert = name => name.startsWith("set") || name.startsWith("assert");
/** @type {(node: TSESTree.Node) => boolean} */
const isTrivia = node => {
if (node.type === AST_NODE_TYPES.Identifier) {
return node.name === "undefined";
}
if (node.type === AST_NODE_TYPES.Literal) {
// eslint-disable-next-line no-restricted-syntax
return node.value === null || node.value === true || node.value === false;
}
return false;
};
/** @type {(node: CallOrNewExpression) => boolean} */
const shouldIgnoreCalledExpression = node => {
if (node.callee && node.callee.type === AST_NODE_TYPES.MemberExpression) {
const methodName = node.callee.property.type === AST_NODE_TYPES.Identifier
? node.callee.property.name
: "";
if (isSetOrAssert(methodName)) {
return true;
}
switch (methodName) {
case "apply":
case "call":
case "equal":
case "stringify":
case "push":
return true;
}
return false;
}
if (node.callee && node.callee.type === AST_NODE_TYPES.Identifier) {
const functionName = node.callee.name;
if (isSetOrAssert(functionName)) {
return true;
}
switch (functionName) {
case "contains":
return true;
}
return false;
}
return false;
};
/** @type {(node: TSESTree.Node, i: number, getSignature: () => ts.Signature | undefined) => void} */
const checkArg = (node, i, getSignature) => {
if (!isTrivia(node)) {
return;
}
const getExpectedName = memoize(() => {
const signature = getSignature();
if (signature) {
const expectedName = signature.parameters[i]?.escapedName;
if (expectedName) {
const name = ts.unescapeLeadingUnderscores(expectedName);
// If a parameter is unused, we prepend an underscore. Ignore this
// so that we can switch between used and unused without modifying code,
// requiring that arguments are tagged with the non-underscored name.
return name.startsWith("_") ? name.slice(1) : name;
}
}
return undefined;
});
const comments = sourceCode.getCommentsBefore(node);
/** @type {TSESTree.Comment | undefined} */
const comment = comments[comments.length - 1];
if (!comment || comment.type !== "Block") {
const expectedName = getExpectedName();
if (expectedName) {
context.report({
messageId: "argumentTriviaArgumentError",
node,
fix: fixer => {
return fixer.insertTextBefore(node, `/*${expectedName}*/ `);
},
});
}
else {
context.report({ messageId: "argumentTriviaArgumentError", node });
}
return;
}
const argRangeStart = node.range[0];
const commentRangeEnd = comment.range[1];
const expectedName = getExpectedName();
if (expectedName) {
const got = comment.value;
if (got !== expectedName) {
context.report({
messageId: "argumentTriviaArgumentNameError",
data: { got, want: expectedName },
node: comment,
fix: fixer => {
return fixer.replaceText(comment, `/*${expectedName}*/`);
},
});
return;
}
}
const hasNewLine = sourceCodeText.slice(commentRangeEnd, argRangeStart).includes("\n");
if (argRangeStart !== commentRangeEnd + 1 && !hasNewLine) {
// TODO(jakebailey): range should be whitespace
context.report({
messageId: "argumentTriviaArgumentSpaceError",
node,
fix: fixer => {
return fixer.replaceTextRange([commentRangeEnd, argRangeStart], " ");
},
});
}
};
/** @type {(node: CallOrNewExpression) => void} */
const checkArgumentTrivia = node => {
if (shouldIgnoreCalledExpression(node)) {
return;
}
const getSignature = memoize(() => {
const parserServices = ESLintUtils.getParserServices(context, /*allowWithoutFullTypeInformation*/ true);
if (parserServices.program) {
const checker = parserServices.program.getTypeChecker();
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
return checker.getResolvedSignature(tsNode);
}
return undefined;
});
for (let i = 0; i < node.arguments.length; i++) {
const arg = node.arguments[i];
checkArg(arg, i, getSignature);
}
};
return {
CallExpression: checkArgumentTrivia,
NewExpression: checkArgumentTrivia,
};
},
});