79 lines
No EOL
10 KiB
JavaScript
79 lines
No EOL
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbXBvcnQtbW9kdWxlLWV4cG9ydHMuanMiXSwibmFtZXMiOlsiZ2V0RW50cnlQb2ludCIsImNvbnRleHQiLCJwa2dQYXRoIiwiY3dkIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwicmVxdWlyZSIsInJlc29sdmUiLCJwYXRoIiwiZGlybmFtZSIsImVycm9yIiwiZmluZFNjb3BlIiwiaWRlbnRpZmllciIsImdldFNvdXJjZUNvZGUiLCJzY29wZU1hbmFnZXIiLCJzY29wZXMiLCJzbGljZSIsInJldmVyc2UiLCJmaW5kIiwic2NvcGUiLCJ2YXJpYWJsZXMiLCJzb21lIiwidmFyaWFibGUiLCJpZGVudGlmaWVycyIsIm5vZGUiLCJuYW1lIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImRlc2NyaXB0aW9uIiwiY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