95 lines
9.5 KiB
JavaScript
95 lines
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJzcGVjaWZpZXJFeHBvcnRDb3VudCIsImhhc0RlZmF1bHRFeHBvcnQiLCJoYXNTdGFyRXhwb3J0IiwiaGFzVHlwZUV4cG9ydCIsIm5hbWVkRXhwb3J0Tm9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