'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 };} //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ /** * @fileoverview Rule to disallow namespace import * @author Radek Benkel */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;}() }; /** * @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} */ 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; } //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsImNhdGVnb3J5IiwiZGVzY3JpcHRpb24iLCJ1cmwiLCJmaXhhYmxlIiwic2NoZW1hIiwicHJvcGVydGllcyIsImlnbm9yZSIsIml0ZW1zIiwidW5pcXVlSXRlbXMiLCJjcmVhdGUiLCJjb250ZXh0IiwiZmlyc3RPcHRpb24iLCJvcHRpb25zIiwiaWdub3JlR2xvYnMiLCJJbXBvcnROYW1lc3BhY2VTcGVjaWZpZXIiLCJub2RlIiwiZmluZCIsImdsb2IiLCJwYXJlbnQiLCJzb3VyY2UiLCJ2YWx1ZSIsIm1hdGNoQmFzZSIsInNjb3BlVmFyaWFibGVzIiwiZ2V0U2NvcGUiLCJ2YXJpYWJsZXMiLCJuYW1lc3BhY2VWYXJpYWJsZSIsInZhcmlhYmxlIiwiZGVmcyIsIm5hbWVzcGFjZVJlZmVyZW5jZXMiLCJyZWZlcmVuY2VzIiwibmFtZXNwYWNlSWRlbnRpZmllcnMiLCJtYXAiLCJyZWZlcmVuY2UiLCJpZGVudGlmaWVyIiwiY2FuRml4IiwibGVuZ3RoIiwidXNlc05hbWVzcGFjZUFzT2JqZWN0IiwicmVwb3J0IiwibWVzc2FnZSIsImZpeCIsImZpeGVyIiwic2NvcGVNYW5hZ2VyIiwiZ2V0U291cmNlQ29kZSIsImZpeGVzIiwiaW1wb3J0TmFtZUNvbmZsaWN0cyIsImZvckVhY2giLCJpbXBvcnROYW1lIiwiZ2V0TWVtYmVyUHJvcGVydHlOYW1lIiwibG9jYWxDb25mbGljdHMiLCJnZXRWYXJpYWJsZU5hbWVzSW5TY29wZSIsImMiLCJhZGQiLCJpbXB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pY3RzID0ge307XG4gICAgICAgICAgICBuYW1lc3BhY2VJZGVudGlmaWVycy5mb3JFYWNoKChpZGVudGlmaWVyKSA9PiB7XG4gICAgICAgICAgICAgIGNvbnN0IHBhcmVudCA9IGlkZW50aWZpZXIucGFyZW50O1xuICAgICAgICAgICAgICBpZiAocGFyZW50ICYmIHBhcmVudC50eXBlID09PSAnTWVtYmVyRXhwcmVzc2lvbicpIHtcbiAgICAgICAgICAgICAgICBjb25zdCBpbXBvcnROYW1lID0gZ2V0TWVtYmVyUHJvcGVydHlOYW1lKHBhcmVudCk7XG4gICAgICAgICAgICAgICAgY29uc3QgbG9jYWxDb25mbGljdHMgPSBnZXRWYXJpYWJsZU5hbWVzSW5TY29wZShzY29wZU1hbmFnZXIsIHBhcmVudCk7XG4gICAgICAgICAgICAgICAgaWYgKCFpbXBvcnROYW1lQ29uZmxpY3RzW2ltcG9ydE5hbWVdKSB7XG4gICAgICAgICAgICAgICAgICBpbXBvcnROYW1lQ29uZmxpY3RzW2ltcG9ydE5hbWVdID0gbG9jYWxDb25mbGljdHM7XG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgIGxvY2FsQ29uZmxpY3RzLmZvckVhY2goKGMpID0+IGltcG9ydE5hbWVDb25mbGljdHNbaW1wb3J0TmFtZV0uYWRkKGMpKTtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH0pO1xuXG4gICAgICAgICAgICAvLyBDaG9vc2UgbmV3IG5hbWVzIGZvciBlYWNoIGltcG9ydFxuICAgICAgICAgICAgY29uc3QgaW1wb3J0TmFtZXMgPSBPYmplY3Qua2V5cyhpbXBvcnROYW1lQ29uZmxpY3RzKTtcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydExvY2FsTmFtZXMgPSBnZW5lcmF0ZUxvY2FsTmFtZXMoXG4gICAgICAgICAgICAgIGltcG9ydE5hbWVzLFxuICAgICAgICAgICAgICBpbXBvcn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