fix(client): use @freecodecamp/strip-comments for js comments (#42254)

This commit is contained in:
Oliver Eyton-Williams
2021-06-01 01:37:45 +02:00
committed by GitHub
parent a7556a82d5
commit 3b115a1876
5 changed files with 39 additions and 20 deletions

View File

@ -1569,6 +1569,11 @@
} }
} }
}, },
"@freecodecamp/strip-comments": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@freecodecamp/strip-comments/-/strip-comments-3.0.0.tgz",
"integrity": "sha512-Bc7IYRrYa45NBw2f6yhJpuPXFBRqLQ7zpQOdGKlAcq8YyRsUsN4K3XNwfLAYYYxyksBH8pEqg24t5py8VpwiGA=="
},
"@gatsbyjs/reach-router": { "@gatsbyjs/reach-router": {
"version": "1.3.6", "version": "1.3.6",
"resolved": "https://registry.npmjs.org/@gatsbyjs/reach-router/-/reach-router-1.3.6.tgz", "resolved": "https://registry.npmjs.org/@gatsbyjs/reach-router/-/reach-router-1.3.6.tgz",
@ -3858,6 +3863,15 @@
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
"integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==" "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ=="
}, },
"bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"optional": true,
"requires": {
"file-uri-to-path": "1.0.0"
}
},
"bl": { "bl": {
"version": "4.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
@ -7666,6 +7680,12 @@
"typedarray-to-buffer": "^3.1.5" "typedarray-to-buffer": "^3.1.5"
} }
}, },
"file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"optional": true
},
"filesize": { "filesize": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/filesize/-/filesize-6.1.0.tgz", "resolved": "https://registry.npmjs.org/filesize/-/filesize-6.1.0.tgz",
@ -12660,6 +12680,12 @@
"resolved": "https://registry.npmjs.org/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz", "resolved": "https://registry.npmjs.org/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz",
"integrity": "sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w=" "integrity": "sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w="
}, },
"nan": {
"version": "2.14.2",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz",
"integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==",
"optional": true
},
"nanoid": { "nanoid": {
"version": "3.1.23", "version": "3.1.23",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz",
@ -18818,7 +18844,11 @@
"version": "1.2.13", "version": "1.2.13",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz",
"integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==",
"optional": true "optional": true,
"requires": {
"bindings": "^1.5.0",
"nan": "^2.12.1"
}
}, },
"glob-parent": { "glob-parent": {
"version": "3.1.0", "version": "3.1.0",

View File

@ -48,6 +48,7 @@
"@freecodecamp/loop-protect": "2.2.1", "@freecodecamp/loop-protect": "2.2.1",
"@freecodecamp/react-bootstrap": "0.32.3", "@freecodecamp/react-bootstrap": "0.32.3",
"@freecodecamp/react-calendar-heatmap": "1.0.0", "@freecodecamp/react-calendar-heatmap": "1.0.0",
"@freecodecamp/strip-comments": "3.0.0",
"@loadable/component": "5.15.0", "@loadable/component": "5.15.0",
"@reach/router": "1.3.4", "@reach/router": "1.3.4",
"algoliasearch": "4.9.1", "algoliasearch": "4.9.1",

View File

@ -1,2 +1,3 @@
// eslint-disable-next-line import/unambiguous // eslint-disable-next-line import/unambiguous
declare module '@freecodecamp/react-bootstrap'; declare module '@freecodecamp/react-bootstrap';
declare module '@freecodecamp/strip-comments';

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,6 +1,5 @@
import { parse } from '@babel/parser';
import generate from '@babel/generator';
import CSSHelp from './css-help'; import CSSHelp from './css-help';
import strip from '@freecodecamp/strip-comments';
const removeHtmlComments = (str: string): string => const removeHtmlComments = (str: string): string =>
str.replace(/<!--[\s\S]*?(-->|$)/g, ''); str.replace(/<!--[\s\S]*?(-->|$)/g, '');
@ -9,19 +8,10 @@ const removeCssComments = (str: string): string =>
str.replace(/\/\*[\s\S]+?\*\//g, ''); str.replace(/\/\*[\s\S]+?\*\//g, '');
export const removeJSComments = (codeStr: string): string => { export const removeJSComments = (codeStr: string): string => {
// Note: removes trailing new lines and tailing spaces at end of lines // TODO: publish type declarations and reenable eslint
const options = {
comments: false,
retainLines: true,
compact: false,
concise: false,
minified: false
};
try { try {
const ast = parse(codeStr); // eslint-disable-next-line @typescript-eslint/no-unsafe-call
// TODO: Sort out type error on ast return strip(codeStr) as string;
const { code } = generate(ast, options, codeStr);
return code;
} catch (err) { } catch (err) {
return codeStr; return codeStr;
} }