Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Fix code scanning alert #194700

Merged
merged 2 commits into from
Oct 3, 2024
Merged
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
23 changes: 21 additions & 2 deletions src/plugins/console/public/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,28 @@ export function formatRequestBodyDoc(data: string[], indent: boolean) {
};
}

// Regular expression to match different types of comments:
// - Block comments, single and multiline (/* ... */)
// - Single-line comments (// ...)
// - Hash comments (# ...)
export function hasComments(data: string) {
// matches single line and multiline comments
const re = /(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)|(#.*)/;
/*
1. (\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/)
- (\/\*): Matches the start of a block comment
- [^*]*: Matches any number of characters that are NOT an asterisk (*), to avoid prematurely closing the comment.
- \*+: Matches one or more asterisks (*), which is part of the block comment closing syntax.
- (?:[^/*][^*]*\*+)*: This non-capturing group ensures that any characters between asterisks and slashes are correctly matched and prevents mismatching on nested or unclosed comments.
- \*\/: Matches the closing of a block comment

2. (\/\/.*)
- Matches single-line comments starting with '//'.
- .*: Matches any characters that follow until the end of the line.

3. (#.*)
- Matches single-line comments starting with a hash (#).
- .*: Matches any characters that follow until the end of the line.
*/
const re = /(\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/)|(\/\/.*)|(#.*)/;
return re.test(data);
}

Expand Down