prefer-read-only-props.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @fileoverview Require component props to be typed as read-only.
  3. * @author Luke Zapart
  4. */
  5. 'use strict';
  6. const flatMap = require('array.prototype.flatmap');
  7. const values = require('object.values');
  8. const Components = require('../util/Components');
  9. const docsUrl = require('../util/docsUrl');
  10. const report = require('../util/report');
  11. function isFlowPropertyType(node) {
  12. return node.type === 'ObjectTypeProperty';
  13. }
  14. function isTypescriptPropertyType(node) {
  15. return node.type === 'TSPropertySignature';
  16. }
  17. function isCovariant(node) {
  18. return (node.variance && node.variance.kind === 'plus')
  19. || (
  20. node.parent
  21. && node.parent.parent
  22. && node.parent.parent.parent
  23. && node.parent.parent.parent.id
  24. && node.parent.parent.parent.id.name === '$ReadOnly'
  25. );
  26. }
  27. function isReadonly(node) {
  28. return (
  29. node.typeAnnotation
  30. && node.typeAnnotation.parent
  31. && node.typeAnnotation.parent.readonly
  32. );
  33. }
  34. // ------------------------------------------------------------------------------
  35. // Rule Definition
  36. // ------------------------------------------------------------------------------
  37. const messages = {
  38. readOnlyProp: 'Prop \'{{name}}\' should be read-only.',
  39. };
  40. module.exports = {
  41. meta: {
  42. docs: {
  43. description: 'Enforce that props are read-only',
  44. category: 'Stylistic Issues',
  45. recommended: false,
  46. url: docsUrl('prefer-read-only-props'),
  47. },
  48. fixable: 'code',
  49. messages,
  50. schema: [],
  51. },
  52. create: Components.detect((context, components) => {
  53. function reportReadOnlyProp(prop, propName, fixer) {
  54. report(context, messages.readOnlyProp, 'readOnlyProp', {
  55. node: prop.node,
  56. data: {
  57. name: propName,
  58. },
  59. fix: fixer,
  60. });
  61. }
  62. return {
  63. 'Program:exit'() {
  64. flatMap(
  65. values(components.list()),
  66. (component) => component.declaredPropTypes || []
  67. ).forEach((declaredPropTypes) => {
  68. Object.keys(declaredPropTypes).forEach((propName) => {
  69. const prop = declaredPropTypes[propName];
  70. if (!prop.node) {
  71. return;
  72. }
  73. if (isFlowPropertyType(prop.node)) {
  74. if (!isCovariant(prop.node)) {
  75. reportReadOnlyProp(prop, propName, (fixer) => {
  76. if (!prop.node.variance) {
  77. // Insert covariance
  78. return fixer.insertTextBefore(prop.node, '+');
  79. }
  80. // Replace contravariance with covariance
  81. return fixer.replaceText(prop.node.variance, '+');
  82. });
  83. }
  84. return;
  85. }
  86. if (isTypescriptPropertyType(prop.node)) {
  87. if (!isReadonly(prop.node)) {
  88. reportReadOnlyProp(prop, propName, (fixer) => (
  89. fixer.insertTextBefore(prop.node, 'readonly ')
  90. ));
  91. }
  92. }
  93. });
  94. });
  95. },
  96. };
  97. }),
  98. };