no-named-as-default-member.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. var _ExportMap = require('../ExportMap');var _ExportMap2 = _interopRequireDefault(_ExportMap);
  3. var _importDeclaration = require('../importDeclaration');var _importDeclaration2 = _interopRequireDefault(_importDeclaration);
  4. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. module.exports = {
  9. meta: {
  10. type: 'suggestion',
  11. docs: {
  12. url: (0, _docsUrl2['default'])('no-named-as-default-member') },
  13. schema: [] },
  14. create: function () {function create(context) {
  15. var fileImports = new Map();
  16. var allPropertyLookups = new Map();
  17. function handleImportDefault(node) {
  18. var declaration = (0, _importDeclaration2['default'])(context);
  19. var exportMap = _ExportMap2['default'].get(declaration.source.value, context);
  20. if (exportMap == null) return;
  21. if (exportMap.errors.length) {
  22. exportMap.reportErrors(context, declaration);
  23. return;
  24. }
  25. fileImports.set(node.local.name, {
  26. exportMap: exportMap,
  27. sourcePath: declaration.source.value });
  28. }
  29. function storePropertyLookup(objectName, propName, node) {
  30. var lookups = allPropertyLookups.get(objectName) || [];
  31. lookups.push({ node: node, propName: propName });
  32. allPropertyLookups.set(objectName, lookups);
  33. }
  34. function handlePropLookup(node) {
  35. var objectName = node.object.name;
  36. var propName = node.property.name;
  37. storePropertyLookup(objectName, propName, node);
  38. }
  39. function handleDestructuringAssignment(node) {
  40. var isDestructure =
  41. node.id.type === 'ObjectPattern' &&
  42. node.init != null &&
  43. node.init.type === 'Identifier';
  44. if (!isDestructure) return;
  45. var objectName = node.init.name;var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined;try {
  46. for (var _iterator = node.id.properties[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {var _ref = _step.value;var key = _ref.key;
  47. if (key == null) continue; // true for rest properties
  48. storePropertyLookup(objectName, key.name, key);
  49. }} catch (err) {_didIteratorError = true;_iteratorError = err;} finally {try {if (!_iteratorNormalCompletion && _iterator['return']) {_iterator['return']();}} finally {if (_didIteratorError) {throw _iteratorError;}}}
  50. }
  51. function handleProgramExit() {
  52. allPropertyLookups.forEach(function (lookups, objectName) {
  53. var fileImport = fileImports.get(objectName);
  54. if (fileImport == null) return;var _iteratorNormalCompletion2 = true;var _didIteratorError2 = false;var _iteratorError2 = undefined;try {
  55. for (var _iterator2 = lookups[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {var _ref2 = _step2.value;var propName = _ref2.propName,node = _ref2.node;
  56. // the default import can have a "default" property
  57. if (propName === 'default') continue;
  58. if (!fileImport.exportMap.namespace.has(propName)) continue;
  59. context.report({
  60. node: node,
  61. message:
  62. 'Caution: `' + String(objectName) + '` also has a named export ' + ('`' + String(
  63. propName) + '`. Check if you meant to write ') + ('`import {' + String(
  64. propName) + '} from \'' + String(fileImport.sourcePath) + '\'` ') +
  65. 'instead.' });
  66. }} catch (err) {_didIteratorError2 = true;_iteratorError2 = err;} finally {try {if (!_iteratorNormalCompletion2 && _iterator2['return']) {_iterator2['return']();}} finally {if (_didIteratorError2) {throw _iteratorError2;}}}
  67. });
  68. }
  69. return {
  70. 'ImportDefaultSpecifier': handleImportDefault,
  71. 'MemberExpression': handlePropLookup,
  72. 'VariableDeclarator': handleDestructuringAssignment,
  73. 'Program:exit': handleProgramExit };
  74. }return create;}() }; /**
  75. * @fileoverview Rule to warn about potentially confused use of name exports
  76. * @author Desmond Brand
  77. * @copyright 2016 Desmond Brand. All rights reserved.
  78. * See LICENSE in root directory for full license.
  79. */
  80. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY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