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:
Evgeny Klimenchenko
2021-04-28 16:18:54 +01:00
committed by GitHub
parent ebe8f99345
commit db369fbed1
47 changed files with 103 additions and 149 deletions

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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 }) {

View File

@ -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
};

View File

@ -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'

View File

@ -65,7 +65,7 @@ assert.deepEqual(copyMachine(['it works'], 3), [
`copyMachine` 函数中应对 `arr` 使用展开运算符(`spread operator`)。
```js
assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));
assert(code.match(/\.\.\.arr/));
```
# --seed--

View File

@ -3,6 +3,7 @@ id: bd7123c9c441eddfaeb4bdef
title: 给代码添加注释
challengeType: 1
videoUrl: 'https://scrimba.com/c/c7ynnTp'
removeComments: false
forumTopicId: 16783
dashedName: comment-your-javascript-code
---

View File

@ -45,7 +45,7 @@ assert.throws(declared, ReferenceError);
```js
assert(
/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
__helpers.removeWhiteSpace(code)
)
);

View File

@ -66,9 +66,7 @@ assert.equal(sum([2, 3, 4, 5], 3), 9);
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -76,7 +74,7 @@ assert(
```js
assert(
__helpers.removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1
sum.toString().match(/sum\(.*\)/g).length > 1
);
```

View File

@ -59,9 +59,7 @@ assert.deepStrictEqual(countdown(5), [5, 4, 3, 2, 1]);
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -69,7 +67,7 @@ assert(
```js
assert(
__helpers.removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)/)
countdown.toString().match(/countdown\s*\(.+\)/)
);
```

View File

@ -26,9 +26,7 @@ assert(Array.isArray(rangeOfNumbers(5, 10)));
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -36,9 +34,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(rangeOfNumbers.toString())
.match(/rangeOfNumbers\s*\(.+\)/)
rangeOfNumbers.toString().match(/rangeOfNumbers\s*\(.+\)/)
);
```

View File

@ -32,9 +32,7 @@ dashedName: understanding-the-differences-between-the-freecodecamp-and-browser-c
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.match(/console.clear\(\)/)
__helpers.removeWhiteSpace(code).match(/console.clear\(\)/)
);
```

View File

@ -32,9 +32,7 @@ const myPromise = new Promise((resolve, reject) => {
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g
)
);
@ -44,9 +42,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g
)
);

View File

@ -43,9 +43,7 @@ const { name, age } = user;
```js
assert(
!__helpers
.removeJSComments(code)
.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
!code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
);
```
@ -53,9 +51,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
@ -65,9 +61,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);

View File

@ -39,7 +39,7 @@ const person = {
不应使用传统的函数定义方法。
```js
(getUserInput) => assert(!__helpers.removeJSComments(code).match(/function/));
(getUserInput) => assert(!code.match(/function/));
```
`setGear` 应是一个声明函数。

View File

@ -28,7 +28,7 @@ assert.typeOf(squareList, 'function'),
不应该使用 `for``while` 或者 `forEach`
```js
assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
assert(!code.match(/for|while|forEach/g));
```
应该使用 `map``filter` 或者 `reduce`
@ -36,7 +36,7 @@ assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.removeWhiteSpace(code)
.match(/\.(map|filter|reduce)\(/g)
);
```

View File

@ -52,7 +52,7 @@ assert(
不能使用 `for` 循环。
```js
assert(!__helpers.removeJSComments(code).match(/for\s*?\([\s\S]*?\)/));
assert(!code.match(/for\s*?\([\s\S]*?\)/));
```
你的代码应使用 `map` 方法。

View File

@ -49,9 +49,7 @@ You should not copy and return the array.
```js
assert(
!__helpers
.removeJSComments(code)
.match(
!code.match(
/(1430952867)|(1460357289)|(1406357289)|(4130952867)|(4160357289)|(4106357289)/
)
);

View File

@ -65,7 +65,7 @@ assert.deepEqual(copyMachine(['it works'], 3), [
The `copyMachine` function should utilize the `spread operator` with array `arr`
```js
assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));
assert(code.match(/\.\.\.arr/));
```
# --seed--

View File

@ -2,6 +2,7 @@
id: bd7123c9c441eddfaeb4bdef
title: Comment Your JavaScript Code
challengeType: 1
removeComments: false
videoUrl: 'https://scrimba.com/c/c7ynnTp'
forumTopicId: 16783
dashedName: comment-your-javascript-code

View File

@ -45,7 +45,7 @@ You should add a local `myVar` variable.
```js
assert(
/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
__helpers.removeWhiteSpace(code)
)
);

View File

