170 lines
22 KiB
JavaScript
170 lines
22 KiB
JavaScript
|
'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 };}
|
||
|
|
||
|
/**
|
||
|
* @param {MemberExpression} memberExpression
|
||
|
* @returns {string} the name of the member in the object expression, e.g. the `x` in `namespace.x`
|
||
|
*/ /**
|
||
|
* @fileoverview Rule to disallow namespace import
|
||
|
* @author Radek Benkel
|
||
|
*/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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @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'));
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
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;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsiZ2V0TWVtYmVyUHJvcGVydHlOYW1lIiwibWVtYmVyRXhwcmVzc2lvbiIsInByb3BlcnR5IiwidHlwZSIsIm5hbWUiLCJ2YWx1ZSIsImdldFZhcmlhYmxlTmFtZXNJblNjb3BlIiwic2NvcGVNYW5hZ2VyIiwibm9kZSIsImN1cnJlbnROb2RlIiwic2NvcGUiLCJhY3F1aXJlIiwicGFyZW50IiwiU2V0IiwidmFyaWFibGVzIiwiY29uY2F0IiwidXBwZXIiLCJtYXAiLCJ2YXJpYWJsZSIsImdlbmVyYXRlTG9jYWxOYW1lcyIsIm5hbWVzIiwibmFtZUNvbmZsaWN0cyIsIm5hbWVzcGFjZU5hbWUiLCJsb2NhbE5hbWVzIiwiZm9yRWFjaCIsImxvY2FsTmFtZSIsImhhcyIsImkiLCJJbmZpbml0eSIsInVzZXNOYW1lc3BhY2VBc09iamVjdCIsIm5hbWVzcGFjZUlkZW50aWZpZXJzIiwiZXZlcnkiLCJpZGVudGlmaWVyIiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJkb2NzIiwiY2F0ZWdvcnkiLCJkZXNjcmlwdGlvbiIsInVybCIsImZpeGFibGUiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiaWdub3JlIiwiaXRlbXMiLCJ1bmlxdWVJdGVtcyIsImNyZWF0ZSIsImNvbnRleHQiLCJmaXJzdE9wdGlvbiIsIm9wdGlvbnMiLCJpZ25vcmVHbG9icyIsIkltcG9ydE5hbWVz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