Skip to content

Commit

Permalink
Fix parsing of /** **/ nested block comments
Browse files Browse the repository at this point in the history
Fixes #747
  • Loading branch information
nene committed Jun 20, 2024
1 parent 7aee250 commit 862f7d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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 862f7d3

Please sign in to comment.