UNPKG

eslint-plugin-prefer-named-export

Version:

<!-- [![npm version](https://img.shields.io/npm/v/eslint-plugin-prefer-named-export.svg)](https://www.npmjs.com/package/eslint-plugin-prefer-named-export) [![Downloads/month](https://img.shields.io/npm/dm/eslint-plugin-prefer-named-export.svg)](http://www

54 lines (53 loc) 2.45 kB
"use strict"; const rule = { defaultOptions: [], meta: { type: 'problem', docs: { description: [ 'This rule enforces the use of named exports instead of default exports.', 'It checks for export statements that use the export default syntax with an identifier as the default export,', 'and suggests exporting the variable directly with a named export instead.', ].join(' '), recommended: 'error', }, messages: { preferNamedExport: 'Export the variable {{variableName}} directly.', }, hasSuggestions: true, fixable: 'code', schema: [], }, create(context) { return { ExportDefaultDeclaration(node) { if (node.declaration.type === 'Identifier') { const variableName = node.declaration.name; const { variables } = context.getScope(); const variable = variables.find((v) => v.name === variableName); const variableNode = variable === null || variable === void 0 ? void 0 : variable.defs[0].node.parent; if ((variableNode === null || variableNode === void 0 ? void 0 : variableNode.type) === 'VariableDeclaration') { const sourceCode = context.getSourceCode(); const parentNode = variableNode === null || variableNode === void 0 ? void 0 : variableNode.parent; const parentNodeSourceCode = sourceCode.getText(parentNode); const isAlreadyBeignExported = parentNodeSourceCode.startsWith('export '); context.report({ node: variableNode, messageId: 'preferNamedExport', data: { variableName }, fix(fixer) { const fixers = [ !isAlreadyBeignExported && fixer.insertTextBefore(variableNode, 'export '), fixer.remove(node), ]; return fixers.filter(Boolean); }, }); } } }, }; }, }; module.exports = rule;