|
| 1 | +import { TSESTree } from '@typescript-eslint/typescript-estree'; |
| 2 | +import { isOpeningParenToken } from 'eslint-utils'; |
| 3 | +import * as util from '../util'; |
| 4 | + |
| 5 | +export type Options = [ |
| 6 | + 'never' | 'always', |
| 7 | + { |
| 8 | + allowNewlines?: boolean; |
| 9 | + }? |
| 10 | +]; |
| 11 | +export type MessageIds = 'unexpected' | 'missing'; |
| 12 | + |
| 13 | +export default util.createRule<Options, MessageIds>({ |
| 14 | + name: 'func-call-spacing', |
| 15 | + meta: { |
| 16 | + type: 'layout', |
| 17 | + docs: { |
| 18 | + description: |
| 19 | + 'require or disallow spacing between function identifiers and their invocations', |
| 20 | + category: 'Stylistic Issues', |
| 21 | + recommended: false, |
| 22 | + }, |
| 23 | + fixable: 'whitespace', |
| 24 | + schema: { |
| 25 | + anyOf: [ |
| 26 | + { |
| 27 | + type: 'array', |
| 28 | + items: [ |
| 29 | + { |
| 30 | + enum: ['never'], |
| 31 | + }, |
| 32 | + ], |
| 33 | + minItems: 0, |
| 34 | + maxItems: 1, |
| 35 | + }, |
| 36 | + { |
| 37 | + type: 'array', |
| 38 | + items: [ |
| 39 | + { |
| 40 | + enum: ['always'], |
| 41 | + }, |
| 42 | + { |
| 43 | + type: 'object', |
| 44 | + properties: { |
| 45 | + allowNewlines: { |
| 46 | + type: 'boolean', |
| 47 | + }, |
| 48 | + }, |
| 49 | + additionalProperties: false, |
| 50 | + }, |
| 51 | + ], |
| 52 | + minItems: 0, |
| 53 | + maxItems: 2, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + |
| 58 | + messages: { |
| 59 | + unexpected: |
| 60 | + 'Unexpected space or newline between function name and paren.', |
| 61 | + missing: 'Missing space between function name and paren.', |
| 62 | + }, |
| 63 | + }, |
| 64 | + defaultOptions: ['never', {}], |
| 65 | + create(context, [option, config]) { |
| 66 | + const sourceCode = context.getSourceCode(); |
| 67 | + const text = sourceCode.getText(); |
| 68 | + |
| 69 | + /** |
| 70 | + * Check if open space is present in a function name |
| 71 | + * @param {ASTNode} node node to evaluate |
| 72 | + * @returns {void} |
| 73 | + * @private |
| 74 | + */ |
| 75 | + function checkSpacing( |
| 76 | + node: TSESTree.CallExpression | TSESTree.NewExpression, |
| 77 | + ): void { |
| 78 | + const closingParenToken = sourceCode.getLastToken(node)!; |
| 79 | + const lastCalleeTokenWithoutPossibleParens = sourceCode.getLastToken( |
| 80 | + node.typeParameters || node.callee, |
| 81 | + )!; |
| 82 | + const openingParenToken = sourceCode.getFirstTokenBetween( |
| 83 | + lastCalleeTokenWithoutPossibleParens, |
| 84 | + closingParenToken, |
| 85 | + isOpeningParenToken, |
| 86 | + ); |
| 87 | + if (!openingParenToken || openingParenToken.range[1] >= node.range[1]) { |
| 88 | + // new expression with no parens... |
| 89 | + return; |
| 90 | + } |
| 91 | + const lastCalleeToken = sourceCode.getTokenBefore(openingParenToken)!; |
| 92 | + |
| 93 | + const textBetweenTokens = text |
| 94 | + .slice(lastCalleeToken.range[1], openingParenToken.range[0]) |
| 95 | + .replace(/\/\*.*?\*\//gu, ''); |
| 96 | + const hasWhitespace = /\s/u.test(textBetweenTokens); |
| 97 | + const hasNewline = |
| 98 | + hasWhitespace && util.LINEBREAK_MATCHER.test(textBetweenTokens); |
| 99 | + |
| 100 | + if (option === 'never') { |
| 101 | + if (hasWhitespace) { |
| 102 | + return context.report({ |
| 103 | + node, |
| 104 | + loc: lastCalleeToken.loc.start, |
| 105 | + messageId: 'unexpected', |
| 106 | + fix(fixer) { |
| 107 | + /* |
| 108 | + * Only autofix if there is no newline |
| 109 | + * https://github.com/eslint/eslint/issues/7787 |
| 110 | + */ |
| 111 | + if (!hasNewline) { |
| 112 | + return fixer.removeRange([ |
| 113 | + lastCalleeToken.range[1], |
| 114 | + openingParenToken.range[0], |
| 115 | + ]); |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + }, |
| 120 | + }); |
| 121 | + } |
| 122 | + } else { |
| 123 | + if (!hasWhitespace) { |
| 124 | + context.report({ |
| 125 | + node, |
| 126 | + loc: lastCalleeToken.loc.start, |
| 127 | + messageId: 'missing', |
| 128 | + fix(fixer) { |
| 129 | + return fixer.insertTextBefore(openingParenToken, ' '); |
| 130 | + }, |
| 131 | + }); |
| 132 | + } else if (!config!.allowNewlines && hasNewline) { |
| 133 | + context.report({ |
| 134 | + node, |
| 135 | + loc: lastCalleeToken.loc.start, |
| 136 | + messageId: 'unexpected', |
| 137 | + fix(fixer) { |
| 138 | + return fixer.replaceTextRange( |
| 139 | + [lastCalleeToken.range[1], openingParenToken.range[0]], |
| 140 | + ' ', |
| 141 | + ); |
| 142 | + }, |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return { |
| 149 | + CallExpression: checkSpacing, |
| 150 | + NewExpression: checkSpacing, |
| 151 | + }; |
| 152 | + }, |
| 153 | +}); |
0 commit comments