disable-enable-pair.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const DisabledArea = require("../internal/disabled-area")
  7. const utils = require("../internal/utils")
  8. module.exports = {
  9. meta: {
  10. docs: {
  11. description:
  12. "require a `eslint-enable` comment for every `eslint-disable` comment",
  13. category: "Best Practices",
  14. recommended: true,
  15. url:
  16. "https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/disable-enable-pair.html",
  17. },
  18. fixable: null,
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. allowWholeFile: {
  24. type: "boolean",
  25. },
  26. },
  27. additionalProperties: false,
  28. },
  29. ],
  30. type: "suggestion",
  31. },
  32. create(context) {
  33. const allowWholeFile =
  34. context.options[0] && context.options[0].allowWholeFile
  35. const sourceCode = context.getSourceCode()
  36. const disabledArea = DisabledArea.get(sourceCode)
  37. return {
  38. Program(node) {
  39. if (allowWholeFile && node.body.length === 0) {
  40. return
  41. }
  42. for (const area of disabledArea.areas) {
  43. if (area.end != null) {
  44. continue
  45. }
  46. if (
  47. allowWholeFile &&
  48. utils.lte(area.start, node.loc.start)
  49. ) {
  50. continue
  51. }
  52. context.report({
  53. loc: utils.toRuleIdLocation(area.comment, area.ruleId),
  54. message: area.ruleId
  55. ? "Requires 'eslint-enable' directive for '{{ruleId}}'."
  56. : "Requires 'eslint-enable' directive.",
  57. data: area,
  58. })
  59. }
  60. },
  61. }
  62. },
  63. }