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

160 lines
20 KiB
JavaScript
Raw Normal View History

'use strict';
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: [] },
create: function (context) {
return {
'ImportNamespaceSpecifier': function (node) {
const scopeVariables = context.getScope().variables;
const namespaceVariable = scopeVariables.find(variable =>
variable.defs[0].node === node);
const namespaceReferences = namespaceVariable.references;
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier);
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers);
context.report({
node,
message: `Unexpected namespace import.`,
fix: canFix && (fixer => {
const scopeManager = context.getSourceCode().scopeManager;
const 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
const importNameConflicts = {};
namespaceIdentifiers.forEach(identifier => {
const parent = identifier.parent;
if (parent && parent.type === 'MemberExpression') {
const importName = getMemberPropertyName(parent);
const localConflicts = getVariableNamesInScope(scopeManager, parent);
if (!importNameConflicts[importName]) {
importNameConflicts[importName] = localConflicts;
} else {
localConflicts.forEach(c => importNameConflicts[importName].add(c));
}
}
});
// Choose new names for each import
const importNames = Object.keys(importNameConflicts);
const importLocalNames = generateLocalNames(
importNames,
importNameConflicts,
namespaceVariable.name);
// Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
const namedImportSpecifiers = importNames.map(importName =>
importName === importLocalNames[importName] ?
importName :
`${importName} as ${importLocalNames[importName]}`);
fixes.push(fixer.replaceText(node, `{ ${namedImportSpecifiers.join(', ')} }`));
// Pass 2: Replace references to the namespace with references to the named imports
namespaceIdentifiers.forEach(identifier => {
const parent = identifier.parent;
if (parent && parent.type === 'MemberExpression') {
const importName = getMemberPropertyName(parent);
fixes.push(fixer.replaceText(parent, importLocalNames[importName]));
}
});
return fixes;
}) });
} };
}
/**
* @param {Identifier[]} namespaceIdentifiers
* @returns {boolean} `true` if the namespace variable is more than just a glorified constant
*/ };
function usesNamespaceAsObject(namespaceIdentifiers) {
return !namespaceIdentifiers.every(identifier => {
const 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) {
let currentNode = node;
let scope = scopeManager.acquire(currentNode);
while (scope == null) {
currentNode = currentNode.parent;
scope = scopeManager.acquire(currentNode, true);
}
return new Set([].concat(_toConsumableArray(
scope.variables.map(variable => variable.name)), _toConsumableArray(
scope.upper.variables.map(variable => variable.name))));
}
/**
*
* @param {*} names
* @param {*} nameConflicts
* @param {*} namespaceName
*/
function generateLocalNames(names, nameConflicts, namespaceName) {
const localNames = {};
names.forEach(name => {
let localName;
if (!nameConflicts[name].has(name)) {
localName = name;
} else if (!nameConflicts[name].has(`${namespaceName}_${name}`)) {
localName = `${namespaceName}_${name}`;
} else {
for (let i = 1; i < Infinity; i++) {
if (!nameConflicts[name].has(`${namespaceName}_${name}_${i}`)) {
localName = `${namespaceName}_${name}_${i}`;
break;
}
}
}
localNames[name] = localName;
});
return localNames;
}
//# sourceMappingURL=data:application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