useReadOnlySpread.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  7. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  8. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  9. var meta = {
  10. messages: {
  11. readonlySpread: 'Flow type with spread property and all readonly properties must be ' + 'wrapped in \'$ReadOnly<…>\' to prevent accidental loss of readonly-ness.'
  12. }
  13. };
  14. var create = function create(context) {
  15. return {
  16. TypeAlias: function TypeAlias(node) {
  17. if (node.right.type === 'GenericTypeAnnotation' && node.right.id.name === '$ReadOnly') {// it's already $ReadOnly<…>, nothing to do
  18. } else if (node.right.type === 'ObjectTypeAnnotation') {
  19. // let's iterate all props and if everything is readonly then throw
  20. var shouldThrow = false;
  21. var hasSpread = false;
  22. var _iterator = _createForOfIteratorHelper(node.right.properties),
  23. _step;
  24. try {
  25. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  26. var property = _step.value;
  27. if (property.type === 'ObjectTypeProperty') {
  28. if (property.variance && property.variance.kind === 'plus') {
  29. shouldThrow = true;
  30. } else {
  31. shouldThrow = false;
  32. break;
  33. }
  34. } else if (property.type === 'ObjectTypeSpreadProperty') {
  35. hasSpread = true;
  36. }
  37. }
  38. } catch (err) {
  39. _iterator.e(err);
  40. } finally {
  41. _iterator.f();
  42. }
  43. if (hasSpread === true && shouldThrow === true) {
  44. context.report({
  45. messageId: 'readonlySpread',
  46. node: node.right
  47. });
  48. }
  49. }
  50. }
  51. };
  52. };
  53. var _default = {
  54. create: create,
  55. meta: meta
  56. };
  57. exports["default"] = _default;
  58. module.exports = exports.default;