@yuu1111/comment-check 0.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja.md ADDED
@@ -0,0 +1,49 @@
1
+ [English](README.md)
2
+
3
+ # @yuu1111/comment-check
4
+
5
+ baselineを持つ小さなcomment検査 抑制commentとplaceholder commentの増殖を止める
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ bun add -D @yuu1111/comment-check
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ 現在の検出を一度baselineへ記録し、新しく出たものだけを失敗にする
16
+
17
+ ```bash
18
+ comment-check --update-baseline .
19
+ comment-check .
20
+ ```
21
+
22
+ ```
23
+ src/queue.ts:18:2 undocumented-directive TypeScript directive needs a description
24
+ Checked 42 files: 1 new, 0 resolved, 3 baselined
25
+ ```
26
+
27
+ ## Rules
28
+
29
+ | Rule | 検出対象 |
30
+ |------|---------|
31
+ | `broad-suppression` | `biome-ignore-all`、`@ts-nocheck`、ruleを書いていない `eslint-disable` |
32
+ | `undocumented-directive` | 説明の無い `@ts-ignore` と `@ts-expect-error` |
33
+ | `placeholder-comment` | `TODO`、`FIXME`、`XXX`、`HACK` |
34
+ | `separator-comment` | 記号だけで作った装飾comment |
35
+
36
+ ## Options
37
+
38
+ | Option | Description |
39
+ |--------|-------------|
40
+ | `--baseline <path>` | 読み書きするbaseline file(既定は `comment-baseline.json`) |
41
+ | `--ignore <path>` | 検査から外すpath 複数指定できる |
42
+ | `--update-baseline` | baselineを現在の検出で置き換える |
43
+ | `--json` | 新規と解消済みの検出をJSONで出力する |
44
+
45
+ ## Notes
46
+
47
+ 生成物のディレクトリなどProjectが持つpathは `--ignore` で検査から外す 例えば `--ignore src/generated`
48
+
49
+ baselineはrule、file、comment本文をキーにするため、行が動いてもそのcommentを新規とは報告しない Biomeは `biome-ignore` の理由と未使用の抑制を既に扱うため、このcheckerはBiomeが読まないcommentの細部だけを担当する
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [日本語](README.ja.md)
2
+
1
3
  # @yuu1111/comment-check
2
4
 
3
5
  Small comment checker with a baseline, used to keep suppressions and placeholder
package/package.json CHANGED
@@ -1,26 +1,27 @@
1
- {
2
- "name": "@yuu1111/comment-check",
3
- "version": "0.0.0",
4
- "description": "Shared comment and suppression checker",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/yuu1111/configs.git",
9
- "directory": "packages/comment-check"
10
- },
11
- "type": "module",
12
- "bin": {
13
- "comment-check": "src/cli.ts"
14
- },
15
- "exports": {
16
- ".": "./src/scan.ts"
17
- },
18
- "files": [
19
- "src"
20
- ],
21
- "keywords": [
22
- "comment",
23
- "lint",
24
- "suppression"
25
- ]
26
- }
1
+ {
2
+ "name": "@yuu1111/comment-check",
3
+ "version": "1.0.1",
4
+ "description": "Shared comment and suppression checker",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/yuu1111/configs.git",
9
+ "directory": "packages/comment-check"
10
+ },
11
+ "type": "module",
12
+ "bin": {
13
+ "comment-check": "src/cli.ts"
14
+ },
15
+ "exports": {
16
+ ".": "./src/scan.ts"
17
+ },
18
+ "files": [
19
+ "README.ja.md",
20
+ "src"
21
+ ],
22
+ "keywords": [
23
+ "comment",
24
+ "lint",
25
+ "suppression"
26
+ ]
27
+ }
package/src/baseline.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Finding } from "./rules";
2
2
 
