no-default-export.js 6.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
  2. module.exports = {
  3. meta: {
  4. type: 'suggestion',
  5. docs: {
  6. category: 'Style guide',
  7. description: 'Forbid default exports.',
  8. url: (0, _docsUrl2['default'])('no-default-export') },
  9. schema: [] },
  10. create: function () {function create(context) {
  11. // ignore non-modules
  12. if (context.parserOptions.sourceType !== 'module') {
  13. return {};
  14. }
  15. var preferNamed = 'Prefer named exports.';
  16. var noAliasDefault = function () {function noAliasDefault(_ref) {var local = _ref.local;return 'Do not alias `' + String(local.name) + '` as `default`. Just export `' + String(local.name) + '` itself instead.';}return noAliasDefault;}();
  17. return {
  18. ExportDefaultDeclaration: function () {function ExportDefaultDeclaration(node) {var _ref2 =
  19. context.getSourceCode().getFirstTokens(node)[1] || {},loc = _ref2.loc;
  20. context.report({ node: node, message: preferNamed, loc: loc });
  21. }return ExportDefaultDeclaration;}(),
  22. ExportNamedDeclaration: function () {function ExportNamedDeclaration(node) {
  23. node.specifiers.
  24. filter(function (specifier) {return (specifier.exported.name || specifier.exported.value) === 'default';}).
  25. forEach(function (specifier) {var _ref3 =
  26. context.getSourceCode().getFirstTokens(node)[1] || {},loc = _ref3.loc;
  27. if (specifier.type === 'ExportDefaultSpecifier') {
  28. context.report({ node: node, message: preferNamed, loc: loc });
  29. } else if (specifier.type === 'ExportSpecifier') {
  30. context.report({ node: node, message: noAliasDefault(specifier), loc: loc });
  31. }
  32. });
  33. }return ExportNamedDeclaration;}() };
  34. }return create;}() };
  35. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1kZWZhdWx0LWV4cG9ydC5qcyJdLCJuYW1lcyI6WyJtb2R1bGUiLCJleHBvcnRzIiwib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