91 lines
8.0 KiB
JavaScript
91 lines
8.0 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')
|
|
}
|
|
},
|
|
|
|
create: function (context) {
|
|
let specifierExportCount = 0;
|
|
let hasDefaultExport = false;
|
|
let hasStarExport = false;
|
|
let namedExportNode = null;
|
|
|
|
function captureDeclaration(identifierOrPattern) {
|
|
if (identifierOrPattern.type === 'ObjectPattern') {
|
|
// recursively capture
|
|
identifierOrPattern.properties.forEach(function (property) {
|
|
captureDeclaration(property.value);
|
|
});
|
|
} else {
|
|
// assume it's a single standard identifier
|
|
specifierExportCount++;
|
|
}
|
|
}
|
|
|
|
return {
|
|
'ExportDefaultSpecifier': function () {
|
|
hasDefaultExport = true;
|
|
},
|
|
|
|
'ExportSpecifier': function (node) {
|
|
if (node.exported.name === 'default') {
|
|
hasDefaultExport = true;
|
|
} else {
|
|
specifierExportCount++;
|
|
namedExportNode = node;
|
|
}
|
|
},
|
|
|
|
'ExportNamedDeclaration': function (node) {
|
|
// if there are specifiers, node.declaration should be null
|
|
if (!node.declaration) return;
|
|
|
|
// don't warn on single type aliases, declarations, or interfaces
|
|
if (node.exportKind === 'type') return;
|
|
|
|
const type = node.declaration.type;
|
|
|
|
|
|
if (type === 'TSTypeAliasDeclaration' || type === 'TypeAlias' || type === 'TSInterfaceDeclaration' || type === 'InterfaceDeclaration') {
|
|
return;
|
|
}
|
|
|
|
if (node.declaration.declarations) {
|
|
node.declaration.declarations.forEach(function (declaration) {
|
|
captureDeclaration(declaration.id);
|
|
});
|
|
} else {
|
|
// captures 'export function foo() {}' syntax
|
|
specifierExportCount++;
|
|
}
|
|
|
|
namedExportNode = node;
|
|
},
|
|
|
|
'ExportDefaultDeclaration': function () {
|
|
hasDefaultExport = true;
|
|
},
|
|
|
|
'ExportAllDeclaration': function () {
|
|
hasStarExport = true;
|
|
},
|
|
|
|
'Program:exit': function () {
|
|
if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport) {
|
|
context.report(namedExportNode, 'Prefer default export.');
|
|
}
|
|
}
|
|
};
|
|
}
|
|
};
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImNyZWF0ZSIsImNvbnRleHQiLCJzcGVjaWZpZXJFeHBvcnRDb3VudCIsImhhc0RlZmF1bHRFeHBvcnQiLCJoYXNTdGFyRXhwb3J0IiwibmFtZWRFeHBvcnROb2RlIiwiY2FwdHVyZURlY2xhcmF0aW9uIiwiaWRlbnRpZmllck9yUGF0dGVybiIsInByb3BlcnRpZXMiLCJmb3JFYWNoIiwicHJvcGVydHkiLCJ2YWx1ZSIsIm5vZGUiLCJleHBvcnRlZCIsIm5hbWUiLCJkZWNsYXJhdGlvbiIsImV4cG9ydEtpbmQiLCJkZWNsYXJhdGlvbnMiLCJpZCIsInJlcG9ydCJdLCJtYXBwaW5ncyI6IkFBQUE7O0FBRUE7Ozs7OztBQUVBQSxPQUFPQyxPQUFQLEdBQWlCO0FBQ2ZDLFFBQU07QUFDSkMsVUFBTSxZQURGO0FBRUpDLFVBQU07QUFDSkMsV0FBSyx1QkFBUSx1QkFBUjtBQUREO0FBRkYsR0FEUzs7QUFRZkMsVUFBUSxVQUFTQyxPQUFULEVBQWtCO0FBQ3hCLFFBQUlDLHVCQUF1QixDQUEzQjtBQUNBLFFBQUlDLG1CQUFtQixLQUF2QjtBQUNBLFFBQUlDLGdCQUFnQ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