securityos/node_modules/eslint-plugin-import/lib/rules/prefer-default-export.js

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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsiU0lOR0xFX0VYUE9SVF9FUlJPUl9NRVNTQUdFIiwiQU5ZX0VYUE9SVF9FUlJPUl9NRVNTQUdFIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwidGFyZ2V0IiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJjcmVhdGUiLCJjb250ZXh0Iiwic3BlY2lmaWVyRXhwb3J0Q291bnQiLCJoYXNEZWZhdWx0RXhwb3J0IiwiaGFzU3RhckV4cG9ydCIsImhhc1R5cGVFeHBvcnQiLCJuYW1lZEV4cG9ydE5vZGUiLCJvcHRpb25zIiwiY2FwdHVyZURlY2xhcmF0aW9uIiwiaWRlbnRpZmll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