Skip to content

Commit

Permalink
[8.x] [Console] Fix code scanning alert (#194700) (#194836)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[Console] Fix code scanning alert
(#194700)](#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":"[email protected]"},"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 <[email protected]>
  • Loading branch information
kibanamachine and sabarasaba authored Oct 3, 2024
1 parent 5ad385c commit 0e793ba
Showing 1 changed file with 21 additions and 2 deletions.
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

0 comments on commit 0e793ba

Please sign in to comment.