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

177 lines
22 KiB
JavaScript
Raw Permalink Normal View History

2022-06-23 02:27:43 +02: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 };}function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {arr2[i] = arr[i];}return arr2;} else {return Array.from(arr);}} /**
* @fileoverview Rule to disallow namespace import
* @author Radek Benkel
*/ //------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
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([].concat(_toConsumableArray(
scope.variables.map(function (variable) {return variable.name;})), _toConsumableArray(
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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiaWdub3JlIiwiaXRlbXMiLCJ1bmlxdWVJdGVtcyIsImNyZWF0ZSIsImNvbnRleHQiLCJmaXJzdE9wdGlvbiIsIm9wdGlvbnMiLCJpZ25vcmVHbG9icyIsIkltcG9ydE5hbWVzcGFjZVNwZWNpZmllciIsIm5vZGUiLCJmaW5kIiwicGFyZW50Iiwic291cmNlIiwidmFsdWUiLCJnbG9iIiwibWF0Y2hCYXNlIiwic2NvcGVWYXJpYWJsZXMiLCJnZXRTY29wZSIsInZhcmlhYmxlcyIsIm5hbWVzcGFjZVZhcmlhYmxlIiwidmFyaWFibGUiLCJkZWZzIiwibmFtZXNwYWNlUmVmZXJlbmNlcyIsInJlZmVyZW5jZXMiLCJuYW1lc3BhY2VJZGVudGlmaWVycyIsIm1hcCIsInJlZmVyZW5jZSIsImlkZW50aWZpZXIiLCJjYW5GaXgiLCJsZW5ndGgiLCJ1c2VzTmFtZXNwYWNlQXNPYm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