From b16fadb4361ea8b753c9c6213ce47d92d943dd49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20J=C3=B8rgensen?= <28780271+lasjorg@users.noreply.github.com> Date: Mon, 10 May 2021 11:03:04 +0200 Subject: [PATCH] fix(client): use strip-comments library for js comments removal (#41960) --- client/package-lock.json | 5 ++++ client/package.json | 1 + .../curriculum-helpers-javascript.js | 7 ++---- client/src/utils/curriculum-helpers.js | 23 +++++-------------- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 1c42374dbf..5a28cac151 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -23802,6 +23802,11 @@ "resolved": "https://registry.npmjs.org/strip-bom-string/-/strip-bom-string-1.0.0.tgz", "integrity": "sha1-5SEekiQ2n7uB1jOi8ABE3IztrZI=" }, + "strip-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-comments/-/strip-comments-2.0.1.tgz", + "integrity": "sha512-ZprKx+bBLXv067WTCALv8SSz5l2+XhpYCsVtSqlMnkAXMWDq+/ekVbl1ghqP9rUHTzv6sm/DwCOiYutU/yp1fw==" + }, "strip-eof": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", diff --git a/client/package.json b/client/package.json index a76871ee24..570e86aeef 100644 --- a/client/package.json +++ b/client/package.json @@ -119,6 +119,7 @@ "sass.js": "0.11.1", "store": "2.0.12", "stream-browserify": "3.0.0", + "strip-comments": "^2.0.1", "typescript": "4.2.4", "validator": "13.6.0" }, diff --git a/client/src/utils/__fixtures/curriculum-helpers-javascript.js b/client/src/utils/__fixtures/curriculum-helpers-javascript.js index 399f5d8461..0086ae638e 100644 --- a/client/src/utils/__fixtures/curriculum-helpers-javascript.js +++ b/client/src/utils/__fixtures/curriculum-helpers-javascript.js @@ -12,10 +12,7 @@ nonMutatingPush(first, second);`; const jsCodeWithSingleAndMultLineCommentsRemoved = ` function nonMutatingPush(original, newItem) { - - - - return original.push(newItem); + return original.push(newItem); } var first = [1, 2, 3]; @@ -30,7 +27,7 @@ function nonMutatingPush(original, newItem) { const jsCodeWithUrlUnchanged = ` function nonMutatingPush(original, newItem) { - var url = 'https://freecodecamp.org'; + var url = 'https://freecodecamp.org'; return original.push(newItem); }`; diff --git a/client/src/utils/curriculum-helpers.js b/client/src/utils/curriculum-helpers.js index 18d7015291..bc02f375f4 100644 --- a/client/src/utils/curriculum-helpers.js +++ b/client/src/utils/curriculum-helpers.js @@ -1,28 +1,17 @@ -import { parse } from '@babel/parser'; -import generate from '@babel/generator'; - -const removeHtmlComments = str => str.replace(/|$)/g, ''); - -const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, ''); +import strip from 'strip-comments'; export const removeJSComments = codeStr => { - // Note: removes trailing new lines and tailing spaces at end of lines - const options = { - comments: false, - retainLines: true, - compact: false, - concise: false, - minified: false - }; try { - const ast = parse(codeStr); - const { code } = generate(ast, options, codeStr); - return code; + return strip(codeStr); } catch (err) { return codeStr; } }; +const removeHtmlComments = str => str.replace(/|$)/g, ''); + +const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, ''); + const removeWhiteSpace = (str = '') => { return str.replace(/\s/g, ''); };