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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsiZ2V0TWVtYmVyUHJvcGVydHlOYW1lIiwibWVtYmVyRXhwcmVzc2lvbiIsInByb3BlcnR5IiwidHlwZSIsIm5h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NcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydE5hbWVDb25mbGljdHMgPSB7fTtcbiAgICAgICAgICAgIG5hbWVzcGFjZUlkZW50aWZpZXJzLmZvckVhY2goKGlkZW50aWZpZXIpID0+IHtcbiAgICAgICAgICAgICAgY29uc3QgcGFyZW50ID0gaWRlbnRpZmllci5wYXJlbnQ7XG4gICAgICAgICAgICAgIGlmIChwYXJlbnQgJiYgcGFyZW50LnR5cGUgPT09ICdNZW1iZXJFeHByZXNzaW9uJykge1xuICAgICAgICAgICAgICAgIGNvbnN0IGltcG9ydE5hbWUgPSBnZXRNZW1iZXJQcm9wZXJ0eU5hbWUocGFyZW50KTtcbiAgICAgICAgICAgICAgICBjb25zdCBsb2NhbENvbmZsaWN0cyA9IGdldFZhcmlhYmxlTmFtZXNJblNjb3BlKHNjb3BlTWFuYWdlciwgcGFyZW50KTtcbiAgICAgICAgICAgICAgICBpZiAoIWltcG9ydE5hbWVDb25mbGljdHNbaW1wb3J0TmFtZV0pIHtcbiAgICAgICAgICAgICAgICAgIGltcG9ydE5hbWVDb25mbGljdHNbaW1wb3J0TmFtZV0gPSBsb2NhbENvbmZsaWN0cztcbiAgICAgICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgICAgbG9jYWxDb25mbGljdHMuZm9yRWFjaCgoYykgPT4gaW1wb3J0TmFtZUNvbmZsaWN0c1tpbXBvcnROYW1lXS5hZGQoYykpO1xuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSk7XG5cbiAgICAgICAgICAgIC8vIENob29zZSBuZXcgbmFtZXMgZm9yIGVhY2ggaW1wb3J0XG4gICAgICAgICAgICBjb25zdCBpbXBvcnROYW1lcyA9IE9iamVjdC5rZXlzKGltcG9ydE5hbWVDb25mbGljdHMpO1xuICAgICAgICAgICAgY29uc3QgaW1wb3J0TG9jYWxOYW1lcyA9IGdlbmVyYXRlTG9jYWxOYW1lcyhcbiAgICAgICAgIC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9wZXJ0eU5hbWUocGFyZW50KTtcbiAgICAgICAgICAgICAgICBmaXhlcy5wdXNoKGZpeGVyLnJlcGxhY2VUZXh0KHBhcmVudCwgaW1wb3J0TG9jYWxOYW1lc1tpbXBvcnROYW1lXSkpO1xuICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9KTtcblxuICAgICAgICAgICAgcmV0dXJuIGZpeGVzO1xuICAgICAgICAgIH0pLFxuICAgICAgICB9KTtcbiAgICAgIH0sXG4gICAgfTtcbiAgfSxcbn07XG4iXX0=
|