prefer-read-only-props.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /** @type {import('eslint').Rule.RuleModule} */
  41. module.exports = {
  42. meta: {
  43. docs: {
  44. description: 'Enforce that props are read-only',
  45. category: 'Stylistic Issues',
  46. recommended: false,
  47. url: docsUrl('prefer-read-only-props'),
  48. },
  49. fixable: 'code',
  50. messages,
  51. schema: [],
  52. },
  53. create: Components.detect((context, components) => {
  54. function reportReadOnlyProp(prop, propName, fixer) {
  55. report(context, messages.readOnlyProp, 'readOnlyProp', {
  56. node: prop.node,
  57. data: {
  58. name: propName,
  59. },
  60. fix: fixer,
  61. });
  62. }
  63. return {
  64. 'Program:exit'() {
  65. flatMap(
  66. values(components.list()),
  67. (component) => component.declaredPropTypes || []
  68. ).forEach((declaredPropTypes) => {
  69. Object.keys(declaredPropTypes).forEach((propName) => {
  70. const prop = declaredPropTypes[propName];
  71. if (!prop.node) {
  72. return;
  73. }
  74. if (isFlowPropertyType(prop.node)) {
  75. if (!isCovariant(prop.node)) {
  76. reportReadOnlyProp(prop, propName, (fixer) => {
  77. if (!prop.node.variance) {
  78. // Insert covariance
  79. return fixer.insertTextBefore(prop.node, '+');
  80. }
  81. // Replace contravariance with covariance
  82. return fixer.replaceText(prop.node.variance, '+');
  83. });
  84. }
  85. return;
  86. }
  87. if (isTypescriptPropertyType(prop.node)) {
  88. if (!isReadonly(prop.node)) {
  89. reportReadOnlyProp(prop, propName, (fixer) => (
  90. fixer.insertTextBefore(prop.node, 'readonly ')
  91. ));
  92. }
  93. }
  94. });
  95. });
  96. },
  97. };
  98. }),
  99. };