79 lines
10 KiB
JavaScript
79 lines
10 KiB
JavaScript
|
'use strict';var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
|
||
|
var _path = require('path');var _path2 = _interopRequireDefault(_path);
|
||
|
var _pkgUp = require('eslint-module-utils/pkgUp');var _pkgUp2 = _interopRequireDefault(_pkgUp);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
||
|
|
||
|
function getEntryPoint(context) {
|
||
|
var pkgPath = (0, _pkgUp2['default'])({ cwd: context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename() });
|
||
|
try {
|
||
|
return require.resolve(_path2['default'].dirname(pkgPath));
|
||
|
} catch (error) {
|
||
|
// Assume the package has no entrypoint (e.g. CLI packages)
|
||
|
// in which case require.resolve would throw.
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function findScope(context, identifier) {var _context$getSourceCod =
|
||
|
context.getSourceCode(),scopeManager = _context$getSourceCod.scopeManager;
|
||
|
|
||
|
return scopeManager && scopeManager.scopes.slice().reverse().find(function (scope) {return scope.variables.some(function (variable) {return variable.identifiers.some(function (node) {return node.name === identifier;});});});
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'problem',
|
||
|
docs: {
|
||
|
description: 'Disallow import statements with module.exports',
|
||
|
category: 'Best Practices',
|
||
|
recommended: true },
|
||
|
|
||
|
fixable: 'code',
|
||
|
schema: [
|
||
|
{
|
||
|
'type': 'object',
|
||
|
'properties': {
|
||
|
'exceptions': { 'type': 'array' } },
|
||
|
|
||
|
'additionalProperties': false }] },
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function create(context) {
|
||
|
var importDeclarations = [];
|
||
|
var entryPoint = getEntryPoint(context);
|
||
|
var options = context.options[0] || {};
|
||
|
var alreadyReported = false;
|
||
|
|
||
|
function report(node) {
|
||
|
var fileName = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
|
||
|
var isEntryPoint = entryPoint === fileName;
|
||
|
var isIdentifier = node.object.type === 'Identifier';
|
||
|
var hasKeywords = /^(module|exports)$/.test(node.object.name);
|
||
|
var objectScope = hasKeywords && findScope(context, node.object.name);
|
||
|
var hasCJSExportReference = hasKeywords && (!objectScope || objectScope.type === 'module');
|
||
|
var isException = !!options.exceptions && options.exceptions.some(function (glob) {return (0, _minimatch2['default'])(fileName, glob);});
|
||
|
|
||
|
if (isIdentifier && hasCJSExportReference && !isEntryPoint && !isException) {
|
||
|
importDeclarations.forEach(function (importDeclaration) {
|
||
|
context.report({
|
||
|
node: importDeclaration,
|
||
|
message: 'Cannot use import declarations in modules that export using ' + 'CommonJS (module.exports = \'foo\' or exports.bar = \'hi\')' });
|
||
|
|
||
|
|
||
|
});
|
||
|
alreadyReported = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
ImportDeclaration: function () {function ImportDeclaration(node) {
|
||
|
importDeclarations.push(node);
|
||
|
}return ImportDeclaration;}(),
|
||
|
MemberExpression: function () {function MemberExpression(node) {
|
||
|
if (!alreadyReported) {
|
||
|
report(node);
|
||
|
}
|
||
|
}return MemberExpression;}() };
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbXBvcnQtbW9kdWxlLWV4cG9ydHMuanMiXSwibmFtZXMiOlsiZ2V0RW50cnlQb2ludCIsImNvbnRleHQiLCJwa2dQYXRoIiwiY3dkIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwicmVxdWlyZSIsInJlc29sdmUiLCJwYXRoIiwiZGlybmFtZSIsImVycm9yIiwiZmluZFNjb3BlIiwiaWRlbnRpZmllciIsImdldFNvdXJjZUNvZGUiLCJzY29wZU1hbmFnZXIiLCJzY29wZXMiLCJzbGljZSIsInJldmVyc2UiLCJmaW5kIiwic2NvcGUi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