securityos/node_modules/eslint-plugin-import/lib/rules/group-exports.js

155 lines
16 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
var _object = require('object.values');var _object2 = _interopRequireDefault(_object);
var _arrayPrototype = require('array.prototype.flat');var _arrayPrototype2 = _interopRequireDefault(_arrayPrototype);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
var meta = {
type: 'suggestion',
docs: {
category: 'Style guide',
description: 'Prefer named exports to be grouped together in a single export declaration',
url: (0, _docsUrl2['default'])('group-exports') } };
/* eslint-disable max-len */
var errors = {
ExportNamedDeclaration: 'Multiple named export declarations; consolidate all named exports into a single export declaration',
AssignmentExpression: 'Multiple CommonJS exports; consolidate all exports into a single assignment to `module.exports`' };
/* eslint-enable max-len */
/**
* Returns an array with names of the properties in the accessor chain for MemberExpression nodes
*
* Example:
*
* `module.exports = {}` => ['module', 'exports']
* `module.exports.property = true` => ['module', 'exports', 'property']
*
* @param {Node} node AST Node (MemberExpression)
* @return {Array} Array with the property names in the chain
* @private
*/
function accessorChain(node) {
var chain = [];
do {
chain.unshift(node.property.name);
if (node.object.type === 'Identifier') {
chain.unshift(node.object.name);
break;
}
node = node.object;
} while (node.type === 'MemberExpression');
return chain;
}
function create(context) {
var nodes = {
modules: {
set: new Set(),
sources: {} },
types: {
set: new Set(),
sources: {} },
commonjs: {
set: new Set() } };
return {
ExportNamedDeclaration: function () {function ExportNamedDeclaration(node) {
var target = node.exportKind === 'type' ? nodes.types : nodes.modules;
if (!node.source) {
target.set.add(node);
} else if (Array.isArray(target.sources[node.source.value])) {
target.sources[node.source.value].push(node);
} else {
target.sources[node.source.value] = [node];
}
}return ExportNamedDeclaration;}(),
AssignmentExpression: function () {function AssignmentExpression(node) {
if (node.left.type !== 'MemberExpression') {
return;
}
var chain = accessorChain(node.left);
// Assignments to module.exports
// Deeper assignments are ignored since they just modify what's already being exported
// (ie. module.exports.exported.prop = true is ignored)
if (chain[0] === 'module' && chain[1] === 'exports' && chain.length <= 3) {
nodes.commonjs.set.add(node);
return;
}
// Assignments to exports (exports.* = *)
if (chain[0] === 'exports' && chain.length === 2) {
nodes.commonjs.set.add(node);
return;
}
}return AssignmentExpression;}(),
'Program:exit': function () {function onExit() {
// Report multiple `export` declarations (ES2015 modules)
if (nodes.modules.set.size > 1) {
nodes.modules.set.forEach(function (node) {
context.report({
node: node,
message: errors[node.type] });
});
}
// Report multiple `aggregated exports` from the same module (ES2015 modules)
(0, _arrayPrototype2['default'])((0, _object2['default'])(nodes.modules.sources).
filter(function (nodesWithSource) {return Array.isArray(nodesWithSource) && nodesWithSource.length > 1;})).
forEach(function (node) {
context.report({
node: node,
message: errors[node.type] });
});
// Report multiple `export type` declarations (FLOW ES2015 modules)
if (nodes.types.set.size > 1) {
nodes.types.set.forEach(function (node) {
context.report({
node: node,
message: errors[node.type] });
});
}
// Report multiple `aggregated type exports` from the same module (FLOW ES2015 modules)
(0, _arrayPrototype2['default'])((0, _object2['default'])(nodes.types.sources).
filter(function (nodesWithSource) {return Array.isArray(nodesWithSource) && nodesWithSource.length > 1;})).
forEach(function (node) {
context.report({
node: node,
message: errors[node.type] });
});
// Report multiple `module.exports` assignments (CommonJS)
if (nodes.commonjs.set.size > 1) {
nodes.commonjs.set.forEach(function (node) {
context.report({
node: node,
message: errors[node.type] });
});
}
}return onExit;}() };
}
module.exports = {
meta: meta,
create: create };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9ncm91cC1leHBvcnRzLmpzIl0sIm5hbWVzIjpbIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJlcnJvcnMiLCJFeHBvcnROYW1lZERlY2xhcmF0aW9uIiwiQXNzaWdubWVudEV4cHJlc3Npb24iLCJhY2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