no-anonymous-default-export.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
  3. var _has = require('has');var _has2 = _interopRequireDefault(_has);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} /**
  4. * @fileoverview Rule to disallow anonymous default exports.
  5. * @author Duncan Beevers
  6. */const defs = { ArrayExpression: {
  7. option: 'allowArray',
  8. description: 'If `false`, will report default export of an array',
  9. message: 'Assign array to a variable before exporting as module default' },
  10. ArrowFunctionExpression: {
  11. option: 'allowArrowFunction',
  12. description: 'If `false`, will report default export of an arrow function',
  13. message: 'Assign arrow function to a variable before exporting as module default' },
  14. CallExpression: {
  15. option: 'allowCallExpression',
  16. description: 'If `false`, will report default export of a function call',
  17. message: 'Assign call result to a variable before exporting as module default',
  18. default: true },
  19. ClassDeclaration: {
  20. option: 'allowAnonymousClass',
  21. description: 'If `false`, will report default export of an anonymous class',
  22. message: 'Unexpected default export of anonymous class',
  23. forbid: node => !node.declaration.id },
  24. FunctionDeclaration: {
  25. option: 'allowAnonymousFunction',
  26. description: 'If `false`, will report default export of an anonymous function',
  27. message: 'Unexpected default export of anonymous function',
  28. forbid: node => !node.declaration.id },
  29. Literal: {
  30. option: 'allowLiteral',
  31. description: 'If `false`, will report default export of a literal',
  32. message: 'Assign literal to a variable before exporting as module default' },
  33. ObjectExpression: {
  34. option: 'allowObject',
  35. description: 'If `false`, will report default export of an object expression',
  36. message: 'Assign object to a variable before exporting as module default' },
  37. TemplateLiteral: {
  38. option: 'allowLiteral',
  39. description: 'If `false`, will report default export of a literal',
  40. message: 'Assign literal to a variable before exporting as module default' } };
  41. const schemaProperties = Object.keys(defs).
  42. map(key => defs[key]).
  43. reduce((acc, def) => {
  44. acc[def.option] = {
  45. description: def.description,
  46. type: 'boolean' };
  47. return acc;
  48. }, {});
  49. const defaults = Object.keys(defs).
  50. map(key => defs[key]).
  51. reduce((acc, def) => {
  52. acc[def.option] = (0, _has2.default)(def, 'default') ? def.default : false;
  53. return acc;
  54. }, {});
  55. module.exports = {
  56. meta: {
  57. type: 'suggestion',
  58. docs: {
  59. url: (0, _docsUrl2.default)('no-anonymous-default-export') },
  60. schema: [
  61. {
  62. type: 'object',
  63. properties: schemaProperties,
  64. 'additionalProperties': false }] },
  65. create: function (context) {
  66. const options = Object.assign({}, defaults, context.options[0]);
  67. return {
  68. 'ExportDefaultDeclaration': node => {
  69. const def = defs[node.declaration.type];
  70. // Recognized node type and allowed by configuration,
  71. // and has no forbid check, or forbid check return value is truthy
  72. if (def && !options[def.option] && (!def.forbid || def.forbid(node))) {
  73. context.report({ node, message: def.message });
  74. }
  75. } };
  76. } };
  77. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1hbm9ueW1vdXMtZGVmYXVsdC1leHBvcnQuanMiXSwibmFtZXMiOlsiZGVmcyIsIkFycmF5RXhwcmVzc2lvbiIsIm9wdGlvbiIsImRlc2NyaXB0aW9uIiwibWVzc2FnZSIsIkFycm93RnVuY3Rpb25FeHByZXNzaW9uIiwiQ2FsbEV4cHJlc3Npb24iLCJkZWZhdWx0IiwiQ2xhc3NEZWNsYXJhdGlvbiIsImZvcmJpZCIsIm5vZGUiLCJkZWNsYXJhdGlvbiIsImlkIiwiRnVuY3Rpb25EZWNsYXJhdGlvbiIsIkxpdGVyYWwiLCJPYmplY3RFeHByZXNzaW9uIiwiVGVtcGxhdGVMaXRlcmFsIiwic2NoZW1hUHJvcGVydGllcyIsIk9iamVjdCIsImtleXMiLCJtYXAiLCJrZXkiLCJyZWR1Y2UiLCJhY2MiLCJkZWYiLCJ0eXBlIiwiZGVmYXVsdHMiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiL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