@chengzhao-dev/dsh-cpp-workflow 0.7.9 → 0.7.11
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.en.md +8 -7
- package/README.md +5 -4
- package/cordis.patch.yml +2 -2
- package/icon.svg +5 -5
- package/lib/client.js +542 -538
- package/lib/comment-policy.js +37 -33
- package/lib/workspace.js +8 -10
- package/locale/en.json +4 -4
- package/locale/zh.json +4 -4
- package/package.json +100 -100
- package/skills/building-cpp/SKILL.md +5 -5
- package/skills/building-cpp/assets/config/.clang-tidy +4 -5
- package/skills/building-cpp/assets/config/.clangd +5 -7
- package/skills/building-cpp/assets/config/.vscode/extensions.json +5 -5
- package/skills/building-cpp/assets/config/.vscode/settings.json +56 -58
- package/skills/building-cpp/examples/environment.md +13 -102
- package/skills/building-cpp/references/01-build-script.md +3 -3
- package/skills/building-cpp/references/02-environment.md +8 -8
- package/skills/building-cpp/scripts/checking-environment.mjs +424 -424
- package/skills/building-cpp/steps/01-build.md +10 -10
- package/skills/building-cpp/steps/02-tooling.md +6 -6
- package/skills/building-cpp/steps/03-check.md +2 -2
- package/skills/building-cpp/steps/04-vscode.md +4 -6
- package/skills/developing-cpp/SKILL.md +8 -9
- package/skills/developing-cpp/references/comments/01-comment-rules.md +50 -26
- package/skills/developing-cpp/references/comments/02-style.md +33 -25
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +14 -14
- package/skills/developing-cpp/references/comments/examples/04-declaration-file.md +12 -8
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +20 -10
- package/skills/developing-cpp/references/comments/examples/06-declaration-member.md +4 -4
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +2 -2
- package/skills/developing-cpp/references/comments/examples/08-declaration-struct.md +6 -6
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +41 -41
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +17 -16
- package/skills/developing-cpp/references/design/01-project-design.md +21 -10
- package/skills/developing-cpp/references/design/02-raii.md +5 -4
- package/skills/developing-cpp/references/design/03-third-party.md +2 -2
- package/skills/developing-cpp/references/design/examples/02-coordinate-table.md +12 -12
- package/skills/developing-cpp/references/design/examples/03-header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/04-library-boundary.md +6 -6
- package/skills/developing-cpp/references/design/examples/06-move-result.md +29 -28
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +24 -23
- package/skills/developing-cpp/references/design/examples/08-screen-enter-exit.md +19 -18
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +6 -6
- package/skills/developing-cpp/references/design/session-layer.md +13 -22
- package/skills/developing-cpp/references/exceptions/01-exception-rules.md +57 -57
- package/skills/developing-cpp/references/exceptions/examples/01-throw-boundary.md +7 -20
- package/skills/developing-cpp/references/exceptions/examples/02-throw-build-mode.md +10 -11
- package/skills/developing-cpp/references/exceptions/examples/03-throw-catch.md +4 -4
- package/skills/developing-cpp/references/exceptions/examples/04-throw-destructor.md +9 -5
- package/skills/developing-cpp/references/exceptions/examples/07-throw-when.md +2 -2
- package/skills/developing-cpp/references/index.md +32 -35
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +3 -2
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +9 -2
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +2 -3
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +3 -3
- package/skills/developing-cpp/references/language/language-basics/10-language-checklist.md +9 -9
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -1
- package/skills/developing-cpp/references/language/performance/07-checklist.md +6 -6
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +4 -5
- package/skills/developing-cpp/references/language/standard-library/02-access.md +2 -5
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +22 -20
- package/skills/developing-cpp/references/language/standard-library/{11-format-ecosystem.md → 07-format-ecosystem.md} +2 -11
- package/skills/developing-cpp/references/language/standard-library/{07-time.md → 08-time.md} +1 -1
- package/skills/developing-cpp/references/language/standard-library/{09-files.md → 10-files.md} +1 -1
- package/skills/developing-cpp/references/language/standard-library/11-library-checklist.md +8 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +7 -7
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +14 -2
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +2 -2
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +5 -5
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -1
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +1 -1
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +7 -7
- package/skills/developing-cpp/references/layout/01-source-layout.md +13 -13
- package/skills/developing-cpp/references/layout/02-file-template.md +10 -10
- package/skills/developing-cpp/references/layout/03-declaration-order.md +83 -42
- package/skills/developing-cpp/references/naming/01-identifier-names.md +32 -30
- package/skills/developing-cpp/references/naming/examples/06-naming-lower-layer.md +15 -15
- package/skills/developing-cpp/references/naming/examples/07-naming-multiple-return.md +2 -2
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +24 -23
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/02-position.md +9 -9
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +5 -1
- package/skills/developing-cpp/references/naming/glossary/04-square.md +3 -3
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +2 -2
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +7 -7
- package/skills/developing-cpp/references/naming/glossary/07-stream.md +1 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +13 -23
- package/skills/developing-cpp/scripts/checking-comments.mjs +961 -755
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +512 -515
- package/skills/developing-cpp/steps/01-behavior.md +0 -1
- package/skills/developing-cpp/steps/02-boundary.md +2 -3
- package/skills/developing-cpp/steps/03-declaration.md +9 -10
- package/skills/developing-cpp/steps/04-implementation.md +7 -8
- package/skills/engineering/SKILL.md +9 -10
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +1 -1
- package/skills/engineering/references/cmake/linking/02-rpath.md +5 -6
- package/skills/engineering/references/cmake/modules/01-functions.md +9 -9
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +6 -6
- package/skills/engineering/references/cmake/standards/index.md +8 -29
- package/skills/engineering/references/cmake/targets/01-target-rules.md +14 -13
- package/skills/engineering/references/cmake/targets/examples/01-rpath.md +1 -1
- package/skills/engineering/references/cmake/testing/01-ctest.md +3 -3
- package/skills/engineering/references/formatting/01-encoding.md +5 -5
- package/skills/engineering/references/formatting/02-clang-format.md +2 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +5 -5
- package/skills/engineering/references/formatting/04-clangd.md +32 -39
- package/skills/engineering/references/formatting/05-config-comments.md +88 -0
- package/skills/engineering/references/formatting/examples/02-clang-tidy.md +6 -6
- package/skills/engineering/references/formatting/examples/03-clangd.md +17 -17
- package/skills/engineering/references/index.md +5 -4
- package/skills/engineering/scripts/checking-encoding.mjs +369 -369
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +1 -2
- package/skills/engineering/steps/03-check.md +2 -3
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/templates/cpp-workflow-project/environment.json +4 -4
- package/skills/developing-cpp/references/design/examples/05-library-split.md +0 -8
- package/skills/developing-cpp/references/language/standard-library/10-library-checklist.md +0 -10
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +0 -4
- package/skills/developing-cpp/references/naming/examples/10-naming-rename.md +0 -6
- package/skills/engineering/references/formatting/examples/04-config-comments.md +0 -70
- /package/skills/developing-cpp/references/language/standard-library/{08-random.md → 09-random.md} +0 -0
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// 只读注释审计:按 references/comments/01-comment-rules.md 的硬规则检查 C/C++ 源文件。
|
|
3
3
|
//
|
|
4
|
-
// 检查项:Doxygen
|
|
5
|
-
// 成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param
|
|
4
|
+
// 检查项:Doxygen 文件头、注释行宽度(含行首缩进)、列表缩进、标签、标签间空行、注释与声明的贴合、
|
|
5
|
+
// 成员功能清单、设计论证(方案对比)、否定式边界、领域尺寸、谓词首句、@param 完整性、@return 完整性、
|
|
6
|
+
// @retval 顺序、特殊成员首句、函数体内 ///、摘要裸参数名、枚举成员注释一致性、枚举行尾句式(不收句号)、
|
|
7
|
+
// 数据成员主注释位置、控制序列常量注释、定义处重复声明注释、只在 .cpp 出现的函数契约;
|
|
8
|
+
// 另查实现注释复述下一行声明、指向别的头文件或目录、注释代替类型、能力边界、枚举行尾空冒号。
|
|
6
9
|
// 不重写任何文件。
|
|
7
10
|
//
|
|
8
11
|
// 用法:
|
|
9
|
-
// node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--json] [--quiet]
|
|
12
|
+
// node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--no-brief] [--no-param] [--no-return] [--allow-tag TAG] [--json] [--quiet] [--help]
|
|
10
13
|
//
|
|
11
14
|
// 退出码:0 无问题,1 发现问题,2 用法或读取错误。
|
|
12
15
|
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
|
@@ -16,15 +19,15 @@ import { pathToFileURL } from 'node:url'
|
|
|
16
19
|
export const EXIT = { ok: 0, issues: 1, usage: 2 }
|
|
17
20
|
|
|
18
21
|
const SOURCE_EXT = new Set([
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
'.h', '.hh', '.hpp', '.hxx', '.h++', '.inl', '.ipp', '.tpp',
|
|
23
|
+
'.cpp', '.cc', '.cxx', '.c++', '.c', '.m', '.mm',
|
|
21
24
|
])
|
|
22
25
|
const HEADER_EXT = new Set(['.h', '.hh', '.hpp', '.hxx', '.h++', '.inl', '.ipp', '.tpp'])
|
|
23
26
|
const CPP_EXT = new Set(['.cpp', '.cc', '.cxx', '.c++', '.c'])
|
|
24
27
|
const SKIP_DIR = new Set([
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
'.git', '.hg', '.svn', 'build', 'build-release', 'out', 'node_modules', '_book', '.quarto', '.cache',
|
|
29
|
+
'.tmp', 'temp', 'third_party', 'third-party', 'external', 'vendor', 'deps', 'cmake-build-debug',
|
|
30
|
+
'cmake-build-release', '.vs', '.idea',
|
|
28
31
|
])
|
|
29
32
|
const STATIC_FORBIDDEN_TAGS = ['@returns', '@details', '@since', '@todo']
|
|
30
33
|
const PRIVACY_TAGS = ['@author', '@date', '@copyright', '@version']
|
|
@@ -45,852 +48,1055 @@ const ESCAPE_SEQUENCE = /\\033|\\x1[bB]|\\e(?![A-Za-z0-9])/
|
|
|
45
48
|
const STRING_SEQUENCE_CONSTANT = /^(?:static\s+)?(?:inline\s+)?constexpr\s+(?:std::string_view|(?:const\s+)?char\s*(?:const\s*)?\*)\s+([A-Za-z_]\w*)\s*=\s*"((?:[^"\\]|\\.)*)"\s*;/
|
|
46
49
|
const CHAR_SEQUENCE_CONSTANT = /^(?:static\s+)?(?:inline\s+)?constexpr\s+char\s+([A-Za-z_]\w*)\s*=\s*'(\\033|\\x1[bB]|\\e)'\s*;/
|
|
47
50
|
const SEQUENCE_FIRST = /的(?:控制|转义)序列(?::.+)?。$/
|
|
48
|
-
const DOMAIN_SIZE = /\d+\s*[×xX]\s*\d
|
|
51
|
+
const DOMAIN_SIZE = /\d+\s*[×xX]\s*\d+|\d+\s*行\s*\d+\s*列|九个?方?格|边长/
|
|
52
|
+
const SPECIAL_MEMBER_VAGUE = /创建对象|建立初始状态|初始化成员/
|
|
49
53
|
const PREDICATE_DECL = /^(?:\[\[nodiscard\]\]\s*)?(?:static\s+)?bool\s+(?:Is|Has|Can|Completes|Would)[A-Za-z0-9_]*\s*\(|operator==/
|
|
50
54
|
const PREDICATE_BANNED_PREFIX = /^(?:检查|判断|校验|验证)/
|
|
51
55
|
const isPredicateFirstOk = (declaration, firstDocLine) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
declaration.includes('operator==')
|
|
57
|
+
? firstDocLine.startsWith('相等运算符')
|
|
58
|
+
: firstDocLine.includes('是否') && !PREDICATE_BANNED_PREFIX.test(firstDocLine)
|
|
55
59
|
const DECL_KEYWORDS = /^(class|struct|union|enum|namespace|using|typedef|template|#|if|else|for|while|switch|return|case|default)\b/
|
|
56
60
|
|
|
61
|
+
const USAGE = '用法:node checking-comments.mjs [--root DIR] [--width N] [--comment-style slash|block] [--no-header] [--no-brief] [--no-param] [--no-return] [--allow-tag TAG] [--json] [--quiet] [--help]\n'
|
|
62
|
+
|
|
57
63
|
function usage(message) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
if (message) process.stderr.write(`错误:${message}\n`)
|
|
65
|
+
process.stderr.write(USAGE)
|
|
66
|
+
return EXIT.usage
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function help() {
|
|
70
|
+
process.stdout.write(USAGE)
|
|
71
|
+
return EXIT.ok
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
function parseArgs(argv) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
75
|
+
const options = { root: undefined, width: 120, widthExplicit: false, commentStyle: 'slash', requireHeader: true, requireBrief: true, requireParam: true, requireReturn: true, allowTags: new Set(), json: false, quiet: false }
|
|
76
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
77
|
+
const arg = argv[index]
|
|
78
|
+
if (arg === '--root') {
|
|
79
|
+
options.root = argv[index + 1]
|
|
80
|
+
index += 1
|
|
81
|
+
} else if (arg === '--width') {
|
|
82
|
+
options.width = Number(argv[index + 1])
|
|
83
|
+
options.widthExplicit = true
|
|
84
|
+
index += 1
|
|
85
|
+
} else if (arg === '--comment-style') {
|
|
86
|
+
const value = argv[index + 1]
|
|
87
|
+
if (value !== 'slash' && value !== 'block') throw new Error('--comment-style 只接受 slash 或 block')
|
|
88
|
+
options.commentStyle = value
|
|
89
|
+
index += 1
|
|
90
|
+
} else if (arg === '--no-header') {
|
|
91
|
+
options.requireHeader = false
|
|
92
|
+
} else if (arg === '--no-brief') {
|
|
93
|
+
options.requireBrief = false
|
|
94
|
+
} else if (arg === '--no-param') {
|
|
95
|
+
options.requireParam = false
|
|
96
|
+
} else if (arg === '--no-return') {
|
|
97
|
+
options.requireReturn = false
|
|
98
|
+
} else if (arg === '--allow-tag') {
|
|
99
|
+
const value = argv[index + 1]
|
|
100
|
+
if (!value) throw new Error('--allow-tag 需要标签名,例如 author')
|
|
101
|
+
options.allowTags.add(value.replace(/^@/, ''))
|
|
102
|
+
index += 1
|
|
103
|
+
} else if (arg === '--json') {
|
|
104
|
+
options.json = true
|
|
105
|
+
} else if (arg === '--quiet') {
|
|
106
|
+
options.quiet = true
|
|
107
|
+
} else if (arg === '--help' || arg === '-h') {
|
|
108
|
+
options.help = true
|
|
109
|
+
} else {
|
|
110
|
+
throw new Error(`未知参数 ${arg}`)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!Number.isInteger(options.width) || options.width < 40) {
|
|
114
|
+
throw new Error('--width 需要不小于 40 的整数')
|
|
115
|
+
}
|
|
116
|
+
return options
|
|
102
117
|
}
|
|
103
118
|
|
|
104
119
|
/** 从指定目录的 .clang-format 读取 ColumnLimit;没有配置时返回 undefined。 */
|
|
105
120
|
function readColumnLimit(root) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
const dir = statSync(root).isDirectory() ? root : path.dirname(root)
|
|
122
|
+
try {
|
|
123
|
+
const text = readFileSync(path.join(dir, '.clang-format'), 'utf8')
|
|
124
|
+
const match = text.match(/^\s*ColumnLimit:\s*(\d+)\s*$/m)
|
|
125
|
+
return match ? Number(match[1]) : undefined
|
|
126
|
+
} catch {
|
|
127
|
+
return undefined
|
|
128
|
+
}
|
|
114
129
|
}
|
|
115
130
|
|
|
116
131
|
/** 从文件所在目录向上查找最近的 .clang-format,直到工程根。 */
|
|
117
132
|
function readColumnLimitNear(file, root) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
const stop = path.resolve(root)
|
|
134
|
+
let dir = path.dirname(path.resolve(file))
|
|
135
|
+
while (true) {
|
|
136
|
+
const limit = readColumnLimit(dir)
|
|
137
|
+
if (limit !== undefined) return limit
|
|
138
|
+
if (dir === stop) return undefined
|
|
139
|
+
const parent = path.dirname(dir)
|
|
140
|
+
if (parent === dir) return undefined
|
|
141
|
+
dir = parent
|
|
142
|
+
}
|
|
128
143
|
}
|
|
129
144
|
|
|
130
145
|
/** 中文按两列、tab 按 4 列计宽,用于判断渲染后的行宽。 */
|
|
131
146
|
export function displayWidth(text) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
147
|
+
let width = 0
|
|
148
|
+
for (const char of text) {
|
|
149
|
+
if (char === '\t') {
|
|
150
|
+
width += 4 - (width % 4)
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
const code = char.codePointAt(0)
|
|
154
|
+
const wide =
|
|
155
|
+
(code >= 0x1100 && code <= 0x115f) ||
|
|
156
|
+
(code >= 0x2e80 && code <= 0xa4cf) ||
|
|
157
|
+
(code >= 0xac00 && code <= 0xd7a3) ||
|
|
158
|
+
(code >= 0xf900 && code <= 0xfaff) ||
|
|
159
|
+
(code >= 0xfe30 && code <= 0xfe6f) ||
|
|
160
|
+
(code >= 0xff00 && code <= 0xff60) ||
|
|
161
|
+
(code >= 0xffe0 && code <= 0xffe6) ||
|
|
162
|
+
(code >= 0x1f300 && code <= 0x1f64f) ||
|
|
163
|
+
(code >= 0x20000 && code <= 0x3fffd)
|
|
164
|
+
width += wide ? 2 : 1
|
|
165
|
+
}
|
|
166
|
+
return width
|
|
152
167
|
}
|
|
153
168
|
|
|
154
169
|
const TAG_GROUPS = {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
170
|
+
param: 'param',
|
|
171
|
+
tparam: 'param',
|
|
172
|
+
return: 'return',
|
|
173
|
+
returns: 'return',
|
|
174
|
+
retval: 'return',
|
|
175
|
+
result: 'return',
|
|
176
|
+
throws: 'throws',
|
|
177
|
+
throw: 'throws',
|
|
178
|
+
exception: 'throws',
|
|
179
|
+
note: 'note',
|
|
180
|
+
see: 'note',
|
|
181
|
+
pre: 'pre',
|
|
182
|
+
post: 'pre',
|
|
167
183
|
}
|
|
168
184
|
|
|
169
185
|
/** 取标签所属分组;参数、返回值、异常、前置条件、说明各成一组。 */
|
|
170
186
|
function tagGroupOf(tag) {
|
|
171
|
-
|
|
172
|
-
|
|
187
|
+
const name = tag.slice(1).toLowerCase().split(/[\s{[(]/)[0]
|
|
188
|
+
return TAG_GROUPS[name] ?? 'other'
|
|
173
189
|
}
|
|
174
190
|
|
|
175
191
|
/** 分组之间允许空行;同一分组内出现空行才算标签粘连。摘要与首个标签之间的空行另由 brief-tag-gap 检查。 */
|
|
176
192
|
function hasTagGap(contents) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
193
|
+
let lastGroup = null
|
|
194
|
+
let pendingGap = false
|
|
195
|
+
for (const content of contents) {
|
|
196
|
+
const trimmed = content.trim()
|
|
197
|
+
if (trimmed.startsWith('@')) {
|
|
198
|
+
const group = tagGroupOf(trimmed)
|
|
199
|
+
if (pendingGap && lastGroup === group) return true
|
|
200
|
+
pendingGap = false
|
|
201
|
+
lastGroup = group
|
|
202
|
+
continue
|
|
203
|
+
}
|
|
204
|
+
if (trimmed === '') {
|
|
205
|
+
if (lastGroup !== null) pendingGap = true
|
|
206
|
+
continue
|
|
207
|
+
}
|
|
208
|
+
// 空行后接正文是标签说明内的段落,不是分组分隔,取消待定空行
|
|
209
|
+
pendingGap = false
|
|
210
|
+
}
|
|
211
|
+
return false
|
|
196
212
|
}
|
|
197
213
|
|
|
198
214
|
/** 摘要或详细描述与首个标签之间必须有空行。 */
|
|
199
215
|
function hasBriefTagGap(contents) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
216
|
+
let sawText = false
|
|
217
|
+
for (let index = 0; index < contents.length; index += 1) {
|
|
218
|
+
const trimmed = contents[index].trim()
|
|
219
|
+
if (trimmed.startsWith('@')) {
|
|
220
|
+
return sawText && index > 0 && contents[index - 1].trim() !== ''
|
|
221
|
+
}
|
|
222
|
+
if (trimmed !== '') sawText = true
|
|
205
223
|
}
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
return false
|
|
224
|
+
return false
|
|
209
225
|
}
|
|
210
226
|
|
|
211
227
|
/** 取出顶层逗号分隔的参数段,忽略模板与括号内的逗号。 */
|
|
212
228
|
function splitParams(text) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
229
|
+
const parts = []
|
|
230
|
+
let depth = 0
|
|
231
|
+
let current = ''
|
|
232
|
+
for (const char of text) {
|
|
233
|
+
if (char === '<' || char === '(' || char === '[' || char === '{') depth += 1
|
|
234
|
+
if (char === '>' || char === ')' || char === ']' || char === '}') depth = Math.max(0, depth - 1)
|
|
235
|
+
if (char === ',' && depth === 0) {
|
|
236
|
+
parts.push(current)
|
|
237
|
+
current = ''
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
current += char
|
|
241
|
+
}
|
|
242
|
+
if (current.trim() !== '') parts.push(current)
|
|
243
|
+
return parts.map((part) => part.trim()).filter((part) => part !== '' && part !== 'void')
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 参数片段里只可能是类型的关键字:末词命中即说明这个参数没写名字。 */
|
|
247
|
+
const TYPE_WORDS = new Set([
|
|
248
|
+
'const', 'volatile', 'signed', 'unsigned',
|
|
249
|
+
'void', 'bool', 'char', 'short', 'int', 'long', 'float', 'double', 'auto',
|
|
250
|
+
'wchar_t', 'char8_t', 'char16_t', 'char32_t',
|
|
251
|
+
])
|
|
252
|
+
|
|
253
|
+
/** 取参数名:去掉默认值与数组后缀;不具名参数返回 undefined。 */
|
|
231
254
|
function paramName(part) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
255
|
+
let text = part
|
|
256
|
+
if (text.includes('=')) text = text.slice(0, text.indexOf('='))
|
|
257
|
+
text = text.replace(/\[[^\]]*\]/g, ' ').trim()
|
|
258
|
+
if (text === '...') return undefined
|
|
259
|
+
const pointer = text.match(/\(\s*[*&]+\s*([A-Za-z_]\w*)\s*\)/)
|
|
260
|
+
if (pointer) return pointer[1]
|
|
261
|
+
if (/[&*>]$/.test(text)) return undefined
|
|
262
|
+
const tokens = text.match(/[A-Za-z_]\w*/g) ?? []
|
|
263
|
+
const last = tokens.at(-1)
|
|
264
|
+
if (!last) return undefined
|
|
265
|
+
if (TYPE_WORDS.has(last)) return undefined
|
|
266
|
+
if (text.endsWith(`::${last}`)) return undefined
|
|
267
|
+
const bare = tokens.filter((token) => !['const', 'volatile'].includes(token))
|
|
268
|
+
if (bare.length < 2) return undefined
|
|
269
|
+
return last
|
|
241
270
|
}
|
|
242
271
|
|
|
243
272
|
/** 提取函数声明的参数名列表;不是函数声明时返回 undefined。 */
|
|
244
273
|
export function functionParams(declaration) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
274
|
+
const decl = declaration.trim().replace(/^template\s*<[^>]*>\s*/, '')
|
|
275
|
+
if (DECL_KEYWORDS.test(decl)) return undefined
|
|
276
|
+
const open = decl.indexOf('(')
|
|
277
|
+
if (open === -1) return undefined
|
|
278
|
+
const namePart = decl.slice(0, open).trim()
|
|
279
|
+
if (!/[~A-Za-z_]\w*$|operator\W/.test(namePart)) return undefined
|
|
280
|
+
let depth = 0
|
|
281
|
+
let close = -1
|
|
282
|
+
for (let index = open; index < decl.length; index += 1) {
|
|
283
|
+
const char = decl[index]
|
|
284
|
+
if (char === '(') depth += 1
|
|
285
|
+
if (char === ')') {
|
|
286
|
+
depth -= 1
|
|
287
|
+
if (depth === 0) {
|
|
288
|
+
close = index
|
|
289
|
+
break
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (close === -1) return undefined
|
|
294
|
+
const tail = decl.slice(close + 1).trim()
|
|
295
|
+
if (tail !== '' && !/^[;{:]/.test(tail) && !/^(const|noexcept|override|final|->|=\s*(0|default|delete))/.test(tail)) {
|
|
296
|
+
return undefined
|
|
297
|
+
}
|
|
298
|
+
return splitParams(decl.slice(open + 1, close)).map(paramName).filter(Boolean)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** 从 start 起拼接跨行声明,直到见到结尾的 `;` 或 `{`。 */
|
|
302
|
+
function joinedDeclaration(lines, start) {
|
|
303
|
+
let declaration = lines[start].trim()
|
|
304
|
+
let end = start
|
|
305
|
+
while (!/[;{]\s*$/.test(declaration) && end + 1 < lines.length && end - start < 20) {
|
|
306
|
+
end += 1
|
|
307
|
+
declaration = `${declaration} ${lines[end].trim()}`
|
|
308
|
+
}
|
|
309
|
+
return declaration
|
|
270
310
|
}
|
|
271
311
|
|
|
272
312
|
/** 收集头文件里声明的函数名,供 .cpp 定义处配对;声明可以跨行。 */
|
|
273
313
|
function declarationNames(text) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
314
|
+
const names = new Set()
|
|
315
|
+
let buffer = ''
|
|
316
|
+
for (const line of text.split(/\r?\n/)) {
|
|
317
|
+
const trimmed = line.trim()
|
|
318
|
+
if (trimmed === '' || trimmed.startsWith('//') || trimmed.startsWith('*') || trimmed.startsWith('/*') || trimmed.startsWith('#')) continue
|
|
319
|
+
buffer = buffer === '' ? trimmed : `${buffer} ${trimmed}`
|
|
320
|
+
if (!buffer.includes(';')) continue
|
|
321
|
+
const declaration = buffer
|
|
322
|
+
buffer = ''
|
|
323
|
+
if (/^(if|for|while|switch|catch|else|return|case|default|do)\b/.test(declaration)) continue
|
|
324
|
+
const match = declaration.match(/^(.*?)\b([A-Za-z_]\w*)\s*\(([^;{}]*)\)\s*(?:const\s*)?(?:noexcept\s*)?(?:override\s*)?(?:final\s*)?(?:=\s*(?:0|default|delete)\s*)?;$/)
|
|
325
|
+
if (match) names.add(match[2])
|
|
326
|
+
}
|
|
327
|
+
return names
|
|
288
328
|
}
|
|
289
329
|
|
|
290
330
|
/** 取定义行上的函数名、返回类型与参数名;签名可跨行,非定义返回 undefined。 */
|
|
291
331
|
function definitionAt(lines, index) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
332
|
+
const line = lines[index]
|
|
333
|
+
if (line.trim() === '' || /^\s*(\/\/|\/\*|\*)/.test(line) || /^\s*#/.test(line)) return undefined
|
|
334
|
+
if (/^\s*(if|for|while|switch|catch|else|return|case|default|do)\b/.test(line)) return undefined
|
|
335
|
+
if (/^\s*[A-Z][A-Z0-9_]*\s*\(/.test(line)) return undefined
|
|
336
|
+
if (/^\s*~[A-Za-z_]\w*\s*\(/.test(line)) return undefined
|
|
337
|
+
// 定义的首行必须出现参数表左括号;类头、namespace、控制语句等不参与。
|
|
338
|
+
if (!line.includes('(')) return undefined
|
|
339
|
+
const pattern = /^(\s*)(.*?)\b([A-Za-z_]\w*)\s*\(((?:[^()]|\([^()]*\))*)\)\s*(?:const\s*)?(?:noexcept\s*)?(?:->\s*[^{;]+)?\{\s*.*$/
|
|
340
|
+
let match = line.match(pattern)
|
|
341
|
+
if (!match) {
|
|
342
|
+
// 签名被换行拆开时续拼,直到出现函数体左花括号、语句结束或超出上限。
|
|
343
|
+
let buffer = line
|
|
344
|
+
for (let next = index + 1; next < lines.length && next <= index + 12; next += 1) {
|
|
345
|
+
buffer = `${buffer} ${lines[next].trim()}`
|
|
346
|
+
match = buffer.match(pattern)
|
|
347
|
+
if (match) break
|
|
348
|
+
if (/[;}]/.test(lines[next])) break
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (!match) return undefined
|
|
352
|
+
const prefix = match[2].trim()
|
|
353
|
+
if (/^(if|for|while|switch|catch|else|return|case|default|do)$/.test(match[3])) return undefined
|
|
354
|
+
if (prefix.endsWith('::~')) return undefined
|
|
355
|
+
const returnType = prefix.replace(/[A-Za-z_]\w*::$/, '').trim()
|
|
356
|
+
return { name: match[3], returnType, params: splitParams(match[4]).map(paramName).filter(Boolean) }
|
|
317
357
|
}
|
|
318
358
|
|
|
319
359
|
/** 取定义上一行紧贴的 /// 文档块。 */
|
|
320
360
|
function docAbove(lines, index) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
361
|
+
let cursor = index - 1
|
|
362
|
+
if (cursor < 0 || !lines[cursor].trim().startsWith('///')) return undefined
|
|
363
|
+
const contents = []
|
|
364
|
+
let start = cursor
|
|
365
|
+
while (cursor >= 0 && lines[cursor].trim().startsWith('///')) {
|
|
366
|
+
contents.unshift(lines[cursor].trim().replace(/^\/\/\/\s?/, ''))
|
|
367
|
+
start = cursor
|
|
368
|
+
cursor -= 1
|
|
369
|
+
}
|
|
370
|
+
return { start, contents }
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** 统计一行代码里花括号的净增量,忽略注释与字符串字面量。 */
|
|
374
|
+
function braceDelta(line) {
|
|
375
|
+
let depth = 0
|
|
376
|
+
let quote = ''
|
|
377
|
+
let block = false
|
|
378
|
+
for (let index = 0; index < line.length; index += 1) {
|
|
379
|
+
const char = line[index]
|
|
380
|
+
const next = line[index + 1]
|
|
381
|
+
if (block) {
|
|
382
|
+
if (char === '*' && next === '/') {
|
|
383
|
+
block = false
|
|
384
|
+
index += 1
|
|
385
|
+
}
|
|
386
|
+
continue
|
|
387
|
+
}
|
|
388
|
+
if (quote) {
|
|
389
|
+
if (char === '\\') {
|
|
390
|
+
index += 1
|
|
391
|
+
continue
|
|
392
|
+
}
|
|
393
|
+
if (char === quote) quote = ''
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
if (char === '/' && next === '/') break
|
|
397
|
+
if (char === '/' && next === '*') {
|
|
398
|
+
block = true
|
|
399
|
+
index += 1
|
|
400
|
+
continue
|
|
401
|
+
}
|
|
402
|
+
if (char === '"' || char === "'") {
|
|
403
|
+
quote = char
|
|
404
|
+
continue
|
|
405
|
+
}
|
|
406
|
+
if (char === '{') depth += 1
|
|
407
|
+
else if (char === '}') depth -= 1
|
|
408
|
+
}
|
|
409
|
+
return depth
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** 取定义体的行范围:左花括号行到深度回零的收尾行;找不到左花括号时返回 undefined。 */
|
|
413
|
+
function bodyRange(lines, index) {
|
|
414
|
+
let start = -1
|
|
415
|
+
for (let cursor = index; cursor < lines.length && cursor <= index + 12; cursor += 1) {
|
|
416
|
+
if (lines[cursor].includes('{')) {
|
|
417
|
+
start = cursor
|
|
418
|
+
break
|
|
419
|
+
}
|
|
420
|
+
if (cursor > index && /[;}]/.test(lines[cursor])) break
|
|
421
|
+
}
|
|
422
|
+
if (start === -1) return undefined
|
|
423
|
+
let depth = 0
|
|
424
|
+
for (let cursor = start; cursor < lines.length; cursor += 1) {
|
|
425
|
+
depth += braceDelta(lines[cursor])
|
|
426
|
+
if (depth <= 0) return { start, end: cursor }
|
|
427
|
+
}
|
|
428
|
+
return { start, end: lines.length - 1 }
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** 检查 .cpp 定义处的注释落点:函数体内用 // 写实现注释,头文件已声明的定义不写 ///,只在 .cpp 出现的函数写完整契约。 */
|
|
432
|
+
export function checkDefinitionDocs(text, headerNames = new Set(), options = {}) {
|
|
433
|
+
const issues = []
|
|
434
|
+
const lines = text.split(/\r?\n/)
|
|
435
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
436
|
+
const definition = definitionAt(lines, index)
|
|
437
|
+
if (!definition) continue
|
|
438
|
+
const body = bodyRange(lines, index)
|
|
439
|
+
if (body) {
|
|
440
|
+
for (let cursor = body.start; cursor <= body.end; cursor += 1) {
|
|
441
|
+
if (lines[cursor].trim().startsWith('///')) {
|
|
442
|
+
issues.push({ line: cursor + 1, rule: 'doc-in-body', message: '函数体内用 // 写实现注释:/// 会接到函数的 Doxygen 详细说明后面' })
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (definition.name === 'main' || definition.name.startsWith('operator')) continue
|
|
447
|
+
const doc = docAbove(lines, index)
|
|
448
|
+
if (headerNames.has(definition.name)) {
|
|
449
|
+
if (doc) {
|
|
450
|
+
issues.push({ line: doc.start + 1, rule: 'doc-on-definition', message: '定义处重复了头文件声明注释:删掉这里的 ///,契约留在声明处' })
|
|
451
|
+
}
|
|
452
|
+
continue
|
|
453
|
+
}
|
|
454
|
+
const needParam = options.requireParam !== false && definition.params.length > 0
|
|
455
|
+
const needReturn = options.requireReturn !== false && definition.returnType !== '' && definition.returnType !== 'void'
|
|
456
|
+
if (!needParam && !needReturn) continue
|
|
457
|
+
const wanted = [needParam ? '@param' : '', needReturn ? '@return' : ''].filter(Boolean).join('/')
|
|
458
|
+
if (!doc) {
|
|
459
|
+
issues.push({ line: index + 1, rule: 'cpp-contract-missing', message: `只在 .cpp 出现的函数缺少完整契约:在定义上一行补 ${wanted}` })
|
|
460
|
+
continue
|
|
461
|
+
}
|
|
462
|
+
const block = doc.contents.join('\n')
|
|
463
|
+
if (needParam) {
|
|
464
|
+
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
465
|
+
for (const name of definition.params) {
|
|
466
|
+
if (!documented.includes(name)) {
|
|
467
|
+
issues.push({ line: doc.start + 1, rule: 'cpp-contract-missing', message: `只在 .cpp 出现的函数缺少 @param ${name}:补完整契约` })
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (needReturn && !/(?:@return|@retval)\b/.test(block)) {
|
|
472
|
+
issues.push({ line: doc.start + 1, rule: 'cpp-contract-missing', message: '只在 .cpp 出现的函数缺少 @return:补完整契约' })
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return issues
|
|
365
476
|
}
|
|
366
477
|
|
|
367
478
|
/**
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
479
|
+
* 识别保存控制序列的常量声明,返回常量名;不是则返回 undefined。
|
|
480
|
+
* 只认 `constexpr std::string_view`、`constexpr char*` 与 `constexpr char`,
|
|
481
|
+
* 且字面量里出现 ESC(`\\033`、`\\x1b`、`\\e`)。
|
|
482
|
+
*/
|
|
372
483
|
function escapedControlConstant(declaration) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
484
|
+
const text = declaration.trim()
|
|
485
|
+
const string = STRING_SEQUENCE_CONSTANT.exec(text)
|
|
486
|
+
if (string && ESCAPE_SEQUENCE.test(string[2])) return string[1]
|
|
487
|
+
const character = CHAR_SEQUENCE_CONSTANT.exec(text)
|
|
488
|
+
return character ? character[1] : undefined
|
|
378
489
|
}
|
|
379
490
|
|
|
380
491
|
/** 取紧贴在声明上方的文档注释;块注释与 `///` 都认。 */
|
|
381
492
|
function attachedDoc(lines, index, style) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
493
|
+
if (style !== 'block') return docAbove(lines, index)
|
|
494
|
+
let end = index - 1
|
|
495
|
+
if (end < 0 || !lines[end].trim().endsWith('*/')) return undefined
|
|
496
|
+
let start = end
|
|
497
|
+
while (start >= 0 && !lines[start].trim().startsWith('/**')) start -= 1
|
|
498
|
+
if (start < 0) return undefined
|
|
499
|
+
return blockDocAt(lines, start)
|
|
389
500
|
}
|
|
390
501
|
|
|
391
502
|
/** 取函数注释第一段:遇到空行或 Doxygen 标签就结束。 */
|
|
392
503
|
function summaryParagraph(contents) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
504
|
+
const paragraph = []
|
|
505
|
+
for (const line of contents) {
|
|
506
|
+
const text = line.trim()
|
|
507
|
+
if (text === '' || text.startsWith('@')) break
|
|
508
|
+
paragraph.push(text)
|
|
509
|
+
}
|
|
510
|
+
return paragraph.join(' ')
|
|
400
511
|
}
|
|
401
512
|
|
|
402
513
|
/** 判断摘要中是否直接出现了参数名。 */
|
|
403
514
|
function mentionsParam(text, name) {
|
|
404
|
-
|
|
405
|
-
|
|
515
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
516
|
+
return new RegExp(`(^|[^A-Za-z0-9_])${escaped}([^A-Za-z0-9_]|$)`).test(text)
|
|
406
517
|
}
|
|
407
518
|
|
|
408
519
|
/** 标记每一行是否位于 class/struct 体内,供数据成员注释位置检查使用。 */
|
|
409
|
-
function classMemberLines(lines) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
520
|
+
function classMemberLines(lines, kinds = 'class|struct') {
|
|
521
|
+
const flags = new Array(lines.length).fill(false)
|
|
522
|
+
const starts = []
|
|
523
|
+
let depth = 0
|
|
524
|
+
let pending = false
|
|
525
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
526
|
+
const code = lines[index].replace(/\/\/.*$/, '').trim()
|
|
527
|
+
if (new RegExp(`^(?:template\\s*<[^>]*>\\s*)?(?:${kinds})\\b`).test(code)) pending = true
|
|
528
|
+
const opens = (code.match(/\{/g) ?? []).length
|
|
529
|
+
const closes = (code.match(/\}/g) ?? []).length
|
|
530
|
+
if (pending && opens > 0) {
|
|
531
|
+
starts.push(depth)
|
|
532
|
+
pending = false
|
|
533
|
+
} else if (pending && opens === 0 && code.endsWith(';')) {
|
|
534
|
+
pending = false
|
|
535
|
+
}
|
|
536
|
+
flags[index] = starts.length > 0 && depth > starts[starts.length - 1]
|
|
537
|
+
depth += opens - closes
|
|
538
|
+
while (starts.length > 0 && depth <= starts[starts.length - 1]) starts.pop()
|
|
539
|
+
}
|
|
540
|
+
return flags
|
|
430
541
|
}
|
|
431
542
|
|
|
432
543
|
/**
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
544
|
+
* 取从 `index` 开始的 Doxygen 块注释;不是块注释或没有收尾时返回 undefined。
|
|
545
|
+
* `contents` 去掉开头标记、结尾标记与每行的 `*` 前缀,空行保留为空串。
|
|
546
|
+
*/
|
|
436
547
|
export function blockDocAt(lines, index) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
548
|
+
const first = lines[index]?.trim() ?? ''
|
|
549
|
+
if (!first.startsWith('/**') || first.startsWith('/**<')) return undefined
|
|
550
|
+
const contents = []
|
|
551
|
+
if (first.includes('*/')) {
|
|
552
|
+
const inner = first.slice(first.indexOf('/**') + 3, first.lastIndexOf('*/')).trim()
|
|
553
|
+
if (inner) contents.push(inner.replace(/^\*\s?/, '').trim())
|
|
554
|
+
return { start: index, end: index, contents }
|
|
555
|
+
}
|
|
556
|
+
const opening = first.slice(first.indexOf('/**') + 3).trim().replace(/^\*\s?/, '')
|
|
557
|
+
if (opening) contents.push(opening)
|
|
558
|
+
for (let cursor = index + 1; cursor < lines.length; cursor += 1) {
|
|
559
|
+
const line = lines[cursor]
|
|
560
|
+
const trimmed = line.trim()
|
|
561
|
+
if (trimmed.includes('*/')) {
|
|
562
|
+
const before = trimmed.slice(0, trimmed.indexOf('*/')).replace(/^\*\s?/, '').trim()
|
|
563
|
+
if (before) contents.push(before)
|
|
564
|
+
return { start: index, end: cursor, contents }
|
|
565
|
+
}
|
|
566
|
+
contents.push(trimmed.replace(/^\*\s?/, '').trim())
|
|
567
|
+
}
|
|
568
|
+
return undefined
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** 取声明去掉模板、属性与限定符后的返回类型;构造函数与析构函数返回空串。 */
|
|
572
|
+
function declaredReturnType(declaration) {
|
|
573
|
+
const decl = declaration.trim().replace(/^template\s*<[^>]*>\s*/, '')
|
|
574
|
+
const open = decl.indexOf('(')
|
|
575
|
+
if (open === -1) return ''
|
|
576
|
+
let prefix = decl.slice(0, open).trim()
|
|
577
|
+
if (prefix.includes('~') || prefix.includes('operator')) return ''
|
|
578
|
+
prefix = prefix
|
|
579
|
+
.replace(/\[\[[^\]]*\]\]/g, ' ')
|
|
580
|
+
.replace(/^(?:(?:static|virtual|inline|constexpr|explicit|friend|extern)\s+)+/, '')
|
|
581
|
+
.trim()
|
|
582
|
+
const name = /[A-Za-z_]\w*$/.exec(prefix)
|
|
583
|
+
if (!name) return ''
|
|
584
|
+
return prefix.slice(0, name.index).trim().replace(/[A-Za-z_]\w*::$/, '').trim()
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** 判断声明属于哪类特殊成员;是则返回规定的文档首句标签。 */
|
|
588
|
+
function specialMemberLabel(declaration) {
|
|
589
|
+
const decl = declaration.trim().replace(/^template\s*<[^>]*>\s*/, '')
|
|
590
|
+
const open = decl.indexOf('(')
|
|
591
|
+
if (open === -1) return undefined
|
|
592
|
+
const prefix = decl
|
|
593
|
+
.slice(0, open)
|
|
594
|
+
.trim()
|
|
595
|
+
.replace(/\[\[[^\]]*\]\]/g, ' ')
|
|
596
|
+
.replace(/^(?:(?:static|virtual|inline|constexpr|explicit|friend|extern)\s+)+/, '')
|
|
597
|
+
.trim()
|
|
598
|
+
if (prefix.includes('operator')) return undefined
|
|
599
|
+
if (prefix.startsWith('~') || /::~$/.test(prefix)) return '析构函数:'
|
|
600
|
+
const name = /[A-Za-z_]\w*$/.exec(prefix)
|
|
601
|
+
if (!name) return undefined
|
|
602
|
+
const owner = name[0]
|
|
603
|
+
const before = prefix.slice(0, name.index).trim().replace(/[A-Za-z_]\w*::$/, '').trim()
|
|
604
|
+
if (before !== '') return undefined
|
|
605
|
+
const close = decl.indexOf(')', open)
|
|
606
|
+
if (close === -1) return undefined
|
|
607
|
+
const params = splitParams(decl.slice(open + 1, close))
|
|
608
|
+
if (params.length === 0) return '默认构造函数:'
|
|
609
|
+
if (params.length > 1) return '构造函数:'
|
|
610
|
+
const match = /^\s*(?:const\s+)?([A-Za-z_][\w:<>]*)\s*(&&|&)\s*(?:[A-Za-z_]\w*)?\s*$/.exec(params[0])
|
|
611
|
+
if (!match || match[1].split('::').pop() !== owner) return '构造函数:'
|
|
612
|
+
return match[2] === '&&' ? '移动构造函数:' : '拷贝构造函数:'
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** 检查特殊成员首句与函数返回值文档;返回 { rule, message } 列表。 */
|
|
616
|
+
function functionDocIssues(declaration, docContents, options = {}) {
|
|
617
|
+
const issues = []
|
|
618
|
+
const label = specialMemberLabel(declaration)
|
|
619
|
+
const firstLine = docContents.find((content) => content.trim() !== '')?.trim() ?? ''
|
|
620
|
+
if (label) {
|
|
621
|
+
if (!firstLine.startsWith(label)) {
|
|
622
|
+
issues.push({ rule: 'special-member-first', message: `特殊成员注释首句要以「${label}」开头:先写这是哪一类构造或析构,再写后置条件` })
|
|
623
|
+
}
|
|
624
|
+
if (SPECIAL_MEMBER_VAGUE.test(firstLine)) {
|
|
625
|
+
issues.push({ rule: 'special-member-vague', message: '特殊成员注释只在复述「创建对象」「初始化成员」「建立初始状态」:改成具体的后置条件或职责' })
|
|
626
|
+
}
|
|
627
|
+
return issues
|
|
628
|
+
}
|
|
629
|
+
if (!declaration.endsWith(';') || /\bmain\s*\(/.test(declaration)) return issues
|
|
630
|
+
if (functionParams(declaration) === undefined) return issues
|
|
631
|
+
const returnType = declaredReturnType(declaration)
|
|
632
|
+
if (returnType === '' || /(?:^|\s)void$/.test(returnType)) return issues
|
|
633
|
+
const documented = docContents.some((content) => /^@return\b/.test(content.trim()))
|
|
634
|
+
|| docContents.some((content) => /^@retval\s+/.test(content.trim()))
|
|
635
|
+
if (!documented && options.requireReturn !== false) {
|
|
636
|
+
issues.push({ rule: 'return-missing', message: '非 void 函数缺少 @return(或用 @retval 逐值说明):写清返回值怎么读、失败时返回什么' })
|
|
637
|
+
}
|
|
638
|
+
return issues
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** 检查 @retval 组的顺序:成功项在最前,且只出现一次。 */
|
|
642
|
+
function retvalOrderIssues(docContents) {
|
|
643
|
+
const rows = docContents
|
|
644
|
+
.map((content) => /^@retval\s+(\S+)\s*(.*)$/.exec(content.trim()))
|
|
645
|
+
.filter(Boolean)
|
|
646
|
+
const issues = []
|
|
647
|
+
if (rows.length === 0) return issues
|
|
648
|
+
const successIndex = rows.findIndex((row) => row[2].trim().startsWith('成功:'))
|
|
649
|
+
if (successIndex === -1) return issues
|
|
650
|
+
if (successIndex !== 0) {
|
|
651
|
+
issues.push({ rule: 'retval-order', message: '第一个 @retval 要写成功项,说明以「成功:」开头' })
|
|
652
|
+
}
|
|
653
|
+
for (const [index, row] of rows.entries()) {
|
|
654
|
+
if (index !== successIndex && row[2].includes('成功:')) {
|
|
655
|
+
issues.push({ rule: 'retval-order', message: `@retval ${row[1]} 又写了「成功:」:成功项只保留在最前面的一个 @retval` })
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
return issues
|
|
458
659
|
}
|
|
459
660
|
|
|
460
661
|
/** 检查一段文本,返回问题列表;每个问题带 1 起始的行号。 */
|
|
461
662
|
export function checkText(text, options = {}) {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
if (
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const line = lines[index]
|
|
524
|
-
const trimmed = line.trim()
|
|
525
|
-
if (trimmed.startsWith('///') || (trimmed.startsWith('//') && !trimmed.startsWith('//!'))) {
|
|
526
|
-
if (displayWidth(trimmed) > width) add(index + 1, 'comment-width', `注释行 ${displayWidth(trimmed)} 列,超过 ${width} 列`)
|
|
527
|
-
if (/^\/\/\/ {0,2}- /.test(trimmed)) {
|
|
528
|
-
add(index + 1, 'list-indent', '注释列表项要在 /// 后空三格再写 -:`/// - `,与总起句拉开层级')
|
|
529
|
-
}
|
|
530
|
-
for (const tag of FORBIDDEN_TAGS) {
|
|
531
|
-
if (options.allowTags?.has(tag.slice(1))) continue
|
|
532
|
-
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
if (style === 'block') {
|
|
663
|
+
const width = options.width ?? 120
|
|
664
|
+
const requireHeader = options.requireHeader ?? true
|
|
665
|
+
const lines = text.split(/\r?\n/)
|
|
666
|
+
const issues = []
|
|
667
|
+
const add = (line, rule, message) => issues.push({ line, rule, message })
|
|
668
|
+
const inClass = classMemberLines(lines)
|
|
669
|
+
const inStruct = classMemberLines(lines, 'struct')
|
|
670
|
+
|
|
671
|
+
const style = options.commentStyle === 'block' ? 'block' : 'slash'
|
|
672
|
+
const firstContent = lines.findIndex((line) => line.trim() !== '')
|
|
673
|
+
const firstTrimmed = firstContent === -1 ? '' : lines[firstContent].trim()
|
|
674
|
+
const headerBlock = style === 'block' && firstContent !== -1 ? blockDocAt(lines, firstContent) : undefined
|
|
675
|
+
const hasDoxyHeader = style === 'block'
|
|
676
|
+
? Boolean(headerBlock && BLOCK_FILE.test(headerBlock.contents[0] ?? ''))
|
|
677
|
+
: DOXY_FILE.test(firstTrimmed)
|
|
678
|
+
let doxyHeaderEnd = -1
|
|
679
|
+
if (hasDoxyHeader && style === 'block') {
|
|
680
|
+
doxyHeaderEnd = headerBlock.end
|
|
681
|
+
} else if (hasDoxyHeader) {
|
|
682
|
+
doxyHeaderEnd = firstContent
|
|
683
|
+
while (doxyHeaderEnd + 1 < lines.length && lines[doxyHeaderEnd + 1].trim().startsWith('///')) doxyHeaderEnd += 1
|
|
684
|
+
}
|
|
685
|
+
if (requireHeader && firstContent !== -1 && !hasDoxyHeader) {
|
|
686
|
+
if (style === 'block') add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /** ... */,第一行 @file <文件名>,随后写 @brief')
|
|
687
|
+
else add(firstContent + 1, 'header-missing', '文件开头缺少文件头:写 /// @file <文件名>,随后写 /// @brief')
|
|
688
|
+
}
|
|
689
|
+
if (firstContent !== -1 && hasDoxyHeader) {
|
|
690
|
+
const own = options.fileName ? options.fileName.split(/[\\/]/).pop() : ''
|
|
691
|
+
const declaredText = style === 'block' ? (headerBlock.contents[0] ?? '') : lines[firstContent].trim()
|
|
692
|
+
if (own) {
|
|
693
|
+
const declared = /@file\s+(\S+)/.exec(declaredText)
|
|
694
|
+
if (declared && declared[1] !== own) {
|
|
695
|
+
add(firstContent + 1, 'header-file-name', `文件头写的是 ${declared[1]},本文件是 ${own}:@file 必须等于文件名`)
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (options.requireBrief !== false) {
|
|
699
|
+
const slashBrief = (lines[firstContent + 1] ?? '').trim()
|
|
700
|
+
const slashGap = /^\/\/\/\s*$/.test(slashBrief)
|
|
701
|
+
const blockBrief = headerBlock?.contents[1] ?? ''
|
|
702
|
+
const blockGap = style === 'block' && blockBrief.trim() === ''
|
|
703
|
+
const hasBrief = style === 'block'
|
|
704
|
+
? BLOCK_BRIEF.test(blockBrief) || (blockGap && BLOCK_BRIEF.test(headerBlock?.contents[2] ?? ''))
|
|
705
|
+
: DOXY_BRIEF.test(slashBrief) || (slashGap && DOXY_BRIEF.test((lines[firstContent + 2] ?? '').trim()))
|
|
706
|
+
if (!hasBrief) {
|
|
707
|
+
add(style === 'block' ? headerBlock.start + 3 : firstContent + 2, 'header-brief', '文件头在 @file 之后缺少 @brief 一句话说明')
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const headerLines = style === 'block'
|
|
711
|
+
? headerBlock.contents.slice(1).map((content, offset) => ({ line: headerBlock.start + offset + 2, text: content }))
|
|
712
|
+
: lines.slice(firstContent + 1, doxyHeaderEnd + 1).map((content, offset) => ({ line: firstContent + offset + 2, text: content.trim() }))
|
|
713
|
+
for (const { line: headerLine, text } of headerLines) {
|
|
714
|
+
const names = text.match(/\b[A-Za-z0-9_-]+\.(?:h|hpp|cpp|cc)\b/g) ?? []
|
|
715
|
+
const fileMatches = text.match(/@file\s+(\S+)/)
|
|
716
|
+
const namesToCheck = fileMatches ? names.filter((name) => name !== fileMatches[1]) : names
|
|
717
|
+
for (const name of namesToCheck) {
|
|
718
|
+
if (name !== own) add(headerLine, 'header-cross-file', `文件头提到了别的源文件 ${name}:只介绍本文件,删掉跨文件句子`)
|
|
719
|
+
}
|
|
720
|
+
if (/\b(?:app|src|include|tests)\//.test(text)) add(headerLine, 'header-cross-file', '文件头指向了另一个目录:只介绍本文件')
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
538
724
|
for (let index = 0; index < lines.length; index += 1) {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
const declaration = lines[next].trim()
|
|
584
|
-
const block = lines.slice(start, end + 1).join('\n')
|
|
585
|
-
const docContents = lines.slice(start, end + 1).map((line) => line.trim().replace(/^\/\/\/\s?/, ''))
|
|
586
|
-
if (hasBriefTagGap(docContents)) {
|
|
587
|
-
add(start + 1, 'brief-tag-gap', '摘要或详细描述与标签块之间要空一行:在首个标签前补一行 `///`')
|
|
588
|
-
}
|
|
589
|
-
if (hasTagGap(docContents)) {
|
|
590
|
-
add(start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
591
|
-
}
|
|
592
|
-
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
593
|
-
if (!isType && CALLER_LIST.test(block) && MEMBER_REF.test(block)) {
|
|
594
|
-
add(start + 1, 'caller-list', '函数注释在点名调用者:调用关系会随重构过期,只写这个函数做什么、参数、返回值与异常')
|
|
595
|
-
}
|
|
596
|
-
if (RATIONALE.test(block)) {
|
|
597
|
-
add(start + 1, 'rationale', '注释在写设计取舍:方案对比与理由移到正文或实现注释,接口注释只留契约')
|
|
598
|
-
}
|
|
599
|
-
if (isType && MEMBER_LIST.test(block)) {
|
|
600
|
-
add(start + 1, 'member-list', '类型注释在罗列成员功能,应改写为概念与使用约定')
|
|
601
|
-
}
|
|
602
|
-
if (isType && FORWARD_REF.test(block)) {
|
|
603
|
-
add(start + 1, 'forward-ref', '类型注释在交代别的类型负责什么:只写本类型的契约,删掉这句,不要挪到文件头')
|
|
604
|
-
}
|
|
605
|
-
if (isType && TYPE_SYSTEM.test(block)) {
|
|
606
|
-
add(start + 1, 'type-system', '类型注释在复述类型系统已表达的性质:可拷贝与 const 查询由声明给出,删掉这句')
|
|
607
|
-
}
|
|
608
|
-
if (isType && NEGATED_BOUNDARY.test(block)) {
|
|
609
|
-
add(start + 1, 'negated-boundary', '类型注释用「只保存/不拥有/不含」写否定式边界:改成名词短语,边界交给成员类型表达')
|
|
610
|
-
}
|
|
611
|
-
if (NEGATIVE_SCOPE.test(block)) {
|
|
612
|
-
add(start + 1, 'negative-scope', '注释用「不检查/不校验/不负责/不判断」写能力边界:改成正向限定,前置条件写「调用方保证…」或 @pre')
|
|
613
|
-
}
|
|
614
|
-
if (isType && DOMAIN_SIZE.test(block)) {
|
|
615
|
-
add(start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由 squares_ 之类成员声明表达,删掉这句')
|
|
616
|
-
}
|
|
617
|
-
const firstDocLine = lines[start].trim().replace(/^\/\/\/\s*/, '')
|
|
618
|
-
if (!isType && PREDICATE_DECL.test(declaration) && !isPredicateFirstOk(declaration, firstDocLine)) {
|
|
619
|
-
add(start + 1, 'predicate-first', '布尔查询的第一句应写「…是否…」,句首不用「检查/判断/校验/验证」;operator== 写「相等运算符」')
|
|
620
|
-
}
|
|
621
|
-
const params = functionParams(declaration)
|
|
622
|
-
if (!params) continue
|
|
623
|
-
const summary = summaryParagraph(docContents)
|
|
624
|
-
const named = params.filter((name) => mentionsParam(summary, name))
|
|
625
|
-
if (named.length > 0) {
|
|
626
|
-
add(start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
627
|
-
}
|
|
628
|
-
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
629
|
-
for (const name of params) {
|
|
630
|
-
if (!documented.includes(name)) add(start + 1, 'param-missing', `参数 ${name} 没有 @param 说明`)
|
|
631
|
-
}
|
|
632
|
-
for (const name of documented) {
|
|
633
|
-
if (!params.includes(name)) add(start + 1, 'param-extra', `@param ${name} 在声明里不存在`)
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
if (style === 'block') {
|
|
725
|
+
const line = lines[index]
|
|
726
|
+
const trimmed = line.trim()
|
|
727
|
+
if (trimmed.startsWith('///') || (trimmed.startsWith('//') && !trimmed.startsWith('//!'))) {
|
|
728
|
+
const measured = line.replace(/\s+$/, '')
|
|
729
|
+
if (displayWidth(measured) > width) add(index + 1, 'comment-width', `注释行 ${displayWidth(measured)} 列,超过 ${width} 列`)
|
|
730
|
+
if (/^\/\/\/ {0,2}- /.test(trimmed)) {
|
|
731
|
+
add(index + 1, 'list-indent', '注释列表项要在 /// 后空三格再写 -:`/// - `,与总起句拉开层级')
|
|
732
|
+
}
|
|
733
|
+
for (const tag of FORBIDDEN_TAGS) {
|
|
734
|
+
if (PRIVACY_TAGS.includes(tag) && options.allowTags?.has(tag.slice(1))) continue
|
|
735
|
+
if (trimmed.includes(tag)) add(index + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (style === 'block') {
|
|
741
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
742
|
+
const doc = blockDocAt(lines, index)
|
|
743
|
+
if (!doc) continue
|
|
744
|
+
for (let raw = doc.start; raw <= doc.end; raw += 1) {
|
|
745
|
+
const measured = lines[raw].replace(/\s+$/, '')
|
|
746
|
+
if (displayWidth(measured) > width) add(raw + 1, 'comment-width', `注释行 ${displayWidth(measured)} 列,超过 ${width} 列`)
|
|
747
|
+
}
|
|
748
|
+
for (const content of doc.contents) {
|
|
749
|
+
for (const tag of FORBIDDEN_TAGS) {
|
|
750
|
+
if (PRIVACY_TAGS.includes(tag) && options.allowTags?.has(tag.slice(1))) continue
|
|
751
|
+
if (content.includes(tag)) add(doc.start + 1, 'forbidden-tag', `禁止使用 ${tag},按 references/comments/01-comment-rules.md 的标签表改写`)
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
for (let raw = doc.start; raw <= doc.end; raw += 1) {
|
|
755
|
+
if (/^\s*\* {0,2}- /.test(lines[raw])) {
|
|
756
|
+
add(raw + 1, 'list-indent', '块注释列表项要在 * 后空三格再写 -:` * - `,与总起句拉开层级')
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (!doc.contents.some((content) => content.includes('@file')) && hasBriefTagGap(doc.contents)) {
|
|
760
|
+
add(doc.start + 1, 'brief-tag-gap', '摘要或详细描述与标签块之间要空一行:在首个标签前补一行 `///`')
|
|
761
|
+
}
|
|
762
|
+
if (!doc.contents.some((content) => content.includes('@file')) && hasTagGap(doc.contents)) {
|
|
763
|
+
add(doc.start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
764
|
+
}
|
|
765
|
+
index = doc.end
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
638
769
|
for (let index = 0; index < lines.length; index += 1) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
770
|
+
if (doxyHeaderEnd !== -1 && index <= doxyHeaderEnd) {
|
|
771
|
+
index = doxyHeaderEnd
|
|
772
|
+
continue
|
|
773
|
+
}
|
|
774
|
+
if (!lines[index].trim().startsWith('///')) continue
|
|
775
|
+
if (lines[index].trim().startsWith('///<')) continue
|
|
776
|
+
const start = index
|
|
777
|
+
let end = index
|
|
778
|
+
while (end + 1 < lines.length && lines[end + 1].trim().startsWith('///') && !lines[end + 1].trim().startsWith('///<')) {
|
|
779
|
+
end += 1
|
|
780
|
+
}
|
|
781
|
+
index = end
|
|
782
|
+
let next = end + 1
|
|
783
|
+
if (next < lines.length && lines[next].trim() === '') {
|
|
784
|
+
add(end + 1, 'doc-gap', '注释与声明之间有空行:注释会挂到下一个声明,请删掉空行')
|
|
785
|
+
while (next < lines.length && lines[next].trim() === '') next += 1
|
|
786
|
+
}
|
|
787
|
+
if (next >= lines.length) continue
|
|
788
|
+
const declaration = joinedDeclaration(lines, next)
|
|
789
|
+
const block = lines.slice(start, end + 1).join('\n')
|
|
790
|
+
const docContents = lines.slice(start, end + 1).map((line) => line.trim().replace(/^\/\/\/\s?/, ''))
|
|
791
|
+
if (hasBriefTagGap(docContents)) {
|
|
792
|
+
add(start + 1, 'brief-tag-gap', '摘要或详细描述与标签块之间要空一行:在首个标签前补一行 `///`')
|
|
793
|
+
}
|
|
794
|
+
if (hasTagGap(docContents)) {
|
|
795
|
+
add(start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
796
|
+
}
|
|
797
|
+
for (const issue of retvalOrderIssues(docContents)) add(start + 1, issue.rule, issue.message)
|
|
798
|
+
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
799
|
+
if (!isType) {
|
|
800
|
+
for (const issue of functionDocIssues(declaration, docContents, options)) add(start + 1, issue.rule, issue.message)
|
|
801
|
+
}
|
|
802
|
+
if (!isType && CALLER_LIST.test(block) && MEMBER_REF.test(block)) {
|
|
803
|
+
add(start + 1, 'caller-list', '函数注释在点名调用者:调用关系会随重构过期,只写这个函数做什么、参数、返回值与异常')
|
|
804
|
+
}
|
|
805
|
+
if (RATIONALE.test(block)) {
|
|
806
|
+
add(start + 1, 'rationale', '注释在写设计取舍:方案对比与理由移到正文或实现注释,接口注释只留契约')
|
|
807
|
+
}
|
|
808
|
+
if (isType && MEMBER_LIST.test(block)) {
|
|
809
|
+
add(start + 1, 'member-list', '类型注释在罗列成员功能,应改写为概念与使用约定')
|
|
810
|
+
}
|
|
811
|
+
if (isType && FORWARD_REF.test(block)) {
|
|
812
|
+
add(start + 1, 'forward-ref', '类型注释在交代别的类型负责什么:只写本类型的契约,删掉这句,不要挪到文件头')
|
|
813
|
+
}
|
|
814
|
+
if (isType && TYPE_SYSTEM.test(block)) {
|
|
815
|
+
add(start + 1, 'type-system', '类型注释在复述类型系统已表达的性质:可拷贝与 const 查询由声明给出,删掉这句')
|
|
816
|
+
}
|
|
817
|
+
if (isType && NEGATED_BOUNDARY.test(block)) {
|
|
818
|
+
add(start + 1, 'negated-boundary', '类型注释用「只保存/不拥有/不含」写否定式边界:改成名词短语,边界交给成员类型表达')
|
|
819
|
+
}
|
|
820
|
+
if (NEGATIVE_SCOPE.test(block)) {
|
|
821
|
+
add(start + 1, 'negative-scope', '注释用「不检查/不校验/不负责/不判断」写能力边界:改成正向限定,前置条件写「调用方保证…」或 @pre')
|
|
822
|
+
}
|
|
823
|
+
if (isType && DOMAIN_SIZE.test(block)) {
|
|
824
|
+
add(start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由 squares_ 之类成员声明表达,删掉这句')
|
|
825
|
+
}
|
|
826
|
+
const firstDocLine = lines[start].trim().replace(/^\/\/\/\s*/, '')
|
|
827
|
+
if (!isType && PREDICATE_DECL.test(declaration) && !isPredicateFirstOk(declaration, firstDocLine)) {
|
|
828
|
+
add(start + 1, 'predicate-first', '布尔查询的第一句应写「…是否…」,句首不用「检查/判断/校验/验证」;operator== 写「相等运算符」')
|
|
829
|
+
}
|
|
830
|
+
if (options.requireParam !== false) {
|
|
831
|
+
const params = functionParams(declaration)
|
|
832
|
+
if (!params) continue
|
|
833
|
+
const summary = summaryParagraph(docContents)
|
|
834
|
+
const named = params.filter((name) => mentionsParam(summary, name))
|
|
835
|
+
if (named.length > 0) {
|
|
836
|
+
add(start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
837
|
+
}
|
|
838
|
+
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
839
|
+
for (const name of params) {
|
|
840
|
+
if (!documented.includes(name)) add(start + 1, 'param-missing', `参数 ${name} 没有 @param 说明`)
|
|
841
|
+
}
|
|
842
|
+
for (const name of documented) {
|
|
843
|
+
if (!params.includes(name)) add(start + 1, 'param-extra', `@param ${name} 在声明里不存在`)
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
if (style === 'block') {
|
|
849
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
850
|
+
if (doxyHeaderEnd !== -1 && index <= doxyHeaderEnd) {
|
|
851
|
+
index = doxyHeaderEnd
|
|
852
|
+
continue
|
|
853
|
+
}
|
|
854
|
+
const doc = blockDocAt(lines, index)
|
|
855
|
+
if (!doc) continue
|
|
856
|
+
index = doc.end
|
|
857
|
+
let next = doc.end + 1
|
|
858
|
+
if (next < lines.length && lines[next].trim() === '') {
|
|
859
|
+
add(doc.end + 1, 'doc-gap', '注释与声明之间有空行:注释会挂到下一个声明,请删掉空行')
|
|
860
|
+
while (next < lines.length && lines[next].trim() === '') next += 1
|
|
861
|
+
}
|
|
862
|
+
if (next >= lines.length) continue
|
|
863
|
+
const declaration = joinedDeclaration(lines, next)
|
|
864
|
+
const block = doc.contents.join('\n')
|
|
865
|
+
for (const issue of retvalOrderIssues(doc.contents)) add(doc.start + 1, issue.rule, issue.message)
|
|
866
|
+
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
867
|
+
if (!isType) {
|
|
868
|
+
for (const issue of functionDocIssues(declaration, doc.contents, options)) add(doc.start + 1, issue.rule, issue.message)
|
|
869
|
+
}
|
|
870
|
+
if (!isType && CALLER_LIST.test(block) && MEMBER_REF.test(block)) {
|
|
871
|
+
add(doc.start + 1, 'caller-list', '函数注释在点名调用者:调用关系会随重构过期,只写这个函数做什么、参数、返回值与异常')
|
|
872
|
+
}
|
|
873
|
+
if (RATIONALE.test(block)) add(doc.start + 1, 'rationale', '注释在写设计取舍:方案对比与理由移到正文或实现注释,接口注释只留契约')
|
|
874
|
+
if (isType && MEMBER_LIST.test(block)) add(doc.start + 1, 'member-list', '类型注释在罗列成员功能,应改写为概念与使用约定')
|
|
875
|
+
if (isType && FORWARD_REF.test(block)) add(doc.start + 1, 'forward-ref', '类型注释在交代别的类型负责什么:只写本类型的契约,删掉这句,不要挪到文件头')
|
|
876
|
+
if (isType && TYPE_SYSTEM.test(block)) add(doc.start + 1, 'type-system', '类型注释在复述类型系统已表达的性质:可拷贝与 const 查询由声明给出,删掉这句')
|
|
877
|
+
if (isType && NEGATED_BOUNDARY.test(block)) add(doc.start + 1, 'negated-boundary', '类型注释用「只保存/不拥有/不含」写否定式边界:改成名词短语,边界交给成员类型表达')
|
|
878
|
+
if (NEGATIVE_SCOPE.test(block)) add(doc.start + 1, 'negative-scope', '注释用「不检查/不校验/不负责/不判断」写能力边界:改成正向限定,前置条件写「调用方保证…」或 @pre')
|
|
879
|
+
if (isType && DOMAIN_SIZE.test(block)) add(doc.start + 1, 'domain-size', '类型注释在写领域尺寸:格子数由 squares_ 之类成员声明表达,删掉这句')
|
|
880
|
+
const firstDocLine = doc.contents[0] ?? ''
|
|
881
|
+
if (!isType && PREDICATE_DECL.test(declaration) && !isPredicateFirstOk(declaration, firstDocLine)) {
|
|
882
|
+
add(doc.start + 1, 'predicate-first', '布尔查询的第一句应写「…是否…」,句首不用「检查/判断/校验/验证」;operator== 写「相等运算符」')
|
|
883
|
+
}
|
|
884
|
+
if (options.requireParam !== false) {
|
|
885
|
+
const params = functionParams(declaration)
|
|
886
|
+
if (!params) continue
|
|
887
|
+
const summary = summaryParagraph(doc.contents)
|
|
888
|
+
const named = params.filter((name) => mentionsParam(summary, name))
|
|
889
|
+
if (named.length > 0) {
|
|
890
|
+
add(doc.start + 1, 'param-in-summary', `函数摘要直接写了参数名 ${named.join('、')}:改用「指定坐标」「传入的值」这类语义名词,参数名只放在 @param 行`)
|
|
891
|
+
}
|
|
892
|
+
const documented = [...block.matchAll(/@param(?:\[[^\]]*\])?\s+(\w+)/g)].map((match) => match[1])
|
|
893
|
+
for (const name of params) {
|
|
894
|
+
if (!documented.includes(name)) add(doc.start + 1, 'param-missing', `参数 ${name} 没有 @param 说明`)
|
|
895
|
+
}
|
|
896
|
+
for (const name of documented) {
|
|
897
|
+
if (!params.includes(name)) add(doc.start + 1, 'param-extra', `@param ${name} 在声明里不存在`)
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
904
|
+
if (doxyHeaderEnd !== -1 && index <= doxyHeaderEnd) continue
|
|
905
|
+
if (/^\s/.test(lines[index])) continue
|
|
906
|
+
const name = escapedControlConstant(lines[index])
|
|
907
|
+
if (!name) continue
|
|
908
|
+
const doc = attachedDoc(lines, index, style)
|
|
909
|
+
if (!doc) {
|
|
910
|
+
add(index + 1, 'constant-comment-missing', `控制序列常量 ${name} 缺少注释:在声明上一行写「……的控制序列:……。」`)
|
|
911
|
+
continue
|
|
912
|
+
}
|
|
913
|
+
const summary = summaryParagraph(doc.contents)
|
|
914
|
+
const firstSentence = summary.match(/[^。]*。/)?.[0] ?? summary
|
|
915
|
+
if (!SEQUENCE_FIRST.test(firstSentence)) {
|
|
916
|
+
add(doc.start + 1, 'constant-sequence-first', `控制序列常量 ${name} 的第一句要写成名词短语「……的控制序列:……。」:先说是哪个序列,再写名字和转义写法看不出的事实`)
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
for (let index = 0; index + 1 < lines.length; index += 1) {
|
|
921
|
+
const trimmed = lines[index].trim()
|
|
922
|
+
if (!trimmed.startsWith('//') || trimmed.startsWith('///') || trimmed.startsWith('//!')) continue
|
|
923
|
+
const next = lines[index + 1].trim()
|
|
924
|
+
if (next === '' || next.startsWith('//')) continue
|
|
925
|
+
if (!/(下标|存储|对应坐标|行优先|存放)/.test(trimmed)) continue
|
|
926
|
+
const identifiers = next.match(/[A-Za-z_]\w*/g) ?? []
|
|
927
|
+
if (!identifiers.some((identifier) => trimmed.includes(identifier))) continue
|
|
928
|
+
let residue = trimmed
|
|
929
|
+
for (const identifier of identifiers) residue = residue.split(identifier).join('')
|
|
930
|
+
residue = residue.replace(
|
|
931
|
+
/(对应坐标|行优先|下标|存储|存放|声明|类型|棋盘|格子|元素|坐标|行列|行|列|当前|每个|所有|用来|用于|表示|保存|初始化|的是|的了|在|中|里|和|与|及|或|按|class|struct|enum|const|auto|row|col|index|indices|cell|cells|board|data|value|values)/g,
|
|
932
|
+
'',
|
|
933
|
+
)
|
|
934
|
+
residue = residue.replace(/[\s\p{P}\p{S}\p{N}]/gu, '')
|
|
935
|
+
if (residue === '') {
|
|
936
|
+
add(index + 1, 'restates-next', '实现注释只重复下一行声明的标识符或类型:删掉这句,让声明自己表达')
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
941
|
+
if (!inClass[index]) continue
|
|
942
|
+
const line = lines[index]
|
|
943
|
+
const commentAt = line.indexOf('//')
|
|
944
|
+
const code = (commentAt === -1 ? line : line.slice(0, commentAt)).trim()
|
|
945
|
+
if (!code.endsWith(';') || code.includes('(')) continue
|
|
946
|
+
const names = code.match(/\b[A-Za-z_]\w*_\b/g)
|
|
947
|
+
if (!names) continue
|
|
948
|
+
const member = names.at(-1)
|
|
949
|
+
const previous = index > 0 ? lines[index - 1].trim() : ''
|
|
950
|
+
if (commentAt !== -1) {
|
|
951
|
+
if (line.slice(commentAt).startsWith('///<')) {
|
|
952
|
+
if (inStruct[index]) continue
|
|
953
|
+
add(index + 1, 'member-comment-trailing', `数据成员 ${member} 只用行尾 ///< 注释:///< 只留给枚举项和 struct 的短字段,类数据成员的主注释写在声明上一行`)
|
|
954
|
+
continue
|
|
955
|
+
}
|
|
956
|
+
if (!previous.startsWith('//')) {
|
|
957
|
+
add(index + 1, 'member-comment-missing', `数据成员 ${member} 只有行尾注释:主注释写在声明上一行,行尾不写补充`)
|
|
958
|
+
} else {
|
|
959
|
+
add(index + 1, 'member-comment-trailing', `数据成员 ${member} 的行尾注释应删掉:主注释只写在声明上一行,行尾只允许 ///< 用于枚举项或 struct 短字段`)
|
|
960
|
+
}
|
|
961
|
+
} else if (previous.startsWith('//') && !previous.startsWith('///')) {
|
|
962
|
+
add(index + 1, 'member-comment-slash', `数据成员 ${member} 的主注释用了 //:类数据成员统一用 ///,写在声明上一行`)
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
967
|
+
const trimmed = lines[index].trim()
|
|
968
|
+
if (!/^(template\s*<[^>]*>\s*)?enum\b/.test(trimmed)) continue
|
|
969
|
+
let body = index
|
|
970
|
+
while (body < lines.length && !lines[body].includes('{')) body += 1
|
|
971
|
+
if (body >= lines.length) continue
|
|
972
|
+
let commented = 0
|
|
973
|
+
let bare = 0
|
|
974
|
+
for (let cursor = body + 1; cursor < lines.length; cursor += 1) {
|
|
975
|
+
const line = lines[cursor].trim()
|
|
976
|
+
if (line.startsWith('}')) break
|
|
977
|
+
if (line === '' || line.startsWith('//') || line.startsWith('#')) continue
|
|
978
|
+
const code = line.replace(/\/\/.*$/, '').trim()
|
|
979
|
+
if (/^[A-Za-z_]\w*\s*(=[^,]+)?,?$/.test(code)) {
|
|
980
|
+
if (line.includes('///<')) commented += 1
|
|
981
|
+
else bare += 1
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
if (commented > 0 && bare > 0) {
|
|
985
|
+
add(index + 1, 'enum-partial', '枚举成员注释需全写或全不写,当前只有部分成员有 ///<')
|
|
986
|
+
}
|
|
987
|
+
index = body
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
991
|
+
const marker = lines[index].indexOf('///<')
|
|
992
|
+
if (marker === -1) continue
|
|
993
|
+
const content = lines[index].slice(marker + 4).trim()
|
|
994
|
+
if (content.endsWith('。')) {
|
|
995
|
+
add(index + 1, 'enum-trailing-period', '行尾短句不收句号:删掉 ///< 说明末尾的「。」')
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1000
|
+
const marker = lines[index].indexOf('///<')
|
|
1001
|
+
if (marker === -1) continue
|
|
1002
|
+
const content = lines[index].slice(marker + 4).trim().replace(/。$/, '').trim()
|
|
1003
|
+
const colon = content.indexOf(':')
|
|
1004
|
+
if (colon !== -1 && content.slice(colon + 1).trim() === '') {
|
|
1005
|
+
add(index + 1, 'enum-trailing-empty', '行尾注释冒号后为空:写「类别:说明」,冒号两侧都不能为空')
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
return issues.sort((left, right) => left.line - right.line)
|
|
804
1010
|
}
|
|
805
1011
|
|
|
806
1012
|
/** 递归收集源文件,跳过生成目录与隐藏目录。 */
|
|
807
1013
|
export function collectFiles(root) {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
1014
|
+
const files = []
|
|
1015
|
+
const walk = (dir) => {
|
|
1016
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
1017
|
+
const full = path.join(dir, entry.name)
|
|
1018
|
+
if (entry.isDirectory()) {
|
|
1019
|
+
if (SKIP_DIR.has(entry.name.toLowerCase()) || entry.name.startsWith('.')) continue
|
|
1020
|
+
walk(full)
|
|
1021
|
+
continue
|
|
1022
|
+
}
|
|
1023
|
+
if (entry.isFile() && SOURCE_EXT.has(path.extname(entry.name).toLowerCase())) files.push(full)
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (statSync(root).isFile()) return [root]
|
|
1027
|
+
walk(root)
|
|
1028
|
+
return files.sort()
|
|
823
1029
|
}
|
|
824
1030
|
|
|
825
1031
|
function main(argv) {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
try {
|
|
845
|
-
files = collectFiles(root)
|
|
846
|
-
} catch (error) {
|
|
847
|
-
return usage(`读取 ${root} 失败:${error.message}`)
|
|
848
|
-
}
|
|
849
|
-
const results = []
|
|
850
|
-
const texts = new Map()
|
|
851
|
-
for (const file of files) {
|
|
852
|
-
let text
|
|
1032
|
+
let options
|
|
1033
|
+
try {
|
|
1034
|
+
options = parseArgs(argv)
|
|
1035
|
+
} catch (error) {
|
|
1036
|
+
return usage(error.message)
|
|
1037
|
+
}
|
|
1038
|
+
if (options.help) return help()
|
|
1039
|
+
const root = options.root ? path.resolve(options.root) : process.cwd()
|
|
1040
|
+
try {
|
|
1041
|
+
statSync(root)
|
|
1042
|
+
} catch {
|
|
1043
|
+
return usage(`根目录不存在或无法读取:${root}`)
|
|
1044
|
+
}
|
|
1045
|
+
const columnLimit = readColumnLimit(root)
|
|
1046
|
+
if (columnLimit !== undefined) {
|
|
1047
|
+
if (!options.widthExplicit) options.width = columnLimit
|
|
1048
|
+
}
|
|
1049
|
+
let files
|
|
853
1050
|
try {
|
|
854
|
-
|
|
1051
|
+
files = collectFiles(root)
|
|
855
1052
|
} catch (error) {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
texts
|
|
860
|
-
const
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
1053
|
+
return usage(`读取 ${root} 失败:${error.message}`)
|
|
1054
|
+
}
|
|
1055
|
+
const results = []
|
|
1056
|
+
const texts = new Map()
|
|
1057
|
+
for (const file of files) {
|
|
1058
|
+
let text
|
|
1059
|
+
try {
|
|
1060
|
+
text = readFileSync(file, 'utf8')
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
process.stderr.write(`错误:读取 ${file} 失败:${error.message}\n`)
|
|
1063
|
+
return EXIT.usage
|
|
1064
|
+
}
|
|
1065
|
+
texts.set(file, text)
|
|
1066
|
+
const width = options.widthExplicit
|
|
1067
|
+
? options.width
|
|
1068
|
+
: (readColumnLimitNear(file, root) ?? options.width)
|
|
1069
|
+
for (const issue of checkText(text, { ...options, width, fileName: path.relative(root, file) || file })) {
|
|
1070
|
+
results.push({ file: path.relative(root, file) || file, ...issue })
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
const headerNames = new Set()
|
|
1074
|
+
for (const file of files) {
|
|
1075
|
+
if (HEADER_EXT.has(path.extname(file).toLowerCase())) {
|
|
1076
|
+
for (const name of declarationNames(texts.get(file))) headerNames.add(name)
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
for (const file of files) {
|
|
1080
|
+
if (!CPP_EXT.has(path.extname(file).toLowerCase())) continue
|
|
1081
|
+
for (const issue of checkDefinitionDocs(texts.get(file), headerNames, options)) {
|
|
1082
|
+
results.push({ file: path.relative(root, file) || file, ...issue })
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
results.sort((left, right) => left.file.localeCompare(right.file) || left.line - right.line)
|
|
1086
|
+
if (options.json) {
|
|
1087
|
+
process.stdout.write(`${JSON.stringify({ root, files: files.length, issues: results }, null, 2)}\n`)
|
|
1088
|
+
} else if (results.length > 0) {
|
|
1089
|
+
for (const issue of results) process.stdout.write(`${issue.file}:${issue.line}: [${issue.rule}] ${issue.message}\n`)
|
|
1090
|
+
const touched = new Set(results.map((issue) => issue.file)).size
|
|
1091
|
+
process.stdout.write(`发现 ${results.length} 处注释问题,涉及 ${touched} 个文件。\n`)
|
|
1092
|
+
} else if (!options.quiet) {
|
|
1093
|
+
process.stdout.write(`注释检查通过:${files.length} 个文件。\n`)
|
|
1094
|
+
}
|
|
1095
|
+
return results.length === 0 ? EXIT.ok : EXIT.issues
|
|
890
1096
|
}
|
|
891
1097
|
|
|
892
1098
|
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
|
|
893
|
-
|
|
1099
|
+
process.exitCode = main(process.argv.slice(2))
|
|
894
1100
|
}
|
|
895
1101
|
|
|
896
1102
|
export { HEADER_EXT }
|