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

177 lines
22 KiB
JavaScript
Raw Permalink Normal View History

2022-04-25 02:47:15 +00: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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiaWdub3JlIiwiaXRlbXMiLCJ1bmlxdWVJdGVtcyIsImNyZWF0ZSIsImNvbnRleHQiLCJmaXJzdE9wdGlvbiIsIm9wdGlvbnMiLCJpZ25vcmVHbG9icyIsIkltcG9ydE5hbWVzcGFjZVNwZWNpZmllciIsIm5vZGUiLCJmaW5kIiwicGFyZW50Iiwic291cmNlIiwidmFsdWUiLCJnbG9iIiwibWF0Y2hCYXNlIiwic2NvcGVWYXJpYWJsZXMiLCJnZXRTY29wZSIsInZhcmlhYmxlcyIsIm5hbWVzcGFjZVZhcmlhYmxlIiwidmFyaWFibGUiLCJkZWZzIiwibmFtZXNwYWNlUmVmZXJlbmNlcyIsInJlZmVyZW5jZXMiLCJuYW1lc3BhY2VJZGVudGlmaWVycyIsIm1hcCIsInJlZmVyZW5jZSIsImlkZW50aWZpZXIiLCJjYW5GaXgiLCJsZW5ndGgiLCJ1c2VzTmFtZXNwYWNlQX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