PromoCursed/node_modules/eslint-plugin-import/lib/rules/no-namespace.js

175 lines
21 KiB
JavaScript
Raw Normal View History

2024-08-20 23:25:37 +04:00
'use strict';
var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/**
* @fileoverview Rule to disallow namespace import
* @author Radek Benkel
*/module.exports = { meta: { type: 'suggestion',
docs: {
category: 'Style guide',
description: 'Forbid namespace (a.k.a. "wildcard" `*`) imports.',
url: (0, _docsUrl2['default'])('no-namespace') },
fixable: 'code',
schema: [{
type: 'object',
properties: {
ignore: {
type: 'array',
items: {
type: 'string' },
uniqueItems: true } } }] },
create: function () {function create(context) {
var firstOption = context.options[0] || {};
var ignoreGlobs = firstOption.ignore;
return {
ImportNamespaceSpecifier: function () {function ImportNamespaceSpecifier(node) {
if (ignoreGlobs && ignoreGlobs.find(function (glob) {return (0, _minimatch2['default'])(node.parent.source.value, glob, { matchBase: true });})) {
return;
}
var scopeVariables = context.getScope().variables;
var namespaceVariable = scopeVariables.find(function (variable) {return variable.defs[0].node === node;});
var namespaceReferences = namespaceVariable.references;
var namespaceIdentifiers = namespaceReferences.map(function (reference) {return reference.identifier;});
var canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers);
context.report({
node: node,
message: 'Unexpected namespace import.',
fix: canFix && function (fixer) {
var scopeManager = context.getSourceCode().scopeManager;
var fixes = [];
// Pass 1: Collect variable names that are already in scope for each reference we want
// to transform, so that we can be sure that we choose non-conflicting import names
var importNameConflicts = {};
namespaceIdentifiers.forEach(function (identifier) {
var parent = identifier.parent;
if (parent && parent.type === 'MemberExpression') {
var importName = getMemberPropertyName(parent);
var localConflicts = getVariableNamesInScope(scopeManager, parent);
if (!importNameConflicts[importName]) {
importNameConflicts[importName] = localConflicts;
} else {
localConflicts.forEach(function (c) {return importNameConflicts[importName].add(c);});
}
}
});
// Choose new names for each import
var importNames = Object.keys(importNameConflicts);
var importLocalNames = generateLocalNames(
importNames,
importNameConflicts,
namespaceVariable.name);
// Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
var namedImportSpecifiers = importNames.map(function (importName) {return importName === importLocalNames[importName] ?
importName : String(
importName) + ' as ' + String(importLocalNames[importName]);});
fixes.push(fixer.replaceText(node, '{ ' + String(namedImportSpecifiers.join(', ')) + ' }'));
// Pass 2: Replace references to the namespace with references to the named imports
namespaceIdentifiers.forEach(function (identifier) {
var parent = identifier.parent;
if (parent && parent.type === 'MemberExpression') {
var importName = getMemberPropertyName(parent);
fixes.push(fixer.replaceText(parent, importLocalNames[importName]));
}
});
return fixes;
} });
}return ImportNamespaceSpecifier;}() };
}return create;}() };
/**
* @param {Identifier[]} namespaceIdentifiers
* @returns {boolean} `true` if the namespace variable is more than just a glorified constant
*/
function usesNamespaceAsObject(namespaceIdentifiers) {
return !namespaceIdentifiers.every(function (identifier) {
var parent = identifier.parent;
// `namespace.x` or `namespace['x']`
return (
parent &&
parent.type === 'MemberExpression' && (
parent.property.type === 'Identifier' || parent.property.type === 'Literal'));
});
}
/**
* @param {MemberExpression} memberExpression
* @returns {string} the name of the member in the object expression, e.g. the `x` in `namespace.x`
*/
function getMemberPropertyName(memberExpression) {
return memberExpression.property.type === 'Identifier' ?
memberExpression.property.name :
memberExpression.property.value;
}
/**
* @param {ScopeManager} scopeManager
* @param {ASTNode} node
* @return {Set<string>}
*/
function getVariableNamesInScope(scopeManager, node) {
var currentNode = node;
var scope = scopeManager.acquire(currentNode);
while (scope == null) {
currentNode = currentNode.parent;
scope = scopeManager.acquire(currentNode, true);
}
return new Set(scope.variables.concat(scope.upper.variables).map(function (variable) {return variable.name;}));
}
/**
*
* @param {*} names
* @param {*} nameConflicts
* @param {*} namespaceName
*/
function generateLocalNames(names, nameConflicts, namespaceName) {
var localNames = {};
names.forEach(function (name) {
var localName = void 0;
if (!nameConflicts[name].has(name)) {
localName = name;
} else if (!nameConflicts[name].has(String(namespaceName) + '_' + String(name))) {
localName = String(namespaceName) + '_' + String(name);
} else {
for (var i = 1; i < Infinity; i++) {
if (!nameConflicts[name].has(String(namespaceName) + '_' + String(name) + '_' + String(i))) {
localName = String(namespaceName) + '_' + String(name) + '_' + String(i);
break;
}
}
}
localNames[name] = localName;
});
return localNames;
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJmaXhhYmxlIiwic2NoZW1hIiwicHJvcGVydGllcyIsImlnbm9yZSIsIml0ZW1zIiwidW5pcXVlSXRlbXMiLCJjcmVhdGUiLCJjb250ZXh0IiwiZmlyc3RPcHRpb24iLCJvcHRpb25zIiwiaWdub3JlR2xvYnMiLCJJbXBvcnROYW1lc3BhY2VTcGVjaWZpZXIiLCJub2RlIiwiZmluZCIsImdsb2IiLCJwYXJlbnQiLCJzb3VyY2UiLCJ2YWx1ZSIsIm1hdGNoQmFzZSIsInNjb3BlVmFyaWFibGVzIiwiZ2V0U2NvcGUiLCJ2YXJpYWJsZXMiLCJuYW1lc3BhY2VWYXJpYWJsZSIsInZhcmlhYmxlIiwiZGVmcyIsIm5hbWVzcGFjZVJlZmVyZW5jZXMiLCJyZWZlcmVuY2VzIiwibmFtZXNwYWNlSWRlbnRpZmllcnMiLCJtYXAiLCJyZWZlcmVuY2UiLCJpZGVudGlmaWVyIiwiY2FuRml4IiwibGVuZ3RoIiwidXNlc05hbWVzcGFjZUFzT2JqZWN0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwic2Nvc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