|
| 1 | +import { SyntaxKind } from "typescript"; |
| 2 | +import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils"; |
| 3 | +import { getEsTreeNodeToTSNodeMap, createRule } from "./utils"; |
| 4 | + |
| 5 | +export = createRule({ |
| 6 | + name: "boolean-trivia", |
| 7 | + meta: { |
| 8 | + docs: { |
| 9 | + description: ``, |
| 10 | + category: "Best Practices", |
| 11 | + recommended: "error", |
| 12 | + }, |
| 13 | + messages: { |
| 14 | + booleanTriviaArgumentError: `Tag argument with parameter name`, |
| 15 | + booleanTriviaArgumentSpaceError: `There should be 1 space between an argument and its comment`, |
| 16 | + }, |
| 17 | + schema: [], |
| 18 | + type: "problem", |
| 19 | + }, |
| 20 | + defaultOptions: [], |
| 21 | + |
| 22 | + create(context) { |
| 23 | + const esTreeNodeToTSNodeMap = getEsTreeNodeToTSNodeMap(context.parserServices); |
| 24 | + const sourceCode = context.getSourceCode(); |
| 25 | + const sourceCodeText = sourceCode.getText(); |
| 26 | + |
| 27 | + const isSetOrAssert = (name: string): boolean => name.startsWith("set") || name.startsWith("assert"); |
| 28 | + const isTrivia = (node: TSESTree.Expression): boolean => { |
| 29 | + const tsNode = esTreeNodeToTSNodeMap.get(node); |
| 30 | + |
| 31 | + if (tsNode.kind === SyntaxKind.Identifier) { |
| 32 | + return tsNode.originalKeywordKind === SyntaxKind.UndefinedKeyword; |
| 33 | + } |
| 34 | + |
| 35 | + return [SyntaxKind.TrueKeyword, SyntaxKind.FalseKeyword, SyntaxKind.NullKeyword].indexOf(tsNode.kind) >= 0; |
| 36 | + }; |
| 37 | + |
| 38 | + const shouldIgnoreCalledExpression = (node: TSESTree.CallExpression): boolean => { |
| 39 | + if (node.callee && node.callee.type === AST_NODE_TYPES.MemberExpression) { |
| 40 | + const methodName = node.callee.property.type === AST_NODE_TYPES.Identifier |
| 41 | + ? node.callee.property.name |
| 42 | + : ""; |
| 43 | + |
| 44 | + if (isSetOrAssert(methodName)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + return ["apply", "call", "equal", "fail", "isTrue", "output", "stringify", "push"].indexOf(methodName) >= 0; |
| 49 | + } |
| 50 | + |
| 51 | + if (node.callee && node.callee.type === AST_NODE_TYPES.Identifier) { |
| 52 | + const functionName = node.callee.name; |
| 53 | + |
| 54 | + if (isSetOrAssert(functionName)) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + return [ |
| 59 | + "createImportSpecifier", |
| 60 | + "createAnonymousType", |
| 61 | + "createSignature", |
| 62 | + "createProperty", |
| 63 | + "resolveName", |
| 64 | + "contains", |
| 65 | + ].indexOf(functionName) >= 0; |
| 66 | + } |
| 67 | + |
| 68 | + return false; |
| 69 | + }; |
| 70 | + |
| 71 | + const checkArg = (node: TSESTree.Expression): void => { |
| 72 | + if (!isTrivia(node)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const comments = sourceCode.getCommentsBefore(node); |
| 77 | + if (!comments || comments.length !== 1 || comments[0].type !== "Block") { |
| 78 | + context.report({ messageId: "booleanTriviaArgumentError", node }); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + const argRangeStart = node.range[0]; |
| 83 | + const commentRangeEnd = comments[0].range[1]; |
| 84 | + const hasNewLine = sourceCodeText.slice(commentRangeEnd, argRangeStart).indexOf("\n") >= 0; |
| 85 | + |
| 86 | + if (argRangeStart !== commentRangeEnd + 1 && !hasNewLine) { |
| 87 | + context.report({ messageId: "booleanTriviaArgumentSpaceError", node }); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const checkBooleanTrivia = (node: TSESTree.CallExpression) => { |
| 92 | + if (shouldIgnoreCalledExpression(node)) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + for (const arg of node.arguments) { |
| 97 | + checkArg(arg); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + return { |
| 102 | + CallExpression: checkBooleanTrivia, |
| 103 | + }; |
| 104 | + }, |
| 105 | +}); |
0 commit comments