prefer-default-export.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  3. module.exports = {
  4. meta: {
  5. type: 'suggestion',
  6. docs: {
  7. url: (0, _docsUrl2.default)('prefer-default-export') },
  8. schema: [] },
  9. create: function (context) {
  10. let specifierExportCount = 0;
  11. let hasDefaultExport = false;
  12. let hasStarExport = false;
  13. let hasTypeExport = false;
  14. let namedExportNode = null;
  15. function captureDeclaration(identifierOrPattern) {
  16. if (identifierOrPattern && identifierOrPattern.type === 'ObjectPattern') {
  17. // recursively capture
  18. identifierOrPattern.properties.
  19. forEach(function (property) {
  20. captureDeclaration(property.value);
  21. });
  22. } else if (identifierOrPattern && identifierOrPattern.type === 'ArrayPattern') {
  23. identifierOrPattern.elements.
  24. forEach(captureDeclaration);
  25. } else {
  26. // assume it's a single standard identifier
  27. specifierExportCount++;
  28. }
  29. }
  30. return {
  31. 'ExportDefaultSpecifier': function () {
  32. hasDefaultExport = true;
  33. },
  34. 'ExportSpecifier': function (node) {
  35. if (node.exported.name === 'default') {
  36. hasDefaultExport = true;
  37. } else {
  38. specifierExportCount++;
  39. namedExportNode = node;
  40. }
  41. },
  42. 'ExportNamedDeclaration': function (node) {
  43. // if there are specifiers, node.declaration should be null
  44. if (!node.declaration) return;const
  45. type = node.declaration.type;
  46. if (
  47. type === 'TSTypeAliasDeclaration' ||
  48. type === 'TypeAlias' ||
  49. type === 'TSInterfaceDeclaration' ||
  50. type === 'InterfaceDeclaration')
  51. {
  52. specifierExportCount++;
  53. hasTypeExport = true;
  54. return;
  55. }
  56. if (node.declaration.declarations) {
  57. node.declaration.declarations.forEach(function (declaration) {
  58. captureDeclaration(declaration.id);
  59. });
  60. } else
  61. {
  62. // captures 'export function foo() {}' syntax
  63. specifierExportCount++;
  64. }
  65. namedExportNode = node;
  66. },
  67. 'ExportDefaultDeclaration': function () {
  68. hasDefaultExport = true;
  69. },
  70. 'ExportAllDeclaration': function () {
  71. hasStarExport = true;
  72. },
  73. 'Program:exit': function () {
  74. if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport && !hasTypeExport) {
  75. context.report(namedExportNode, 'Prefer default export.');
  76. }
  77. } };
  78. } };
  79. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9wcmVmZXItZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJ0eXBlIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImNyZWF0ZSIsImNvbnRleHQiLCJzcGVjaWZpZXJFeHBvcnRDb3VudCIsImhhc0RlZmF1bHRFeHBvcnQiLCJoYXNTdGFyRXhwb3J0IiwiaGFzVHlwZUV4cG9ydCIsIm5hbWVkRXhwb3J0Tm9kZSIsImNhcHR1cmVEZWNsYXJhdGlvbiIsImlkZW50aWZpZXJPclBhdHRlcm4iLCJwcm9wZXJ0aWVzIiwiZm9yRWFjaCIsInByb3BlcnR5IiwidmFsdWUiLCJlbGVtZW50cyIsIm5vZGUiLCJleHBvcnRlZCIsIm5hbWUiLCJkZWNsYXJhdGlvbiIsImRlY2xhcmF0aW9ucyIsImlkIiwicmVwb3J0Il0sIm1hcHBpbmdzIjoiQUFBQTs7QUFFQSxxQzs7QUFFQUEsT0FBT0MsT0FBUCxHQUFpQjtBQUNmQyxRQUFNO0FBQ0pDLFVBQU0sWUFERjtBQUVKQyxVQUFNO0FBQ0pDLFdBQUssdUJBQVEsdUJBQVIsQ0FERCxFQUZGOztBQUtKQyxZQUFRLEVBTEosRUFEUzs7O0FBU2ZDLFVBQVEsVUFBU0MsT0FBVCxFQUFrQjtBQUN4QixRQUFJQyx1QkFBdU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