-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#4137): Cleanup comments before parsing
- Loading branch information
1 parent
e4a2c74
commit 48d267c
Showing
9 changed files
with
96 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// tests to check that comments are removed | ||
|
||
import { cleanupComments } from './comments'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
describe('comments', () => { | ||
it('should remove comments', () => { | ||
const text = ` | ||
%% This is a comment | ||
%% This is another comment | ||
graph TD | ||
A-->B | ||
%% This is a comment | ||
`; | ||
expect(cleanupComments(text)).toMatchInlineSnapshot(` | ||
" | ||
graph TD | ||
A-->B | ||
" | ||
`); | ||
}); | ||
|
||
it('should keep init statements when removing comments', () => { | ||
const text = ` | ||
%% This is a comment | ||
%% This is another comment | ||
%%{init: {'theme': 'forest'}}%% | ||
%%{init: {'theme': 'space after ending'}}%% | ||
graph TD | ||
A-->B | ||
B-->C | ||
%% This is a comment | ||
`; | ||
expect(cleanupComments(text)).toMatchInlineSnapshot(` | ||
" | ||
%%{init: {'theme': 'forest'}}%% | ||
%%{init: {'theme': 'space after ending'}}%% | ||
graph TD | ||
A-->B | ||
B-->C | ||
" | ||
`); | ||
}); | ||
|
||
it('should remove indented comments', () => { | ||
const text = ` | ||
%% This is a comment | ||
graph TD | ||
A-->B | ||
%% This is a comment | ||
C-->D | ||
`; | ||
expect(cleanupComments(text)).toMatchInlineSnapshot(` | ||
" | ||
graph TD | ||
A-->B | ||
C-->D | ||
" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Remove all lines starting with `%%` from the text that don't contain a `%%{` | ||
* @param text - The text to remove comments from | ||
* @returns cleaned text | ||
*/ | ||
export const cleanupComments = (text: string): string => { | ||
return text.replace(/^\s*%%(?!{)[^\n]+/gm, ''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters