fix(client): remove JS comments from user code for tests (#41873)
* Removes comments in js challanges by default * fix local-scope-and-functions test regex * fix all languages * revert language changes * removed unnecessary removeJSComments from challenges * fix challanges in other languages * removed removeJSComments from all challanges
This commit is contained in:
committed by
GitHub
parent
ebe8f99345
commit
db369fbed1
@@ -38,6 +38,7 @@ export const ChallengeNode = PropTypes.shape({
|
||||
helpCategory: PropTypes.string,
|
||||
instructions: PropTypes.string,
|
||||
isComingSoon: PropTypes.bool,
|
||||
removeComments: PropTypes.bool,
|
||||
isLocked: PropTypes.bool,
|
||||
isPrivate: PropTypes.bool,
|
||||
order: PropTypes.number,
|
||||
|
@@ -160,6 +160,7 @@ class ShowClassic extends Component {
|
||||
files,
|
||||
fields: { tests },
|
||||
challengeType,
|
||||
removeComments,
|
||||
helpCategory
|
||||
}
|
||||
},
|
||||
@@ -171,6 +172,7 @@ class ShowClassic extends Component {
|
||||
updateChallengeMeta({
|
||||
...challengeMeta,
|
||||
title,
|
||||
removeComments,
|
||||
challengeType,
|
||||
helpCategory
|
||||
});
|
||||
@@ -365,6 +367,7 @@ export const query = graphql`
|
||||
title
|
||||
description
|
||||
instructions
|
||||
removeComments
|
||||
challengeType
|
||||
helpCategory
|
||||
videoUrl
|
||||
|
@@ -87,6 +87,7 @@ export function* executeChallengeSaga({
|
||||
const protect = isLoopProtected(challengeMeta);
|
||||
const buildData = yield buildChallengeData(challengeData, {
|
||||
preview: false,
|
||||
removeComments: challengeMeta.removeComments,
|
||||
protect
|
||||
});
|
||||
const document = yield getContext('document');
|
||||
@@ -201,6 +202,7 @@ function* previewChallengeSaga({ flushLogs = true } = {}) {
|
||||
const protect = isLoopProtected(challengeMeta);
|
||||
const buildData = yield buildChallengeData(challengeData, {
|
||||
preview: true,
|
||||
removeComments: challengeMeta.removeComments,
|
||||
protect
|
||||
});
|
||||
// evaluate the user code in the preview frame or in the worker
|
||||
|
@@ -13,6 +13,7 @@ import {
|
||||
import frameRunnerData from '../../../../../config/client/frame-runner.json';
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import testEvaluatorData from '../../../../../config/client/test-evaluator.json';
|
||||
import { removeJSComments } from '../../../utils/curriculum-helpers';
|
||||
|
||||
const { filename: runner } = frameRunnerData;
|
||||
const { filename: testEvaluator } = testEvaluatorData;
|
||||
@@ -164,16 +165,27 @@ export function buildJSChallenge({ files }, options) {
|
||||
.map(pipeLine);
|
||||
return Promise.all(finalFiles)
|
||||
.then(checkFilesErrors)
|
||||
.then(files => ({
|
||||
challengeType: challengeTypes.js,
|
||||
build: files
|
||||
.then(files => {
|
||||
let build = files
|
||||
.reduce(
|
||||
(body, file) => [...body, file.head, file.contents, file.tail],
|
||||
[]
|
||||
)
|
||||
.join('\n'),
|
||||
sources: buildSourceMap(files)
|
||||
}));
|
||||
.join('\n');
|
||||
let sources = buildSourceMap(files);
|
||||
if (options?.removeComments !== false) {
|
||||
build = removeJSComments(build);
|
||||
sources = {
|
||||
...sources,
|
||||
index: removeJSComments(sources.index)
|
||||
};
|
||||
}
|
||||
return {
|
||||
challengeType: challengeTypes.js,
|
||||
build,
|
||||
sources
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function buildBackendChallenge({ url }) {
|
||||
|
@@ -5,7 +5,7 @@ const removeHtmlComments = str => str.replace(/<!--[\s\S]*?(-->|$)/g, '');
|
||||
|
||||
const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, '');
|
||||
|
||||
const removeJSComments = codeStr => {
|
||||
export const removeJSComments = codeStr => {
|
||||
// Note: removes trailing new lines and tailing spaces at end of lines
|
||||
const options = {
|
||||
comments: false,
|
||||
@@ -30,7 +30,6 @@ const removeWhiteSpace = (str = '') => {
|
||||
const curriculumHelpers = {
|
||||
removeHtmlComments,
|
||||
removeCssComments,
|
||||
removeJSComments,
|
||||
removeWhiteSpace
|
||||
};
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/* global describe it expect */
|
||||
|
||||
import __testHelpers from './curriculum-helpers';
|
||||
import __testHelpers, { removeJSComments } from './curriculum-helpers';
|
||||
import jsTestValues from './__fixtures/curriculum-helpers-javascript';
|
||||
import cssTestValues from './__fixtures/curriculum-helpers-css';
|
||||
import htmlTestValues from './__fixtures/curriculum-helpers-html';
|
||||
@@ -40,7 +40,6 @@ describe('removeWhiteSpace', () => {
|
||||
});
|
||||
|
||||
describe('removeJSComments', () => {
|
||||
const { removeJSComments } = __testHelpers;
|
||||
it('returns a string', () => {
|
||||
expect(typeof removeJSComments('const should = "return a string"')).toBe(
|
||||
'string'
|
||||
|
Reference in New Issue
Block a user