no-named-as-default-member.js 11 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 (context) {
  15. const fileImports = new Map();
  16. const allPropertyLookups = new Map();
  17. function handleImportDefault(node) {
  18. const declaration = (0, _importDeclaration2.default)(context);
  19. const 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,
  27. sourcePath: declaration.source.value });
  28. }
  29. function storePropertyLookup(objectName, propName, node) {
  30. const lookups = allPropertyLookups.get(objectName) || [];
  31. lookups.push({ node, propName });
  32. allPropertyLookups.set(objectName, lookups);
  33. }
  34. function handlePropLookup(node) {
  35. const objectName = node.object.name;
  36. const propName = node.property.name;
  37. storePropertyLookup(objectName, propName, node);
  38. }
  39. function handleDestructuringAssignment(node) {
  40. const isDestructure =
  41. node.id.type === 'ObjectPattern' &&
  42. node.init != null &&
  43. node.init.type === 'Identifier';
  44. if (!isDestructure) return;
  45. const objectName = node.init.name;
  46. for (const _ref of node.id.properties) {const key = _ref.key;
  47. if (key == null) continue; // true for rest properties
  48. storePropertyLookup(objectName, key.name, key);
  49. }
  50. }
  51. function handleProgramExit() {
  52. allPropertyLookups.forEach((lookups, objectName) => {
  53. const fileImport = fileImports.get(objectName);
  54. if (fileImport == null) return;
  55. for (const _ref2 of lookups) {const propName = _ref2.propName;const 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,
  61. message:
  62. `Caution: \`${objectName}\` also has a named export ` +
  63. `\`${propName}\`. Check if you meant to write ` +
  64. `\`import {${propName}} from '${fileImport.sourcePath}'\` ` +
  65. 'instead.' });
  66. }
  67. });
  68. }
  69. return {
  70. 'ImportDefaultSpecifier': handleImportDefault,
  71. 'MemberExpression': handlePropLookup,
  72. 'VariableDeclarator': handleDestructuringAssignment,
  73. 'Program:exit': handleProgramExit };
  74. } }; /**
  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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1uYW1lZC1hcy1kZWZhdWx0LW1lbWJlci5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsInR5cGUiLCJkb2NzIiwidXJsIiwic2NoZW1hIiwiY3JlYXRlIiwiY29udGV4dCIsImZpbGVJbXBvcnRzIiwiTWFwIiwiYWxsUHJvcGVydHlMb29rdXBzIiwiaGFuZGxlSW1wb3J0RGVmYXVsdCIsIm5vZGUiLCJkZWNsYXJhdGlvbiIsImV4cG9ydE1hcCIsIkV4cG9ydHMiLCJnZXQiLCJzb3VyY2UiLCJ2YWx1ZSIsImVycm9ycyIsImxlbmd0aCIsInJlcG9ydEVycm9ycyIsInNldCIsImxvY2FsIiwibmFtZSIsInNvdXJjZVBhdGgiLCJzdG9yZVByb3BlcnR5TG9va3VwIiwib2JqZWN0TmFtZSIsInByb3BOYW1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