odin-js-fundamentals-part-5/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js

85 lines
12 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;});});});
}
function findDefinition(objectScope, identifier) {
var variable = objectScope.variables.find(function (variable) {return variable.name === identifier;});
return variable.defs.find(function (def) {return def.name.name === identifier;});
}
module.exports = {
meta: {
type: 'problem',
docs: {
category: 'Module systems',
description: 'Forbid import statements with CommonJS module.exports.',
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 variableDefinition = objectScope && findDefinition(objectScope, node.object.name);
var isImportBinding = variableDefinition && variableDefinition.type === 'ImportBinding';
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 && !isImportBinding) {
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbXBvcnQtbW9kdWxlLWV4cG9ydHMuanMiXSwibmFtZXMiOlsiZ2V0RW50cnlQb2ludCIsImNvbnRleHQiLCJwa2dQYXRoIiwiY3dkIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwicmVxdWlyZSIsInJlc29sdmUiLCJwYXRoIiwiZGlybmFtZSIsImVycm9yIiwiZmluZFNjb3BlIiwiaWRlbnRpZmllciIsImdldFNvdXJjZUNvZGUiLCJzY29wZU1hbmFnZXIiLCJzY29wZXMiLCJzbGljZSIsInJldmVyc2UiLCJmaW5kIiwic2NvcGUiLCJ2YXJpYWJsZXMiLCJzb21lIiwidmFyaWFibGUiLCJpZGVudGlmaWVycyIsIm5vZGUiLCJuYW1lIiwiZmluZERlZmluaXRpb24iLCJvYmplY3RTY29wZSIsImRlZnMiLCJkZWYiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwiY2F0ZWdvcnkiLCJkZXNjcmlwdGlvbiIsInJlY29tbWVuZGVkIiwiZml4YWJsZSIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJleGNlcHRpb25zIiwiYWRkaXRpb25h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