skills/check-test-code-quality/rules/R013/SKILL.md
# R013: 注释的废弃代码 ## 规则信息 | 属性 | 值 | |------|-----| | 规则编号 | R013 | | 问题类型 | 注释的废弃代码 | | 严重级别 | Warning | | 规则复杂度 | complex | | 扫描范围 | 测试文件(`.test.ets`, `.test.ts`, `.test.js`) | | testcase字段 | 所属的it()名称,不在it()块内时为`-` | ## 问题描述 存在大量注释的废弃代码。测试文件中包含连续多行的注释代码块,这些代码已经不再使用但仍然保留在文件中,影响代码可读性。 ## 修复建议 直接删除注释的废弃代码。使用版本控制系统(如Git)保留历史记录,不需要在代码中注释保留。 ## 扫描逻辑 ### Step 1: 筛选测试文件 ```python import os TEST_EXTENSIONS = ('.test.ets', '.test.ts', '.test.js') def find_test_files(scan_root): test_files
npx skillsauth add openharmonyinsight/openharmony-skills skills/check-test-code-quality/rules/R013Install this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| 属性 | 值 |
|------|-----|
| 规则编号 | R013 |
| 问题类型 | 注释的废弃代码 |
| 严重级别 | Warning |
| 规则复杂度 | complex |
| 扫描范围 | 测试文件(.test.ets, .test.ts, .test.js) |
| testcase字段 | 所属的it()名称,不在it()块内时为- |
存在大量注释的废弃代码。测试文件中包含连续多行的注释代码块,这些代码已经不再使用但仍然保留在文件中,影响代码可读性。
直接删除注释的废弃代码。使用版本控制系统(如Git)保留历史记录,不需要在代码中注释保留。
import os
TEST_EXTENSIONS = ('.test.ets', '.test.ts', '.test.js')
def find_test_files(scan_root):
test_files = []
for dirpath, dirnames, filenames in os.walk(scan_root):
for fn in filenames:
if fn.endswith(TEST_EXTENSIONS):
test_files.append(os.path.join(dirpath, fn))
return test_files
检测连续3行及以上的注释行组成的代码块。
import re
def find_comment_blocks(content):
lines = content.split('\n')
blocks = []
current_block = []
for i, line in enumerate(lines, 1):
stripped = line.strip()
is_comment = (
stripped.startswith('//') or
stripped.startswith('*') or
stripped.startswith('/*') or
(current_block and stripped.startswith('*/'))
)
if is_comment:
current_block.append((i, line))
else:
if len(current_block) >= 3:
blocks.append(list(current_block))
current_block = []
if len(current_block) >= 3:
blocks.append(list(current_block))
return blocks
判断注释块内容是否包含代码特征(而非普通文档注释)。
CODE_PATTERNS = [
r'\bfunction\b',
r'\bvar\b',
r'\blet\b',
r'\bconst\b',
r'\breturn\b',
r'\bif\s*\(',
r'\bfor\s*\(',
r'\bwhile\s*\(',
r'\bswitch\s*\(',
r'\bcase\b',
r'\bbreak\b',
r'\bclass\b',
r'\bimport\b',
r'\bexport\b',
r'\basync\b',
r'\bawait\b',
r'\btry\b',
r'\bcatch\b',
r'\bthrow\b',
r'\bnew\b',
r'\bthis\b',
r'\bexpect\b',
r'\bit\s*\(',
r'\bdescribe\s*\(',
r'\{',
r'\}',
r';',
r'\=\>',
r'\.\w+\s*\(',
]
def extract_comment_text(block):
texts = []
for _, line in block:
stripped = line.strip()
if stripped.startswith('//'):
texts.append(stripped[2:].strip())
elif stripped.startswith('*') and not stripped.startswith('*/'):
texts.append(stripped[1:].strip())
elif stripped.startswith('/*'):
texts.append(stripped[2:].strip())
elif stripped.startswith('*/'):
texts.append(stripped[2:].strip())
else:
texts.append(stripped)
return '\n'.join(texts)
def has_code_characteristics(comment_text):
pattern_count = 0
for pattern in CODE_PATTERNS:
if re.search(pattern, comment_text):
pattern_count += 1
return pattern_count >= 2
检查注释块中是否包含完整的函数定义或测试用例声明。
def has_complete_function(comment_text):
patterns = [
r'function\s+\w+\s*\([^)]*\)\s*\{',
r'(?:async\s+)?\w+\s*=\s*(?:async\s+)?\([^)]*\)\s*(?:=>|\{)',
r'it\s*\(\s*["\'][^"\']+["\']',
r'describe\s*\(\s*["\'][^"\']+["\']',
r'\bclass\s+\w+',
]
match_count = sum(1 for p in patterns if re.search(p, comment_text))
return match_count >= 1
def is_javadoc_like(comment_text):
javadoc_markers = [
r'@tc\.name',
r'@tc\.number',
r'@tc\.desc',
r'@tc\.size',
r'@tc\.type',
r'@tc\.level',
r'@param',
r'@return',
r'@throws',
r'@since',
r'@deprecated',
]
match_count = sum(1 for m in javadoc_markers if re.search(m, comment_text))
return match_count >= 2
解析文件中所有it()块的范围,判断注释块的起始行落在哪个it()块内。
def find_it_blocks(content):
it_pattern = re.compile(
r"it\s*\(\s*['\"]([^'\"]+)['\"]",
re.MULTILINE
)
it_blocks = []
for match in it_pattern.finditer(content):
name = match.group(1)
start_line = content[:match.start()].count('\n') + 1
it_blocks.append((start_line, name))
return sorted(it_blocks, key=lambda x: x[0])
def get_testcase_for_line(line_num, it_blocks):
testcase = '-'
for i, (start, name) in enumerate(it_blocks):
if i + 1 < len(it_blocks):
next_start = it_blocks[i + 1][0]
if start < line_num < next_start:
testcase = name
break
else:
if line_num > start:
testcase = name
break
return testcase
def scan_r013(scan_root, base_dir):
issues = []
test_files = find_test_files(scan_root)
for file_path in test_files:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
it_blocks = find_it_blocks(content)
comment_blocks = find_comment_blocks(content)
for block in comment_blocks:
start_line = block[0][0]
end_line = block[-1][0]
comment_text = extract_comment_text(block)
if is_javadoc_like(comment_text):
continue
if not has_code_characteristics(comment_text):
continue
is_function = has_complete_function(comment_text)
if not is_function and len(block) < 5:
continue
rel_path = os.path.relpath(file_path, base_dir)
testcase = get_testcase_for_line(start_line, it_blocks)
block_preview = '\n'.join(line for _, line in block[:5])
if len(block) > 5:
block_preview += '\n ... (省略' + str(len(block) - 5) + '行)'
issues.append({
'rule': 'R013',
'type': '注释的废弃代码',
'severity': 'Warning',
'file': rel_path,
'line': start_line,
'testcase': testcase,
'snippet': block_preview,
'suggestion': (
f'第{start_line}-{end_line}行存在注释的废弃代码(共{len(block)}行)。'
f'建议直接删除,使用版本控制系统保留历史记录。'
),
})
return issues
// 错误1:大量注释的废弃代码
// 废弃的旧方法 - 不要删除,保留参考
// function oldMethod() {
// let value = testFunction();
// expect(value).assertEqual('expected');
// return value;
// }
function newMethod() {
let newValue = newTestFunction();
expect(newValue).assertEqual('newExpected');
}
// 错误2:注释掉的完整测试用例
it('test001', () => {
// 废弃的测试逻辑
// let value = testFunction();
// expect(value).assertEqual('expected');
// done();
let newValue = newTestFunction();
expect(newValue).assertEqual('newExpected');
});
// 错误3:注释掉的函数定义(3行以上)
// function oldTestMethod(done: Function) {
// console.info('old test start');
// let result = doSomething();
// expect(result).assertTrue();
// done();
// }
// 正确:直接删除废弃代码,保持代码简洁
it('test001', () => {
let newValue = newTestFunction();
expect(newValue).assertEqual('newExpected');
});
// 正确:如果需要保留旧逻辑作为参考,使用Git历史记录
function newMethod() {
let currentValue = currentFunction();
expect(currentValue).assertEqual('expected');
}
以下注释块不应被报告:
@tc.name、@tc.number、@param、@return等标记的注释// let x = 1;一行,不构成"废弃代码块"每条issue的字段:
| 字段 | 值 |
|------|-----|
| rule | R013 |
| type | 注释的废弃代码 |
| severity | Warning |
| file | 相对路径(如xxx/test.test.ets) |
| line | 注释块起始行号 |
| testcase | 所属的it()名称,不在it()块内时为- |
| snippet | 注释块前5行预览 |
| suggestion | 第X-Y行存在注释的废弃代码(共N行)。建议直接删除,使用版本控制系统保留历史记录。 |
development
Run local code quality checks covering a subset of OpenHarmony gate CI (copyright, CodeArts C/C++) plus additional local checks (pylint/flake8, shellcheck/bashate, gn format). Use before committing to reduce gate failures. Triggers on: /oh-precommit-codecheck, "门禁检查", "门禁预检", "检查代码", "run codecheck", "check code quality", "lint my code", "代码检查", or after completing code implementation. WHEN to use: before git commit, before creating PR, after modifying C/C++/Python/Shell/GN files, when gate CI fails with codecheck defects, or when you want to preview what gate will flag.
development
OpenHarmony PR full lifecycle workflow. Five modes: - Commit: standardized commit with DCO sign-off and Issue linking - Create PR: commit + push to fork + create Issue + create PR on upstream - Fix Codecheck: fetch gate CI codecheck defects from a PR and auto-fix them - Review PR: fetch a PR's changes to local for code review - Fix Review: fetch unresolved review comments from a PR and auto-fix them Triggers on: /oh-pr-workflow, "提交代码", "创建PR", "提个PR", "commit", "修复告警", "修复门禁", "修复codecheck", "fix codecheck", "review pr", "review这个pr", "看下这个pr", "检视pr", "修复review", "修复检视意见", "fix review", or a GitCode PR URL with fix/review intent.
testing
分析 HM Desktop PRD 文档,提取需求信息、验证完整性、检查章节顺序(需求来源→需求背景→需求价值分析→竞品分析→需求描述)、检查 KEP 定义、检测需求冲突并生成结构化分析报告。适用于用户请求:(1) 分析或审查 PRD 文档, (2) 从需求中提取 KEP 列表, (3) 检查 PRD 完整性或一致性, (4) 将需求映射到模块架构, (5) 验证 PRD 格式合规性, (6) 验证竞品分析章节完整性。关键词:PRD分析, requirement extraction, KEP验证, completeness check, chapter order validation, 竞品分析检查, analyze PRD, 需求提取, 完整性检查, 章节顺序验证
development
基于 PRD 文档自动生成鸿蒙系统设计文档,包括架构设计文档和功能设计文档。生成前会分析 OpenHarmony 存量代码结构,确保与现有架构兼容。架构设计文档第2章必须为竞品方案分析,位于需求背景之后。适用于用户请求:(1) 生成架构设计文档, (2) 生成功能设计文档, (3) 从 PRD 生成设计文档, (4) 创建系统架构设计, (5) 编写功能规格说明, (6) 分析 OH 代码结构。关键词:architecture design, functional design, design doc, 竞品方案分析, OpenHarmony code analysis, 架构设计, 功能设计, 设计文档生成, OH代码分析, analyze codebase, competitor analysis