@ -66,9 +66,7 @@ Your code should not rely on any kind of loops (`for` or `while` or higher order
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -76,7 +74,7 @@ You should use recursion to solve this problem.
```js
assert(
__helpers.removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1
sum.toString().match(/sum\(.*\)/g).length > 1
);
```

View File

@ -59,9 +59,7 @@ Your code should not rely on any kind of loops (`for`, `while` or higher order f
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -69,7 +67,7 @@ You should use recursion to solve this problem.
```js
assert(
__helpers.removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)/)
countdown.toString().match(/countdown\s*\(.+\)/)
);
```

View File

@ -26,9 +26,7 @@ Your code should not use any loop syntax (`for` or `while` or higher order funct
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -36,9 +34,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(rangeOfNumbers.toString())
.match(/rangeOfNumbers\s*\(.+\)/)
rangeOfNumbers.toString().match(/rangeOfNumbers\s*\(.+\)/)
);
```

View File

@ -33,7 +33,7 @@ You should use `console.clear()` to clear the browser console.
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.removeWhiteSpace(code)
.match(/console.clear\(\)/)
);
```

View File

@ -32,11 +32,7 @@ Make the promise handle success and failure. If `responseFromServer` is `true`,
```js
assert(
__helpers
.removeJSComments(code)
.match(
/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g
)
code.match(/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g)
);
```
@ -44,11 +40,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(code)
.match(
/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g
)
code.match(/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g)
);
```

View File

@ -43,9 +43,7 @@ You should remove the ES5 assignment syntax.
```js
assert(
!__helpers
.removeJSComments(code)
.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
!code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
);
```
@ -53,11 +51,7 @@ You should use destructuring to create the `today` variable.
```js
assert(
__helpers
.removeJSComments(code)
.match(
/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
code.match(/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
);
```
@ -65,11 +59,7 @@ You should use destructuring to create the `tomorrow` variable.
```js
assert(
__helpers
.removeJSComments(code)
.match(
/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
code.match(/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g)
);
```

View File

@ -39,7 +39,7 @@ Refactor the function `setGear` inside the object `bicycle` to use the shorthand
Traditional function expression should not be used.
```js
(getUserInput) => assert(!__helpers.removeJSComments(code).match(/function/));
(getUserInput) => assert(!code.match(/function/));
```
`setGear` should be a declarative function.

View File

@ -28,7 +28,7 @@ assert.typeOf(squareList, 'function'),
`for`, `while`, and `forEach` should not be used.
```js
assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
assert(!code.match(/for|while|forEach/g));
```
`map`, `filter`, or `reduce` should be used.
@ -36,7 +36,7 @@ assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.removeWhiteSpace(code)
.match(/\.(map|filter|reduce)\(/g)
);
```

View File

@ -52,7 +52,7 @@ assert(
Your code should not use a `for` loop.
```js
assert(!__helpers.removeJSComments(code).match(/for\s*?\([\s\S]*?\)/));
assert(!code.match(/for\s*?\([\s\S]*?\)/));
```
Your code should use the `map` method.

View File

@ -49,11 +49,7 @@ You should not copy and return the array.
```js
assert(
!__helpers
.removeJSComments(code)
.match(
/(1430952867)|(1460357289)|(1406357289)|(4130952867)|(4160357289)|(4106357289)/
)
!code.match(/(1430952867)|(1460357289)|(1406357289)|(4130952867)|(4160357289)|(4106357289)/)
);
```

View File

@ -65,7 +65,7 @@ assert.deepEqual(copyMachine(['it works'], 3), [
La función `copyMachine` debe utilizar el `spread operator` (operador de propagación) con el arreglo `arr`
```js
assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));
assert(code.match(/\.\.\.arr/));
```
# --seed--

View File

@ -3,6 +3,7 @@ id: bd7123c9c441eddfaeb4bdef
title: Comenta tu código de JavaScript
challengeType: 1
videoUrl: 'https://scrimba.com/c/c7ynnTp'
removeComments: false
forumTopicId: 16783
dashedName: comment-your-javascript-code
---

View File

@ -45,7 +45,7 @@ Debes agregar una variable local `myVar`.
```js
assert(
/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
__helpers.removeWhiteSpace(code)
)
);

View File

@ -66,9 +66,7 @@ Tu código no debe depender de ningún tipo de bluces (`for` o `while`) o funcio
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -76,7 +74,7 @@ Debes usar recursión para resolver este problema.
```js
assert(
__helpers.removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1
sum.toString().match(/sum\(.*\)/g).length > 1
);
```

View File

@ -59,9 +59,7 @@ Tu código no debe depender de ningún tipo de bucles (`for`, `while` o funcione
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -69,7 +67,7 @@ Debes usar recursión para resolver este problema.
```js
assert(
__helpers.removeJSComments(countdown.toString()).match(/countdown\s*\(.+\)/)
countdown.toString().match(/countdown\s*\(.+\)/)
);
```

View File

@ -26,9 +26,7 @@ Tu código no debe utilizar bucles (`for`, `while` o funciones de orden superior
```js
assert(
!__helpers
.removeJSComments(code)
.match(/for|while|forEach|map|filter|reduce/g)
!code.match(/for|while|forEach|map|filter|reduce/g)
);
```
@ -36,9 +34,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(rangeOfNumbers.toString())
.match(/rangeOfNumbers\s*\(.+\)/)
rangeOfNumbers.toString().match(/rangeOfNumbers\s*\(.+\)/)
);
```

View File

@ -33,7 +33,7 @@ Debes utilizar `console.clear()` para limpiar la consola del navegador.
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.removeWhiteSpace(code)
.match(/console.clear\(\)/)
);
```

View File

@ -32,9 +32,7 @@ Haz una función promesa que maneje el éxito y el fallo. Si `responseFromServer
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/if\s*\(\s*responseFromServer\s*\)\s*{\s*resolve\s*\(\s*('|"|`)We got the data\1\s*\)(\s*|\s*;\s*)}/g
)
);
@ -44,9 +42,7 @@ assert(
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/}\s*else\s*{\s*reject\s*\(\s*('|"|`)Data not received\1\s*\)(\s*|\s*;\s*)}/g
)
);

View File

@ -43,9 +43,7 @@ Debes eliminar la sintaxis de asignación ES5.
```js
assert(
!__helpers
.removeJSComments(code)
.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
!code.match(/today\s*=\s*HIGH_TEMPERATURES\.(today|tomorrow)/g)
);
```
@ -53,9 +51,7 @@ Debes usar desestructuración para crear la variable `today`.
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/(var|let|const)\s*{\s*(today[^}]*|[^,]*,\s*today)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);
@ -65,9 +61,7 @@ Debes usar desestructuración para crear la variable `tomorrow`.
```js
assert(
__helpers
.removeJSComments(code)
.match(
code.match(
/(var|let|const)\s*{\s*(tomorrow[^}]*|[^,]*,\s*tomorrow)\s*}\s*=\s*HIGH_TEMPERATURES(;|\s+|\/\/)/g
)
);

View File

@ -39,7 +39,7 @@ Refactoriza la función `setGear` dentro del objeto `bicycle` para que utilice l
La expresión tradicional "function" no debe ser utilizada.
```js
(getUserInput) => assert(!__helpers.removeJSComments(code).match(/function/));
(getUserInput) => assert(!code.match(/function/));
```
`setGear` debe ser una función declarativa.

View File

@ -28,7 +28,7 @@ assert.typeOf(squareList, 'function'),
`for`, `while`, y `forEach` no deben ser usados.
```js
assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
assert(!code.match(/for|while|forEach/g));
```
`map`, `filter`, o `reduce` deben ser usados.
@ -36,7 +36,7 @@ assert(!__helpers.removeJSComments(code).match(/for|while|forEach/g));
```js
assert(
__helpers
.removeWhiteSpace(__helpers.removeJSComments(code))
.removeWhiteSpace(code)
.match(/\.(map|filter|reduce)\(/g)
);
```

View File

@ -52,7 +52,7 @@ assert(
Tu código no debe usar un bucle `for`.
```js
assert(!__helpers.removeJSComments(code).match(/for\s*?\([\s\S]*?\)/));
assert(!code.match(/for\s*?\([\s\S]*?\)/));
```
Tu código debe usar el método `map`.

View File

@ -49,9 +49,7 @@ You should not copy and return the array.
```js
assert(
!__helpers
.removeJSComments(code)
.match(
!code.match(
/(1430952867)|(1460357289)|(1406357289)|(4130952867)|(4160357289)|(4106357289)/
)
);

View File

@ -25,6 +25,7 @@ const schema = Joi.object()
block: Joi.string().regex(slugRE),
blockId: Joi.objectId(),
challengeOrder: Joi.number(),
removeComments: Joi.bool(),
challengeType: Joi.number().min(0).max(11).required(),
checksum: Joi.number(),
// __commentCounts is only used to test the comment replacement

View File

@ -520,11 +520,17 @@ async function createTestRunner(
files[key].editableContents = solution[key].editableContents;
});
const { build, sources, loadEnzyme } = await buildChallenge({
const { build, sources, loadEnzyme } = await buildChallenge(
{
files,
required,
template
});
},
{
removeComments: challenge.removeComments
}
);
const code = {
contents: sources.index,
editableContents: sources.editableContents