123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- '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);}}
-
- 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 = [];
-
-
- 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));
- }
- }
- });
-
- const importNames = Object.keys(importNameConflicts);
- const importLocalNames = generateLocalNames(
- importNames,
- importNameConflicts,
- namespaceVariable.name);
-
- const namedImportSpecifiers = importNames.map(importName =>
- importName === importLocalNames[importName] ?
- importName :
- `${importName} as ${importLocalNames[importName]}`);
- fixes.push(fixer.replaceText(node, `{ ${namedImportSpecifiers.join(', ')} }`));
-
- 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;
- }) });
- } };
- } };
- function usesNamespaceAsObject(namespaceIdentifiers) {
- return !namespaceIdentifiers.every(identifier => {
- const parent = identifier.parent;
-
- return (
- parent && parent.type === 'MemberExpression' && (
- parent.property.type === 'Identifier' || parent.property.type === 'Literal'));
- });
- }
- function getMemberPropertyName(memberExpression) {
- return memberExpression.property.type === 'Identifier' ?
- memberExpression.property.name :
- memberExpression.property.value;
- }
- 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))));
- }
- 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;
- }
|