-
Notifications
You must be signed in to change notification settings - Fork 140
Quality: Update exercise examples to use expression-based evaluation instead of const declarations
#1157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Quality: Update exercise examples to use expression-based evaluation instead of const declarations
#1157
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,57 +1,31 @@ | ||||||
| import { recursiveProcessTextJson } from "../parseXmlJson"; | ||||||
| import { missingExerciseWarning } from "./warnings.js"; | ||||||
| import { referenceStore } from "./processReferenceJson"; | ||||||
|
|
||||||
| let unlabeledEx = 0; | ||||||
| const processExerciseJson = (node, obj) => { | ||||||
| const label = node.getElementsByTagName("LABEL")[0]; | ||||||
| let labelName = ""; | ||||||
| const solution = node.getElementsByTagName("SOLUTION")[0]; | ||||||
|
|
||||||
| if (!label) { | ||||||
| unlabeledEx++; | ||||||
| labelName = "ex:unlabeled" + unlabeledEx; | ||||||
| } else { | ||||||
| labelName = label.getAttribute("NAME"); | ||||||
| } | ||||||
|
|
||||||
| if (!referenceStore[labelName]) { | ||||||
| missingExerciseWarning(labelName); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const displayName = referenceStore[labelName].displayName; | ||||||
|
|
||||||
| obj["tag"] = "EXERCISE"; | ||||||
| obj["title"] = "Exercise " + displayName; | ||||||
| obj["id"] = `#ex-${displayName}`; | ||||||
|
|
||||||
| recursiveProcessTextJson(node.firstChild, obj); | ||||||
|
|
||||||
| if (solution) { | ||||||
| const childObj = {}; | ||||||
| recursiveProcessTextJson(solution.firstChild, childObj); | ||||||
| obj["solution"] = childObj["child"]; | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| export const getIdForExerciseJson = node => { | ||||||
| const label = node.getElementsByTagName("LABEL")[0]; | ||||||
| let labelName = ""; | ||||||
|
|
||||||
| if (!label) { | ||||||
| labelName = "ex:unlabeled" + unlabeledEx; | ||||||
| } else { | ||||||
| labelName = label.getAttribute("NAME"); | ||||||
| } | ||||||
|
|
||||||
| if (!referenceStore[labelName]) { | ||||||
| missingExerciseWarning(labelName); | ||||||
| return undefined; | ||||||
| import { getChildrenByTagName } from "../utilityFunctions.js"; | ||||||
| import { recursivelyProcessTextSnippetJson } from "./processSnippetJson.js"; | ||||||
|
|
||||||
| const processExerciseJson = (node, writeTo) => { | ||||||
| const exercise = { | ||||||
| type: "exercise", | ||||||
| content: [] | ||||||
| }; | ||||||
| const children = Array.from(node.childNodes); | ||||||
| for (const child of children) { | ||||||
| if (child.nodeName === "SNIPPET") { | ||||||
| const snippet = []; | ||||||
| recursivelyProcessTextSnippetJson(child.firstChild, snippet); | ||||||
| const code = snippet.join("").trim(); | ||||||
| exercise.content.push({ | ||||||
| type: "snippet", | ||||||
| code: code.replace(/const\s+(\w+)\s*=\s*/g, "$1 = ") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regular expression
A safer and more robust approach is to anchor the regex to the beginning of a line and preserve leading whitespace. This will correctly handle all forms of
Suggested change
|
||||||
| }); | ||||||
| } else if (child.nodeName === "TEXT") { | ||||||
| const text = []; | ||||||
| recursivelyProcessTextSnippetJson(child.firstChild, text); | ||||||
| exercise.content.push({ | ||||||
| type: "text", | ||||||
| content: text.join("").trim() | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const displayName = referenceStore[labelName].displayName; | ||||||
| return `#ex-${displayName}`; | ||||||
| writeTo.push(exercise); | ||||||
| }; | ||||||
|
Comment on lines
+4
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pull request introduces breaking changes that will likely cause the application to crash at runtime.
It appears that |
||||||
|
|
||||||
| export default processExerciseJson; | ||||||
| export default processExerciseJson; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import
getChildrenByTagNameis unused in this file and can be removed to improve code cleanliness.