Comment removal edge cases
A comment marker only starts a comment in code. Inside a string, a regular expression or a URL it is just text, so a tool that deletes everything after // cuts https://example.com in half. Use these snippets to test any comment remover before you trust it.
Each case shows an input, the correct output and the mistake a simple tool makes. Every output is the actual result of Remove Comments with the preset named in the heading.
1. URLs in strings and template literals (C-like)
const api = "https://example.com/v1"; // base address
const url = `https://example.com/users/${id}`; // user page
fetch(url);
Correct result:
const api = "https://example.com/v1";
const url = `https://example.com/users/${id}`;
fetch(url);
Tools that know " and ' but not backticks cut the second line at https:, leaving an unclosed template literal.
2. A quote inside a comment (C-like)
// Don't call this twice
let total = 0;
/* Old rate, kept for reference
const rate = 0.15; */
const rate = 0.18;
Correct result:
let total = 0;
const rate = 0.18;
The apostrophe in Don't is read as the start of a string, so the tool silently keeps the block comment with the old rate. The code still runs, and nothing tells you the cleanup did not happen.
3. Comment markers in a regular expression (C-like)
const onlySlashes = /^\/*$/; // matches "", "/", "//"
const keep = true;
/* end of checks */
Correct result:
const onlySlashes = /^\/*$/;
const keep = true;
/* inside the pattern is taken as a block comment, and everything up to the next */ is deleted, including const keep = true;. Because / is also division, Appscodes treats it as a regular expression only after an operator such as =, ( or ,, or a keyword such as return. It is a rule of thumb, so check files with unusual patterns.
4. Python docstrings and triple quoted strings (Python)
def area(r):
"""Return the area of a circle."""
return 3.14159 * r * r # approximate pi
BANNER = '''Report # 1
Generated daily'''
Correct result:
def area(r):
"""Return the area of a circle."""
return 3.14159 * r * r
BANNER = '''Report # 1
Generated daily'''
Triple quoted text is a string, even as a docstring, so it must stay. Removing it empties BANNER, and a function whose body is only a docstring fails with IndentationError. The # inside '''Report # 1''' is part of the string too.
5. C# verbatim and raw strings (C-like)
var folder = @"C:\Exports\"; // output folder
var sql = """
SELECT * FROM Orders -- not a comment here
"""; // raw string literal
/* removed */
Export(folder, sql);
Correct result:
var folder = @"C:\Exports\";
var sql = """
SELECT * FROM Orders -- not a comment here
""";
Export(folder, sql);
In a verbatim string a backslash is a normal character, so \" at the end of @"C:\Exports\" really closes it. A tool that treats the backslash as an escape thinks the string is still open and skips the comments that follow.
6. SQL doubled quotes (SQL)
SELECT 'it''s -- fine' AS note, -- label
'/* not a comment */' AS literal
FROM orders /* main table */
WHERE status = 'open';
Correct result:
SELECT 'it''s -- fine' AS note,
'/* not a comment */' AS literal
FROM orders
WHERE status = 'open';
Two single quotes inside a SQL string stand for one quote. A tool that does not know this ends the string after it and removes -- fine' AS note,, which breaks the query.
7. A comment between two words (C-like)
int/*width*/x = 10;
return price/*before tax*/*rate;
Correct result:
int x = 10;
return price *rate;
Compilers treat a block comment like a space. Deleting it without leaving a space joins int and x into intx. On the second line the result must keep *rate.
8. Lines that held only a comment (C-like)
function save(user) {
// validate first
validate(user);
/*
* write to disk
*/
write(user); // done
}
Correct result:
function save(user) {
validate(user);
write(user);
}
A simple tool leaves indentation where each comment was and trailing spaces after write(user);. Those invisible leftovers then show up as changes in every diff.
9. The MATLAB transpose operator (MATLAB)
B = A'; % transpose
disp('Progress: 100% done') % message
%{
Old plotting code
plot(x, y)
%}
x = B';
Correct result:
B = A';
disp('Progress: 100% done')
x = B';
' after a name or closing bracket is the transpose operator, not a quote. A tool that always reads it as a quote gets the next line wrong and may cut 'Progress: 100% done' at the %.
10. Apostrophes in HTML text (HTML)
<p>We're open on Friday.</p>
<!-- TODO: add holiday hours -->
<p>Call us for bookings.</p>
Correct result:
<p>We're open on Friday.</p>
<p>Call us for bookings.</p>
A tool that tracks quotes in HTML text treats We're as an open string and keeps every comment after it. HTML comments do not care about quotes, so the HTML preset does not track them.
Known limits of Remove Comments
- Nested block comments (Rust, Swift, Kotlin, Haskell, PostgreSQL, SQL Server). The comment ends at the first closing marker, so
/* outer /* inner */ still a comment */leavesstill a comment */behind. - Heredocs and nowdocs in PHP, Bash and Ruby are not read as strings.
#inside a word in shell scripts. Bash keepsa#b, but the Python preset removes#b, and#topfromURL=http://example.com/#top.- MySQL
--without a space is not a comment in MySQL, but the SQL preset removes it. - Code inside HTML. The HTML preset removes HTML comments only. Run
<script>and<style>parts through the C-like or CSS preset. - Directives such as
// @ts-expect-errorare removed like any other comment.
To check a real file after cleanup, follow the safe cleanup workflow.