noFlowFixMeComments.js.flow 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const schema = [
  2. {
  3. type: 'string',
  4. },
  5. ];
  6. const message = '$FlowFixMe is treated as \'any\' and must be fixed.';
  7. const isIdentifier = (node, name) => (
  8. node && node.type === 'Identifier' && node.name.match(name)
  9. );
  10. const create = (context) => {
  11. const allowedPattern = context.options[0] ? new RegExp(context.options[0], 'u') : null;
  12. const extraMessage = allowedPattern ? ` Fix it or match '${allowedPattern.toString()}'.` : '';
  13. const passesExtraRegex = (value) => {
  14. if (!allowedPattern) {
  15. return false;
  16. }
  17. return value.match(allowedPattern);
  18. };
  19. const handleComment = (comment) => {
  20. const value = comment.value.trim();
  21. if (/\$FlowFixMe/u.test(value) && !passesExtraRegex(value)) {
  22. context.report({
  23. message: message + extraMessage,
  24. node: comment,
  25. });
  26. }
  27. };
  28. return {
  29. GenericTypeAnnotation(node) {
  30. if (isIdentifier(node.id, /\$FlowFixMe/u)) {
  31. context.report({
  32. message,
  33. node: node.id,
  34. });
  35. }
  36. },
  37. Program() {
  38. for (const comment of context
  39. .getSourceCode()
  40. .getAllComments()
  41. .filter((node) => node.type === 'Block' || node.type === 'Line')) {
  42. handleComment(comment);
  43. }
  44. },
  45. };
  46. };
  47. export default {
  48. create,
  49. schema,
  50. };