From ce69d1e35ff7e92881e24f9bdfc9a0ee50fc9807 Mon Sep 17 00:00:00 2001 From: fajar-apri-alaska Date: Tue, 30 Jan 2024 15:49:39 +0700 Subject: [PATCH] fix: update inefficient RegEx #357 - Update below regex to make it more efficient: \r\n|\r|\n -> \r?\n|\r(?!\n) 1. The \r\n is redundant to \r checking in current pattern, and I think we can combine it with the \n checking by using \r?\n 2. At first, I want to also remove the standalone \r because it only for older Mac OS, but I think we should keep it for backward compatibility, so adding | \r to the pattern. 3. Additional (?!\n) in the end is to make sure the \r is not followed by \n, to avoid exponential backtracking on strings starting with ''' and containing many repetitions of '\r\n'. (per latest [CodeQL analysis alert](https://github.com/AlaskaAirlines/WC-Generator/security/code-scanning/3)) Changes to be committed: modified: package-lock.json modified: template/scripts/generateDocs.mjs --- package-lock.json | 4 ++-- template/scripts/generateDocs.mjs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 32c6a71..badc853 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aurodesignsystem/wc-generator", - "version": "4.3.1", + "version": "4.4.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@aurodesignsystem/wc-generator", - "version": "4.3.1", + "version": "4.4.9", "license": "Apache-2.0", "dependencies": { "arg": "^5.0.2", diff --git a/template/scripts/generateDocs.mjs b/template/scripts/generateDocs.mjs index dab70bb..349ef7d 100755 --- a/template/scripts/generateDocs.mjs +++ b/template/scripts/generateDocs.mjs @@ -61,7 +61,7 @@ function formatTemplateFileContents(content, destination) { result = result.replace(/>(\r\n|\r|\n){2,}/g, '>\r\n'); // Remove empty lines directly after a closing html tag. result = result.replace(/>(\r\n|\r|\n)```/g, '>\r\n\r\n```'); // Ensure an empty line before code samples. result = result.replace(/>(\r\n|\r|\n){2,}```(\r\n|\r|\n)/g, '>\r\n```\r\n'); // Ensure no empty lines before close of code sample. - result = result.replace(/([^(\r\n|\r|\n)])(\r\n|\r|\n)+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections. + result = result.replace(/([^(\r\n|\r|\n)])(\r?\n|\r(?!\n))+#/g, "$1\r\n\r\n#"); // Ensure empty line before header sections. /** * Write the result to the destination file