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

This commit is contained in:
Lasse Jørgensen
2021-05-10 11:03:04 +02:00
committed by GitHub
parent b5cde4d01e
commit b16fadb436
4 changed files with 14 additions and 22 deletions

View File

@ -23802,6 +23802,11 @@
"resolved": "https://registry.npmjs.org/strip-bom-string/-/strip-bom-string-1.0.0.tgz", "resolved": "https://registry.npmjs.org/strip-bom-string/-/strip-bom-string-1.0.0.tgz",
"integrity": "sha1-5SEekiQ2n7uB1jOi8ABE3IztrZI=" "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": { "strip-eof": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz",

View File

@ -119,6 +119,7 @@
"sass.js": "0.11.1", "sass.js": "0.11.1",
"store": "2.0.12", "store": "2.0.12",
"stream-browserify": "3.0.0", "stream-browserify": "3.0.0",
"strip-comments": "^2.0.1",
"typescript": "4.2.4", "typescript": "4.2.4",
"validator": "13.6.0" "validator": "13.6.0"
}, },

View File

@ -12,10 +12,7 @@ nonMutatingPush(first, second);`;
const jsCodeWithSingleAndMultLineCommentsRemoved = ` const jsCodeWithSingleAndMultLineCommentsRemoved = `
function nonMutatingPush(original, newItem) { function nonMutatingPush(original, newItem) {
return original.push(newItem);
return original.push(newItem);
} }
var first = [1, 2, 3]; var first = [1, 2, 3];
@ -30,7 +27,7 @@ function nonMutatingPush(original, newItem) {
const jsCodeWithUrlUnchanged = ` const jsCodeWithUrlUnchanged = `
function nonMutatingPush(original, newItem) { function nonMutatingPush(original, newItem) {
var url = 'https://freecodecamp.org'; var url = 'https://freecodecamp.org';
return original.push(newItem); return original.push(newItem);
}`; }`;

View File

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