Skip to content

Commit

Permalink
Fix parsing of /** **/ nested block comments (#751)
Browse files Browse the repository at this point in the history
Don't try to be too clever with matching lots of characters with that
funky MIDDLE regex. Just match a single char.

Fixes #747
  • Loading branch information
nene authored Jun 20, 2024
2 parents 7aee250 + 6308190 commit b0e07e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ Toliver <teejae@gmail.com>
Toni Müller <toni.mueller@datameer.com>
Tyler Jones <tyler.jones@txwormhole.com>
Uku Pattak <ukupat@gmail.com>
Wylie Conlon <wylie@superblockshq.com>
Xin Hu <hoosin.git@gmail.com>
Zhongxian Liang <zhongxian.liang@shopee.com>
4 changes: 2 additions & 2 deletions src/lexer/NestedComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { RegExpLike } from './TokenizerEngine.js';

const START = /\/\*/uy; // matches: /*
const MIDDLE = /([^/*]|\*[^/]|\/[^*])+/uy; // matches text NOT containing /* or */
const ANY_CHAR = /[\s\S]/uy; // matches single character
const END = /\*\//uy; // matches: */

/**
Expand Down Expand Up @@ -31,7 +31,7 @@ export class NestedComment implements RegExpLike {
} else if ((match = this.matchSection(END, input))) {
result += match;
nestLevel--;
} else if ((match = this.matchSection(MIDDLE, input))) {
} else if ((match = this.matchSection(ANY_CHAR, input))) {
result += match;
} else {
return null;
Expand Down
6 changes: 6 additions & 0 deletions test/features/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ export default function supportsComments(format: FormatFn, opts: CommentsConfig
`);
});

// Regression test for #747
it('handles block comments with /** and **/ patterns', () => {
const sql = `/** This is a block comment **/`;
expect(format(sql)).toBe(sql);
});

if (opts.hashComments) {
it('supports # line comment', () => {
const result = format('SELECT alpha # commment\nFROM beta');
Expand Down

0 comments on commit b0e07e0

Please sign in to comment.