|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('node:path'); |
| 4 | +const { |
| 5 | + getFunctionHeadLocation, |
| 6 | + getFunctionNameWithKind, |
| 7 | + isOpeningParenToken, |
| 8 | +} = require('@eslint-community/eslint-utils'); |
| 9 | +const { |
| 10 | + isIdentifierName, |
| 11 | +} = require('@babel/helper-validator-identifier'); |
| 12 | +const getClassHeadLocation = require('./utils/get-class-head-location.js'); |
| 13 | +const {upperFirst, camelCase} = require('./utils/lodash.js'); |
| 14 | +const {getParenthesizedRange} = require('./utils/parentheses.js'); |
| 15 | +const { |
| 16 | + getScopes, |
| 17 | + avoidCapture, |
| 18 | +} = require('./utils/index.js'); |
| 19 | +const {isMemberExpression} = require('./ast/index.js'); |
| 20 | + |
| 21 | +const MESSAGE_ID_ERROR = 'no-anonymous-default-export/error'; |
| 22 | +const MESSAGE_ID_SUGGESTION = 'no-anonymous-default-export/suggestion'; |
| 23 | +const messages = { |
| 24 | + [MESSAGE_ID_ERROR]: 'The {{description}} should be named.', |
| 25 | + [MESSAGE_ID_SUGGESTION]: 'Name it as `{{name}}`.', |
| 26 | +}; |
| 27 | + |
| 28 | +const isClassKeywordToken = token => token.type === 'Keyword' && token.value === 'class'; |
| 29 | +const isAnonymousClassOrFunction = node => |
| 30 | + ( |
| 31 | + ( |
| 32 | + node.type === 'FunctionDeclaration' |
| 33 | + || node.type === 'FunctionExpression' |
| 34 | + || node.type === 'ClassDeclaration' |
| 35 | + || node.type === 'ClassExpression' |
| 36 | + ) |
| 37 | + && !node.id |
| 38 | + ) |
| 39 | + || node.type === 'ArrowFunctionExpression'; |
| 40 | + |
| 41 | +function getSuggestionName(node, filename, sourceCode) { |
| 42 | + if (filename === '<input>' || filename === '<text>') { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let [name] = path.basename(filename).split('.'); |
| 47 | + name = camelCase(name); |
| 48 | + |
| 49 | + if (!isIdentifierName(name)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + name = node.type === 'ClassDeclaration' ? upperFirst(name) : name; |
| 54 | + name = avoidCapture(name, getScopes(sourceCode.getScope(node))); |
| 55 | + |
| 56 | + return name; |
| 57 | +} |
| 58 | + |
| 59 | +function addName(fixer, node, name, sourceCode) { |
| 60 | + switch (node.type) { |
| 61 | + case 'ClassDeclaration': |
| 62 | + case 'ClassExpression': { |
| 63 | + const lastDecorator = node.decorators?.at(-1); |
| 64 | + const classToken = lastDecorator |
| 65 | + ? sourceCode.getTokenAfter(lastDecorator, isClassKeywordToken) |
| 66 | + : sourceCode.getFirstToken(node, isClassKeywordToken); |
| 67 | + return fixer.insertTextAfter(classToken, ` ${name}`); |
| 68 | + } |
| 69 | + |
| 70 | + case 'FunctionDeclaration': |
| 71 | + case 'FunctionExpression': { |
| 72 | + const openingParenthesisToken = sourceCode.getFirstToken( |
| 73 | + node, |
| 74 | + isOpeningParenToken, |
| 75 | + ); |
| 76 | + return fixer.insertTextBefore( |
| 77 | + openingParenthesisToken, |
| 78 | + `${sourceCode.text.charAt(openingParenthesisToken.range[0] - 1) === ' ' ? '' : ' '}${name} `, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + case 'ArrowFunctionExpression': { |
| 83 | + const [exportDeclarationStart, exportDeclarationEnd] |
| 84 | + = node.parent.type === 'ExportDefaultDeclaration' |
| 85 | + ? node.parent.range |
| 86 | + : node.parent.parent.range; |
| 87 | + const [arrowFunctionStart, arrowFunctionEnd] = getParenthesizedRange(node, sourceCode); |
| 88 | + |
| 89 | + let textBefore = sourceCode.text.slice(exportDeclarationStart, arrowFunctionStart); |
| 90 | + let textAfter = sourceCode.text.slice(arrowFunctionEnd, exportDeclarationEnd); |
| 91 | + |
| 92 | + textBefore = `\n${textBefore}`; |
| 93 | + if (!/\s$/.test(textBefore)) { |
| 94 | + textBefore = `${textBefore} `; |
| 95 | + } |
| 96 | + |
| 97 | + if (!textAfter.endsWith(';')) { |
| 98 | + textAfter = `${textAfter};`; |
| 99 | + } |
| 100 | + |
| 101 | + return [ |
| 102 | + fixer.replaceTextRange( |
| 103 | + [exportDeclarationStart, arrowFunctionStart], |
| 104 | + `const ${name} = `, |
| 105 | + ), |
| 106 | + fixer.replaceTextRange( |
| 107 | + [arrowFunctionEnd, exportDeclarationEnd], |
| 108 | + ';', |
| 109 | + ), |
| 110 | + fixer.insertTextAfterRange( |
| 111 | + [exportDeclarationEnd, exportDeclarationEnd], |
| 112 | + `${textBefore}${name}${textAfter}`, |
| 113 | + ), |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + // No default |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function getProblem(node, context) { |
| 122 | + const {sourceCode, physicalFilename} = context; |
| 123 | + |
| 124 | + const suggestionName = getSuggestionName(node, physicalFilename, sourceCode); |
| 125 | + |
| 126 | + let loc; |
| 127 | + let description; |
| 128 | + if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') { |
| 129 | + loc = getClassHeadLocation(node, sourceCode); |
| 130 | + description = 'class'; |
| 131 | + } else { |
| 132 | + loc = getFunctionHeadLocation(node, sourceCode); |
| 133 | + // [TODO: @fisker]: Ask `@eslint-community/eslint-utils` to expose `getFunctionKind` |
| 134 | + const nameWithKind = getFunctionNameWithKind(node); |
| 135 | + description = nameWithKind.replace(/ '.*?'$/, ''); |
| 136 | + } |
| 137 | + |
| 138 | + const problem = { |
| 139 | + node, |
| 140 | + loc, |
| 141 | + messageId: MESSAGE_ID_ERROR, |
| 142 | + data: { |
| 143 | + description, |
| 144 | + }, |
| 145 | + }; |
| 146 | + |
| 147 | + if (!suggestionName) { |
| 148 | + return problem; |
| 149 | + } |
| 150 | + |
| 151 | + problem.suggest = [ |
| 152 | + { |
| 153 | + messageId: MESSAGE_ID_SUGGESTION, |
| 154 | + data: { |
| 155 | + name: suggestionName, |
| 156 | + }, |
| 157 | + fix: fixer => addName(fixer, node, suggestionName, sourceCode), |
| 158 | + }, |
| 159 | + ]; |
| 160 | + |
| 161 | + return problem; |
| 162 | +} |
| 163 | + |
| 164 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 165 | +const create = context => { |
| 166 | + context.on('ExportDefaultDeclaration', node => { |
| 167 | + if (!isAnonymousClassOrFunction(node.declaration)) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + return getProblem(node.declaration, context); |
| 172 | + }); |
| 173 | + |
| 174 | + context.on('AssignmentExpression', node => { |
| 175 | + if ( |
| 176 | + !isAnonymousClassOrFunction(node.right) |
| 177 | + || !( |
| 178 | + node.parent.type === 'ExpressionStatement' |
| 179 | + && node.parent.expression === node |
| 180 | + ) |
| 181 | + || !( |
| 182 | + isMemberExpression(node.left, { |
| 183 | + object: 'module', |
| 184 | + property: 'exports', |
| 185 | + computed: false, |
| 186 | + optional: false, |
| 187 | + }) |
| 188 | + || ( |
| 189 | + node.left.type === 'Identifier', |
| 190 | + node.left.name === 'exports' |
| 191 | + ) |
| 192 | + ) |
| 193 | + ) { |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + return getProblem(node.right, context); |
| 198 | + }); |
| 199 | +}; |
| 200 | + |
| 201 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 202 | +module.exports = { |
| 203 | + create, |
| 204 | + meta: { |
| 205 | + type: 'suggestion', |
| 206 | + docs: { |
| 207 | + description: 'Disallow anonymous functions and classes as the default export.', |
| 208 | + }, |
| 209 | + hasSuggestions: true, |
| 210 | + messages, |
| 211 | + }, |
| 212 | +}; |
0 commit comments