Path: blob/main/src/rules/no-anonymous-default-export.js
829 views
/**1* @fileoverview Rule to disallow anonymous default exports.2* @author Duncan Beevers3*/45import docsUrl from '../docsUrl';6import has from 'has';78const defs = {9ArrayExpression: {10option: 'allowArray',11description: 'If `false`, will report default export of an array',12message: 'Assign array to a variable before exporting as module default',13},14ArrowFunctionExpression: {15option: 'allowArrowFunction',16description: 'If `false`, will report default export of an arrow function',17message: 'Assign arrow function to a variable before exporting as module default',18},19CallExpression: {20option: 'allowCallExpression',21description: 'If `false`, will report default export of a function call',22message: 'Assign call result to a variable before exporting as module default',23default: true,24},25ClassDeclaration: {26option: 'allowAnonymousClass',27description: 'If `false`, will report default export of an anonymous class',28message: 'Unexpected default export of anonymous class',29forbid: (node) => !node.declaration.id,30},31FunctionDeclaration: {32option: 'allowAnonymousFunction',33description: 'If `false`, will report default export of an anonymous function',34message: 'Unexpected default export of anonymous function',35forbid: (node) => !node.declaration.id,36},37Literal: {38option: 'allowLiteral',39description: 'If `false`, will report default export of a literal',40message: 'Assign literal to a variable before exporting as module default',41},42ObjectExpression: {43option: 'allowObject',44description: 'If `false`, will report default export of an object expression',45message: 'Assign object to a variable before exporting as module default',46},47TemplateLiteral: {48option: 'allowLiteral',49description: 'If `false`, will report default export of a literal',50message: 'Assign literal to a variable before exporting as module default',51},52};5354const schemaProperties = Object.keys(defs)55.map((key) => defs[key])56.reduce((acc, def) => {57acc[def.option] = {58description: def.description,59type: 'boolean',60};6162return acc;63}, {});6465const defaults = Object.keys(defs)66.map((key) => defs[key])67.reduce((acc, def) => {68acc[def.option] = has(def, 'default') ? def.default : false;69return acc;70}, {});7172module.exports = {73meta: {74type: 'suggestion',75docs: {76url: docsUrl('no-anonymous-default-export'),77},7879schema: [80{81type: 'object',82properties: schemaProperties,83'additionalProperties': false,84},85],86},8788create(context) {89const options = Object.assign({}, defaults, context.options[0]);9091return {92'ExportDefaultDeclaration': (node) => {93const def = defs[node.declaration.type];9495// Recognized node type and allowed by configuration,96// and has no forbid check, or forbid check return value is truthy97if (def && !options[def.option] && (!def.forbid || def.forbid(node))) {98context.report({ node, message: def.message });99}100},101};102},103};104105106