Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const charCode = {
dash: 45,
slash: 47,
asterisk: 42,
exclamation: 33,
plus: 43,
questionMark: 63,
newline: 10,
space: 32,
Expand Down Expand Up @@ -129,11 +131,22 @@ const skipSqlContext = (sql: string, position: number): number => {
}

if (currentChar === charCode.dash && nextChar === charCode.dash) {
const lineBreak = sql.indexOf('\n', position + 2);
return lineBreak === -1 ? sql.length : lineBreak + 1;
const afterDash = sql.charCodeAt(position + 2);

@wellwelwel wellwelwel Jun 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @spokodev! I believe we can remove 100% of the need for any comments in the entire implementation, since the logic and variable names are already clear for long-term maintenance 🙋🏻‍♂️


if (Number.isNaN(afterDash) || afterDash <= charCode.space) {
const lineBreak = sql.indexOf('\n', position + 2);
return lineBreak === -1 ? sql.length : lineBreak + 1;
}

return -1;
}

if (currentChar === charCode.slash && nextChar === charCode.asterisk) {
const markerChar = sql.charCodeAt(position + 2);

if (markerChar === charCode.exclamation || markerChar === charCode.plus)
return -1;

const commentEnd = sql.indexOf('*/', position + 2);
return commentEnd === -1 ? sql.length : commentEnd + 2;
}
Expand Down
15 changes: 15 additions & 0 deletions test/delimiters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ describe('Query comments for debugging', () => {
"SELECT * FROM users -- get all users\nWHERE status = 'active'"
);
});

test('double dash without trailing whitespace is not a comment', () => {
const sql = format('SELECT 1--2, ?', ['VAL']);
assert.equal(sql, "SELECT 1--2, 'VAL'");
});

test('executable comment placeholder is substituted, not skipped', () => {
const sql = format('SELECT /*!40101 ? */ , ?', ['A', 'B']);
assert.equal(sql, "SELECT /*!40101 'A' */ , 'B'");
});

test('optimizer hint placeholder is substituted, not skipped', () => {
const sql = format('SELECT /*+ MAX_EXECUTION_TIME(?) */ id FROM t', [1000]);
assert.equal(sql, 'SELECT /*+ MAX_EXECUTION_TIME(1000) */ id FROM t');
});
});

describe('Real edge cases', () => {
Expand Down
Loading