This commit is contained in:
sagarishere 2024-09-18 16:41:41 +03:00 committed by GitHub
commit 0338dfad69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 8 deletions

View File

@ -149,17 +149,27 @@ ${tests.trim()};`.trim()
} }
const loadAndSanitizeSolution = async () => { const loadAndSanitizeSolution = async () => {
const path = `${solutionPath}/${name}.js` try {
const rawCode = await read(path, "student solution") const path = `${solutionPath}/${name}.js`
const rawCode = await read(path, "student solution")
const sanitizedCode = removeComments(rawCode)
// this is a very crude and basic removal of comments if (sanitizedCode.includes("import ")) { // space is important as it prevents "imported" or "importance" or other words containing "import"
// since checking code is only use to prevent cheating throw new Error("The use of the 'import' keyword is not allowed.")
// it's not that important if it doesn't work 100% of the time. }
const code = rawCode.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "").trim() return { code: sanitizedCode, rawCode, path }
if (code.includes("import")) fatal("import keyword not allowed") } catch (error) {
return { code, rawCode, path } console.error(error)
}
} }
const removeComments = (code) => {
// removes JS single line and multi-line comments only. Not for bash files etc.
// for use with multiple file-types, I suggest writing a removeComments function with language-type as input and then handling accordingly
return code.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "").trim()
}
const runTests = async ({ url, path, code }) => { const runTests = async ({ url, path, code }) => {
const { setup, tests } = await import(url).catch(err => const { setup, tests } = await import(url).catch(err =>
fatal(`Unable to execute ${name}, error:\n${stackFmt(err, url)}`), fatal(`Unable to execute ${name}, error:\n${stackFmt(err, url)}`),