Constnium/node_modules/eslint-plugin-import/lib/rules/prefer-default-export.js
2022-06-23 02:27:43 +02:00

95 lines
No EOL
9.5 KiB
JavaScript

'use strict';
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: (0, _docsUrl2['default'])('prefer-default-export') },
schema: [] },
create: function () {function create(context) {
var specifierExportCount = 0;
var hasDefaultExport = false;
var hasStarExport = false;
var hasTypeExport = false;
var namedExportNode = null;
function captureDeclaration(identifierOrPattern) {
if (identifierOrPattern && identifierOrPattern.type === 'ObjectPattern') {
// recursively capture
identifierOrPattern.properties.
forEach(function (property) {
captureDeclaration(property.value);
});
} else if (identifierOrPattern && identifierOrPattern.type === 'ArrayPattern') {
identifierOrPattern.elements.
forEach(captureDeclaration);
} else {
// assume it's a single standard identifier
specifierExportCount++;
}
}
return {
'ExportDefaultSpecifier': function () {function ExportDefaultSpecifier() {
hasDefaultExport = true;
}return ExportDefaultSpecifier;}(),
'ExportSpecifier': function () {function ExportSpecifier(node) {
if ((node.exported.name || node.exported.value) === 'default') {
hasDefaultExport = true;
} else {
specifierExportCount++;
namedExportNode = node;
}
}return ExportSpecifier;}(),
'ExportNamedDeclaration': function () {function ExportNamedDeclaration(node) {
// if there are specifiers, node.declaration should be null
if (!node.declaration) return;var
type = node.declaration.type;
if (
type === 'TSTypeAliasDeclaration' ||
type === 'TypeAlias' ||
type === 'TSInterfaceDeclaration' ||
type === 'InterfaceDeclaration')
{
specifierExportCount++;
hasTypeExport = true;
return;
}
if (node.declaration.declarations) {
node.declaration.declarations.forEach(function (declaration) {
captureDeclaration(declaration.id);
});
} else {
// captures 'export function foo() {}' syntax
specifierExportCount++;
}
namedExportNode = node;
}return ExportNamedDeclaration;}(),
'ExportDefaultDeclaration': function () {function ExportDefaultDeclaration() {
hasDefaultExport = true;
}return ExportDefaultDeclaration;}(),
'ExportAllDeclaration': function () {function ExportAllDeclaration() {
hasStarExport = true;
}return ExportAllDeclaration;}(),
'Program:exit': function () {function ProgramExit() {
if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport && !hasTypeExport) {
context.report(namedExportNode, 'Prefer default export.');
}
}return ProgramExit;}() };
}return create;}() };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJzcGVjaWZpZXJFeHBvcnRDb3VudCIsImhhc0RlZmF1bHRFeHBvcnQiLCJoYXNTdGFyRXhwb3J0IiwiaGFzVHlwZUV4cG9ydCIsIm5hbWVkRXhwb3J0Tm9kZSIsImNhcHR1cmVEZWNsYXJhdGlvbiIsImlkZW50aWZpZXJPclBhdHRlcm4iLCJwcm9wZXJ0aWVzIiwiZm9yRWFjaCIsInByb3BlcnR5IiwidmFsdWUiLCJlbGVtZW50cyIsIm5vZGUiLCJleHBvcnRlZCIsIm5hbWUiLCJkZWNsYXJhdGlvbiIsImRlY2xhcmF0aW9ucyIsImlkIiwicmVwb3J0Il0sIm1hcHBpbmdzIjoiQUFBQTs7QUFFQSxxQzs7QUFFQUEsT0FBT0MsT0FBUCxHQUFpQjtBQUNmQyxRQUFNO0FBQ0pDLFVBQU0sWUFERjtBQUVKQyxVQUFNO0FBQ0pDLFdBQUssMEJBQVEsdUJBQVIsQ0FERCxFQUZGOztBQUtKQyxZQUFRLEVBTEosRUFEUzs7O0FBU2ZDLFFBVGUsK0JBU1JDLE9BVFEsRUFTQztBQUNkLFVBQUlDLHVCQUF1QixDQUEzQjtBQUNBLFVBQUlDLG1CQUFtQixLQUF2QjtBQUNBLFVBQUlDLGdCQUFn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