Files
freeCodeCamp/tools/challenge-parser/parser/plugins/add-hooks.js
Oliver Eyton-Williams 2a7b220a4f feat: support beforeEach and afterEach (#60921)
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2025-07-07 10:46:09 +00:00

41 lines
1.1 KiB
JavaScript

const { getSection } = require('./utils/get-section');
function plugin() {
return transformer;
function transformer(tree, file) {
const beforeAll = getHook(tree, '--before-all--');
const beforeEach = getHook(tree, '--before-each--');
const afterEach = getHook(tree, '--after-each--');
if (!beforeAll && !beforeEach && !afterEach) return;
file.data.hooks = file.data.hooks = {
...(beforeAll && { beforeAll }),
...(beforeEach && { beforeEach }),
...(afterEach && { afterEach })
};
}
}
function getHook(tree, sectionName) {
const section = getSection(tree, sectionName);
if (section.length === 0) return;
if (section.length > 1)
throw Error(
`# ${sectionName} section must only contain a single code block`
);
const codeNode = section[0];
if (codeNode.type !== 'code')
throw Error(`# ${sectionName} section must contain a code block`);
if (codeNode.lang !== 'javascript' && codeNode.lang !== 'js')
throw Error(`# ${sectionName} hook must be written in JavaScript`);
return codeNode.value;
}
module.exports = plugin;