Revert "fix(client): use strip-comments library for js comments removal (#41960)" (#42103)

This reverts commit b16fadb436.
This commit is contained in:
Oliver Eyton-Williams
2021-05-11 19:12:48 +02:00
committed by GitHub
parent 6b0cd89df4
commit 854fee4779
4 changed files with 25 additions and 17 deletions

View File

@ -23673,11 +23673,6 @@
"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",

View File

@ -119,7 +119,6 @@
"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"
},

View File

@ -12,7 +12,10 @@ nonMutatingPush(first, second);`;
const jsCodeWithSingleAndMultLineCommentsRemoved = `
function nonMutatingPush(original, newItem) {
return original.push(newItem);
return original.push(newItem);
}
var first = [1, 2, 3];
@ -27,7 +30,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);
}`;

View File

@ -1,17 +1,28 @@
import strip from 'strip-comments';
export const removeJSComments = codeStr => {
try {
return strip(codeStr);
} catch (err) {
return codeStr;
}
};
import { parse } from '@babel/parser';
import generate from '@babel/generator';
const removeHtmlComments = str => str.replace(/<!--[\s\S]*?(-->|$)/g, '');
const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, '');
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;
} catch (err) {
return codeStr;
}
};
const removeWhiteSpace = (str = '') => {
return str.replace(/\s/g, '');
};