noFlowSuppressionsInStrictFiles.js.flow 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // @flow
  2. import _ from 'lodash';
  3. import type { Rule$Create } from 'eslint';
  4. import { suppressionTypes } from '../utilities';
  5. const FLOW_STRICT_MATCHER = /^\s*@(?:no)?flow\s*strict(?:-local)?\s*$/u;
  6. const isStrictFlowFile = (context) => context
  7. .getAllComments()
  8. .some((comment) => FLOW_STRICT_MATCHER.test(comment.value));
  9. const message = 'No suppression comments are allowed in "strict" Flow files. Either remove the error suppression, or lower the strictness of this module.';
  10. const schema = [
  11. {
  12. additionalProperties: false,
  13. properties: ({}: { [key: string]: {| type: 'boolean' |}}),
  14. type: 'object',
  15. },
  16. ];
  17. suppressionTypes.forEach((o) => {
  18. schema[0].properties[o] = {
  19. type: 'boolean',
  20. };
  21. });
  22. const create: Rule$Create = (context) => {
  23. const suppressionOptions = _.get(context, 'options[0]', {});
  24. if (!isStrictFlowFile(context)) {
  25. // Skip this file - nothing to check here
  26. return {};
  27. }
  28. return {
  29. Program: () => {
  30. const comments = context
  31. .getSourceCode()
  32. .getAllComments()
  33. .filter((node) => node.type === 'Block' || node.type === 'Line');
  34. for (const commentNode of comments) {
  35. const comment = commentNode.value.trimStart();
  36. const match = suppressionTypes.some((prefix) => {
  37. if (suppressionOptions[prefix] === false) return false;
  38. return comment.startsWith(prefix);
  39. });
  40. if (match) {
  41. context.report({
  42. message,
  43. node: commentNode,
  44. });
  45. }
  46. }
  47. },
  48. };
  49. };
  50. export default {
  51. create,
  52. schema,
  53. };