noFlowFixMeComments.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 schema = [{
  10. type: 'string'
  11. }];
  12. var message = '$FlowFixMe is treated as \'any\' and must be fixed.';
  13. var isIdentifier = function isIdentifier(node, name) {
  14. return node && node.type === 'Identifier' && node.name.match(name);
  15. };
  16. var create = function create(context) {
  17. var allowedPattern = context.options[0] ? new RegExp(context.options[0], 'u') : null;
  18. var extraMessage = allowedPattern ? " Fix it or match '".concat(allowedPattern.toString(), "'.") : '';
  19. var passesExtraRegex = function passesExtraRegex(value) {
  20. if (!allowedPattern) {
  21. return false;
  22. }
  23. return value.match(allowedPattern);
  24. };
  25. var handleComment = function handleComment(comment) {
  26. var value = comment.value.trim();
  27. if (/\$FlowFixMe/.test(value) && !passesExtraRegex(value)) {
  28. context.report({
  29. message: message + extraMessage,
  30. node: comment
  31. });
  32. }
  33. };
  34. return {
  35. GenericTypeAnnotation: function GenericTypeAnnotation(node) {
  36. if (isIdentifier(node.id, /\$FlowFixMe/)) {
  37. context.report({
  38. message: message,
  39. node: node.id
  40. });
  41. }
  42. },
  43. Program: function Program() {
  44. var _iterator = _createForOfIteratorHelper(context.getSourceCode().getAllComments().filter(function (node) {
  45. return node.type === 'Block' || node.type === 'Line';
  46. })),
  47. _step;
  48. try {
  49. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  50. var comment = _step.value;
  51. handleComment(comment);
  52. }
  53. } catch (err) {
  54. _iterator.e(err);
  55. } finally {
  56. _iterator.f();
  57. }
  58. }
  59. };
  60. };
  61. var _default = {
  62. create: create,
  63. schema: schema
  64. };
  65. exports["default"] = _default;
  66. module.exports = exports.default;