110 lines
9.0 KiB
JavaScript
110 lines
9.0 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');
|
||
|
|
||
|
var _docsUrl2 = _interopRequireDefault(_docsUrl);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
const meta = {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
url: (0, _docsUrl2.default)('group-exports')
|
||
|
}
|
||
|
/* eslint-disable max-len */
|
||
|
};const 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) {
|
||
|
const 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) {
|
||
|
const nodes = {
|
||
|
modules: new Set(),
|
||
|
commonjs: new Set()
|
||
|
};
|
||
|
|
||
|
return {
|
||
|
ExportNamedDeclaration(node) {
|
||
|
nodes.modules.add(node);
|
||
|
},
|
||
|
|
||
|
AssignmentExpression(node) {
|
||
|
if (node.left.type !== 'MemberExpression') {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const 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.add(node);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Assignments to exports (exports.* = *)
|
||
|
if (chain[0] === 'exports' && chain.length === 2) {
|
||
|
nodes.commonjs.add(node);
|
||
|
return;
|
||
|
}
|
||
|
},
|
||
|
|
||
|
'Program:exit': function onExit() {
|
||
|
// Report multiple `export` declarations (ES2015 modules)
|
||
|
if (nodes.modules.size > 1) {
|
||
|
nodes.modules.forEach(node => {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: errors[node.type]
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Report multiple `module.exports` assignments (CommonJS)
|
||
|
if (nodes.commonjs.size > 1) {
|
||
|
nodes.commonjs.forEach(node => {
|
||
|
context.report({
|
||
|
node,
|
||
|
message: errors[node.type]
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
meta,
|
||
|
create
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9ncm91cC1leHBvcnRzLmpzIl0sIm5hbWVzIjpbIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImVycm9ycyIsIkV4cG9ydE5hbWVkRGVjbGFyYXRpb24iLCJBc3NpZ25tZW50RXhwcmVzc2lvbiIsImFjY2Vzc29yQ2hhaW4iLCJub2RlIiwiY2hhaW4iLCJ1bnNoaWZ0IiwicHJvcGVydHkiLCJuYW1lIiwib2JqZWN0IiwiY3JlYXRlIiwiY29udGV4dCIsIm5vZGVzIiwibW9kdWxlcyIsIlNldCIs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