no-namespace.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
  3. 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);}} /**
  4. * @fileoverview Rule to disallow namespace import
  5. * @author Radek Benkel
  6. */ //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: 'suggestion',
  12. docs: {
  13. url: (0, _docsUrl2['default'])('no-namespace') },
  14. fixable: 'code',
  15. schema: [{
  16. type: 'object',
  17. properties: {
  18. ignore: {
  19. type: 'array',
  20. items: {
  21. type: 'string' },
  22. uniqueItems: true } } }] },
  23. create: function () {function create(context) {
  24. var firstOption = context.options[0] || {};
  25. var ignoreGlobs = firstOption.ignore;
  26. return {
  27. ImportNamespaceSpecifier: function () {function ImportNamespaceSpecifier(node) {
  28. if (ignoreGlobs && ignoreGlobs.find(function (glob) {return (0, _minimatch2['default'])(node.parent.source.value, glob, { matchBase: true });})) {
  29. return;
  30. }
  31. var scopeVariables = context.getScope().variables;
  32. var namespaceVariable = scopeVariables.find(function (variable) {return variable.defs[0].node === node;});
  33. var namespaceReferences = namespaceVariable.references;
  34. var namespaceIdentifiers = namespaceReferences.map(function (reference) {return reference.identifier;});
  35. var canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers);
  36. context.report({
  37. node: node,
  38. message: 'Unexpected namespace import.',
  39. fix: canFix && function (fixer) {
  40. var scopeManager = context.getSourceCode().scopeManager;
  41. var fixes = [];
  42. // Pass 1: Collect variable names that are already in scope for each reference we want
  43. // to transform, so that we can be sure that we choose non-conflicting import names
  44. var importNameConflicts = {};
  45. namespaceIdentifiers.forEach(function (identifier) {
  46. var parent = identifier.parent;
  47. if (parent && parent.type === 'MemberExpression') {
  48. var importName = getMemberPropertyName(parent);
  49. var localConflicts = getVariableNamesInScope(scopeManager, parent);
  50. if (!importNameConflicts[importName]) {
  51. importNameConflicts[importName] = localConflicts;
  52. } else {
  53. localConflicts.forEach(function (c) {return importNameConflicts[importName].add(c);});
  54. }
  55. }
  56. });
  57. // Choose new names for each import
  58. var importNames = Object.keys(importNameConflicts);
  59. var importLocalNames = generateLocalNames(
  60. importNames,
  61. importNameConflicts,
  62. namespaceVariable.name);
  63. // Replace the ImportNamespaceSpecifier with a list of ImportSpecifiers
  64. var namedImportSpecifiers = importNames.map(function (importName) {return (
  65. importName === importLocalNames[importName] ?
  66. importName : String(
  67. importName) + ' as ' + String(importLocalNames[importName]));});
  68. fixes.push(fixer.replaceText(node, '{ ' + String(namedImportSpecifiers.join(', ')) + ' }'));
  69. // Pass 2: Replace references to the namespace with references to the named imports
  70. namespaceIdentifiers.forEach(function (identifier) {
  71. var parent = identifier.parent;
  72. if (parent && parent.type === 'MemberExpression') {
  73. var importName = getMemberPropertyName(parent);
  74. fixes.push(fixer.replaceText(parent, importLocalNames[importName]));
  75. }
  76. });
  77. return fixes;
  78. } });
  79. }return ImportNamespaceSpecifier;}() };
  80. }return create;}() };
  81. /**
  82. * @param {Identifier[]} namespaceIdentifiers
  83. * @returns {boolean} `true` if the namespace variable is more than just a glorified constant
  84. */
  85. function usesNamespaceAsObject(namespaceIdentifiers) {
  86. return !namespaceIdentifiers.every(function (identifier) {
  87. var parent = identifier.parent;
  88. // `namespace.x` or `namespace['x']`
  89. return (
  90. parent && parent.type === 'MemberExpression' && (
  91. parent.property.type === 'Identifier' || parent.property.type === 'Literal'));
  92. });
  93. }
  94. /**
  95. * @param {MemberExpression} memberExpression
  96. * @returns {string} the name of the member in the object expression, e.g. the `x` in `namespace.x`
  97. */
  98. function getMemberPropertyName(memberExpression) {
  99. return memberExpression.property.type === 'Identifier' ?
  100. memberExpression.property.name :
  101. memberExpression.property.value;
  102. }
  103. /**
  104. * @param {ScopeManager} scopeManager
  105. * @param {ASTNode} node
  106. * @return {Set<string>}
  107. */
  108. function getVariableNamesInScope(scopeManager, node) {
  109. var currentNode = node;
  110. var scope = scopeManager.acquire(currentNode);
  111. while (scope == null) {
  112. currentNode = currentNode.parent;
  113. scope = scopeManager.acquire(currentNode, true);
  114. }
  115. return new Set([].concat(_toConsumableArray(
  116. scope.variables.map(function (variable) {return variable.name;})), _toConsumableArray(
  117. scope.upper.variables.map(function (variable) {return variable.name;}))));
  118. }
  119. /**
  120. *
  121. * @param {*} names
  122. * @param {*} nameConflicts
  123. * @param {*} namespaceName
  124. */
  125. function generateLocalNames(names, nameConflicts, namespaceName) {
  126. var localNames = {};
  127. names.forEach(function (name) {
  128. var localName = void 0;
  129. if (!nameConflicts[name].has(name)) {
  130. localName = name;
  131. } else if (!nameConflicts[name].has(String(namespaceName) + '_' + String(name))) {
  132. localName = String(namespaceName) + '_' + String(name);
  133. } else {
  134. for (var i = 1; i < Infinity; i++) {
  135. if (!nameConflicts[name].has(String(namespaceName) + '_' + String(name) + '_' + String(i))) {
  136. localName = String(namespaceName) + '_' + String(name) + '_' + String(i);
  137. break;
  138. }
  139. }
  140. }
  141. localNames[name] = localName;
  142. });
  143. return localNames;
  144. }
  145. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lc3BhY2UuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsImZpeGFibGUiLCJzY2hlb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ROYW1lQ29uZmxpY3RzW2ltcG9ydE5hbWVdID0gbG9jYWxDb25mbGljdHM7XG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgIGxvY2FsQ29uZmxpY3RzLmZvckVhY2goKGMpID0+IGltcG9ydE5hbWVDb25mbGljdHNbaW1wb3J0TmFtZV0uYWRkKGMpKTtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH0pO1xuXG4gICAgICAgICAgICAvLyBDaG9vc2UgbmV3IG5hbWVzIGZvciBlYWNoIGltcG9ydFxuICAgICAgICAgICAgY29uc3QgaW1wb3J0TmFtZXMgPSBPYmplY3Qua2V5cyhpbXBvcnROYW1lQ29uZmxpY3RzKTtcbiAgICAgICAgICAgIGNvbnN0IGltcG9ydExvY2FsTmFtZXMgPSBnZW5lcmF0ZUxvY2FsTmFtZXMoXG4gICAgICAgICAgICAgIGltcG9ydE5hbWVzLFxuICAgICAgICAgICAgICBpbXBvcnROYW1lQ29uZmxpY3RzLFxuICAgICAgICAgICAgICBuYW1lc3BhY2VWYXJpYWJsZS5uYW1lLFxuICAgICAgICAgICAgKTtcblxuICAgICAgICAgICAgLy8gUmVwbGFjZSB0aGUgSW1wb3J0TmFtZXNwYWNlU3BlY2lmaWVyIHdpdGggYSBsaXN0IG9mIEltcG9ydFNwZWNpZmllcnNcbiAgICAgICAgICAgIGNvbnN0IG5hbWVkSW1wb3J0U3BlY2lmaWVycyA9IGltcG9ydE5hbWVzLm1hcCgoaW1wb3J0TmFtZSkgPT4gKFxuICAgICAgICAgICAgICBpbXBvcnROYW1lID09PSBpbXBvcnRMb2NhbE5hbWVzW2ltcG9ydE5hbWVdXG4gICAgICAgICAgICAgICAgPyBpbXBvcnROYW1lXG4gICAgICAgICAgICAgICAgOiBgJHtpbXBvcnROYW1lfSBhcyAke2ltcG9ydExvY2FsTmFtZXNbaW1wb3J0TmFtZV19YFxuICAgICAgICAgICAgKSk7XG4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