116 lines
12 KiB
JavaScript
116 lines
12 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
||
|
|
||
|
var SINGLE_EXPORT_ERROR_MESSAGE = 'Prefer default export on a file with single export.';
|
||
|
var ANY_EXPORT_ERROR_MESSAGE = 'Prefer default export to be present on every file that has export.';
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
category: 'Style guide',
|
||
|
description: 'Prefer a default export if module exports a single name or multiple names.',
|
||
|
url: (0, _docsUrl2['default'])('prefer-default-export') },
|
||
|
|
||
|
schema: [{
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
target: {
|
||
|
type: 'string',
|
||
|
'enum': ['single', 'any'],
|
||
|
'default': 'single' } },
|
||
|
|
||
|
|
||
|
additionalProperties: false }] },
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function create(context) {
|
||
|
var specifierExportCount = 0;
|
||
|
var hasDefaultExport = false;
|
||
|
var hasStarExport = false;
|
||
|
var hasTypeExport = false;
|
||
|
var namedExportNode = null;
|
||
|
// get options. by default we look into files with single export
|
||
|
var _ref = context.options[0] || {},_ref$target = _ref.target,target = _ref$target === undefined ? 'single' : _ref$target;
|
||
|
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 (hasDefaultExport || hasStarExport || hasTypeExport) {
|
||
|
return;
|
||
|
}
|
||
|
if (target === 'single' && specifierExportCount === 1) {
|
||
|
context.report(namedExportNode, SINGLE_EXPORT_ERROR_MESSAGE);
|
||
|
} else if (target === 'any' && specifierExportCount > 0) {
|
||
|
context.report(namedExportNode, ANY_EXPORT_ERROR_MESSAGE);
|
||
|
}
|
||
|
}return ProgramExit;}() };
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsiU0lOR0xFX0VYUE9SVF9FUlJPUl9NRVNTQUdFIiwiQU5ZX0VYUE9SVF9FUlJPUl9NRVNTQUdFIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwidGFyZ2V0IiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJjcmVhdGUiLCJjb250ZXh0Iiwic3BlY2lmaWVyRXhwb3J0Q291bnQiLCJoYXNEZWZhdWx0RXhwb3J0IiwiaGFzU3RhckV4cG9ydCIsImhhc1R5cGVFeHBvcnQiLCJuYW1lZEV4cG9ydE5vZGUiLCJvcHRpb25zIiwiY2FwdHVyZURlY2xhcmF0aW9uIiwiaWRlbnRpZmllck9yUGF0dGVybiIsImZvckVhY2giLCJwcm9wZXJ0eSIsInZhbHVlIiwiZWxlbWVudHMiLCJFeHBvcnREZWZhdWx0U3BlY2lmaWVyIiwiRXhwb3J0U3BlY2lmaWVyIiwibm9kZSIsImV4cG9ydGVkIiwibmFtZSIsIkV4cG9ydE5hbWVkRGVjbGFyYXRpb24iLCJkZWNsYXJhdGlvbiIsImRlY2xhcmF0aW9ucyIsImlkIiwiRXhwb3J0RGVmYXVsdERlY2xhcmF0aW9uIiwiRXhwb3J0QWxsRGVjbGFyYXRpb24iLCJyZXBvcnQiXSwibWFwcGluZ3MiOiJBQUFBOztBQUVBLHFDOztBQUVBLElBQU1BL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