forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboolean-trivia.ts
106 lines (87 loc) · 3.66 KB
/
boolean-trivia.ts
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
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils";
import { createRule } from "./utils";
export = createRule({
name: "boolean-trivia",
meta: {
docs: {
description: ``,
category: "Best Practices",
recommended: "error",
},
messages: {
booleanTriviaArgumentError: `Tag argument with parameter name`,
booleanTriviaArgumentSpaceError: `There should be 1 space between an argument and its comment`,
},
schema: [],
type: "problem",
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();
const sourceCodeText = sourceCode.getText();
const isSetOrAssert = (name: string): boolean => name.startsWith("set") || name.startsWith("assert");
const isTrivia = (node: TSESTree.Expression): boolean => {
if (node.type === AST_NODE_TYPES.Identifier) {
return node.name === "undefined";
}
if (node.type === AST_NODE_TYPES.Literal) {
// eslint-disable-next-line no-null/no-null
return node.value === null || node.value === true || node.value === false;
}
return false;
};
const shouldIgnoreCalledExpression = (node: TSESTree.CallExpression): boolean => {
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;
}
return ["apply", "call", "equal", "fail", "isTrue", "output", "stringify", "push"].indexOf(methodName) >= 0;
}
if (node.callee && node.callee.type === AST_NODE_TYPES.Identifier) {
const functionName = node.callee.name;
if (isSetOrAssert(functionName)) {
return true;
}
return [
"createImportSpecifier",
"createAnonymousType",
"createSignature",
"createProperty",
"resolveName",
"contains",
].indexOf(functionName) >= 0;
}
return false;
};
const checkArg = (node: TSESTree.Expression): void => {
if (!isTrivia(node)) {
return;
}
const comments = sourceCode.getCommentsBefore(node);
if (!comments || comments.length !== 1 || comments[0].type !== "Block") {
context.report({ messageId: "booleanTriviaArgumentError", node });
return;
}
const argRangeStart = node.range[0];
const commentRangeEnd = comments[0].range[1];
const hasNewLine = sourceCodeText.slice(commentRangeEnd, argRangeStart).indexOf("\n") >= 0;
if (argRangeStart !== commentRangeEnd + 1 && !hasNewLine) {
context.report({ messageId: "booleanTriviaArgumentSpaceError", node });
}
};
const checkBooleanTrivia = (node: TSESTree.CallExpression) => {
if (shouldIgnoreCalledExpression(node)) {
return;
}
for (const arg of node.arguments) {
checkArg(arg);
}
};
return {
CallExpression: checkBooleanTrivia,
};
},
});