[8.x] [Console] Fix code scanning alert (#194700) (#194836)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Console] Fix code scanning alert
(#194700)](https://github.com/elastic/kibana/pull/194700)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ignacio
Rivas","email":"rivasign@gmail.com"},"sourceCommit":{"committedDate":"2024-10-03T13:21:52Z","message":"[Console]
Fix code scanning alert
(#194700)","sha":"3dbb7da016a42f846b04d0f88a9fba746238558d","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor"],"title":"[Console]
Fix code scanning alert
","number":194700,"url":"https://github.com/elastic/kibana/pull/194700","mergeCommit":{"message":"[Console]
Fix code scanning alert
(#194700)","sha":"3dbb7da016a42f846b04d0f88a9fba746238558d"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194700","number":194700,"mergeCommit":{"message":"[Console]
Fix code scanning alert
(#194700)","sha":"3dbb7da016a42f846b04d0f88a9fba746238558d"}}]}]
BACKPORT-->

Co-authored-by: Ignacio Rivas <rivasign@gmail.com>
This commit is contained in:
Kibana Machine 2024-10-04 01:03:23 +10:00 committed by GitHub
parent 5ad385c8ab
commit 0e793ba60b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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);
}