3
+ /** baselineに記録する指摘1件の識別情報と件数 */
3
4
  export interface BaselineEntry {
4
5
  rule: string;
5
6
  file: string;
@@ -7,16 +8,19 @@ export interface BaselineEntry {
7
8
  count: number;
8
9
  }
9
10
 
11
+ /** baseline fileの形式と記録済みentryの一覧 */
10
12
  export interface BaselineFile {
11
13
  version: 1;
12
14
  entries: BaselineEntry[];
13
15
  }
14
16
 
17
+ /** baselineと現在の指摘を比較した結果 */
15
18
  export interface BaselineComparison {
16
19
  added: Finding[];
17
20
  resolved: BaselineEntry[];
18
21
  }
19
22
 
23
+ /** entryをbaseline上で一意に識別するkeyを返す */
20
24
  export function entryKey(entry: {
21
25
  rule: string;
22
26
  file: string;
@@ -34,6 +38,7 @@ function compareEntries(left: BaselineEntry, right: BaselineEntry): number {
34
38
  return leftKey < rightKey ? -1 : 1;
35
39
  }
36
40
 
41
+ /** 指摘一覧を件数付きentryへ集計してbaselineを作成する */
37
42
  export function createBaseline(findings: Finding[]): BaselineFile {
38
43
  const entries = new Map<string, BaselineEntry>();
39
44
  for (const finding of findings) {
@@ -53,6 +58,7 @@ export function createBaseline(findings: Finding[]): BaselineFile {
53
58
  return { version: 1, entries: [...entries.values()].sort(compareEntries) };
54
59
  }
55
60
 
61
+ /** 現在の指摘とbaselineを突き合わせ、新規追加分と解消済み分を求める */
56
62
  export function compareWithBaseline(
57
63
  findings: Finding[],
58
64
  baseline: BaselineFile,
package/src/cli.ts CHANGED
File without changes
package/src/comments.ts CHANGED
@@ -1,5 +1,7 @@
1
+ /** 抽出するcommentの種別 */
1
2
  export type CommentKind = "line" | "block";
2
3
 
4
+ /** 抽出したcomment1件の種別と原文上の位置 */
3
5
  export interface CommentPiece {
4
6
  kind: CommentKind;
5
7
  text: string;
@@ -226,6 +228,7 @@ function step(state: ScanState, source: string): void {
226
228
  stepExpression(state, source);
227
229
  }
228
230
 
231
+ /** 文字列や正規表現リテラルを除外してsourceからcommentを抽出する */
229
232
  export function extractComments(source: string): CommentPiece[] {
230
233
  const shebang = source.startsWith("#!") ? source.indexOf("\n") : 0;
231
234
  const state: ScanState = {
package/src/rules.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /** comment-checkが報告するruleの識別子一覧 */
1
2
  export const RULE_IDS = [
2
3
  "broad-suppression",
3
4
  "undocumented-directive",
@@ -5,8 +6,10 @@ export const RULE_IDS = [
5
6
  "separator-comment",
6
7
  ] as const;
7
8
 
9
+ /** RULE_IDSが定義するrule識別子のunion型 */
8
10
  export type RuleId = (typeof RULE_IDS)[number];
9
11
 
12
+ /** 検出したcomment違反1件の内容と位置 */
10
13
  export interface Finding {
11
14
  rule: RuleId;
12
15
  file: string;
@@ -19,6 +22,7 @@ const PLACEHOLDER_PATTERN = /\b(TODO|FIXME|XXX|HACK)\b/;
19
22
  const SEPARATOR_PATTERN = /^[-=*_#~+./\\|]{4,}$/;
20
23
  const DIRECTIVE_PATTERN = /^@ts-(?:ignore|expect-error)\b([\s\S]*)$/;
21
24
 
25
+ /** block commentの記号を除いて空白を揃えた本文を返す */
22
26
  export function normalizeComment(body: string): string {
23
27
  return body
24
28
  .split("\n")
@@ -28,6 +32,7 @@ export function normalizeComment(body: string): string {
28
32
  .trim();
29
33
  }
30
34
 
35
+ /** comment本文を分類し、該当するruleがなければnullを返す */
31
36
  export function classifyComment(body: string): RuleId | null {
32
37
  const text = normalizeComment(body);
33
38
  if (text === "") {
package/src/scan.ts CHANGED
@@ -22,6 +22,7 @@ const IGNORED_DIRECTORIES = new Set([
22
22
  "vendor",
23
23
  ]);
24
24
 
25
+ /** 区切り文字を統一し先頭の./と末尾の/を除いたpathを返す */
25
26
  export function normalizePath(path: string): string {
26
27
  return path.split("\\").join("/").replace(/^\.\//, "").replace(/\/+$/, "");
27
28
  }
@@ -75,6 +76,7 @@ function walk(directory: string, files: Set<string>): void {
75
76
  }
76
77
  }
77
78
 
79
+ /** 対象pathを走査して検査対象のfile一覧を集める */
78
80
  export function collectFiles(
79
81
  targets: string[],
80
82
  cwd = process.cwd(),
@@ -105,6 +107,7 @@ export function collectFiles(
105
107
  .sort();
106
108
  }
107
109
 
110
+ /** source文字列を走査してcomment違反を検出する */
108
111
  export function scanSource(source: string, file: string): Finding[] {
109
112
  const findings: Finding[] = [];
110
113
  for (const comment of extractComments(source)) {
@@ -124,6 +127,7 @@ export function scanSource(source: string, file: string): Finding[] {
124
127
  return findings;
125
128
  }
126
129
 
130
+ /** fileを読み込んでcomment違反を検出する */
127
131
  export function scanFile(file: string, cwd = process.cwd()): Finding[] {
128
132
  return scanSource(
129
133
  readFileSync(file, "utf8"),
@@ -131,6 +135,7 @@ export function scanFile(file: string, cwd = process.cwd()): Finding[] {
131
135
  );
132
136
  }
133
137
 
138
+ /** 複数fileの違反をまとめて位置順に並べる */
134
139
  export function scanFiles(files: string[], cwd = process.cwd()): Finding[] {
135
140
  const findings: Finding[] = [];
136
141
  for (const file of files) {