summaryrefslogtreecommitdiff
path: root/apps/web/eslint-rules/no-comments.mjs
blob: 1a30c8c4fa994d4d1c9f7245709a3716c8f6e4c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const DIRECTIVE_PATTERNS = [
  /^\s*eslint-disable/,
  /^\s*eslint-enable/,
  /^\s*eslint-disable-next-line/,
  /^\s*eslint-disable-line/,
  /^\s*@ts-ignore/,
  /^\s*@ts-expect-error/,
  /^\s*@ts-nocheck/,
  /^\s*@ts-check/,
  /^\s*@type\s/,
  /^\s*@param\s/,
  /^\s*@returns?\s/,
  /^\s*@typedef\s/,
  /^\s*prettier-ignore/,
  /^\s*webpackChunkName/,
  /^\s*\/ <reference /,
]

function isDirectiveComment(value) {
  return DIRECTIVE_PATTERNS.some((pattern) => pattern.test(value))
}

const rule = {
  meta: {
    type: "suggestion",
    docs: {
      description: "disallow comments in favour of self-documenting code",
    },
    messages: {
      noComment:
        "avoid comments — code should be self-documenting. refactor to make the intent clear from the code itself.",
    },
    schema: [],
  },
  create(context) {
    const sourceCode = context.sourceCode ?? context.getSourceCode()

    return {
      Program() {
        for (const comment of sourceCode.getAllComments()) {
          const value = comment.value.trim()

          if (!value) continue

          if (isDirectiveComment(value)) continue

          context.report({
            loc: comment.loc,
            messageId: "noComment",
          })
        }
      },
    }
  },
}

const plugin = {
  meta: {
    name: "asa-no-comments",
    version: "1.0.0",
  },
  rules: {
    "no-comments": rule,
  },
}

export default plugin