no-abusive-eslint-disable.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const MESSAGE_ID = 'no-abusive-eslint-disable';
  3. const messages = {
  4. [MESSAGE_ID]: 'Specify the rules you want to disable.',
  5. };
  6. const disableRegex = /^eslint-disable(?:-next-line|-line)?(?<ruleId>$|(?:\s+(?:@(?:[\w-]+\/){1,2})?[\w-]+)?)/;
  7. /** @param {import('eslint').Rule.RuleContext} context */
  8. const create = () => ({
  9. * Program(node) {
  10. for (const comment of node.comments) {
  11. const value = comment.value.trim();
  12. const result = disableRegex.exec(value);
  13. if (
  14. result // It's a eslint-disable comment
  15. && !result.groups.ruleId // But it did not specify any rules
  16. ) {
  17. yield {
  18. // Can't set it at the given location as the warning
  19. // will be ignored due to the disable comment
  20. loc: {
  21. start: {
  22. ...comment.loc.start,
  23. column: -1,
  24. },
  25. end: comment.loc.end,
  26. },
  27. messageId: MESSAGE_ID,
  28. };
  29. }
  30. }
  31. },
  32. });
  33. /** @type {import('eslint').Rule.RuleModule} */
  34. module.exports = {
  35. create,
  36. meta: {
  37. type: 'suggestion',
  38. docs: {
  39. description: 'Enforce specifying rules to disable in `eslint-disable` comments.',
  40. },
  41. messages,
  42. },
  43. };