UNPKG

next

Version:

The React Framework

81 lines (80 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = NoAnonymousDefaultExport; var _chalk = _interopRequireDefault(require("chalk")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function NoAnonymousDefaultExport({ types: t , ...babel }) { let onWarning = null; babel.caller((caller)=>{ onWarning = caller.onWarning; return '' // Intentionally empty to not invalidate cache ; }); if (typeof onWarning !== 'function') { return { visitor: { } }; } const warn = onWarning; return { visitor: { ExportDefaultDeclaration (path) { const def = path.node.declaration; if (!(def.type === 'ArrowFunctionExpression' || def.type === 'FunctionDeclaration')) { return; } switch(def.type){ case 'ArrowFunctionExpression': { warn([ _chalk.default.yellow.bold('Anonymous arrow functions cause Fast Refresh to not preserve local component state.'), 'Please add a name to your function, for example:', '', _chalk.default.bold('Before'), _chalk.default.cyan('export default () => <div />;'), '', _chalk.default.bold('After'), _chalk.default.cyan('const Named = () => <div />;'), _chalk.default.cyan('export default Named;'), '', `A codemod is available to fix the most common cases: ${_chalk.default.cyan('https://nextjs.link/codemod-ndc')}`, ].join('\n')); break; } case 'FunctionDeclaration': { const isAnonymous = !Boolean(def.id); if (isAnonymous) { warn([ _chalk.default.yellow.bold('Anonymous function declarations cause Fast Refresh to not preserve local component state.'), 'Please add a name to your function, for example:', '', _chalk.default.bold('Before'), _chalk.default.cyan('export default function () { /* ... */ }'), '', _chalk.default.bold('After'), _chalk.default.cyan('export default function Named() { /* ... */ }'), '', `A codemod is available to fix the most common cases: ${_chalk.default.cyan('https://nextjs.link/codemod-ndc')}`, ].join('\n')); } break; } default: { // eslint-disable-next-line @typescript-eslint/no-unused-vars const _ = def; } } } } }; } //# sourceMappingURL=no-anonymous-default-export.js.map