|
| 1 | +import { |
| 2 | + TSESTree, |
| 3 | + AST_NODE_TYPES, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
| 5 | +import { isOpeningParenToken } from 'eslint-utils'; |
| 6 | +import * as util from '../util'; |
| 7 | + |
| 8 | +type Option = 'never' | 'always'; |
| 9 | +type FuncOption = Option | 'ignore'; |
| 10 | + |
| 11 | +export type Options = [ |
| 12 | + |
| 13 | + | Option |
| 14 | + | Partial<{ |
| 15 | + anonymous: FuncOption; |
| 16 | + named: FuncOption; |
| 17 | + asyncArrow: FuncOption; |
| 18 | + }>, |
| 19 | +]; |
| 20 | +export type MessageIds = 'unexpected' | 'missing'; |
| 21 | + |
| 22 | +export default util.createRule<Options, MessageIds>({ |
| 23 | + name: 'space-before-function-paren', |
| 24 | + meta: { |
| 25 | + type: 'layout', |
| 26 | + docs: { |
| 27 | + description: |
| 28 | + 'enforce consistent spacing before `function` definition opening parenthesis', |
| 29 | + category: 'Stylistic Issues', |
| 30 | + recommended: false, |
| 31 | + }, |
| 32 | + fixable: 'whitespace', |
| 33 | + schema: [ |
| 34 | + { |
| 35 | + oneOf: [ |
| 36 | + { |
| 37 | + enum: ['always', 'never'], |
| 38 | + }, |
| 39 | + { |
| 40 | + type: 'object', |
| 41 | + properties: { |
| 42 | + anonymous: { |
| 43 | + enum: ['always', 'never', 'ignore'], |
| 44 | + }, |
| 45 | + named: { |
| 46 | + enum: ['always', 'never', 'ignore'], |
| 47 | + }, |
| 48 | + asyncArrow: { |
| 49 | + enum: ['always', 'never', 'ignore'], |
| 50 | + }, |
| 51 | + }, |
| 52 | + additionalProperties: false, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | + messages: { |
| 58 | + unexpected: 'Unexpected space before function parentheses.', |
| 59 | + missing: 'Missing space before function parentheses.', |
| 60 | + }, |
| 61 | + }, |
| 62 | + defaultOptions: ['always'], |
| 63 | + |
| 64 | + create(context) { |
| 65 | + const sourceCode = context.getSourceCode(); |
| 66 | + const baseConfig = |
| 67 | + typeof context.options[0] === 'string' ? context.options[0] : 'always'; |
| 68 | + const overrideConfig = |
| 69 | + typeof context.options[0] === 'object' ? context.options[0] : {}; |
| 70 | + |
| 71 | + /** |
| 72 | + * Determines whether a function has a name. |
| 73 | + * @param {ASTNode} node The function node. |
| 74 | + * @returns {boolean} Whether the function has a name. |
| 75 | + */ |
| 76 | + function isNamedFunction( |
| 77 | + node: |
| 78 | + | TSESTree.ArrowFunctionExpression |
| 79 | + | TSESTree.FunctionDeclaration |
| 80 | + | TSESTree.FunctionExpression, |
| 81 | + ): boolean { |
| 82 | + if (node.id) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + const parent = node.parent!; |
| 87 | + |
| 88 | + return ( |
| 89 | + parent.type === 'MethodDefinition' || |
| 90 | + (parent.type === 'Property' && |
| 91 | + (parent.kind === 'get' || parent.kind === 'set' || parent.method)) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the config for a given function |
| 97 | + * @param {ASTNode} node The function node |
| 98 | + * @returns {string} "always", "never", or "ignore" |
| 99 | + */ |
| 100 | + function getConfigForFunction( |
| 101 | + node: |
| 102 | + | TSESTree.ArrowFunctionExpression |
| 103 | + | TSESTree.FunctionDeclaration |
| 104 | + | TSESTree.FunctionExpression, |
| 105 | + ): FuncOption { |
| 106 | + if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) { |
| 107 | + // Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar |
| 108 | + if ( |
| 109 | + node.async && |
| 110 | + isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!) |
| 111 | + ) { |
| 112 | + return overrideConfig.asyncArrow || baseConfig; |
| 113 | + } |
| 114 | + } else if (isNamedFunction(node)) { |
| 115 | + return overrideConfig.named || baseConfig; |
| 116 | + |
| 117 | + // `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}` |
| 118 | + } else if (!node.generator) { |
| 119 | + return overrideConfig.anonymous || baseConfig; |
| 120 | + } |
| 121 | + |
| 122 | + return 'ignore'; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Checks the parens of a function node |
| 127 | + * @param {ASTNode} node A function node |
| 128 | + * @returns {void} |
| 129 | + */ |
| 130 | + function checkFunction( |
| 131 | + node: |
| 132 | + | TSESTree.ArrowFunctionExpression |
| 133 | + | TSESTree.FunctionDeclaration |
| 134 | + | TSESTree.FunctionExpression, |
| 135 | + ): void { |
| 136 | + const functionConfig = getConfigForFunction(node); |
| 137 | + |
| 138 | + if (functionConfig === 'ignore') { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + let leftToken: TSESTree.Token, rightToken: TSESTree.Token; |
| 143 | + if (node.typeParameters) { |
| 144 | + leftToken = sourceCode.getLastToken(node.typeParameters)!; |
| 145 | + rightToken = sourceCode.getTokenAfter(leftToken)!; |
| 146 | + } else { |
| 147 | + rightToken = sourceCode.getFirstToken(node, isOpeningParenToken)!; |
| 148 | + leftToken = sourceCode.getTokenBefore(rightToken)!; |
| 149 | + } |
| 150 | + const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken); |
| 151 | + |
| 152 | + if (hasSpacing && functionConfig === 'never') { |
| 153 | + context.report({ |
| 154 | + node, |
| 155 | + loc: leftToken.loc.end, |
| 156 | + messageId: 'unexpected', |
| 157 | + fix: fixer => |
| 158 | + fixer.removeRange([leftToken.range[1], rightToken.range[0]]), |
| 159 | + }); |
| 160 | + } else if ( |
| 161 | + !hasSpacing && |
| 162 | + functionConfig === 'always' && |
| 163 | + (!node.typeParameters || node.id) |
| 164 | + ) { |
| 165 | + context.report({ |
| 166 | + node, |
| 167 | + loc: leftToken.loc.end, |
| 168 | + messageId: 'missing', |
| 169 | + fix: fixer => fixer.insertTextAfter(leftToken, ' '), |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return { |
| 175 | + ArrowFunctionExpression: checkFunction, |
| 176 | + FunctionDeclaration: checkFunction, |
| 177 | + FunctionExpression: checkFunction, |
| 178 | + }; |
| 179 | + }, |
| 180 | +}); |
0 commit comments