Files
freeCodeCamp/tools/challenge-parser/parser/plugins/add-seed.js
Oliver Eyton-Williams e118dda13a fix: order imports and remove circular dependencies (#41824)
* fix: remove circular dependency

redux depended on templates/Challenges/redux and vice versa.  This
meant that import order mattered and confusing bugs could arise.

(cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc)

* feat: require imports to be in alphabetical order

Import order generally does not matter, but there are edge cases
(circular  imports and css imports, for example) where changing order
changes behaviour

(cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c)

* chore: order imports

* fix: lift up challenge description + title comps

This brings the classic Show closer to the others as they
now all create the description and title components

* fix: remove donation-saga/index circular import

(cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207)

* refactor: extract action-types from settings

(cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd)

* fix: lint errors

* feat: prevent useless renames
2021-08-02 08:39:40 -05:00

110 lines
3.6 KiB
JavaScript

const { isEmpty } = require('lodash');
const { root } = require('mdast-builder');
const visitChildren = require('unist-util-visit-children');
const getAllBetween = require('./utils/between-headings');
// const visit = require('unist-util-visit');
const { getFileVisitor } = require('./utils/get-file-visitor');
const editableRegionMarker = '--fcc-editable-region--';
function findRegionMarkers(file) {
const lines = file.contents.split('\n');
const editableLines = lines
.map((line, id) => (line.trim() === editableRegionMarker ? id : -1))
.filter(id => id >= 0);
if (editableLines.length > 2) {
throw Error('Editable region has too many markers: ' + editableLines);
}
if (editableLines.length === 0) {
return null;
} else if (editableLines.length === 1) {
throw Error(`Editable region not closed`);
} else {
return editableLines;
}
}
function removeLines(contents, toRemove) {
const lines = contents.split('\n');
return lines.filter((_, id) => !toRemove.includes(id)).join('\n');
}
// TODO: DRY this. Start with an array of markers and go from there.
function addSeeds() {
function transformer(tree, file) {
const seedTree = root(getAllBetween(tree, `--seed--`));
// Not all challenges have seeds (video challenges, for example), so we stop
// processing in these cases.
if (isEmpty(seedTree.children)) return;
const contentsTree = root(getAllBetween(seedTree, `--seed-contents--`));
const headTree = root(getAllBetween(seedTree, `--before-user-code--`));
const tailTree = root(getAllBetween(seedTree, `--after-user-code--`));
const seeds = {};
// While before and after code are optional, the contents are not
if (isEmpty(contentsTree.children))
throw Error('## --seed-contents-- must appear in # --seed-- sections');
const visitForContents = visitChildren(
getFileVisitor(seeds, 'contents', validateEditableMarkers)
);
const visitForHead = visitChildren(getFileVisitor(seeds, 'head'));
const visitForTail = visitChildren(getFileVisitor(seeds, 'tail'));
visitForContents(contentsTree);
visitForHead(headTree);
visitForTail(tailTree);
file.data = {
...file.data,
files: seeds
};
// process region markers - remove them from content and add them to data
Object.keys(seeds).forEach(key => {
const fileData = seeds[key];
const editRegionMarkers = findRegionMarkers(fileData);
if (editRegionMarkers) {
fileData.contents = removeLines(fileData.contents, editRegionMarkers);
if (editRegionMarkers[1] <= editRegionMarkers[0]) {
throw Error('Editable region must be non zero');
}
fileData.editableRegionBoundaries = editRegionMarkers;
} else {
fileData.editableRegionBoundaries = [];
}
});
}
return transformer;
}
function validateEditableMarkers({ value, position }) {
const twoMarkersRE = RegExp(
editableRegionMarker + '.*' + editableRegionMarker
);
const formattedMarkerRE = /--fcc - editable - region--/;
const lines = value.split('\n');
const baseLineNumber = position.start.line + 1;
lines.forEach((line, index) => {
if (line.match(twoMarkersRE)) {
throw Error(
`Line ${
baseLineNumber + index
} has two markers. Each line should only have one.`
);
}
if (line.match(formattedMarkerRE)) {
throw Error(
`Line ${
baseLineNumber + index
} has a malformed marker. It should be --fcc-editable-region--`
);
}
});
}
module.exports = addSeeds;
module.exports.editableRegionMarker = editableRegionMarker;