securityos/node_modules/eslint-plugin-import/lib/rules/no-import-module-exports.js

85 lines
12 KiB
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1pbXBvcnQtbW9kdWxlLWV4cG9ydHMuanMiXSwibmFtZXMiOlsiZ2V0RW50cnlQb2ludCIsImNvbnRleHQiLCJwa2dQYXRoIiwiY3dkIiwiZ2V0UGh5c2ljYWxGaWxlbmFtZSIsImdldEZpbGVuYW1lIiwicmVxdWlyZSIsInJlc29sdmUiLCJwYXRoIiwiZGlybmFtZSIsImVycm9yIiwiZmluZFNjb3BlIiwiaWRlbnRpZmllciIsImdldFNvdXJjZUNvZGUiLCJzY29wZU1hbmFnZXIiLCJzY29wZXMiLCJzbGljZSIsInJldmVyc2UiLCJmaW5kIiwic2NvcGUiLCJ2YXJpYWJsZXMiLCJzb21lIiwidmFyaWFibGUiLCJpZGVudGlmaWVycyIsIm5vZGUiLCJuYW1lIiwiZmluZERlZmluaXRpb24iLCJvYmplY3RTY29wZSIsImRlZnMiLCJkZWYiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwiY2F0ZWdvcnkiLCJkZXNjcmlwdGlvbiIsInJlY29tbWVuZGVkIiwiZml4YWJsZSIsInNjaGVtYSIsInByb3BlcnRpZXMiLCJleGNlcHRpb25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