prefer-es6-class.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @fileoverview Enforce ES5 or ES6 class for React Components
  3. * @author Dan Hamilton
  4. */
  5. 'use strict';
  6. const Components = require('../util/Components');
  7. const docsUrl = require('../util/docsUrl');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. description: 'Enforce ES5 or ES6 class for React Components',
  15. category: 'Stylistic Issues',
  16. recommended: false,
  17. url: docsUrl('prefer-es6-class')
  18. },
  19. messages: {
  20. shouldUseES6Class: 'Component should use es6 class instead of createClass',
  21. shouldUseCreateClass: 'Component should use createClass instead of es6 class'
  22. },
  23. schema: [{
  24. enum: ['always', 'never']
  25. }]
  26. },
  27. create: Components.detect((context, components, utils) => {
  28. const configuration = context.options[0] || 'always';
  29. return {
  30. ObjectExpression(node) {
  31. if (utils.isES5Component(node) && configuration === 'always') {
  32. context.report({
  33. node,
  34. messageId: 'shouldUseES6Class'
  35. });
  36. }
  37. },
  38. ClassDeclaration(node) {
  39. if (utils.isES6Component(node) && configuration === 'never') {
  40. context.report({
  41. node,
  42. messageId: 'shouldUseCreateClass'
  43. });
  44. }
  45. }
  46. };
  47. })
  48. };