noFlowSuppressionsInStrictFiles.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. var _utilities = require("../utilities");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. 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; } } }; }
  10. 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); }
  11. 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; }
  12. var FLOW_STRICT_MATCHER = /^[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*@(?:no)?flow[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*strict(?:\x2Dlocal)?[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*$/;
  13. var isStrictFlowFile = function isStrictFlowFile(context) {
  14. return context.getAllComments().some(function (comment) {
  15. return FLOW_STRICT_MATCHER.test(comment.value);
  16. });
  17. };
  18. var message = 'No suppression comments are allowed in "strict" Flow files. Either remove the error suppression, or lower the strictness of this module.';
  19. var schema = [{
  20. additionalProperties: false,
  21. properties: {},
  22. type: 'object'
  23. }];
  24. _utilities.suppressionTypes.forEach(function (o) {
  25. schema[0].properties[o] = {
  26. type: 'boolean'
  27. };
  28. });
  29. var create = function create(context) {
  30. var suppressionOptions = _lodash["default"].get(context, 'options[0]', {});
  31. if (!isStrictFlowFile(context)) {
  32. // Skip this file - nothing to check here
  33. return {};
  34. }
  35. return {
  36. Program: function Program() {
  37. var comments = context.getSourceCode().getAllComments().filter(function (node) {
  38. return node.type === 'Block' || node.type === 'Line';
  39. });
  40. var _iterator = _createForOfIteratorHelper(comments),
  41. _step;
  42. try {
  43. var _loop = function _loop() {
  44. var commentNode = _step.value;
  45. var comment = commentNode.value.trimStart();
  46. var match = _utilities.suppressionTypes.some(function (prefix) {
  47. if (suppressionOptions[prefix] === false) return false;
  48. return comment.startsWith(prefix);
  49. });
  50. if (match) {
  51. context.report({
  52. message: message,
  53. node: commentNode
  54. });
  55. }
  56. };
  57. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  58. _loop();
  59. }
  60. } catch (err) {
  61. _iterator.e(err);
  62. } finally {
  63. _iterator.f();
  64. }
  65. }
  66. };
  67. };
  68. var _default = {
  69. create: create,
  70. schema: schema
  71. };
  72. exports["default"] = _default;
  73. module.exports = exports.default;