forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-assert.js
More file actions
63 lines (55 loc) · 2.39 KB
/
debug-assert.js
File metadata and controls
63 lines (55 loc) · 2.39 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
const { AST_NODE_TYPES, TSESTree } = require("@typescript-eslint/utils");
const { createRule } = require("./utils");
module.exports = createRule({
name: "debug-assert",
meta: {
docs: {
description: ``,
recommended: "error",
},
messages: {
secondArgumentDebugAssertError: `Second argument to 'Debug.assert' should be a string literal`,
thirdArgumentDebugAssertError: `Third argument to 'Debug.assert' should be a string literal or arrow function`,
},
schema: [],
type: "problem",
},
defaultOptions: [],
create(context) {
/** @type {(node: TSESTree.Node) => boolean} */
const isArrowFunction = (node) => node.type === AST_NODE_TYPES.ArrowFunctionExpression;
/** @type {(node: TSESTree.Node) => boolean} */
const isStringLiteral = (node) => (
(node.type === AST_NODE_TYPES.Literal && typeof node.value === "string") || node.type === AST_NODE_TYPES.TemplateLiteral
);
/** @type {(node: TSESTree.MemberExpression) => boolean} */
const isDebugAssert = (node) => (
node.object.type === AST_NODE_TYPES.Identifier
&& node.object.name === "Debug"
&& node.property.type === AST_NODE_TYPES.Identifier
&& node.property.name === "assert"
);
/** @type {(node: TSESTree.CallExpression) => void} */
const checkDebugAssert = (node) => {
const args = node.arguments;
const argsLen = args.length;
if (!(node.callee.type === AST_NODE_TYPES.MemberExpression && isDebugAssert(node.callee)) || argsLen < 2) {
return;
}
const message1Node = args[1];
if (message1Node && !isStringLiteral(message1Node)) {
context.report({ messageId: "secondArgumentDebugAssertError", node: message1Node });
}
if (argsLen !== 3) {
return;
}
const message2Node = args[2];
if (message2Node && (!isStringLiteral(message2Node) && !isArrowFunction(message2Node))) {
context.report({ messageId: "thirdArgumentDebugAssertError", node: message2Node });
}
};
return {
CallExpression: checkDebugAssert,
};
},
});