|
| 1 | +/** |
| 2 | + * @fileoverview Use function types instead of interfaces with call signatures |
| 3 | + * @author Benjamin Lichtman |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | +const util = require('../util'); |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {import("eslint").Rule.RuleModule} RuleModule |
| 10 | + * @typedef {import("estree").Node} ESTreeNode |
| 11 | + */ |
| 12 | + |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | +// Rule Definition |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +/** |
| 18 | + * @type {RuleModule} |
| 19 | + */ |
| 20 | +module.exports = { |
| 21 | + meta: { |
| 22 | + docs: { |
| 23 | + description: |
| 24 | + 'Use function types instead of interfaces with call signatures', |
| 25 | + category: 'TypeScript', |
| 26 | + recommended: false, |
| 27 | + extraDescription: [util.tslintRule('prefer-function-type')], |
| 28 | + url: util.metaDocsUrl('prefer-function-type') |
| 29 | + }, |
| 30 | + fixable: 'code', |
| 31 | + messages: { |
| 32 | + functionTypeOverCallableType: |
| 33 | + "{{ type }} has only a call signature - use '{{ sigSuggestion }}' instead." |
| 34 | + }, |
| 35 | + schema: [], |
| 36 | + type: 'suggestion' |
| 37 | + }, |
| 38 | + |
| 39 | + create(context) { |
| 40 | + const sourceCode = context.getSourceCode(); |
| 41 | + |
| 42 | + //---------------------------------------------------------------------- |
| 43 | + // Helpers |
| 44 | + //---------------------------------------------------------------------- |
| 45 | + |
| 46 | + /** |
| 47 | + * Checks if there is no supertype or if the supertype is 'Function' |
| 48 | + * @param {ESTreeNode} node The node being checked |
| 49 | + * @returns {boolean} Returns true iff there is no supertype or if the supertype is 'Function' |
| 50 | + */ |
| 51 | + function noSupertype(node) { |
| 52 | + if (!node.extends || node.extends.length === 0) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + if (node.extends.length !== 1) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + const expr = node.extends[0].expression; |
| 59 | + |
| 60 | + return expr.type === 'Identifier' && expr.name === 'Function'; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param {ESTreeNode} parent The parent of the call signature causing the diagnostic |
| 65 | + * @returns {boolean} true iff the parent node needs to be wrapped for readability |
| 66 | + */ |
| 67 | + function shouldWrapSuggestion(parent) { |
| 68 | + switch (parent.type) { |
| 69 | + case 'TSUnionType': |
| 70 | + case 'TSIntersectionType': |
| 71 | + case 'TSArrayType': |
| 72 | + return true; |
| 73 | + default: |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param {ESTreeNode} call The call signature causing the diagnostic |
| 80 | + * @param {ESTreeNode} parent The parent of the call |
| 81 | + * @returns {string} The suggestion to report |
| 82 | + */ |
| 83 | + function renderSuggestion(call, parent) { |
| 84 | + const start = call.range[0]; |
| 85 | + const colonPos = call.returnType.range[0] - start; |
| 86 | + const text = sourceCode.getText().slice(start, call.range[1]); |
| 87 | + |
| 88 | + let suggestion = `${text.slice(0, colonPos)} =>${text.slice( |
| 89 | + colonPos + 1 |
| 90 | + )}`; |
| 91 | + |
| 92 | + if (shouldWrapSuggestion(parent.parent)) { |
| 93 | + suggestion = `(${suggestion})`; |
| 94 | + } |
| 95 | + if (parent.type === 'TSInterfaceDeclaration') { |
| 96 | + if (typeof parent.typeParameters !== 'undefined') { |
| 97 | + return `type ${sourceCode |
| 98 | + .getText() |
| 99 | + .slice( |
| 100 | + parent.id.range[0], |
| 101 | + parent.typeParameters.range[1] |
| 102 | + )} = ${suggestion}`; |
| 103 | + } |
| 104 | + return `type ${parent.id.name} = ${suggestion}`; |
| 105 | + } |
| 106 | + return suggestion.endsWith(';') ? suggestion.slice(0, -1) : suggestion; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param {ESTreeNode} member The TypeElement being checked |
| 111 | + * @param {ESTreeNode} node The parent of member being checked |
| 112 | + * @returns {void} |
| 113 | + */ |
| 114 | + function checkMember(member, node) { |
| 115 | + if ( |
| 116 | + (member.type === 'TSCallSignatureDeclaration' || |
| 117 | + member.type === 'TSConstructSignatureDeclaration') && |
| 118 | + typeof member.returnType !== 'undefined' |
| 119 | + ) { |
| 120 | + const suggestion = renderSuggestion(member, node); |
| 121 | + const fixStart = |
| 122 | + node.type === 'TSTypeLiteral' |
| 123 | + ? node.range[0] |
| 124 | + : sourceCode |
| 125 | + .getTokens(node) |
| 126 | + .filter( |
| 127 | + token => |
| 128 | + token.type === 'Keyword' && token.value === 'interface' |
| 129 | + )[0].range[0]; |
| 130 | + |
| 131 | + context.report({ |
| 132 | + node: member, |
| 133 | + messageId: 'functionTypeOverCallableType', |
| 134 | + data: { |
| 135 | + type: node.type === 'TSTypeLiteral' ? 'Type literal' : 'Interface', |
| 136 | + sigSuggestion: suggestion |
| 137 | + }, |
| 138 | + fix(fixer) { |
| 139 | + return fixer.replaceTextRange( |
| 140 | + [fixStart, node.range[1]], |
| 141 | + suggestion |
| 142 | + ); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + //---------------------------------------------------------------------- |
| 149 | + // Public |
| 150 | + //---------------------------------------------------------------------- |
| 151 | + |
| 152 | + return { |
| 153 | + /** |
| 154 | + * @param {TSInterfaceDeclaration} node The node being checked |
| 155 | + * @returns {void} |
| 156 | + */ |
| 157 | + TSInterfaceDeclaration(node) { |
| 158 | + if (noSupertype(node) && node.body.body.length === 1) { |
| 159 | + checkMember(node.body.body[0], node); |
| 160 | + } |
| 161 | + }, |
| 162 | + /** |
| 163 | + * @param {TSTypeLiteral} node The node being checked |
| 164 | + * @returns {void} |
| 165 | + */ |
| 166 | + 'TSTypeLiteral[members.length = 1]'(node) { |
| 167 | + checkMember(node.members[0], node); |
| 168 | + } |
| 169 | + }; |
| 170 | + } |
| 171 | +}; |
0 commit comments