no-use.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 utils = require("../internal/utils")
  7. module.exports = {
  8. meta: {
  9. docs: {
  10. description: "disallow ESLint directive-comments",
  11. category: "Stylistic Issues",
  12. recommended: false,
  13. url:
  14. "https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/no-use.html",
  15. },
  16. fixable: null,
  17. schema: [
  18. {
  19. type: "object",
  20. properties: {
  21. allow: {
  22. type: "array",
  23. items: {
  24. enum: [
  25. "eslint",
  26. "eslint-disable",
  27. "eslint-disable-line",
  28. "eslint-disable-next-line",
  29. "eslint-enable",
  30. "eslint-env",
  31. "exported",
  32. "global",
  33. "globals",
  34. ],
  35. },
  36. additionalItems: false,
  37. uniqueItems: true,
  38. },
  39. },
  40. additionalProperties: false,
  41. },
  42. ],
  43. type: "suggestion",
  44. },
  45. create(context) {
  46. const sourceCode = context.getSourceCode()
  47. const allowed = new Set(
  48. (context.options[0] && context.options[0].allow) || []
  49. )
  50. return {
  51. Program() {
  52. for (const comment of sourceCode.getAllComments()) {
  53. const directiveComment = utils.parseDirectiveComment(
  54. comment
  55. )
  56. if (directiveComment == null) {
  57. continue
  58. }
  59. if (!allowed.has(directiveComment.kind)) {
  60. context.report({
  61. loc: utils.toForceLocation(comment.loc),
  62. message: "Unexpected ESLint directive comment.",
  63. })
  64. }
  65. }
  66. },
  67. }
  68. },
  69. }