enforceSuppressionCode.js.flow 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { suppressionTypes } from '../utilities';
  2. const schema = [
  3. {
  4. type: 'string',
  5. },
  6. ];
  7. const message = (suppression = '') => `${suppression} is missing a suppression error code. Please update this suppression to use an error code: ${suppression}[…]`;
  8. const create = (context) => {
  9. const isMissingSuppressionCode = (value) => {
  10. let failedType;
  11. suppressionTypes.forEach((cur) => {
  12. if (value
  13. && value.startsWith(cur)
  14. && !value.startsWith(`${cur}[`)
  15. && !value.endsWith(']')) {
  16. failedType = cur;
  17. }
  18. });
  19. return failedType;
  20. };
  21. const handleComment = (comment) => {
  22. const value = comment.type === 'Block'
  23. ? comment.value.replace(/\*/g, '')
  24. : comment.value;
  25. const suppression = value.trim().split(' ').filter((arg) => arg.length > 0)[0];
  26. const failedType = isMissingSuppressionCode(suppression);
  27. if (failedType) {
  28. context.report(comment, message(failedType));
  29. }
  30. };
  31. return {
  32. Program() {
  33. context
  34. .getSourceCode()
  35. .getAllComments()
  36. .filter((comment) => comment.type === 'Block' || comment.type === 'Line')
  37. .forEach(handleComment);
  38. },
  39. };
  40. };
  41. export default {
  42. create,
  43. schema,
  44. };