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