home / skills / forge-town / skills / remove-comments

remove-comments skill

/skills/remove-comments

This skill removes comments from simple code files across languages like Python, JavaScript, Java, and Go without altering formatting or logic.

npx playbooks add skill forge-town/skills --skill remove-comments

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
1.8 KB
---
name: remove-comments
description: 通过自然语言指导删除代码注释,适用于Python、JavaScript、TypeScript(.ts/.tsx)、Java、C/C++、Go、HTML等语言的简单场景
---

# 代码注释删除器

## 任务目标
- 本 Skill 用于:通过智能体的语言理解能力删除代码文件中的注释
- 触发条件:用户提到"删除注释"、"清理注释"、"去注释"、"remove comments"等
- 适用场景:简单代码的注释清理,复杂代码建议使用专业工具

## 操作步骤

1. 读取代码文件并识别编程语言
2. 根据语言类型识别并删除注释(详见各语言指导)
3. 输出清理后的代码,保持格式和逻辑完整
4. 验证结果,确保无语法错误

## 语言处理指导

详细的注释规则和示例请参考 [references/examples.md](references/examples.md),以下是核心要点:

- **Python**:删除 `#` 单行注释和 `'''` `"""` 多行注释
- **JavaScript/TypeScript/TSX/Java/C/C++/Go**:删除 `//` 单行注释和 `/* */` 多行注释,TSX 需额外删除 `{/* */}` JSX 注释
- **HTML/XML**:删除 `<!-- -->` 注释

**重要原则**:
- 保护字符串中的注释符号(如 URL `http://`、路径 `//server`)
- 保持代码缩进和格式
- 不影响代码逻辑

## 注意事项

⚠️ **局限性**:智能体处理可能不如专门的代码解析工具精确

⚠️ **字符串保护**:注意不误删字符串中的注释符号

⚠️ **复杂情况**:嵌套注释、条件注释等复杂情况可能需要人工检查

✅ **适用场景**:简单的代码清理、学习用途的代码简化

建议:处理重要代码前先备份,验证结果后再使用

## 资源索引

- 参考文档:见 [references/examples.md](references/examples.md)(包含各语言的详细注释删除示例)

Overview

This skill removes comments from source code files on command, using natural-language instructions to detect language and strip common comment styles. It supports Python, JavaScript, TypeScript (.ts/.tsx), Java, C/C++, Go, and HTML/XML for straightforward cleaning tasks. The tool aims to preserve code formatting and logic while protecting comment-like content inside strings.

How this skill works

The skill reads the provided code, auto-detects the language, and applies language-specific rules to remove single-line and multi-line comments. It preserves string literals and common patterns like URLs and file paths to avoid accidental deletion. After cleaning, it returns the modified code and performs a basic syntax sanity check to reduce introduced errors.

When to use it

  • Quickly clean up simple example files or snippets before sharing
  • Remove instructional or redundant comments for readability tests
  • Prepare code samples for teaching or documentation where comments are distracting
  • Strip comments from generated code when minimizing examples
  • Preprocess files for manual review before applying more advanced refactors

Best practices

  • Always back up the original file before bulk comment removal
  • Run unit tests or a syntax check after cleaning important code
  • Manually review outputs for edge cases like nested or conditional comments
  • Use specialized parsers for complex projects or languages with ambiguous comment syntax
  • Provide the language or file extension if automatic detection may be ambiguous

Example use cases

  • Remove line and block comments from a Python script while preserving triple-quoted strings
  • Strip // and /* */ comments from JavaScript/TypeScript files and JSX-specific {/* */} comments in .tsx
  • Clean C/C++ or Java source by removing // and /* */ comments before sharing examples
  • Remove <!-- --> comments from HTML or XML snippets for streamlined demos
  • Quickly strip comments from Go files while keeping string literals and URLs intact

FAQ

Will this change code behavior?

The skill aims to only remove comments and preserve logic, but you should run tests and review results for safety before deploying changes.

Does it handle comments inside strings or URLs?

Yes. The tool protects common string literal patterns and URLs to avoid accidental removal of comment-like sequences.

Can it process large or complex codebases?

It is intended for simple to moderate files. For large or highly complex projects, use a language-aware parser or refactoring tool.